nsprpub/pr/src/bthreads/btsem.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/bthreads/btsem.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     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 <kernel/OS.h>
    1.10 +
    1.11 +#include "primpl.h"
    1.12 +
    1.13 +/*
    1.14 +** Create a new semaphore object.
    1.15 +*/
    1.16 +PR_IMPLEMENT(PRSemaphore*)
    1.17 +    PR_NewSem (PRUintn value)
    1.18 +{
    1.19 +	PRSemaphore *semaphore;
    1.20 +
    1.21 +	if (!_pr_initialized) _PR_ImplicitInitialization();
    1.22 +
    1.23 +	semaphore = PR_NEWZAP(PRSemaphore);
    1.24 +	if (NULL != semaphore) {
    1.25 +		if ((semaphore->sem = create_sem(value, "nspr_sem")) < B_NO_ERROR)
    1.26 +			return NULL;
    1.27 +		else 
    1.28 +			return semaphore;
    1.29 +	}
    1.30 +	return NULL;
    1.31 +}
    1.32 +
    1.33 +/*
    1.34 +** Destroy the given semaphore object.
    1.35 +**
    1.36 +*/
    1.37 +PR_IMPLEMENT(void)
    1.38 +    PR_DestroySem (PRSemaphore *sem)
    1.39 +{
    1.40 +	status_t result;
    1.41 +
    1.42 +	PR_ASSERT(sem != NULL);
    1.43 +	result = delete_sem(sem->sem);
    1.44 +	PR_ASSERT(result == B_NO_ERROR);
    1.45 +	PR_DELETE(sem);
    1.46 +} 
    1.47 +
    1.48 +/*
    1.49 +** Wait on a Semaphore.
    1.50 +** 
    1.51 +** This routine allows a calling thread to wait or proceed depending upon
    1.52 +** the state of the semahore sem. The thread can proceed only if the
    1.53 +** counter value of the semaphore sem is currently greater than 0. If the
    1.54 +** value of semaphore sem is positive, it is decremented by one and the
    1.55 +** routine returns immediately allowing the calling thread to continue. If
    1.56 +** the value of semaphore sem is 0, the calling thread blocks awaiting the
    1.57 +** semaphore to be released by another thread.
    1.58 +** 
    1.59 +** This routine can return PR_PENDING_INTERRUPT if the waiting thread 
    1.60 +** has been interrupted.
    1.61 +*/
    1.62 +PR_IMPLEMENT(PRStatus)
    1.63 +    PR_WaitSem (PRSemaphore *sem)
    1.64 +{
    1.65 +	PR_ASSERT(sem != NULL);
    1.66 +	if (acquire_sem(sem->sem) == B_NO_ERROR)
    1.67 +		return PR_SUCCESS;
    1.68 +	else
    1.69 +		return PR_FAILURE;
    1.70 +}
    1.71 +
    1.72 +/*
    1.73 +** This routine increments the counter value of the semaphore. If other
    1.74 +** threads are blocked for the semaphore, then the scheduler will
    1.75 +** determine which ONE thread will be unblocked.
    1.76 +*/
    1.77 +PR_IMPLEMENT(void)
    1.78 +    PR_PostSem (PRSemaphore *sem)
    1.79 +{
    1.80 +	status_t result;
    1.81 +
    1.82 +	PR_ASSERT(sem != NULL);
    1.83 +	result = release_sem_etc(sem->sem, 1, B_DO_NOT_RESCHEDULE);
    1.84 +	PR_ASSERT(result == B_NO_ERROR);
    1.85 +}
    1.86 +
    1.87 +/*
    1.88 +** Returns the value of the semaphore referenced by sem without affecting
    1.89 +** the state of the semaphore.  The value represents the semaphore value
    1.90 +** at the time of the call, but may not be the actual value when the
    1.91 +** caller inspects it.
    1.92 +*/
    1.93 +PR_IMPLEMENT(PRUintn)
    1.94 +    PR_GetValueSem (PRSemaphore *sem)
    1.95 +{
    1.96 +	sem_info	info;
    1.97 +
    1.98 +	PR_ASSERT(sem != NULL);
    1.99 +	get_sem_info(sem->sem, &info);
   1.100 +	return info.count;
   1.101 +}

mercurial