1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 ***** */
38 #include <kernel/OS.h>
43 ** Create a new condition variable.
45 ** "lock" is the lock used to protect the condition variable.
47 ** Condition variables are synchronization objects that threads can use
48 ** to wait for some condition to occur.
50 ** This may fail if memory is tight or if some operating system resource
51 ** is low. In such cases, a NULL will be returned.
53 PR_IMPLEMENT(PRCondVar*)
54 PR_NewCondVar (PRLock *lock)
56 PRCondVar *cv = PR_NEW( PRCondVar );
57 PR_ASSERT( NULL != lock );
61 cv->sem = create_sem(0, "CVSem");
62 cv->handshakeSem = create_sem(0, "CVHandshake");
63 cv->signalSem = create_sem( 0, "CVSignal");
64 cv->signalBenCount = 0;
66 PR_ASSERT( cv->sem >= B_NO_ERROR );
67 PR_ASSERT( cv->handshakeSem >= B_NO_ERROR );
68 PR_ASSERT( cv->signalSem >= B_NO_ERROR );
74 ** Destroy a condition variable. There must be no thread
75 ** waiting on the condvar. The caller is responsible for guaranteeing
76 ** that the condvar is no longer in use.
80 PR_DestroyCondVar (PRCondVar *cvar)
82 status_t result = delete_sem( cvar->sem );
83 PR_ASSERT( result == B_NO_ERROR );
85 result = delete_sem( cvar->handshakeSem );
86 PR_ASSERT( result == B_NO_ERROR );
88 result = delete_sem( cvar->signalSem );
89 PR_ASSERT( result == B_NO_ERROR );
95 ** The thread that waits on a condition is blocked in a "waiting on
96 ** condition" state until another thread notifies the condition or a
97 ** caller specified amount of time expires. The lock associated with
98 ** the condition variable will be released, which must have be held
99 ** prior to the call to wait.
101 ** Logically a notified thread is moved from the "waiting on condition"
102 ** state and made "ready." When scheduled, it will attempt to reacquire
103 ** the lock that it held when wait was called.
105 ** The timeout has two well known values, PR_INTERVAL_NO_TIMEOUT and
106 ** PR_INTERVAL_NO_WAIT. The former value requires that a condition be
107 ** notified (or the thread interrupted) before it will resume from the
108 ** wait. If the timeout has a value of PR_INTERVAL_NO_WAIT, the effect
109 ** is to release the lock, possibly causing a rescheduling within the
110 ** runtime, then immediately attempting to reacquire the lock and resume.
112 ** Any other value for timeout will cause the thread to be rescheduled
113 ** either due to explicit notification or an expired interval. The latter
114 ** must be determined by treating time as one part of the monitored data
115 ** being protected by the lock and tested explicitly for an expired
118 ** Returns PR_FAILURE if the caller has not locked the lock associated
119 ** with the condition variable or the thread was interrupted (PR_Interrupt()).
120 ** The particular reason can be extracted with PR_GetError().
122 PR_IMPLEMENT(PRStatus)
123 PR_WaitCondVar (PRCondVar *cvar, PRIntervalTime timeout)
126 if( timeout == PR_INTERVAL_NO_WAIT )
128 PR_Unlock( cvar->lock );
129 PR_Lock( cvar->lock );
133 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 )
135 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED)
137 atomic_add( &cvar->signalBenCount, -1 );
142 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
144 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);
147 PR_Unlock( cvar->lock );
148 if( timeout==PR_INTERVAL_NO_TIMEOUT )
150 err = acquire_sem(cvar->sem);
154 err = acquire_sem_etc(cvar->sem, 1, B_RELATIVE_TIMEOUT, PR_IntervalToMicroseconds(timeout) );
157 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 )
159 while (acquire_sem(cvar->signalSem) == B_INTERRUPTED);
164 release_sem_etc(cvar->handshakeSem, 1, B_DO_NOT_RESCHEDULE);
168 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
170 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);
173 PR_Lock( cvar->lock );
182 ** Notify ONE thread that is currently waiting on 'cvar'. Which thread is
183 ** dependent on the implementation of the runtime. Common sense would dictate
184 ** that all threads waiting on a single condition have identical semantics,
185 ** therefore which one gets notified is not significant.
187 ** The calling thead must hold the lock that protects the condition, as
188 ** well as the invariants that are tightly bound to the condition, when
191 ** Returns PR_FAILURE if the caller has not locked the lock associated
192 ** with the condition variable.
194 PR_IMPLEMENT(PRStatus)
195 PR_NotifyCondVar (PRCondVar *cvar)
198 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 )
200 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED)
202 atomic_add( &cvar->signalBenCount, -1 );
206 if (cvar->nw > cvar->ns)
209 release_sem_etc(cvar->sem, 1, B_DO_NOT_RESCHEDULE);
210 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
212 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);
215 while (acquire_sem(cvar->handshakeSem) == B_INTERRUPTED)
222 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
224 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);
231 ** Notify all of the threads waiting on the condition variable. The order
232 ** that the threads are notified is indeterminant. The lock that protects
233 ** the condition must be held.
235 ** Returns PR_FAILURE if the caller has not locked the lock associated
236 ** with the condition variable.
238 PR_IMPLEMENT(PRStatus)
239 PR_NotifyAllCondVar (PRCondVar *cvar)
244 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 )
246 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED)
248 atomic_add( &cvar->signalBenCount, -1 );
253 if (cvar->nw > cvar->ns)
255 handshakes = cvar->nw - cvar->ns;
257 release_sem_etc(cvar->sem, handshakes, B_DO_NOT_RESCHEDULE);
258 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
260 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);
263 while (acquire_sem_etc(cvar->handshakeSem, handshakes, 0, 0) == B_INTERRUPTED)
270 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 )
272 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE);