Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / xpcom / build / IOInterposerPrivate.h
blob1685ca986c4eff1706bbf79a67ec81eba0d07b36
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 namespace mozilla {
18 namespace IOInterposer {
20 /**
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
26 * positives.
29 class Monitor {
30 public:
31 Monitor() : mLock(PR_NewLock()), mCondVar(PR_NewCondVar(mLock)) {}
33 ~Monitor() {
34 PR_DestroyCondVar(mCondVar);
35 mCondVar = nullptr;
36 PR_DestroyLock(mLock);
37 mLock = nullptr;
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; }
50 private:
51 PRLock* mLock;
52 PRCondVar* mCondVar;
55 class MonitorAutoLock {
56 public:
57 explicit MonitorAutoLock(Monitor& aMonitor) : mMonitor(aMonitor) {
58 mMonitor.Lock();
61 ~MonitorAutoLock() { mMonitor.Unlock(); }
63 bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) {
64 return mMonitor.Wait(aTimeout);
67 bool Notify() { return mMonitor.Notify(); }
69 private:
70 Monitor& mMonitor;
73 class MonitorAutoUnlock {
74 public:
75 explicit MonitorAutoUnlock(Monitor& aMonitor) : mMonitor(aMonitor) {
76 mMonitor.Unlock();
79 ~MonitorAutoUnlock() { mMonitor.Lock(); }
81 private:
82 Monitor& mMonitor;
85 class Mutex {
86 public:
87 Mutex() : mPRLock(PR_NewLock()) {}
89 ~Mutex() {
90 PR_DestroyLock(mPRLock);
91 mPRLock = nullptr;
94 void Lock() { PR_Lock(mPRLock); }
96 void Unlock() { PR_Unlock(mPRLock); }
98 private:
99 PRLock* mPRLock;
102 class AutoLock {
103 public:
104 explicit AutoLock(Mutex& aLock) : mLock(aLock) { mLock.Lock(); }
106 ~AutoLock() { mLock.Unlock(); }
108 private:
109 Mutex& mLock;
112 } // namespace IOInterposer
113 } // namespace mozilla
115 #endif // xpcom_build_IOInterposerPrivate_h