Bumping manifests a=b2g-bump
[gecko.git] / netwerk / cache / nsCacheUtils.cpp
blob536052ada6727675193c9de3f0e36a88f3908ee2
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/. */
7 #include "nsCache.h"
8 #include "nsCacheUtils.h"
9 #include "nsThreadUtils.h"
11 using namespace mozilla;
13 class nsDestroyThreadEvent : public nsRunnable {
14 public:
15 explicit nsDestroyThreadEvent(nsIThread *thread)
16 : mThread(thread)
18 NS_IMETHOD Run()
20 mThread->Shutdown();
21 return NS_OK;
23 private:
24 nsCOMPtr<nsIThread> mThread;
27 nsShutdownThread::nsShutdownThread(nsIThread *aThread)
28 : mLock("nsShutdownThread.mLock")
29 , mCondVar(mLock, "nsShutdownThread.mCondVar")
30 , mThread(aThread)
34 nsShutdownThread::~nsShutdownThread()
38 nsresult
39 nsShutdownThread::Shutdown(nsIThread *aThread)
41 nsresult rv;
42 nsRefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread);
43 rv = NS_DispatchToMainThread(ev);
44 if (NS_FAILED(rv)) {
45 NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!");
47 return rv;
50 nsresult
51 nsShutdownThread::BlockingShutdown(nsIThread *aThread)
53 nsresult rv;
55 nsRefPtr<nsShutdownThread> st = new nsShutdownThread(aThread);
56 nsCOMPtr<nsIThread> workerThread;
58 rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread));
59 if (NS_FAILED(rv)) {
60 NS_WARNING("Can't create nsShutDownThread worker thread!");
61 return rv;
65 MutexAutoLock lock(st->mLock);
66 rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL);
67 if (NS_FAILED(rv)) {
68 NS_WARNING(
69 "Dispatching event in nsShutdownThread::BlockingShutdown failed!");
70 } else {
71 st->mCondVar.Wait();
75 return Shutdown(workerThread);
78 NS_IMETHODIMP
79 nsShutdownThread::Run()
81 MutexAutoLock lock(mLock);
82 mThread->Shutdown();
83 mCondVar.Notify();
84 return NS_OK;