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