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++; tab-width: 8; indent-tabs-mode: nil; 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 | #include <kernel/OS.h> |
michael@0 | 7 | |
michael@0 | 8 | #include "primpl.h" |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | ** Create a new semaphore object. |
michael@0 | 12 | */ |
michael@0 | 13 | PR_IMPLEMENT(PRSemaphore*) |
michael@0 | 14 | PR_NewSem (PRUintn value) |
michael@0 | 15 | { |
michael@0 | 16 | PRSemaphore *semaphore; |
michael@0 | 17 | |
michael@0 | 18 | if (!_pr_initialized) _PR_ImplicitInitialization(); |
michael@0 | 19 | |
michael@0 | 20 | semaphore = PR_NEWZAP(PRSemaphore); |
michael@0 | 21 | if (NULL != semaphore) { |
michael@0 | 22 | if ((semaphore->sem = create_sem(value, "nspr_sem")) < B_NO_ERROR) |
michael@0 | 23 | return NULL; |
michael@0 | 24 | else |
michael@0 | 25 | return semaphore; |
michael@0 | 26 | } |
michael@0 | 27 | return NULL; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | /* |
michael@0 | 31 | ** Destroy the given semaphore object. |
michael@0 | 32 | ** |
michael@0 | 33 | */ |
michael@0 | 34 | PR_IMPLEMENT(void) |
michael@0 | 35 | PR_DestroySem (PRSemaphore *sem) |
michael@0 | 36 | { |
michael@0 | 37 | status_t result; |
michael@0 | 38 | |
michael@0 | 39 | PR_ASSERT(sem != NULL); |
michael@0 | 40 | result = delete_sem(sem->sem); |
michael@0 | 41 | PR_ASSERT(result == B_NO_ERROR); |
michael@0 | 42 | PR_DELETE(sem); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | /* |
michael@0 | 46 | ** Wait on a Semaphore. |
michael@0 | 47 | ** |
michael@0 | 48 | ** This routine allows a calling thread to wait or proceed depending upon |
michael@0 | 49 | ** the state of the semahore sem. The thread can proceed only if the |
michael@0 | 50 | ** counter value of the semaphore sem is currently greater than 0. If the |
michael@0 | 51 | ** value of semaphore sem is positive, it is decremented by one and the |
michael@0 | 52 | ** routine returns immediately allowing the calling thread to continue. If |
michael@0 | 53 | ** the value of semaphore sem is 0, the calling thread blocks awaiting the |
michael@0 | 54 | ** semaphore to be released by another thread. |
michael@0 | 55 | ** |
michael@0 | 56 | ** This routine can return PR_PENDING_INTERRUPT if the waiting thread |
michael@0 | 57 | ** has been interrupted. |
michael@0 | 58 | */ |
michael@0 | 59 | PR_IMPLEMENT(PRStatus) |
michael@0 | 60 | PR_WaitSem (PRSemaphore *sem) |
michael@0 | 61 | { |
michael@0 | 62 | PR_ASSERT(sem != NULL); |
michael@0 | 63 | if (acquire_sem(sem->sem) == B_NO_ERROR) |
michael@0 | 64 | return PR_SUCCESS; |
michael@0 | 65 | else |
michael@0 | 66 | return PR_FAILURE; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | /* |
michael@0 | 70 | ** This routine increments the counter value of the semaphore. If other |
michael@0 | 71 | ** threads are blocked for the semaphore, then the scheduler will |
michael@0 | 72 | ** determine which ONE thread will be unblocked. |
michael@0 | 73 | */ |
michael@0 | 74 | PR_IMPLEMENT(void) |
michael@0 | 75 | PR_PostSem (PRSemaphore *sem) |
michael@0 | 76 | { |
michael@0 | 77 | status_t result; |
michael@0 | 78 | |
michael@0 | 79 | PR_ASSERT(sem != NULL); |
michael@0 | 80 | result = release_sem_etc(sem->sem, 1, B_DO_NOT_RESCHEDULE); |
michael@0 | 81 | PR_ASSERT(result == B_NO_ERROR); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* |
michael@0 | 85 | ** Returns the value of the semaphore referenced by sem without affecting |
michael@0 | 86 | ** the state of the semaphore. The value represents the semaphore value |
michael@0 | 87 | ** at the time of the call, but may not be the actual value when the |
michael@0 | 88 | ** caller inspects it. |
michael@0 | 89 | */ |
michael@0 | 90 | PR_IMPLEMENT(PRUintn) |
michael@0 | 91 | PR_GetValueSem (PRSemaphore *sem) |
michael@0 | 92 | { |
michael@0 | 93 | sem_info info; |
michael@0 | 94 | |
michael@0 | 95 | PR_ASSERT(sem != NULL); |
michael@0 | 96 | get_sem_info(sem->sem, &info); |
michael@0 | 97 | return info.count; |
michael@0 | 98 | } |