|
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/. */ |
|
6 |
|
7 #ifndef vm_Monitor_h |
|
8 #define vm_Monitor_h |
|
9 |
|
10 #ifdef JS_THREADSAFE |
|
11 #include "mozilla/DebugOnly.h" |
|
12 #endif |
|
13 |
|
14 #include <stddef.h> |
|
15 |
|
16 #include "jslock.h" |
|
17 |
|
18 #include "js/Utility.h" |
|
19 |
|
20 namespace js { |
|
21 |
|
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. |
|
26 |
|
27 class Monitor |
|
28 { |
|
29 protected: |
|
30 friend class AutoLockMonitor; |
|
31 friend class AutoUnlockMonitor; |
|
32 |
|
33 PRLock *lock_; |
|
34 PRCondVar *condVar_; |
|
35 |
|
36 public: |
|
37 Monitor() |
|
38 : lock_(nullptr), |
|
39 condVar_(nullptr) |
|
40 { } |
|
41 |
|
42 ~Monitor() { |
|
43 #ifdef JS_THREADSAFE |
|
44 if (lock_) |
|
45 PR_DestroyLock(lock_); |
|
46 if (condVar_) |
|
47 PR_DestroyCondVar(condVar_); |
|
48 #endif |
|
49 } |
|
50 |
|
51 bool init(); |
|
52 }; |
|
53 |
|
54 class AutoLockMonitor |
|
55 { |
|
56 private: |
|
57 #ifdef JS_THREADSAFE |
|
58 Monitor &monitor; |
|
59 #endif |
|
60 |
|
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 |
|
71 |
|
72 ~AutoLockMonitor() { |
|
73 #ifdef JS_THREADSAFE |
|
74 PR_Unlock(monitor.lock_); |
|
75 #endif |
|
76 } |
|
77 |
|
78 bool isFor(Monitor &other) const { |
|
79 #ifdef JS_THREADSAFE |
|
80 return monitor.lock_ == other.lock_; |
|
81 #else |
|
82 return true; |
|
83 #endif |
|
84 } |
|
85 |
|
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 } |
|
93 |
|
94 void wait() { |
|
95 #ifdef JS_THREADSAFE |
|
96 wait(monitor.condVar_); |
|
97 #endif |
|
98 } |
|
99 |
|
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 } |
|
106 |
|
107 void notify() { |
|
108 #ifdef JS_THREADSAFE |
|
109 notify(monitor.condVar_); |
|
110 #endif |
|
111 } |
|
112 |
|
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 } |
|
119 |
|
120 void notifyAll() { |
|
121 #ifdef JS_THREADSAFE |
|
122 notifyAll(monitor.condVar_); |
|
123 #endif |
|
124 } |
|
125 }; |
|
126 |
|
127 class AutoUnlockMonitor |
|
128 { |
|
129 private: |
|
130 #ifdef JS_THREADSAFE |
|
131 Monitor &monitor; |
|
132 #endif |
|
133 |
|
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 |
|
144 |
|
145 ~AutoUnlockMonitor() { |
|
146 #ifdef JS_THREADSAFE |
|
147 PR_Lock(monitor.lock_); |
|
148 #endif |
|
149 } |
|
150 |
|
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 }; |
|
159 |
|
160 } // namespace js |
|
161 |
|
162 #endif /* vm_Monitor_h */ |