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/. */
7 #ifndef xpcom_build_IOInterposerPrivate_h
8 #define xpcom_build_IOInterposerPrivate_h
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! */
18 namespace IOInterposer
{
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
31 Monitor() : mLock(PR_NewLock()), mCondVar(PR_NewCondVar(mLock
)) {}
34 PR_DestroyCondVar(mCondVar
);
36 PR_DestroyLock(mLock
);
40 void Lock() { PR_Lock(mLock
); }
42 void Unlock() { PR_Unlock(mLock
); }
44 bool Wait(PRIntervalTime aTimeout
= PR_INTERVAL_NO_TIMEOUT
) {
45 return PR_WaitCondVar(mCondVar
, aTimeout
) == PR_SUCCESS
;
48 bool Notify() { return PR_NotifyCondVar(mCondVar
) == PR_SUCCESS
; }
55 class MonitorAutoLock
{
57 explicit MonitorAutoLock(Monitor
& aMonitor
) : mMonitor(aMonitor
) {
61 ~MonitorAutoLock() { mMonitor
.Unlock(); }
63 bool Wait(PRIntervalTime aTimeout
= PR_INTERVAL_NO_TIMEOUT
) {
64 return mMonitor
.Wait(aTimeout
);
67 bool Notify() { return mMonitor
.Notify(); }
73 class MonitorAutoUnlock
{
75 explicit MonitorAutoUnlock(Monitor
& aMonitor
) : mMonitor(aMonitor
) {
79 ~MonitorAutoUnlock() { mMonitor
.Lock(); }
87 Mutex() : mPRLock(PR_NewLock()) {}
90 PR_DestroyLock(mPRLock
);
94 void Lock() { PR_Lock(mPRLock
); }
96 void Unlock() { PR_Unlock(mPRLock
); }
104 explicit AutoLock(Mutex
& aLock
) : mLock(aLock
) { mLock
.Lock(); }
106 ~AutoLock() { mLock
.Unlock(); }
112 } // namespace IOInterposer
113 } // namespace mozilla
115 #endif // xpcom_build_IOInterposerPrivate_h