nsprpub/pr/src/bthreads/btlocks.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; c-basic-offset: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /*
     7 ** File:        btlocks.c
     8 ** Description: Implemenation for thread locks using bthreads
     9 ** Exports:     prlock.h
    10 */
    12 #include "primpl.h"
    14 #include <string.h>
    15 #include <sys/time.h>
    17 void
    18 _PR_InitLocks (void)
    19 {
    20 }
    22 PR_IMPLEMENT(PRLock*)
    23     PR_NewLock (void)
    24 {
    25     PRLock *lock;
    26     status_t semresult;
    28     if (!_pr_initialized) _PR_ImplicitInitialization();
    30     lock = PR_NEWZAP(PRLock);
    31     if (lock != NULL) {
    33 	lock->benaphoreCount = 0;
    34 	lock->semaphoreID = create_sem( 0, "nsprLockSem" );
    35 	if( lock->semaphoreID < B_NO_ERROR ) {
    37 	    PR_DELETE( lock );
    38 	    lock = NULL;
    39 	}
    40     }
    42     return lock;
    43 }
    45 PR_IMPLEMENT(void)
    46     PR_DestroyLock (PRLock* lock)
    47 {
    48     status_t result;
    50     PR_ASSERT(NULL != lock);
    51     result = delete_sem(lock->semaphoreID);
    52     PR_ASSERT(result == B_NO_ERROR);
    53     PR_DELETE(lock);
    54 }
    56 PR_IMPLEMENT(void)
    57     PR_Lock (PRLock* lock)
    58 {
    59     PR_ASSERT(lock != NULL);
    61     if( atomic_add( &lock->benaphoreCount, 1 ) > 0 ) {
    63 	if( acquire_sem(lock->semaphoreID ) != B_NO_ERROR ) {
    65 	    atomic_add( &lock->benaphoreCount, -1 );
    66 	    return;
    67 	}
    68     }
    70     lock->owner = find_thread( NULL );
    71 }
    73 PR_IMPLEMENT(PRStatus)
    74     PR_Unlock (PRLock* lock)
    75 {
    76     PR_ASSERT(lock != NULL);
    77     lock->owner = NULL;
    78     if( atomic_add( &lock->benaphoreCount, -1 ) > 1 ) {
    80 	release_sem_etc( lock->semaphoreID, 1, B_DO_NOT_RESCHEDULE );
    81     }
    83     return PR_SUCCESS;
    84 }
    86 PR_IMPLEMENT(void)
    87     PR_AssertCurrentThreadOwnsLock(PRLock *lock)
    88 {
    89     PR_ASSERT(lock != NULL);
    90     PR_ASSERT(lock->owner == find_thread( NULL ));
    91 }

mercurial