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 monitor. Monitors are re-entrant locks with a single built-in |
michael@0 | 12 | ** condition variable. |
michael@0 | 13 | ** |
michael@0 | 14 | ** This may fail if memory is tight or if some operating system resource |
michael@0 | 15 | ** is low. |
michael@0 | 16 | */ |
michael@0 | 17 | PR_IMPLEMENT(PRMonitor*) |
michael@0 | 18 | PR_NewMonitor (void) |
michael@0 | 19 | { |
michael@0 | 20 | PRMonitor *mon; |
michael@0 | 21 | PRCondVar *cvar; |
michael@0 | 22 | PRLock *lock; |
michael@0 | 23 | |
michael@0 | 24 | mon = PR_NEWZAP( PRMonitor ); |
michael@0 | 25 | if( mon ) |
michael@0 | 26 | { |
michael@0 | 27 | lock = PR_NewLock(); |
michael@0 | 28 | if( !lock ) |
michael@0 | 29 | { |
michael@0 | 30 | PR_DELETE( mon ); |
michael@0 | 31 | return( 0 ); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | cvar = PR_NewCondVar( lock ); |
michael@0 | 35 | if( !cvar ) |
michael@0 | 36 | { |
michael@0 | 37 | PR_DestroyLock( lock ); |
michael@0 | 38 | PR_DELETE( mon ); |
michael@0 | 39 | return( 0 ); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | mon->cvar = cvar; |
michael@0 | 43 | mon->name = NULL; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | return( mon ); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name) |
michael@0 | 50 | { |
michael@0 | 51 | PRMonitor* mon = PR_NewMonitor(); |
michael@0 | 52 | if( mon ) |
michael@0 | 53 | { |
michael@0 | 54 | mon->name = name; |
michael@0 | 55 | } |
michael@0 | 56 | return mon; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /* |
michael@0 | 60 | ** Destroy a monitor. The caller is responsible for guaranteeing that the |
michael@0 | 61 | ** monitor is no longer in use. There must be no thread waiting on the |
michael@0 | 62 | ** monitor's condition variable and that the lock is not held. |
michael@0 | 63 | ** |
michael@0 | 64 | */ |
michael@0 | 65 | PR_IMPLEMENT(void) |
michael@0 | 66 | PR_DestroyMonitor (PRMonitor *mon) |
michael@0 | 67 | { |
michael@0 | 68 | PR_DestroyLock( mon->cvar->lock ); |
michael@0 | 69 | PR_DestroyCondVar( mon->cvar ); |
michael@0 | 70 | PR_DELETE( mon ); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | ** Enter the lock associated with the monitor. If the calling thread currently |
michael@0 | 75 | ** is in the monitor, the call to enter will silently succeed. In either case, |
michael@0 | 76 | ** it will increment the entry count by one. |
michael@0 | 77 | */ |
michael@0 | 78 | PR_IMPLEMENT(void) |
michael@0 | 79 | PR_EnterMonitor (PRMonitor *mon) |
michael@0 | 80 | { |
michael@0 | 81 | if( mon->cvar->lock->owner == find_thread( NULL ) ) |
michael@0 | 82 | { |
michael@0 | 83 | mon->entryCount++; |
michael@0 | 84 | |
michael@0 | 85 | } else |
michael@0 | 86 | { |
michael@0 | 87 | PR_Lock( mon->cvar->lock ); |
michael@0 | 88 | mon->entryCount = 1; |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /* |
michael@0 | 93 | ** Decrement the entry count associated with the monitor. If the decremented |
michael@0 | 94 | ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the |
michael@0 | 95 | ** calling thread has not entered the monitor. |
michael@0 | 96 | */ |
michael@0 | 97 | PR_IMPLEMENT(PRStatus) |
michael@0 | 98 | PR_ExitMonitor (PRMonitor *mon) |
michael@0 | 99 | { |
michael@0 | 100 | if( mon->cvar->lock->owner != find_thread( NULL ) ) |
michael@0 | 101 | { |
michael@0 | 102 | return( PR_FAILURE ); |
michael@0 | 103 | } |
michael@0 | 104 | if( --mon->entryCount == 0 ) |
michael@0 | 105 | { |
michael@0 | 106 | return( PR_Unlock( mon->cvar->lock ) ); |
michael@0 | 107 | } |
michael@0 | 108 | return( PR_SUCCESS ); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | /* |
michael@0 | 112 | ** Wait for a notify on the monitor's condition variable. Sleep for "ticks" |
michael@0 | 113 | ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is |
michael@0 | 114 | ** indefinite). |
michael@0 | 115 | ** |
michael@0 | 116 | ** While the thread is waiting it exits the monitor (as if it called |
michael@0 | 117 | ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When |
michael@0 | 118 | ** the wait has finished the thread regains control of the monitors lock |
michael@0 | 119 | ** with the same entry count as before the wait began. |
michael@0 | 120 | ** |
michael@0 | 121 | ** The thread waiting on the monitor will be resumed when the monitor is |
michael@0 | 122 | ** notified (assuming the thread is the next in line to receive the |
michael@0 | 123 | ** notify) or when the "ticks" timeout elapses. |
michael@0 | 124 | ** |
michael@0 | 125 | ** Returns PR_FAILURE if the caller has not entered the monitor. |
michael@0 | 126 | */ |
michael@0 | 127 | PR_IMPLEMENT(PRStatus) |
michael@0 | 128 | PR_Wait (PRMonitor *mon, PRIntervalTime ticks) |
michael@0 | 129 | { |
michael@0 | 130 | PRUint32 entryCount; |
michael@0 | 131 | PRUintn status; |
michael@0 | 132 | PRThread *meThread; |
michael@0 | 133 | thread_id me = find_thread( NULL ); |
michael@0 | 134 | meThread = PR_GetCurrentThread(); |
michael@0 | 135 | |
michael@0 | 136 | if( mon->cvar->lock->owner != me ) return( PR_FAILURE ); |
michael@0 | 137 | |
michael@0 | 138 | entryCount = mon->entryCount; |
michael@0 | 139 | mon->entryCount = 0; |
michael@0 | 140 | |
michael@0 | 141 | status = PR_WaitCondVar( mon->cvar, ticks ); |
michael@0 | 142 | |
michael@0 | 143 | mon->entryCount = entryCount; |
michael@0 | 144 | |
michael@0 | 145 | return( status ); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | /* |
michael@0 | 149 | ** Notify a thread waiting on the monitor's condition variable. If a thread |
michael@0 | 150 | ** is waiting on the condition variable (using PR_Wait) then it is awakened |
michael@0 | 151 | ** and attempts to reenter the monitor. |
michael@0 | 152 | */ |
michael@0 | 153 | PR_IMPLEMENT(PRStatus) |
michael@0 | 154 | PR_Notify (PRMonitor *mon) |
michael@0 | 155 | { |
michael@0 | 156 | if( mon->cvar->lock->owner != find_thread( NULL ) ) |
michael@0 | 157 | { |
michael@0 | 158 | return( PR_FAILURE ); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | PR_NotifyCondVar( mon->cvar ); |
michael@0 | 162 | return( PR_SUCCESS ); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | /* |
michael@0 | 166 | ** Notify all of the threads waiting on the monitor's condition variable. |
michael@0 | 167 | ** All of threads waiting on the condition are scheduled to reenter the |
michael@0 | 168 | ** monitor. |
michael@0 | 169 | */ |
michael@0 | 170 | PR_IMPLEMENT(PRStatus) |
michael@0 | 171 | PR_NotifyAll (PRMonitor *mon) |
michael@0 | 172 | { |
michael@0 | 173 | if( mon->cvar->lock->owner != find_thread( NULL ) ) |
michael@0 | 174 | { |
michael@0 | 175 | return( PR_FAILURE ); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | PR_NotifyAllCondVar( mon->cvar ); |
michael@0 | 179 | return( PR_SUCCESS ); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /* |
michael@0 | 183 | ** Return the number of times that the current thread has entered the |
michael@0 | 184 | ** lock. Returns zero if the current thread has not entered the lock. |
michael@0 | 185 | */ |
michael@0 | 186 | PR_IMPLEMENT(PRIntn) |
michael@0 | 187 | PR_GetMonitorEntryCount(PRMonitor *mon) |
michael@0 | 188 | { |
michael@0 | 189 | return( (mon->cvar->lock->owner == find_thread( NULL )) ? |
michael@0 | 190 | mon->entryCount : 0 ); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | /* |
michael@0 | 194 | ** If the current thread is in |mon|, this assertion is guaranteed to |
michael@0 | 195 | ** succeed. Otherwise, the behavior of this function is undefined. |
michael@0 | 196 | */ |
michael@0 | 197 | PR_IMPLEMENT(void) |
michael@0 | 198 | PR_AssertCurrentThreadInMonitor(PRMonitor *mon) |
michael@0 | 199 | { |
michael@0 | 200 | PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock); |
michael@0 | 201 | } |