Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_Monitor_h
9 #define mozilla_Monitor_h
11 #include "mozilla/CondVar.h"
12 #include "mozilla/Mutex.h"
14 namespace mozilla {
16 /**
17 * Monitor provides a *non*-reentrant monitor: *not* a Java-style
18 * monitor. If your code needs support for reentrancy, use
19 * ReentrantMonitor instead. (Rarely should reentrancy be needed.)
20 *
21 * Instead of directly calling Monitor methods, it's safer and simpler
22 * to instead use the RAII wrappers MonitorAutoLock and
23 * MonitorAutoUnlock.
24 */
25 class NS_COM_GLUE Monitor
26 {
27 public:
28 Monitor(const char* aName) :
29 mMutex(aName),
30 mCondVar(mMutex, "[Monitor.mCondVar]")
31 {}
33 ~Monitor() {}
35 void Lock()
36 {
37 mMutex.Lock();
38 }
40 void Unlock()
41 {
42 mMutex.Unlock();
43 }
45 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
46 {
47 return mCondVar.Wait(interval);
48 }
50 nsresult Notify()
51 {
52 return mCondVar.Notify();
53 }
55 nsresult NotifyAll()
56 {
57 return mCondVar.NotifyAll();
58 }
60 void AssertCurrentThreadOwns() const
61 {
62 mMutex.AssertCurrentThreadOwns();
63 }
65 void AssertNotCurrentThreadOwns() const
66 {
67 mMutex.AssertNotCurrentThreadOwns();
68 }
70 private:
71 Monitor();
72 Monitor(const Monitor&);
73 Monitor& operator =(const Monitor&);
75 Mutex mMutex;
76 CondVar mCondVar;
77 };
79 /**
80 * Lock the monitor for the lexical scope instances of this class are
81 * bound to (except for MonitorAutoUnlock in nested scopes).
82 *
83 * The monitor must be unlocked when instances of this class are
84 * created.
85 */
86 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoLock
87 {
88 public:
89 MonitorAutoLock(Monitor& aMonitor) :
90 mMonitor(&aMonitor)
91 {
92 mMonitor->Lock();
93 }
95 ~MonitorAutoLock()
96 {
97 mMonitor->Unlock();
98 }
100 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
101 {
102 return mMonitor->Wait(interval);
103 }
105 nsresult Notify()
106 {
107 return mMonitor->Notify();
108 }
110 nsresult NotifyAll()
111 {
112 return mMonitor->NotifyAll();
113 }
115 private:
116 MonitorAutoLock();
117 MonitorAutoLock(const MonitorAutoLock&);
118 MonitorAutoLock& operator =(const MonitorAutoLock&);
119 static void* operator new(size_t) CPP_THROW_NEW;
120 static void operator delete(void*);
122 Monitor* mMonitor;
123 };
125 /**
126 * Unlock the monitor for the lexical scope instances of this class
127 * are bound to (except for MonitorAutoLock in nested scopes).
128 *
129 * The monitor must be locked by the current thread when instances of
130 * this class are created.
131 */
132 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoUnlock
133 {
134 public:
135 MonitorAutoUnlock(Monitor& aMonitor) :
136 mMonitor(&aMonitor)
137 {
138 mMonitor->Unlock();
139 }
141 ~MonitorAutoUnlock()
142 {
143 mMonitor->Lock();
144 }
146 private:
147 MonitorAutoUnlock();
148 MonitorAutoUnlock(const MonitorAutoUnlock&);
149 MonitorAutoUnlock& operator =(const MonitorAutoUnlock&);
150 static void* operator new(size_t) CPP_THROW_NEW;
151 static void operator delete(void*);
153 Monitor* mMonitor;
154 };
156 } // namespace mozilla
158 #endif // mozilla_Monitor_h