1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: sw=4 ts=4 et :
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef mozilla_CondVar_h
40 : #define mozilla_CondVar_h
41 :
42 : #include "prcvar.h"
43 :
44 : #include "mozilla/BlockingResourceBase.h"
45 : #include "mozilla/Mutex.h"
46 :
47 : namespace mozilla {
48 :
49 :
50 : /**
51 : * CondVar
52 : * Vanilla condition variable. Please don't use this unless you have a
53 : * compelling reason --- Monitor provides a simpler API.
54 : */
55 : class NS_COM_GLUE CondVar : BlockingResourceBase
56 : {
57 : public:
58 : /**
59 : * CondVar
60 : *
61 : * The CALLER owns |lock|.
62 : *
63 : * @param aLock A Mutex to associate with this condition variable.
64 : * @param aName A name which can reference this monitor
65 : * @returns If failure, nsnull.
66 : * If success, a valid Monitor* which must be destroyed
67 : * by Monitor::DestroyMonitor()
68 : **/
69 13327 : CondVar(Mutex& aLock, const char* aName) :
70 : BlockingResourceBase(aName, eCondVar),
71 13327 : mLock(&aLock)
72 : {
73 13327 : MOZ_COUNT_CTOR(CondVar);
74 : // |lock| must necessarily already be known to the deadlock detector
75 13327 : mCvar = PR_NewCondVar(mLock->mLock);
76 13327 : if (!mCvar)
77 0 : NS_RUNTIMEABORT("Can't allocate mozilla::CondVar");
78 13327 : }
79 :
80 : /**
81 : * ~CondVar
82 : * Clean up after this CondVar, but NOT its associated Mutex.
83 : **/
84 13327 : ~CondVar()
85 13327 : {
86 13327 : NS_ASSERTION(mCvar && mLock,
87 : "improperly constructed CondVar or double free");
88 13327 : PR_DestroyCondVar(mCvar);
89 13327 : mCvar = 0;
90 13327 : mLock = 0;
91 13327 : MOZ_COUNT_DTOR(CondVar);
92 13327 : }
93 :
94 : #ifndef DEBUG
95 : /**
96 : * Wait
97 : * @see prcvar.h
98 : **/
99 : nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
100 : {
101 : // NSPR checks for lock ownership
102 : return PR_WaitCondVar(mCvar, interval) == PR_SUCCESS
103 : ? NS_OK : NS_ERROR_FAILURE;
104 : }
105 : #else
106 : nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT);
107 : #endif // ifndef DEBUG
108 :
109 : /**
110 : * Notify
111 : * @see prcvar.h
112 : **/
113 46503 : nsresult Notify()
114 : {
115 : // NSPR checks for lock ownership
116 46503 : return PR_NotifyCondVar(mCvar) == PR_SUCCESS
117 46503 : ? NS_OK : NS_ERROR_FAILURE;
118 : }
119 :
120 : /**
121 : * NotifyAll
122 : * @see prcvar.h
123 : **/
124 93224 : nsresult NotifyAll()
125 : {
126 : // NSPR checks for lock ownership
127 93224 : return PR_NotifyAllCondVar(mCvar) == PR_SUCCESS
128 93224 : ? NS_OK : NS_ERROR_FAILURE;
129 : }
130 :
131 : #ifdef DEBUG
132 : /**
133 : * AssertCurrentThreadOwnsMutex
134 : * @see Mutex::AssertCurrentThreadOwns
135 : **/
136 43838 : void AssertCurrentThreadOwnsMutex()
137 : {
138 43838 : mLock->AssertCurrentThreadOwns();
139 43838 : }
140 :
141 : /**
142 : * AssertNotCurrentThreadOwnsMutex
143 : * @see Mutex::AssertNotCurrentThreadOwns
144 : **/
145 : void AssertNotCurrentThreadOwnsMutex()
146 : {
147 : mLock->AssertNotCurrentThreadOwns();
148 : }
149 :
150 : #else
151 : void AssertCurrentThreadOwnsMutex()
152 : {
153 : }
154 : void AssertNotCurrentThreadOwnsMutex()
155 : {
156 : }
157 :
158 : #endif // ifdef DEBUG
159 :
160 : private:
161 : CondVar();
162 : CondVar(CondVar&);
163 : CondVar& operator=(CondVar&);
164 :
165 : Mutex* mLock;
166 : PRCondVar* mCvar;
167 : };
168 :
169 :
170 : } // namespace mozilla
171 :
172 :
173 : #endif // ifndef mozilla_CondVar_h
|