michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef xpcom_build_IOInterposerPrivate_h michael@0: #define xpcom_build_IOInterposerPrivate_h michael@0: michael@0: /* This header file contains declarations for helper classes that are michael@0: to be used exclusively by IOInterposer and its observers. This header michael@0: file is not to be used by anything else and MUST NOT be exported! */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: namespace IOInterposer { michael@0: michael@0: /** michael@0: * The following classes are simple wrappers for PRLock and PRCondVar. michael@0: * IOInterposer and friends use these instead of Mozilla::Mutex et al because michael@0: * of the fact that IOInterposer is permitted to run until the process michael@0: * terminates; we can't use anything that plugs into leak checkers or deadlock michael@0: * detectors because IOInterposer will outlive those and generate false michael@0: * positives. michael@0: */ michael@0: michael@0: class Monitor michael@0: { michael@0: public: michael@0: Monitor() michael@0: : mLock(PR_NewLock()) michael@0: , mCondVar(PR_NewCondVar(mLock)) michael@0: { michael@0: } michael@0: michael@0: ~Monitor() michael@0: { michael@0: PR_DestroyCondVar(mCondVar); michael@0: mCondVar = nullptr; michael@0: PR_DestroyLock(mLock); michael@0: mLock = nullptr; michael@0: } michael@0: michael@0: void Lock() michael@0: { michael@0: PR_Lock(mLock); michael@0: } michael@0: michael@0: void Unlock() michael@0: { michael@0: PR_Unlock(mLock); michael@0: } michael@0: michael@0: bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) michael@0: { michael@0: return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS; michael@0: } michael@0: michael@0: bool Notify() michael@0: { michael@0: return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; michael@0: } michael@0: michael@0: private: michael@0: PRLock* mLock; michael@0: PRCondVar* mCondVar; michael@0: }; michael@0: michael@0: class MonitorAutoLock michael@0: { michael@0: public: michael@0: MonitorAutoLock(Monitor &aMonitor) michael@0: : mMonitor(aMonitor) michael@0: { michael@0: mMonitor.Lock(); michael@0: } michael@0: michael@0: ~MonitorAutoLock() michael@0: { michael@0: mMonitor.Unlock(); michael@0: } michael@0: michael@0: bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) michael@0: { michael@0: return mMonitor.Wait(aTimeout); michael@0: } michael@0: michael@0: bool Notify() michael@0: { michael@0: return mMonitor.Notify(); michael@0: } michael@0: michael@0: private: michael@0: Monitor& mMonitor; michael@0: }; michael@0: michael@0: class MonitorAutoUnlock michael@0: { michael@0: public: michael@0: MonitorAutoUnlock(Monitor &aMonitor) michael@0: : mMonitor(aMonitor) michael@0: { michael@0: mMonitor.Unlock(); michael@0: } michael@0: michael@0: ~MonitorAutoUnlock() michael@0: { michael@0: mMonitor.Lock(); michael@0: } michael@0: michael@0: private: michael@0: Monitor& mMonitor; michael@0: }; michael@0: michael@0: class Mutex michael@0: { michael@0: public: michael@0: Mutex() michael@0: : mPRLock(PR_NewLock()) michael@0: { michael@0: } michael@0: michael@0: ~Mutex() michael@0: { michael@0: PR_DestroyLock(mPRLock); michael@0: mPRLock = nullptr; michael@0: } michael@0: michael@0: void Lock() michael@0: { michael@0: PR_Lock(mPRLock); michael@0: } michael@0: michael@0: void Unlock() michael@0: { michael@0: PR_Unlock(mPRLock); michael@0: } michael@0: michael@0: private: michael@0: PRLock* mPRLock; michael@0: }; michael@0: michael@0: class AutoLock michael@0: { michael@0: public: michael@0: AutoLock(Mutex& aLock) michael@0: : mLock(aLock) michael@0: { michael@0: mLock.Lock(); michael@0: } michael@0: michael@0: ~AutoLock() michael@0: { michael@0: mLock.Unlock(); michael@0: } michael@0: michael@0: private: michael@0: Mutex& mLock; michael@0: }; michael@0: michael@0: } // namespace IOInterposer michael@0: } // namespace mozilla michael@0: michael@0: #endif // xpcom_build_IOInterposerPrivate_h michael@0: