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  public:
Monitor()31   Monitor() : mLock(PR_NewLock()), mCondVar(PR_NewCondVar(mLock)) {}
32 
~Monitor()33   ~Monitor() {
34     PR_DestroyCondVar(mCondVar);
35     mCondVar = nullptr;
36     PR_DestroyLock(mLock);
37     mLock = nullptr;
38   }
39 
Lock()40   void Lock() { PR_Lock(mLock); }
41 
Unlock()42   void Unlock() { PR_Unlock(mLock); }
43 
44   bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) {
45     return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS;
46   }
47 
Notify()48   bool Notify() { return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; }
49 
50  private:
51   PRLock* mLock;
52   PRCondVar* mCondVar;
53 };
54 
55 class MonitorAutoLock {
56  public:
MonitorAutoLock(Monitor & aMonitor)57   explicit MonitorAutoLock(Monitor& aMonitor) : mMonitor(aMonitor) {
58     mMonitor.Lock();
59   }
60 
~MonitorAutoLock()61   ~MonitorAutoLock() { mMonitor.Unlock(); }
62 
63   bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) {
64     return mMonitor.Wait(aTimeout);
65   }
66 
Notify()67   bool Notify() { return mMonitor.Notify(); }
68 
69  private:
70   Monitor& mMonitor;
71 };
72 
73 class MonitorAutoUnlock {
74  public:
MonitorAutoUnlock(Monitor & aMonitor)75   explicit MonitorAutoUnlock(Monitor& aMonitor) : mMonitor(aMonitor) {
76     mMonitor.Unlock();
77   }
78 
~MonitorAutoUnlock()79   ~MonitorAutoUnlock() { mMonitor.Lock(); }
80 
81  private:
82   Monitor& mMonitor;
83 };
84 
85 class Mutex {
86  public:
Mutex()87   Mutex() : mPRLock(PR_NewLock()) {}
88 
~Mutex()89   ~Mutex() {
90     PR_DestroyLock(mPRLock);
91     mPRLock = nullptr;
92   }
93 
Lock()94   void Lock() { PR_Lock(mPRLock); }
95 
Unlock()96   void Unlock() { PR_Unlock(mPRLock); }
97 
98  private:
99   PRLock* mPRLock;
100 };
101 
102 class AutoLock {
103  public:
AutoLock(Mutex & aLock)104   explicit AutoLock(Mutex& aLock) : mLock(aLock) { mLock.Lock(); }
105 
~AutoLock()106   ~AutoLock() { mLock.Unlock(); }
107 
108  private:
109   Mutex& mLock;
110 };
111 
112 }  // namespace IOInterposer
113 }  // namespace mozilla
114 
115 #endif  // xpcom_build_IOInterposerPrivate_h
116