Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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 #ifndef vm_Monitor_h
8 #define vm_Monitor_h
10 #ifdef JS_THREADSAFE
11 #include "mozilla/DebugOnly.h"
12 #endif
14 #include <stddef.h>
16 #include "jslock.h"
18 #include "js/Utility.h"
20 namespace js {
22 // A base class used for types intended to be used in a parallel
23 // fashion, such as the workers in the |ThreadPool| class. Combines a
24 // lock and a condition variable. You can acquire the lock or signal
25 // the condition variable using the |AutoLockMonitor| type.
27 class Monitor
28 {
29 protected:
30 friend class AutoLockMonitor;
31 friend class AutoUnlockMonitor;
33 PRLock *lock_;
34 PRCondVar *condVar_;
36 public:
37 Monitor()
38 : lock_(nullptr),
39 condVar_(nullptr)
40 { }
42 ~Monitor() {
43 #ifdef JS_THREADSAFE
44 if (lock_)
45 PR_DestroyLock(lock_);
46 if (condVar_)
47 PR_DestroyCondVar(condVar_);
48 #endif
49 }
51 bool init();
52 };
54 class AutoLockMonitor
55 {
56 private:
57 #ifdef JS_THREADSAFE
58 Monitor &monitor;
59 #endif
61 public:
62 AutoLockMonitor(Monitor &monitor)
63 #ifdef JS_THREADSAFE
64 : monitor(monitor)
65 {
66 PR_Lock(monitor.lock_);
67 }
68 #else
69 {}
70 #endif
72 ~AutoLockMonitor() {
73 #ifdef JS_THREADSAFE
74 PR_Unlock(monitor.lock_);
75 #endif
76 }
78 bool isFor(Monitor &other) const {
79 #ifdef JS_THREADSAFE
80 return monitor.lock_ == other.lock_;
81 #else
82 return true;
83 #endif
84 }
86 void wait(PRCondVar *condVar) {
87 #ifdef JS_THREADSAFE
88 mozilla::DebugOnly<PRStatus> status =
89 PR_WaitCondVar(condVar, PR_INTERVAL_NO_TIMEOUT);
90 MOZ_ASSERT(status == PR_SUCCESS);
91 #endif
92 }
94 void wait() {
95 #ifdef JS_THREADSAFE
96 wait(monitor.condVar_);
97 #endif
98 }
100 void notify(PRCondVar *condVar) {
101 #ifdef JS_THREADSAFE
102 mozilla::DebugOnly<PRStatus> status = PR_NotifyCondVar(condVar);
103 MOZ_ASSERT(status == PR_SUCCESS);
104 #endif
105 }
107 void notify() {
108 #ifdef JS_THREADSAFE
109 notify(monitor.condVar_);
110 #endif
111 }
113 void notifyAll(PRCondVar *condVar) {
114 #ifdef JS_THREADSAFE
115 mozilla::DebugOnly<PRStatus> status = PR_NotifyAllCondVar(monitor.condVar_);
116 MOZ_ASSERT(status == PR_SUCCESS);
117 #endif
118 }
120 void notifyAll() {
121 #ifdef JS_THREADSAFE
122 notifyAll(monitor.condVar_);
123 #endif
124 }
125 };
127 class AutoUnlockMonitor
128 {
129 private:
130 #ifdef JS_THREADSAFE
131 Monitor &monitor;
132 #endif
134 public:
135 AutoUnlockMonitor(Monitor &monitor)
136 #ifdef JS_THREADSAFE
137 : monitor(monitor)
138 {
139 PR_Unlock(monitor.lock_);
140 }
141 #else
142 {}
143 #endif
145 ~AutoUnlockMonitor() {
146 #ifdef JS_THREADSAFE
147 PR_Lock(monitor.lock_);
148 #endif
149 }
151 bool isFor(Monitor &other) const {
152 #ifdef JS_THREADSAFE
153 return monitor.lock_ == other.lock_;
154 #else
155 return true;
156 #endif
157 }
158 };
160 } // namespace js
162 #endif /* vm_Monitor_h */