1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsCacheUtils.h"
9 #include "nsThreadUtils.h"
11 using namespace mozilla
;
13 class nsDestroyThreadEvent
: public nsRunnable
{
15 explicit nsDestroyThreadEvent(nsIThread
*thread
)
24 nsCOMPtr
<nsIThread
> mThread
;
27 nsShutdownThread::nsShutdownThread(nsIThread
*aThread
)
28 : mLock("nsShutdownThread.mLock")
29 , mCondVar(mLock
, "nsShutdownThread.mCondVar")
34 nsShutdownThread::~nsShutdownThread()
39 nsShutdownThread::Shutdown(nsIThread
*aThread
)
42 nsRefPtr
<nsDestroyThreadEvent
> ev
= new nsDestroyThreadEvent(aThread
);
43 rv
= NS_DispatchToMainThread(ev
);
45 NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!");
51 nsShutdownThread::BlockingShutdown(nsIThread
*aThread
)
55 nsRefPtr
<nsShutdownThread
> st
= new nsShutdownThread(aThread
);
56 nsCOMPtr
<nsIThread
> workerThread
;
58 rv
= NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread
));
60 NS_WARNING("Can't create nsShutDownThread worker thread!");
65 MutexAutoLock
lock(st
->mLock
);
66 rv
= workerThread
->Dispatch(st
, NS_DISPATCH_NORMAL
);
69 "Dispatching event in nsShutdownThread::BlockingShutdown failed!");
75 return Shutdown(workerThread
);
79 nsShutdownThread::Run()
81 MutexAutoLock
lock(mLock
);