|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
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 xpcom_build_IOInterposerPrivate_h |
|
8 #define xpcom_build_IOInterposerPrivate_h |
|
9 |
|
10 /* This header file contains declarations for helper classes that are |
|
11 to be used exclusively by IOInterposer and its observers. This header |
|
12 file is not to be used by anything else and MUST NOT be exported! */ |
|
13 |
|
14 #include <prcvar.h> |
|
15 #include <prlock.h> |
|
16 |
|
17 namespace mozilla { |
|
18 namespace IOInterposer { |
|
19 |
|
20 /** |
|
21 * The following classes are simple wrappers for PRLock and PRCondVar. |
|
22 * IOInterposer and friends use these instead of Mozilla::Mutex et al because |
|
23 * of the fact that IOInterposer is permitted to run until the process |
|
24 * terminates; we can't use anything that plugs into leak checkers or deadlock |
|
25 * detectors because IOInterposer will outlive those and generate false |
|
26 * positives. |
|
27 */ |
|
28 |
|
29 class Monitor |
|
30 { |
|
31 public: |
|
32 Monitor() |
|
33 : mLock(PR_NewLock()) |
|
34 , mCondVar(PR_NewCondVar(mLock)) |
|
35 { |
|
36 } |
|
37 |
|
38 ~Monitor() |
|
39 { |
|
40 PR_DestroyCondVar(mCondVar); |
|
41 mCondVar = nullptr; |
|
42 PR_DestroyLock(mLock); |
|
43 mLock = nullptr; |
|
44 } |
|
45 |
|
46 void Lock() |
|
47 { |
|
48 PR_Lock(mLock); |
|
49 } |
|
50 |
|
51 void Unlock() |
|
52 { |
|
53 PR_Unlock(mLock); |
|
54 } |
|
55 |
|
56 bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) |
|
57 { |
|
58 return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS; |
|
59 } |
|
60 |
|
61 bool Notify() |
|
62 { |
|
63 return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; |
|
64 } |
|
65 |
|
66 private: |
|
67 PRLock* mLock; |
|
68 PRCondVar* mCondVar; |
|
69 }; |
|
70 |
|
71 class MonitorAutoLock |
|
72 { |
|
73 public: |
|
74 MonitorAutoLock(Monitor &aMonitor) |
|
75 : mMonitor(aMonitor) |
|
76 { |
|
77 mMonitor.Lock(); |
|
78 } |
|
79 |
|
80 ~MonitorAutoLock() |
|
81 { |
|
82 mMonitor.Unlock(); |
|
83 } |
|
84 |
|
85 bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) |
|
86 { |
|
87 return mMonitor.Wait(aTimeout); |
|
88 } |
|
89 |
|
90 bool Notify() |
|
91 { |
|
92 return mMonitor.Notify(); |
|
93 } |
|
94 |
|
95 private: |
|
96 Monitor& mMonitor; |
|
97 }; |
|
98 |
|
99 class MonitorAutoUnlock |
|
100 { |
|
101 public: |
|
102 MonitorAutoUnlock(Monitor &aMonitor) |
|
103 : mMonitor(aMonitor) |
|
104 { |
|
105 mMonitor.Unlock(); |
|
106 } |
|
107 |
|
108 ~MonitorAutoUnlock() |
|
109 { |
|
110 mMonitor.Lock(); |
|
111 } |
|
112 |
|
113 private: |
|
114 Monitor& mMonitor; |
|
115 }; |
|
116 |
|
117 class Mutex |
|
118 { |
|
119 public: |
|
120 Mutex() |
|
121 : mPRLock(PR_NewLock()) |
|
122 { |
|
123 } |
|
124 |
|
125 ~Mutex() |
|
126 { |
|
127 PR_DestroyLock(mPRLock); |
|
128 mPRLock = nullptr; |
|
129 } |
|
130 |
|
131 void Lock() |
|
132 { |
|
133 PR_Lock(mPRLock); |
|
134 } |
|
135 |
|
136 void Unlock() |
|
137 { |
|
138 PR_Unlock(mPRLock); |
|
139 } |
|
140 |
|
141 private: |
|
142 PRLock* mPRLock; |
|
143 }; |
|
144 |
|
145 class AutoLock |
|
146 { |
|
147 public: |
|
148 AutoLock(Mutex& aLock) |
|
149 : mLock(aLock) |
|
150 { |
|
151 mLock.Lock(); |
|
152 } |
|
153 |
|
154 ~AutoLock() |
|
155 { |
|
156 mLock.Unlock(); |
|
157 } |
|
158 |
|
159 private: |
|
160 Mutex& mLock; |
|
161 }; |
|
162 |
|
163 } // namespace IOInterposer |
|
164 } // namespace mozilla |
|
165 |
|
166 #endif // xpcom_build_IOInterposerPrivate_h |
|
167 |