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