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