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 semaphore object. michael@0: */ michael@0: PR_IMPLEMENT(PRSemaphore*) michael@0: PR_NewSem (PRUintn value) michael@0: { michael@0: PRSemaphore *semaphore; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: semaphore = PR_NEWZAP(PRSemaphore); michael@0: if (NULL != semaphore) { michael@0: if ((semaphore->sem = create_sem(value, "nspr_sem")) < B_NO_ERROR) michael@0: return NULL; michael@0: else michael@0: return semaphore; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: /* michael@0: ** Destroy the given semaphore object. michael@0: ** michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_DestroySem (PRSemaphore *sem) michael@0: { michael@0: status_t result; michael@0: michael@0: PR_ASSERT(sem != NULL); michael@0: result = delete_sem(sem->sem); michael@0: PR_ASSERT(result == B_NO_ERROR); 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 michael@0: ** the state of the semahore sem. The thread can proceed only if the michael@0: ** counter value of the semaphore sem is currently greater than 0. If the michael@0: ** value of semaphore sem is positive, it is decremented by one and the michael@0: ** routine returns immediately allowing the calling thread to continue. If michael@0: ** the value of semaphore sem is 0, the calling thread blocks awaiting the michael@0: ** semaphore to be released by another 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) michael@0: PR_WaitSem (PRSemaphore *sem) michael@0: { michael@0: PR_ASSERT(sem != NULL); michael@0: if (acquire_sem(sem->sem) == B_NO_ERROR) michael@0: return PR_SUCCESS; michael@0: else michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: /* michael@0: ** This routine increments the counter value of the semaphore. If other michael@0: ** threads are blocked for the semaphore, then the scheduler will michael@0: ** determine which ONE thread will be unblocked. michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_PostSem (PRSemaphore *sem) michael@0: { michael@0: status_t result; michael@0: michael@0: PR_ASSERT(sem != NULL); michael@0: result = release_sem_etc(sem->sem, 1, B_DO_NOT_RESCHEDULE); michael@0: PR_ASSERT(result == B_NO_ERROR); michael@0: } michael@0: 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 value michael@0: ** at the time of the call, but may not be the actual value when the michael@0: ** caller inspects it. michael@0: */ michael@0: PR_IMPLEMENT(PRUintn) michael@0: PR_GetValueSem (PRSemaphore *sem) michael@0: { michael@0: sem_info info; michael@0: michael@0: PR_ASSERT(sem != NULL); michael@0: get_sem_info(sem->sem, &info); michael@0: return info.count; michael@0: }