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! */
17 #include "mozilla/ThreadSafety.h"
20 namespace IOInterposer
{
23 * The following classes are simple wrappers for PRLock and PRCondVar.
24 * IOInterposer and friends use these instead of Mozilla::Mutex et al because
25 * of the fact that IOInterposer is permitted to run until the process
26 * terminates; we can't use anything that plugs into leak checkers or deadlock
27 * detectors because IOInterposer will outlive those and generate false
31 class MOZ_CAPABILITY("monitor") Monitor
{
33 Monitor() : mLock(PR_NewLock()), mCondVar(PR_NewCondVar(mLock
)) {}
36 PR_DestroyCondVar(mCondVar
);
38 PR_DestroyLock(mLock
);
42 void Lock() MOZ_CAPABILITY_ACQUIRE() { PR_Lock(mLock
); }
44 void Unlock() MOZ_CAPABILITY_RELEASE() { PR_Unlock(mLock
); }
46 bool Wait(PRIntervalTime aTimeout
= PR_INTERVAL_NO_TIMEOUT
)
48 return PR_WaitCondVar(mCondVar
, aTimeout
) == PR_SUCCESS
;
51 bool Notify() { return PR_NotifyCondVar(mCondVar
) == PR_SUCCESS
; }
58 class MOZ_SCOPED_CAPABILITY MonitorAutoLock
{
60 explicit MonitorAutoLock(Monitor
& aMonitor
) MOZ_CAPABILITY_ACQUIRE(aMonitor
)
61 : mMonitor(aMonitor
) {
65 ~MonitorAutoLock() MOZ_CAPABILITY_RELEASE() { mMonitor
.Unlock(); }
71 class MOZ_SCOPED_CAPABILITY MonitorAutoUnlock
{
73 explicit MonitorAutoUnlock(Monitor
& aMonitor
)
74 MOZ_SCOPED_UNLOCK_RELEASE(aMonitor
)
75 : mMonitor(aMonitor
) {
79 ~MonitorAutoUnlock() MOZ_SCOPED_UNLOCK_REACQUIRE() { mMonitor
.Lock(); }
85 class MOZ_CAPABILITY("mutex") Mutex
{
87 Mutex() : mPRLock(PR_NewLock()) {}
90 PR_DestroyLock(mPRLock
);
94 void Lock() MOZ_CAPABILITY_ACQUIRE() { PR_Lock(mPRLock
); }
96 void Unlock() MOZ_CAPABILITY_RELEASE() { PR_Unlock(mPRLock
); }
102 class MOZ_SCOPED_CAPABILITY AutoLock
{
104 explicit AutoLock(Mutex
& aLock
) MOZ_CAPABILITY_ACQUIRE(aLock
) : mLock(aLock
) {
108 ~AutoLock() MOZ_CAPABILITY_RELEASE() { mLock
.Unlock(); }
114 } // namespace IOInterposer
115 } // namespace mozilla
117 #endif // xpcom_build_IOInterposerPrivate_h