michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "primpl.h" michael@0: michael@0: /* michael@0: ** Create a new condition variable. michael@0: ** michael@0: ** "lock" is the lock used to protect the condition variable. michael@0: ** michael@0: ** Condition variables are synchronization objects that threads can use michael@0: ** to wait for some condition to occur. michael@0: ** michael@0: ** This may fail if memory is tight or if some operating system resource michael@0: ** is low. In such cases, a NULL will be returned. michael@0: */ michael@0: PR_IMPLEMENT(PRCondVar*) michael@0: PR_NewCondVar (PRLock *lock) michael@0: { michael@0: PRCondVar *cv = PR_NEW( PRCondVar ); michael@0: PR_ASSERT( NULL != lock ); michael@0: if( NULL != cv ) michael@0: { michael@0: cv->lock = lock; michael@0: cv->sem = create_sem(0, "CVSem"); michael@0: cv->handshakeSem = create_sem(0, "CVHandshake"); michael@0: cv->signalSem = create_sem( 0, "CVSignal"); michael@0: cv->signalBenCount = 0; michael@0: cv->ns = cv->nw = 0; michael@0: PR_ASSERT( cv->sem >= B_NO_ERROR ); michael@0: PR_ASSERT( cv->handshakeSem >= B_NO_ERROR ); michael@0: PR_ASSERT( cv->signalSem >= B_NO_ERROR ); michael@0: } michael@0: return cv; michael@0: } /* PR_NewCondVar */ michael@0: michael@0: /* michael@0: ** Destroy a condition variable. There must be no thread michael@0: ** waiting on the condvar. The caller is responsible for guaranteeing michael@0: ** that the condvar is no longer in use. michael@0: ** michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_DestroyCondVar (PRCondVar *cvar) michael@0: { michael@0: status_t result = delete_sem( cvar->sem ); michael@0: PR_ASSERT( result == B_NO_ERROR ); michael@0: michael@0: result = delete_sem( cvar->handshakeSem ); michael@0: PR_ASSERT( result == B_NO_ERROR ); michael@0: michael@0: result = delete_sem( cvar->signalSem ); michael@0: PR_ASSERT( result == B_NO_ERROR ); michael@0: michael@0: PR_DELETE( cvar ); michael@0: } michael@0: michael@0: /* michael@0: ** The thread that waits on a condition is blocked in a "waiting on michael@0: ** condition" state until another thread notifies the condition or a michael@0: ** caller specified amount of time expires. The lock associated with michael@0: ** the condition variable will be released, which must have be held michael@0: ** prior to the call to wait. michael@0: ** michael@0: ** Logically a notified thread is moved from the "waiting on condition" michael@0: ** state and made "ready." When scheduled, it will attempt to reacquire michael@0: ** the lock that it held when wait was called. michael@0: ** michael@0: ** The timeout has two well known values, PR_INTERVAL_NO_TIMEOUT and michael@0: ** PR_INTERVAL_NO_WAIT. The former value requires that a condition be michael@0: ** notified (or the thread interrupted) before it will resume from the michael@0: ** wait. If the timeout has a value of PR_INTERVAL_NO_WAIT, the effect michael@0: ** is to release the lock, possibly causing a rescheduling within the michael@0: ** runtime, then immediately attempting to reacquire the lock and resume. michael@0: ** michael@0: ** Any other value for timeout will cause the thread to be rescheduled michael@0: ** either due to explicit notification or an expired interval. The latter michael@0: ** must be determined by treating time as one part of the monitored data michael@0: ** being protected by the lock and tested explicitly for an expired michael@0: ** interval. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable or the thread was interrupted (PR_Interrupt()). michael@0: ** The particular reason can be extracted with PR_GetError(). michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_WaitCondVar (PRCondVar *cvar, PRIntervalTime timeout) michael@0: { michael@0: status_t err; michael@0: if( timeout == PR_INTERVAL_NO_WAIT ) michael@0: { michael@0: PR_Unlock( cvar->lock ); michael@0: PR_Lock( cvar->lock ); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) michael@0: { michael@0: if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) michael@0: { michael@0: atomic_add( &cvar->signalBenCount, -1 ); michael@0: return PR_FAILURE; michael@0: } michael@0: } michael@0: cvar->nw += 1; michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: michael@0: PR_Unlock( cvar->lock ); michael@0: if( timeout==PR_INTERVAL_NO_TIMEOUT ) michael@0: { michael@0: err = acquire_sem(cvar->sem); michael@0: } michael@0: else michael@0: { michael@0: err = acquire_sem_etc(cvar->sem, 1, B_RELATIVE_TIMEOUT, PR_IntervalToMicroseconds(timeout) ); michael@0: } michael@0: michael@0: if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) michael@0: { michael@0: while (acquire_sem(cvar->signalSem) == B_INTERRUPTED); michael@0: } michael@0: michael@0: if (cvar->ns > 0) michael@0: { michael@0: release_sem_etc(cvar->handshakeSem, 1, B_DO_NOT_RESCHEDULE); michael@0: cvar->ns -= 1; michael@0: } michael@0: cvar->nw -= 1; michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: michael@0: PR_Lock( cvar->lock ); michael@0: if(err!=B_NO_ERROR) michael@0: { michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* michael@0: ** Notify ONE thread that is currently waiting on 'cvar'. Which thread is michael@0: ** dependent on the implementation of the runtime. Common sense would dictate michael@0: ** that all threads waiting on a single condition have identical semantics, michael@0: ** therefore which one gets notified is not significant. michael@0: ** michael@0: ** The calling thead must hold the lock that protects the condition, as michael@0: ** well as the invariants that are tightly bound to the condition, when michael@0: ** notify is called. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_NotifyCondVar (PRCondVar *cvar) michael@0: { michael@0: status_t err ; michael@0: if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) michael@0: { michael@0: if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) michael@0: { michael@0: atomic_add( &cvar->signalBenCount, -1 ); michael@0: return PR_FAILURE; michael@0: } michael@0: } michael@0: if (cvar->nw > cvar->ns) michael@0: { michael@0: cvar->ns += 1; michael@0: release_sem_etc(cvar->sem, 1, B_DO_NOT_RESCHEDULE); michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: michael@0: while (acquire_sem(cvar->handshakeSem) == B_INTERRUPTED) michael@0: { michael@0: err = B_INTERRUPTED; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* michael@0: ** Notify all of the threads waiting on the condition variable. The order michael@0: ** that the threads are notified is indeterminant. The lock that protects michael@0: ** the condition must be held. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_NotifyAllCondVar (PRCondVar *cvar) michael@0: { michael@0: int32 handshakes; michael@0: status_t err = B_OK; michael@0: michael@0: if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) michael@0: { michael@0: if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) michael@0: { michael@0: atomic_add( &cvar->signalBenCount, -1 ); michael@0: return PR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: if (cvar->nw > cvar->ns) michael@0: { michael@0: handshakes = cvar->nw - cvar->ns; michael@0: cvar->ns = cvar->nw; michael@0: release_sem_etc(cvar->sem, handshakes, B_DO_NOT_RESCHEDULE); michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: michael@0: while (acquire_sem_etc(cvar->handshakeSem, handshakes, 0, 0) == B_INTERRUPTED) michael@0: { michael@0: err = B_INTERRUPTED; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) michael@0: { michael@0: release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); michael@0: } michael@0: } michael@0: return PR_SUCCESS; michael@0: }