/src/mozilla-central/xpcom/build/IOInterposerPrivate.h
Line | Count | Source (jump to first uncovered line) |
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 | 3 | { |
36 | 3 | } |
37 | | |
38 | | ~Monitor() |
39 | 3 | { |
40 | 3 | PR_DestroyCondVar(mCondVar); |
41 | 3 | mCondVar = nullptr; |
42 | 3 | PR_DestroyLock(mLock); |
43 | 3 | mLock = nullptr; |
44 | 3 | } |
45 | | |
46 | | void Lock() |
47 | 0 | { |
48 | 0 | PR_Lock(mLock); |
49 | 0 | } |
50 | | |
51 | | void Unlock() |
52 | 0 | { |
53 | 0 | PR_Unlock(mLock); |
54 | 0 | } |
55 | | |
56 | | bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) |
57 | 0 | { |
58 | 0 | return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS; |
59 | 0 | } |
60 | | |
61 | | bool Notify() |
62 | 0 | { |
63 | 0 | return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; |
64 | 0 | } |
65 | | |
66 | | private: |
67 | | PRLock* mLock; |
68 | | PRCondVar* mCondVar; |
69 | | }; |
70 | | |
71 | | class MonitorAutoLock |
72 | | { |
73 | | public: |
74 | | explicit MonitorAutoLock(Monitor& aMonitor) |
75 | | : mMonitor(aMonitor) |
76 | 0 | { |
77 | 0 | mMonitor.Lock(); |
78 | 0 | } |
79 | | |
80 | | ~MonitorAutoLock() |
81 | 0 | { |
82 | 0 | mMonitor.Unlock(); |
83 | 0 | } |
84 | | |
85 | | bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) |
86 | 0 | { |
87 | 0 | return mMonitor.Wait(aTimeout); |
88 | 0 | } |
89 | | |
90 | | bool Notify() |
91 | 0 | { |
92 | 0 | return mMonitor.Notify(); |
93 | 0 | } |
94 | | |
95 | | private: |
96 | | Monitor& mMonitor; |
97 | | }; |
98 | | |
99 | | class MonitorAutoUnlock |
100 | | { |
101 | | public: |
102 | | explicit MonitorAutoUnlock(Monitor& aMonitor) |
103 | | : mMonitor(aMonitor) |
104 | 0 | { |
105 | 0 | mMonitor.Unlock(); |
106 | 0 | } |
107 | | |
108 | | ~MonitorAutoUnlock() |
109 | 0 | { |
110 | 0 | mMonitor.Lock(); |
111 | 0 | } |
112 | | |
113 | | private: |
114 | | Monitor& mMonitor; |
115 | | }; |
116 | | |
117 | | class Mutex |
118 | | { |
119 | | public: |
120 | | Mutex() |
121 | | : mPRLock(PR_NewLock()) |
122 | 3 | { |
123 | 3 | } |
124 | | |
125 | | ~Mutex() |
126 | 0 | { |
127 | 0 | PR_DestroyLock(mPRLock); |
128 | 0 | mPRLock = nullptr; |
129 | 0 | } |
130 | | |
131 | | void Lock() |
132 | 6 | { |
133 | 6 | PR_Lock(mPRLock); |
134 | 6 | } |
135 | | |
136 | | void Unlock() |
137 | 6 | { |
138 | 6 | PR_Unlock(mPRLock); |
139 | 6 | } |
140 | | |
141 | | private: |
142 | | PRLock* mPRLock; |
143 | | }; |
144 | | |
145 | | class AutoLock |
146 | | { |
147 | | public: |
148 | | explicit AutoLock(Mutex& aLock) |
149 | | : mLock(aLock) |
150 | 6 | { |
151 | 6 | mLock.Lock(); |
152 | 6 | } |
153 | | |
154 | | ~AutoLock() |
155 | 6 | { |
156 | 6 | mLock.Unlock(); |
157 | 6 | } |
158 | | |
159 | | private: |
160 | | Mutex& mLock; |
161 | | }; |
162 | | |
163 | | } // namespace IOInterposer |
164 | | } // namespace mozilla |
165 | | |
166 | | #endif // xpcom_build_IOInterposerPrivate_h |
167 | | |