|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=8 et : |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #ifndef mozilla_Monitor_h |
|
9 #define mozilla_Monitor_h |
|
10 |
|
11 #include "mozilla/CondVar.h" |
|
12 #include "mozilla/Mutex.h" |
|
13 |
|
14 namespace mozilla { |
|
15 |
|
16 /** |
|
17 * Monitor provides a *non*-reentrant monitor: *not* a Java-style |
|
18 * monitor. If your code needs support for reentrancy, use |
|
19 * ReentrantMonitor instead. (Rarely should reentrancy be needed.) |
|
20 * |
|
21 * Instead of directly calling Monitor methods, it's safer and simpler |
|
22 * to instead use the RAII wrappers MonitorAutoLock and |
|
23 * MonitorAutoUnlock. |
|
24 */ |
|
25 class NS_COM_GLUE Monitor |
|
26 { |
|
27 public: |
|
28 Monitor(const char* aName) : |
|
29 mMutex(aName), |
|
30 mCondVar(mMutex, "[Monitor.mCondVar]") |
|
31 {} |
|
32 |
|
33 ~Monitor() {} |
|
34 |
|
35 void Lock() |
|
36 { |
|
37 mMutex.Lock(); |
|
38 } |
|
39 |
|
40 void Unlock() |
|
41 { |
|
42 mMutex.Unlock(); |
|
43 } |
|
44 |
|
45 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT) |
|
46 { |
|
47 return mCondVar.Wait(interval); |
|
48 } |
|
49 |
|
50 nsresult Notify() |
|
51 { |
|
52 return mCondVar.Notify(); |
|
53 } |
|
54 |
|
55 nsresult NotifyAll() |
|
56 { |
|
57 return mCondVar.NotifyAll(); |
|
58 } |
|
59 |
|
60 void AssertCurrentThreadOwns() const |
|
61 { |
|
62 mMutex.AssertCurrentThreadOwns(); |
|
63 } |
|
64 |
|
65 void AssertNotCurrentThreadOwns() const |
|
66 { |
|
67 mMutex.AssertNotCurrentThreadOwns(); |
|
68 } |
|
69 |
|
70 private: |
|
71 Monitor(); |
|
72 Monitor(const Monitor&); |
|
73 Monitor& operator =(const Monitor&); |
|
74 |
|
75 Mutex mMutex; |
|
76 CondVar mCondVar; |
|
77 }; |
|
78 |
|
79 /** |
|
80 * Lock the monitor for the lexical scope instances of this class are |
|
81 * bound to (except for MonitorAutoUnlock in nested scopes). |
|
82 * |
|
83 * The monitor must be unlocked when instances of this class are |
|
84 * created. |
|
85 */ |
|
86 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoLock |
|
87 { |
|
88 public: |
|
89 MonitorAutoLock(Monitor& aMonitor) : |
|
90 mMonitor(&aMonitor) |
|
91 { |
|
92 mMonitor->Lock(); |
|
93 } |
|
94 |
|
95 ~MonitorAutoLock() |
|
96 { |
|
97 mMonitor->Unlock(); |
|
98 } |
|
99 |
|
100 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT) |
|
101 { |
|
102 return mMonitor->Wait(interval); |
|
103 } |
|
104 |
|
105 nsresult Notify() |
|
106 { |
|
107 return mMonitor->Notify(); |
|
108 } |
|
109 |
|
110 nsresult NotifyAll() |
|
111 { |
|
112 return mMonitor->NotifyAll(); |
|
113 } |
|
114 |
|
115 private: |
|
116 MonitorAutoLock(); |
|
117 MonitorAutoLock(const MonitorAutoLock&); |
|
118 MonitorAutoLock& operator =(const MonitorAutoLock&); |
|
119 static void* operator new(size_t) CPP_THROW_NEW; |
|
120 static void operator delete(void*); |
|
121 |
|
122 Monitor* mMonitor; |
|
123 }; |
|
124 |
|
125 /** |
|
126 * Unlock the monitor for the lexical scope instances of this class |
|
127 * are bound to (except for MonitorAutoLock in nested scopes). |
|
128 * |
|
129 * The monitor must be locked by the current thread when instances of |
|
130 * this class are created. |
|
131 */ |
|
132 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoUnlock |
|
133 { |
|
134 public: |
|
135 MonitorAutoUnlock(Monitor& aMonitor) : |
|
136 mMonitor(&aMonitor) |
|
137 { |
|
138 mMonitor->Unlock(); |
|
139 } |
|
140 |
|
141 ~MonitorAutoUnlock() |
|
142 { |
|
143 mMonitor->Lock(); |
|
144 } |
|
145 |
|
146 private: |
|
147 MonitorAutoUnlock(); |
|
148 MonitorAutoUnlock(const MonitorAutoUnlock&); |
|
149 MonitorAutoUnlock& operator =(const MonitorAutoUnlock&); |
|
150 static void* operator new(size_t) CPP_THROW_NEW; |
|
151 static void operator delete(void*); |
|
152 |
|
153 Monitor* mMonitor; |
|
154 }; |
|
155 |
|
156 } // namespace mozilla |
|
157 |
|
158 #endif // mozilla_Monitor_h |