nsprpub/pr/src/bthreads/btlocks.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/bthreads/btlocks.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +/* -*- Mode: C++; 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 +/*
    1.10 +** File:        btlocks.c
    1.11 +** Description: Implemenation for thread locks using bthreads
    1.12 +** Exports:     prlock.h
    1.13 +*/
    1.14 +
    1.15 +#include "primpl.h"
    1.16 +
    1.17 +#include <string.h>
    1.18 +#include <sys/time.h>
    1.19 +
    1.20 +void
    1.21 +_PR_InitLocks (void)
    1.22 +{
    1.23 +}
    1.24 +
    1.25 +PR_IMPLEMENT(PRLock*)
    1.26 +    PR_NewLock (void)
    1.27 +{
    1.28 +    PRLock *lock;
    1.29 +    status_t semresult;
    1.30 +
    1.31 +    if (!_pr_initialized) _PR_ImplicitInitialization();
    1.32 +
    1.33 +    lock = PR_NEWZAP(PRLock);
    1.34 +    if (lock != NULL) {
    1.35 +
    1.36 +	lock->benaphoreCount = 0;
    1.37 +	lock->semaphoreID = create_sem( 0, "nsprLockSem" );
    1.38 +	if( lock->semaphoreID < B_NO_ERROR ) {
    1.39 +
    1.40 +	    PR_DELETE( lock );
    1.41 +	    lock = NULL;
    1.42 +	}
    1.43 +    }
    1.44 +
    1.45 +    return lock;
    1.46 +}
    1.47 +
    1.48 +PR_IMPLEMENT(void)
    1.49 +    PR_DestroyLock (PRLock* lock)
    1.50 +{
    1.51 +    status_t result;
    1.52 +
    1.53 +    PR_ASSERT(NULL != lock);
    1.54 +    result = delete_sem(lock->semaphoreID);
    1.55 +    PR_ASSERT(result == B_NO_ERROR);
    1.56 +    PR_DELETE(lock);
    1.57 +}
    1.58 +
    1.59 +PR_IMPLEMENT(void)
    1.60 +    PR_Lock (PRLock* lock)
    1.61 +{
    1.62 +    PR_ASSERT(lock != NULL);
    1.63 +
    1.64 +    if( atomic_add( &lock->benaphoreCount, 1 ) > 0 ) {
    1.65 +
    1.66 +	if( acquire_sem(lock->semaphoreID ) != B_NO_ERROR ) {
    1.67 +
    1.68 +	    atomic_add( &lock->benaphoreCount, -1 );
    1.69 +	    return;
    1.70 +	}
    1.71 +    }
    1.72 +
    1.73 +    lock->owner = find_thread( NULL );
    1.74 +}
    1.75 +
    1.76 +PR_IMPLEMENT(PRStatus)
    1.77 +    PR_Unlock (PRLock* lock)
    1.78 +{
    1.79 +    PR_ASSERT(lock != NULL);
    1.80 +    lock->owner = NULL;
    1.81 +    if( atomic_add( &lock->benaphoreCount, -1 ) > 1 ) {
    1.82 +
    1.83 +	release_sem_etc( lock->semaphoreID, 1, B_DO_NOT_RESCHEDULE );
    1.84 +    }
    1.85 +
    1.86 +    return PR_SUCCESS;
    1.87 +}
    1.88 +
    1.89 +PR_IMPLEMENT(void)
    1.90 +    PR_AssertCurrentThreadOwnsLock(PRLock *lock)
    1.91 +{
    1.92 +    PR_ASSERT(lock != NULL);
    1.93 +    PR_ASSERT(lock->owner == find_thread( NULL ));
    1.94 +}

mercurial