netwerk/cache/nsCacheUtils.cpp

branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
equal deleted inserted replaced
-1:000000000000 0:9075db45a95b
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/. */
6
7 #include "nsCache.h"
8 #include "nsCacheUtils.h"
9 #include "nsThreadUtils.h"
10
11 using namespace mozilla;
12
13 class nsDestroyThreadEvent : public nsRunnable {
14 public:
15 nsDestroyThreadEvent(nsIThread *thread)
16 : mThread(thread)
17 {}
18 NS_IMETHOD Run()
19 {
20 mThread->Shutdown();
21 return NS_OK;
22 }
23 private:
24 nsCOMPtr<nsIThread> mThread;
25 };
26
27 nsShutdownThread::nsShutdownThread(nsIThread *aThread)
28 : mLock("nsShutdownThread.mLock")
29 , mCondVar(mLock, "nsShutdownThread.mCondVar")
30 , mThread(aThread)
31 {
32 }
33
34 nsShutdownThread::~nsShutdownThread()
35 {
36 }
37
38 nsresult
39 nsShutdownThread::Shutdown(nsIThread *aThread)
40 {
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!");
46 }
47 return rv;
48 }
49
50 nsresult
51 nsShutdownThread::BlockingShutdown(nsIThread *aThread)
52 {
53 nsresult rv;
54
55 nsRefPtr<nsShutdownThread> st = new nsShutdownThread(aThread);
56 nsCOMPtr<nsIThread> workerThread;
57
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;
62 }
63
64 {
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();
72 }
73 }
74
75 return Shutdown(workerThread);
76 }
77
78 NS_IMETHODIMP
79 nsShutdownThread::Run()
80 {
81 MutexAutoLock lock(mLock);
82 mThread->Shutdown();
83 mCondVar.Notify();
84 return NS_OK;
85 }

mercurial