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