/src/mozilla-central/netwerk/cache/nsCacheUtils.cpp
Line | Count | Source (jump to first uncovered line) |
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 Runnable { |
14 | | public: |
15 | | explicit nsDestroyThreadEvent(nsIThread* thread) |
16 | | : mozilla::Runnable("nsDestroyThreadEvent") |
17 | | , mThread(thread) |
18 | 0 | {} |
19 | | NS_IMETHOD Run() override |
20 | 0 | { |
21 | 0 | mThread->Shutdown(); |
22 | 0 | return NS_OK; |
23 | 0 | } |
24 | | private: |
25 | | nsCOMPtr<nsIThread> mThread; |
26 | | }; |
27 | | |
28 | | nsShutdownThread::nsShutdownThread(nsIThread* aThread) |
29 | | : mozilla::Runnable("nsShutdownThread") |
30 | | , mMonitor("nsShutdownThread.mMonitor") |
31 | | , mShuttingDown(false) |
32 | | , mThread(aThread) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | | nsresult |
37 | | nsShutdownThread::Shutdown(nsIThread *aThread) |
38 | 0 | { |
39 | 0 | nsresult rv; |
40 | 0 | RefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread); |
41 | 0 | rv = NS_DispatchToMainThread(ev); |
42 | 0 | if (NS_FAILED(rv)) { |
43 | 0 | NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!"); |
44 | 0 | } |
45 | 0 | return rv; |
46 | 0 | } |
47 | | |
48 | | nsresult |
49 | | nsShutdownThread::BlockingShutdown(nsIThread *aThread) |
50 | 0 | { |
51 | 0 | nsresult rv; |
52 | 0 |
|
53 | 0 | RefPtr<nsShutdownThread> st = new nsShutdownThread(aThread); |
54 | 0 | nsCOMPtr<nsIThread> workerThread; |
55 | 0 |
|
56 | 0 | rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread)); |
57 | 0 | if (NS_FAILED(rv)) { |
58 | 0 | NS_WARNING("Can't create nsShutDownThread worker thread!"); |
59 | 0 | return rv; |
60 | 0 | } |
61 | 0 |
|
62 | 0 | { |
63 | 0 | MonitorAutoLock mon(st->mMonitor); |
64 | 0 | rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL); |
65 | 0 | if (NS_FAILED(rv)) { |
66 | 0 | NS_WARNING( |
67 | 0 | "Dispatching event in nsShutdownThread::BlockingShutdown failed!"); |
68 | 0 | } else { |
69 | 0 | st->mShuttingDown = true; |
70 | 0 | while (st->mShuttingDown) { |
71 | 0 | mon.Wait(); |
72 | 0 | } |
73 | 0 | } |
74 | 0 | } |
75 | 0 |
|
76 | 0 | return Shutdown(workerThread); |
77 | 0 | } |
78 | | |
79 | | NS_IMETHODIMP |
80 | | nsShutdownThread::Run() |
81 | 0 | { |
82 | 0 | MonitorAutoLock mon(mMonitor); |
83 | 0 | mThread->Shutdown(); |
84 | 0 | mShuttingDown = false; |
85 | 0 | mon.Notify(); |
86 | 0 | return NS_OK; |
87 | 0 | } |