1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/threads/prsem.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "primpl.h" 1.10 +#include "obsolete/prsem.h" 1.11 + 1.12 +/************************************************************************/ 1.13 + 1.14 +/* 1.15 +** Create a new semaphore. 1.16 +*/ 1.17 +PR_IMPLEMENT(PRSemaphore*) PR_NewSem(PRUintn value) 1.18 +{ 1.19 + PRSemaphore *sem; 1.20 + PRCondVar *cvar; 1.21 + PRLock *lock; 1.22 + 1.23 + sem = PR_NEWZAP(PRSemaphore); 1.24 + if (sem) { 1.25 +#ifdef HAVE_CVAR_BUILT_ON_SEM 1.26 + _PR_MD_NEW_SEM(&sem->md, value); 1.27 +#else 1.28 + lock = PR_NewLock(); 1.29 + if (!lock) { 1.30 + PR_DELETE(sem); 1.31 + return NULL; 1.32 + } 1.33 + 1.34 + cvar = PR_NewCondVar(lock); 1.35 + if (!cvar) { 1.36 + PR_DestroyLock(lock); 1.37 + PR_DELETE(sem); 1.38 + return NULL; 1.39 + } 1.40 + sem->cvar = cvar; 1.41 + sem->count = value; 1.42 +#endif 1.43 + } 1.44 + return sem; 1.45 +} 1.46 + 1.47 +/* 1.48 +** Destroy a semaphore. There must be no thread waiting on the semaphore. 1.49 +** The caller is responsible for guaranteeing that the semaphore is 1.50 +** no longer in use. 1.51 +*/ 1.52 +PR_IMPLEMENT(void) PR_DestroySem(PRSemaphore *sem) 1.53 +{ 1.54 +#ifdef HAVE_CVAR_BUILT_ON_SEM 1.55 + _PR_MD_DESTROY_SEM(&sem->md); 1.56 +#else 1.57 + PR_ASSERT(sem->waiters == 0); 1.58 + 1.59 + PR_DestroyLock(sem->cvar->lock); 1.60 + PR_DestroyCondVar(sem->cvar); 1.61 +#endif 1.62 + PR_DELETE(sem); 1.63 +} 1.64 + 1.65 +/* 1.66 +** Wait on a Semaphore. 1.67 +** 1.68 +** This routine allows a calling thread to wait or proceed depending upon the 1.69 +** state of the semahore sem. The thread can proceed only if the counter value 1.70 +** of the semaphore sem is currently greater than 0. If the value of semaphore 1.71 +** sem is positive, it is decremented by one and the routine returns immediately 1.72 +** allowing the calling thread to continue. If the value of semaphore sem is 0, 1.73 +** the calling thread blocks awaiting the semaphore to be released by another 1.74 +** thread. 1.75 +** 1.76 +** This routine can return PR_PENDING_INTERRUPT if the waiting thread 1.77 +** has been interrupted. 1.78 +*/ 1.79 +PR_IMPLEMENT(PRStatus) PR_WaitSem(PRSemaphore *sem) 1.80 +{ 1.81 + PRStatus status = PR_SUCCESS; 1.82 + 1.83 +#ifdef HAVE_CVAR_BUILT_ON_SEM 1.84 + return _PR_MD_WAIT_SEM(&sem->md); 1.85 +#else 1.86 + PR_Lock(sem->cvar->lock); 1.87 + while (sem->count == 0) { 1.88 + sem->waiters++; 1.89 + status = PR_WaitCondVar(sem->cvar, PR_INTERVAL_NO_TIMEOUT); 1.90 + sem->waiters--; 1.91 + if (status != PR_SUCCESS) 1.92 + break; 1.93 + } 1.94 + if (status == PR_SUCCESS) 1.95 + sem->count--; 1.96 + PR_Unlock(sem->cvar->lock); 1.97 +#endif 1.98 + 1.99 + return (status); 1.100 +} 1.101 + 1.102 +/* 1.103 +** This routine increments the counter value of the semaphore. If other threads 1.104 +** are blocked for the semaphore, then the scheduler will determine which ONE 1.105 +** thread will be unblocked. 1.106 +*/ 1.107 +PR_IMPLEMENT(void) PR_PostSem(PRSemaphore *sem) 1.108 +{ 1.109 +#ifdef HAVE_CVAR_BUILT_ON_SEM 1.110 + _PR_MD_POST_SEM(&sem->md); 1.111 +#else 1.112 + PR_Lock(sem->cvar->lock); 1.113 + if (sem->waiters) 1.114 + PR_NotifyCondVar(sem->cvar); 1.115 + sem->count++; 1.116 + PR_Unlock(sem->cvar->lock); 1.117 +#endif 1.118 +} 1.119 + 1.120 +#if DEBUG 1.121 +/* 1.122 +** Returns the value of the semaphore referenced by sem without affecting 1.123 +** the state of the semaphore. The value represents the semaphore vaule 1.124 +** at the time of the call, but may not be the actual value when the 1.125 +** caller inspects it. (FOR DEBUGGING ONLY) 1.126 +*/ 1.127 +PR_IMPLEMENT(PRUintn) PR_GetValueSem(PRSemaphore *sem) 1.128 +{ 1.129 + PRUintn rv; 1.130 + 1.131 +#ifdef HAVE_CVAR_BUILT_ON_SEM 1.132 + rv = _PR_MD_GET_VALUE_SEM(&sem->md); 1.133 +#else 1.134 + PR_Lock(sem->cvar->lock); 1.135 + rv = sem->count; 1.136 + PR_Unlock(sem->cvar->lock); 1.137 +#endif 1.138 + 1.139 + return rv; 1.140 +} 1.141 +#endif