Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 | #ifndef prmon_h___ |
michael@0 | 7 | #define prmon_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "prtypes.h" |
michael@0 | 10 | #include "prinrval.h" |
michael@0 | 11 | |
michael@0 | 12 | PR_BEGIN_EXTERN_C |
michael@0 | 13 | |
michael@0 | 14 | typedef struct PRMonitor PRMonitor; |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | ** Create a new monitor. Monitors are re-entrant locks with a single built-in |
michael@0 | 18 | ** condition variable. |
michael@0 | 19 | ** |
michael@0 | 20 | ** This may fail if memory is tight or if some operating system resource |
michael@0 | 21 | ** is low. |
michael@0 | 22 | */ |
michael@0 | 23 | NSPR_API(PRMonitor*) PR_NewMonitor(void); |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | ** Destroy a monitor. The caller is responsible for guaranteeing that the |
michael@0 | 27 | ** monitor is no longer in use. There must be no thread waiting on the monitor's |
michael@0 | 28 | ** condition variable and that the lock is not held. |
michael@0 | 29 | ** |
michael@0 | 30 | */ |
michael@0 | 31 | NSPR_API(void) PR_DestroyMonitor(PRMonitor *mon); |
michael@0 | 32 | |
michael@0 | 33 | /* |
michael@0 | 34 | ** Enter the lock associated with the monitor. If the calling thread currently |
michael@0 | 35 | ** is in the monitor, the call to enter will silently succeed. In either case, |
michael@0 | 36 | ** it will increment the entry count by one. |
michael@0 | 37 | */ |
michael@0 | 38 | NSPR_API(void) PR_EnterMonitor(PRMonitor *mon); |
michael@0 | 39 | |
michael@0 | 40 | /* |
michael@0 | 41 | ** Decrement the entry count associated with the monitor. If the decremented |
michael@0 | 42 | ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the |
michael@0 | 43 | ** calling thread has not entered the monitor. |
michael@0 | 44 | */ |
michael@0 | 45 | NSPR_API(PRStatus) PR_ExitMonitor(PRMonitor *mon); |
michael@0 | 46 | |
michael@0 | 47 | /* |
michael@0 | 48 | ** Wait for a notify on the monitor's condition variable. Sleep for "ticks" |
michael@0 | 49 | ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is |
michael@0 | 50 | ** indefinite). |
michael@0 | 51 | ** |
michael@0 | 52 | ** While the thread is waiting it exits the monitor (as if it called |
michael@0 | 53 | ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When |
michael@0 | 54 | ** the wait has finished the thread regains control of the monitors lock |
michael@0 | 55 | ** with the same entry count as before the wait began. |
michael@0 | 56 | ** |
michael@0 | 57 | ** The thread waiting on the monitor will be resumed when the monitor is |
michael@0 | 58 | ** notified (assuming the thread is the next in line to receive the |
michael@0 | 59 | ** notify) or when the "ticks" timeout elapses. |
michael@0 | 60 | ** |
michael@0 | 61 | ** Returns PR_FAILURE if the caller has not entered the monitor. |
michael@0 | 62 | */ |
michael@0 | 63 | NSPR_API(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks); |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | ** Notify a thread waiting on the monitor's condition variable. If a thread |
michael@0 | 67 | ** is waiting on the condition variable (using PR_Wait) then it is awakened |
michael@0 | 68 | ** and attempts to reenter the monitor. |
michael@0 | 69 | */ |
michael@0 | 70 | NSPR_API(PRStatus) PR_Notify(PRMonitor *mon); |
michael@0 | 71 | |
michael@0 | 72 | /* |
michael@0 | 73 | ** Notify all of the threads waiting on the monitor's condition variable. |
michael@0 | 74 | ** All of threads waiting on the condition are scheduled to reenter the |
michael@0 | 75 | ** monitor. |
michael@0 | 76 | */ |
michael@0 | 77 | NSPR_API(PRStatus) PR_NotifyAll(PRMonitor *mon); |
michael@0 | 78 | |
michael@0 | 79 | /* |
michael@0 | 80 | ** PR_ASSERT_CURRENT_THREAD_IN_MONITOR |
michael@0 | 81 | ** If the current thread is in |mon|, this assertion is guaranteed to |
michael@0 | 82 | ** succeed. Otherwise, the behavior of this function is undefined. |
michael@0 | 83 | */ |
michael@0 | 84 | #if defined(DEBUG) || defined(FORCE_PR_ASSERT) |
michael@0 | 85 | #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon) \ |
michael@0 | 86 | PR_AssertCurrentThreadInMonitor(mon) |
michael@0 | 87 | #else |
michael@0 | 88 | #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon) |
michael@0 | 89 | #endif |
michael@0 | 90 | |
michael@0 | 91 | /* Don't call this function directly. */ |
michael@0 | 92 | NSPR_API(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon); |
michael@0 | 93 | |
michael@0 | 94 | PR_END_EXTERN_C |
michael@0 | 95 | |
michael@0 | 96 | #endif /* prmon_h___ */ |