1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 /************************************************************************/
43 ** Create a new monitor.
45 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
51 mon = PR_NEWZAP(PRMonitor);
59 cvar = PR_NewCondVar(lock);
71 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
73 PRMonitor* mon = PR_NewMonitor();
80 ** Destroy a monitor. There must be no thread waiting on the monitor's
81 ** condition variable. The caller is responsible for guaranteeing that the
82 ** monitor is no longer in use.
84 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
86 PR_DestroyLock(mon->cvar->lock);
87 PR_DestroyCondVar(mon->cvar);
92 ** Enter the lock associated with the monitor.
94 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
96 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
99 PR_Lock(mon->cvar->lock);
105 ** Test and then enter the lock associated with the monitor if it's not
106 ** already entered by some other thread. Return PR_FALSE if some other
107 ** thread owned the lock at the time of the call.
109 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
111 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
115 if (PR_TestAndLock(mon->cvar->lock)) {
124 ** Exit the lock associated with the monitor once.
126 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
128 if (mon->cvar->lock->owner != _PR_MD_CURRENT_THREAD()) {
131 if (--mon->entryCount == 0) {
132 return PR_Unlock(mon->cvar->lock);
138 ** Return the number of times that the current thread has entered the
139 ** lock. Returns zero if the current thread has not entered the lock.
141 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
143 return (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) ?
148 ** If the current thread is in |mon|, this assertion is guaranteed to
149 ** succeed. Otherwise, the behavior of this function is undefined.
151 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
153 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock);
157 ** Wait for a notify on the condition variable. Sleep for "ticks" amount
158 ** of time (if "tick" is 0 then the sleep is indefinite). While
159 ** the thread is waiting it exits the monitors lock (as if it called
160 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When
161 ** the wait has finished the thread regains control of the monitors lock
162 ** with the same entry count as before the wait began.
164 ** The thread waiting on the monitor will be resumed when the monitor is
165 ** notified (assuming the thread is the next in line to receive the
166 ** notify) or when the "ticks" elapses.
168 ** Returns PR_FAILURE if the caller has not locked the lock associated
169 ** with the condition variable.
170 ** This routine can return PR_PENDING_INTERRUPT if the waiting thread
171 ** has been interrupted.
173 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
177 PRThread *me = _PR_MD_CURRENT_THREAD();
179 if (mon->cvar->lock->owner != me) return PR_FAILURE;
181 entryCount = mon->entryCount;
184 status = _PR_WaitCondVar(me, mon->cvar, mon->cvar->lock, ticks);
186 mon->entryCount = entryCount;
192 ** Notify the highest priority thread waiting on the condition
193 ** variable. If a thread is waiting on the condition variable (using
194 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
196 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
198 PRThread *me = _PR_MD_CURRENT_THREAD();
199 if (mon->cvar->lock->owner != me) return PR_FAILURE;
200 PR_NotifyCondVar(mon->cvar);
205 ** Notify all of the threads waiting on the condition variable. All of
206 ** threads are notified in turn. The highest priority thread will
207 ** probably acquire the monitor first when the monitor is exited.
209 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
211 PRThread *me = _PR_MD_CURRENT_THREAD();
212 if (mon->cvar->lock->owner != me) return PR_FAILURE;
213 PR_NotifyAllCondVar(mon->cvar);
217 /************************************************************************/
219 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
223 if (mon->cvar->lock->owner) {
224 nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
225 mon, mon->cvar->lock->owner->id,
226 mon->cvar->lock->owner, mon->entryCount);
228 nb = PR_snprintf(buf, buflen, "[%p]", mon);