1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=8 et :
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at:
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla Code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * The Mozilla Foundation
21 : * Portions created by the Initial Developer are Copyrigght (C) 2011
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Chris Jones <jones.chris.g@gmail.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef mozilla_Monitor_h
42 : #define mozilla_Monitor_h
43 :
44 : #include "mozilla/CondVar.h"
45 : #include "mozilla/Mutex.h"
46 :
47 : namespace mozilla {
48 :
49 : /**
50 : * Monitor provides a *non*-reentrant monitor: *not* a Java-style
51 : * monitor. If your code needs support for reentrancy, use
52 : * ReentrantMonitor instead. (Rarely should reentrancy be needed.)
53 : *
54 : * Instead of directly calling Monitor methods, it's safer and simpler
55 : * to instead use the RAII wrappers MonitorAutoLock and
56 : * MonitorAutoUnlock.
57 : */
58 : class NS_COM_GLUE Monitor
59 : {
60 : public:
61 4801 : Monitor(const char* aName) :
62 : mMutex(aName),
63 4801 : mCondVar(mMutex, "[Monitor.mCondVar]")
64 4801 : {}
65 :
66 4801 : ~Monitor() {}
67 :
68 76785 : void Lock()
69 : {
70 76785 : mMutex.Lock();
71 76785 : }
72 :
73 76785 : void Unlock()
74 : {
75 76785 : mMutex.Unlock();
76 76785 : }
77 :
78 37505 : nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
79 : {
80 37505 : return mCondVar.Wait(interval);
81 : }
82 :
83 40746 : nsresult Notify()
84 : {
85 40746 : return mCondVar.Notify();
86 : }
87 :
88 1023 : nsresult NotifyAll()
89 : {
90 1023 : return mCondVar.NotifyAll();
91 : }
92 :
93 0 : void AssertCurrentThreadOwns() const
94 : {
95 0 : mMutex.AssertCurrentThreadOwns();
96 0 : }
97 :
98 0 : void AssertNotCurrentThreadOwns() const
99 : {
100 0 : mMutex.AssertNotCurrentThreadOwns();
101 0 : }
102 :
103 : private:
104 : Monitor();
105 : Monitor(const Monitor&);
106 : Monitor& operator =(const Monitor&);
107 :
108 : Mutex mMutex;
109 : CondVar mCondVar;
110 : };
111 :
112 : /**
113 : * Lock the monitor for the lexical scope instances of this class are
114 : * bound to (except for MonitorAutoUnlock in nested scopes).
115 : *
116 : * The monitor must be unlocked when instances of this class are
117 : * created.
118 : */
119 : class NS_COM_GLUE NS_STACK_CLASS MonitorAutoLock
120 : {
121 : public:
122 72487 : MonitorAutoLock(Monitor& aMonitor) :
123 72487 : mMonitor(&aMonitor)
124 : {
125 72487 : mMonitor->Lock();
126 72487 : }
127 :
128 72487 : ~MonitorAutoLock()
129 : {
130 72487 : mMonitor->Unlock();
131 72487 : }
132 :
133 1423 : nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
134 : {
135 1423 : return mMonitor->Wait(interval);
136 : }
137 :
138 1423 : nsresult Notify()
139 : {
140 1423 : return mMonitor->Notify();
141 : }
142 :
143 : nsresult NotifyAll()
144 : {
145 : return mMonitor->NotifyAll();
146 : }
147 :
148 : private:
149 : MonitorAutoLock();
150 : MonitorAutoLock(const MonitorAutoLock&);
151 : MonitorAutoLock& operator =(const MonitorAutoLock&);
152 : static void* operator new(size_t) CPP_THROW_NEW;
153 : static void operator delete(void*);
154 :
155 : Monitor* mMonitor;
156 : };
157 :
158 : /**
159 : * Unlock the monitor for the lexical scope instances of this class
160 : * are bound to (except for MonitorAutoLock in nested scopes).
161 : *
162 : * The monitor must be locked by the current thread when instances of
163 : * this class are created.
164 : */
165 : class NS_COM_GLUE NS_STACK_CLASS MonitorAutoUnlock
166 : {
167 : public:
168 4298 : MonitorAutoUnlock(Monitor& aMonitor) :
169 4298 : mMonitor(&aMonitor)
170 : {
171 4298 : mMonitor->Unlock();
172 4298 : }
173 :
174 4298 : ~MonitorAutoUnlock()
175 : {
176 4298 : mMonitor->Lock();
177 4298 : }
178 :
179 : private:
180 : MonitorAutoUnlock();
181 : MonitorAutoUnlock(const MonitorAutoUnlock&);
182 : MonitorAutoUnlock& operator =(const MonitorAutoUnlock&);
183 : static void* operator new(size_t) CPP_THROW_NEW;
184 : static void operator delete(void*);
185 :
186 : Monitor* mMonitor;
187 : };
188 :
189 : } // namespace mozilla
190 :
191 : #endif // mozilla_Monitor_h
|