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