andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: #include "obsolete/prsem.h" andre@0: andre@0: /************************************************************************/ andre@0: andre@0: /* andre@0: ** Create a new semaphore. andre@0: */ andre@0: PR_IMPLEMENT(PRSemaphore*) PR_NewSem(PRUintn value) andre@0: { andre@0: PRSemaphore *sem; andre@0: PRCondVar *cvar; andre@0: PRLock *lock; andre@0: andre@0: sem = PR_NEWZAP(PRSemaphore); andre@0: if (sem) { andre@0: #ifdef HAVE_CVAR_BUILT_ON_SEM andre@0: _PR_MD_NEW_SEM(&sem->md, value); andre@0: #else andre@0: lock = PR_NewLock(); andre@0: if (!lock) { andre@0: PR_DELETE(sem); andre@0: return NULL; andre@0: } andre@0: andre@0: cvar = PR_NewCondVar(lock); andre@0: if (!cvar) { andre@0: PR_DestroyLock(lock); andre@0: PR_DELETE(sem); andre@0: return NULL; andre@0: } andre@0: sem->cvar = cvar; andre@0: sem->count = value; andre@0: #endif andre@0: } andre@0: return sem; andre@0: } andre@0: andre@0: /* andre@0: ** Destroy a semaphore. There must be no thread waiting on the semaphore. andre@0: ** The caller is responsible for guaranteeing that the semaphore is andre@0: ** no longer in use. andre@0: */ andre@0: PR_IMPLEMENT(void) PR_DestroySem(PRSemaphore *sem) andre@0: { andre@0: #ifdef HAVE_CVAR_BUILT_ON_SEM andre@0: _PR_MD_DESTROY_SEM(&sem->md); andre@0: #else andre@0: PR_ASSERT(sem->waiters == 0); andre@0: andre@0: PR_DestroyLock(sem->cvar->lock); andre@0: PR_DestroyCondVar(sem->cvar); andre@0: #endif andre@0: PR_DELETE(sem); andre@0: } andre@0: andre@0: /* andre@0: ** Wait on a Semaphore. andre@0: ** andre@0: ** This routine allows a calling thread to wait or proceed depending upon the andre@0: ** state of the semahore sem. The thread can proceed only if the counter value andre@0: ** of the semaphore sem is currently greater than 0. If the value of semaphore andre@0: ** sem is positive, it is decremented by one and the routine returns immediately andre@0: ** allowing the calling thread to continue. If the value of semaphore sem is 0, andre@0: ** the calling thread blocks awaiting the semaphore to be released by another andre@0: ** thread. andre@0: ** andre@0: ** This routine can return PR_PENDING_INTERRUPT if the waiting thread andre@0: ** has been interrupted. andre@0: */ andre@0: PR_IMPLEMENT(PRStatus) PR_WaitSem(PRSemaphore *sem) andre@0: { andre@0: PRStatus status = PR_SUCCESS; andre@0: andre@0: #ifdef HAVE_CVAR_BUILT_ON_SEM andre@0: return _PR_MD_WAIT_SEM(&sem->md); andre@0: #else andre@0: PR_Lock(sem->cvar->lock); andre@0: while (sem->count == 0) { andre@0: sem->waiters++; andre@0: status = PR_WaitCondVar(sem->cvar, PR_INTERVAL_NO_TIMEOUT); andre@0: sem->waiters--; andre@0: if (status != PR_SUCCESS) andre@0: break; andre@0: } andre@0: if (status == PR_SUCCESS) andre@0: sem->count--; andre@0: PR_Unlock(sem->cvar->lock); andre@0: #endif andre@0: andre@0: return (status); andre@0: } andre@0: andre@0: /* andre@0: ** This routine increments the counter value of the semaphore. If other threads andre@0: ** are blocked for the semaphore, then the scheduler will determine which ONE andre@0: ** thread will be unblocked. andre@0: */ andre@0: PR_IMPLEMENT(void) PR_PostSem(PRSemaphore *sem) andre@0: { andre@0: #ifdef HAVE_CVAR_BUILT_ON_SEM andre@0: _PR_MD_POST_SEM(&sem->md); andre@0: #else andre@0: PR_Lock(sem->cvar->lock); andre@0: if (sem->waiters) andre@0: PR_NotifyCondVar(sem->cvar); andre@0: sem->count++; andre@0: PR_Unlock(sem->cvar->lock); andre@0: #endif andre@0: } andre@0: andre@0: #if DEBUG andre@0: /* andre@0: ** Returns the value of the semaphore referenced by sem without affecting andre@0: ** the state of the semaphore. The value represents the semaphore vaule andre@0: ** at the time of the call, but may not be the actual value when the andre@0: ** caller inspects it. (FOR DEBUGGING ONLY) andre@0: */ andre@0: PR_IMPLEMENT(PRUintn) PR_GetValueSem(PRSemaphore *sem) andre@0: { andre@0: PRUintn rv; andre@0: andre@0: #ifdef HAVE_CVAR_BUILT_ON_SEM andre@0: rv = _PR_MD_GET_VALUE_SEM(&sem->md); andre@0: #else andre@0: PR_Lock(sem->cvar->lock); andre@0: rv = sem->count; andre@0: PR_Unlock(sem->cvar->lock); andre@0: #endif andre@0: andre@0: return rv; andre@0: } andre@0: #endif