Bug 1883861 - Part 1: Move visitMemoryBarrier into the common CodeGenerator file...
[gecko.git] / xpcom / build / IOInterposerPrivate.h
blob14f71603b8110bbb2bb5cb58efc4cef9c64bf5c9
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! */
14 #include <prcvar.h>
15 #include <prlock.h>
17 #include "mozilla/ThreadSafety.h"
19 namespace mozilla {
20 namespace IOInterposer {
22 /**
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
28 * positives.
31 class MOZ_CAPABILITY("monitor") Monitor {
32 public:
33 Monitor() : mLock(PR_NewLock()), mCondVar(PR_NewCondVar(mLock)) {}
35 ~Monitor() {
36 PR_DestroyCondVar(mCondVar);
37 mCondVar = nullptr;
38 PR_DestroyLock(mLock);
39 mLock = nullptr;
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)
47 MOZ_REQUIRES(this) {
48 return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS;
51 bool Notify() { return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; }
53 private:
54 PRLock* mLock;
55 PRCondVar* mCondVar;
58 class MOZ_SCOPED_CAPABILITY MonitorAutoLock {
59 public:
60 explicit MonitorAutoLock(Monitor& aMonitor) MOZ_CAPABILITY_ACQUIRE(aMonitor)
61 : mMonitor(aMonitor) {
62 mMonitor.Lock();
65 ~MonitorAutoLock() MOZ_CAPABILITY_RELEASE() { mMonitor.Unlock(); }
67 private:
68 Monitor& mMonitor;
71 class MOZ_SCOPED_CAPABILITY MonitorAutoUnlock {
72 public:
73 explicit MonitorAutoUnlock(Monitor& aMonitor)
74 MOZ_SCOPED_UNLOCK_RELEASE(aMonitor)
75 : mMonitor(aMonitor) {
76 mMonitor.Unlock();
79 ~MonitorAutoUnlock() MOZ_SCOPED_UNLOCK_REACQUIRE() { mMonitor.Lock(); }
81 private:
82 Monitor& mMonitor;
85 class MOZ_CAPABILITY("mutex") Mutex {
86 public:
87 Mutex() : mPRLock(PR_NewLock()) {}
89 ~Mutex() {
90 PR_DestroyLock(mPRLock);
91 mPRLock = nullptr;
94 void Lock() MOZ_CAPABILITY_ACQUIRE() { PR_Lock(mPRLock); }
96 void Unlock() MOZ_CAPABILITY_RELEASE() { PR_Unlock(mPRLock); }
98 private:
99 PRLock* mPRLock;
102 class MOZ_SCOPED_CAPABILITY AutoLock {
103 public:
104 explicit AutoLock(Mutex& aLock) MOZ_CAPABILITY_ACQUIRE(aLock) : mLock(aLock) {
105 mLock.Lock();
108 ~AutoLock() MOZ_CAPABILITY_RELEASE() { mLock.Unlock(); }
110 private:
111 Mutex& mLock;
114 } // namespace IOInterposer
115 } // namespace mozilla
117 #endif // xpcom_build_IOInterposerPrivate_h