OLD | NEW |
| (Empty) |
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 #include "primpl.h" | |
7 | |
8 /************************************************************************/ | |
9 | |
10 /* | |
11 ** Create a new monitor. | |
12 */ | |
13 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor() | |
14 { | |
15 PRMonitor *mon; | |
16 PRCondVar *cvar; | |
17 PRLock *lock; | |
18 | |
19 mon = PR_NEWZAP(PRMonitor); | |
20 if (mon) { | |
21 lock = PR_NewLock(); | |
22 if (!lock) { | |
23 PR_DELETE(mon); | |
24 return 0; | |
25 } | |
26 | |
27 cvar = PR_NewCondVar(lock); | |
28 if (!cvar) { | |
29 PR_DestroyLock(lock); | |
30 PR_DELETE(mon); | |
31 return 0; | |
32 } | |
33 mon->cvar = cvar; | |
34 mon->name = NULL; | |
35 } | |
36 return mon; | |
37 } | |
38 | |
39 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name) | |
40 { | |
41 PRMonitor* mon = PR_NewMonitor(); | |
42 if (mon) | |
43 mon->name = name; | |
44 return mon; | |
45 } | |
46 | |
47 /* | |
48 ** Destroy a monitor. There must be no thread waiting on the monitor's | |
49 ** condition variable. The caller is responsible for guaranteeing that the | |
50 ** monitor is no longer in use. | |
51 */ | |
52 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon) | |
53 { | |
54 PR_DestroyLock(mon->cvar->lock); | |
55 PR_DestroyCondVar(mon->cvar); | |
56 PR_DELETE(mon); | |
57 } | |
58 | |
59 /* | |
60 ** Enter the lock associated with the monitor. | |
61 */ | |
62 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon) | |
63 { | |
64 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) { | |
65 mon->entryCount++; | |
66 } else { | |
67 PR_Lock(mon->cvar->lock); | |
68 mon->entryCount = 1; | |
69 } | |
70 } | |
71 | |
72 /* | |
73 ** Test and then enter the lock associated with the monitor if it's not | |
74 ** already entered by some other thread. Return PR_FALSE if some other | |
75 ** thread owned the lock at the time of the call. | |
76 */ | |
77 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon) | |
78 { | |
79 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) { | |
80 mon->entryCount++; | |
81 return PR_TRUE; | |
82 } else { | |
83 if (PR_TestAndLock(mon->cvar->lock)) { | |
84 mon->entryCount = 1; | |
85 return PR_TRUE; | |
86 } | |
87 } | |
88 return PR_FALSE; | |
89 } | |
90 | |
91 /* | |
92 ** Exit the lock associated with the monitor once. | |
93 */ | |
94 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon) | |
95 { | |
96 if (mon->cvar->lock->owner != _PR_MD_CURRENT_THREAD()) { | |
97 return PR_FAILURE; | |
98 } | |
99 if (--mon->entryCount == 0) { | |
100 return PR_Unlock(mon->cvar->lock); | |
101 } | |
102 return PR_SUCCESS; | |
103 } | |
104 | |
105 /* | |
106 ** Return the number of times that the current thread has entered the | |
107 ** lock. Returns zero if the current thread has not entered the lock. | |
108 */ | |
109 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon) | |
110 { | |
111 return (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) ? | |
112 mon->entryCount : 0; | |
113 } | |
114 | |
115 /* | |
116 ** If the current thread is in |mon|, this assertion is guaranteed to | |
117 ** succeed. Otherwise, the behavior of this function is undefined. | |
118 */ | |
119 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon) | |
120 { | |
121 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock); | |
122 } | |
123 | |
124 /* | |
125 ** Wait for a notify on the condition variable. Sleep for "ticks" amount | |
126 ** of time (if "tick" is 0 then the sleep is indefinite). While | |
127 ** the thread is waiting it exits the monitors lock (as if it called | |
128 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When | |
129 ** the wait has finished the thread regains control of the monitors lock | |
130 ** with the same entry count as before the wait began. | |
131 ** | |
132 ** The thread waiting on the monitor will be resumed when the monitor is | |
133 ** notified (assuming the thread is the next in line to receive the | |
134 ** notify) or when the "ticks" elapses. | |
135 ** | |
136 ** Returns PR_FAILURE if the caller has not locked the lock associated | |
137 ** with the condition variable. | |
138 ** This routine can return PR_PENDING_INTERRUPT if the waiting thread | |
139 ** has been interrupted. | |
140 */ | |
141 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks) | |
142 { | |
143 PRUintn entryCount; | |
144 PRStatus status; | |
145 PRThread *me = _PR_MD_CURRENT_THREAD(); | |
146 | |
147 if (mon->cvar->lock->owner != me) return PR_FAILURE; | |
148 | |
149 entryCount = mon->entryCount; | |
150 mon->entryCount = 0; | |
151 | |
152 status = _PR_WaitCondVar(me, mon->cvar, mon->cvar->lock, ticks); | |
153 | |
154 mon->entryCount = entryCount; | |
155 | |
156 return status; | |
157 } | |
158 | |
159 /* | |
160 ** Notify the highest priority thread waiting on the condition | |
161 ** variable. If a thread is waiting on the condition variable (using | |
162 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock. | |
163 */ | |
164 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon) | |
165 { | |
166 PRThread *me = _PR_MD_CURRENT_THREAD(); | |
167 if (mon->cvar->lock->owner != me) return PR_FAILURE; | |
168 PR_NotifyCondVar(mon->cvar); | |
169 return PR_SUCCESS; | |
170 } | |
171 | |
172 /* | |
173 ** Notify all of the threads waiting on the condition variable. All of | |
174 ** threads are notified in turn. The highest priority thread will | |
175 ** probably acquire the monitor first when the monitor is exited. | |
176 */ | |
177 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon) | |
178 { | |
179 PRThread *me = _PR_MD_CURRENT_THREAD(); | |
180 if (mon->cvar->lock->owner != me) return PR_FAILURE; | |
181 PR_NotifyAllCondVar(mon->cvar); | |
182 return PR_SUCCESS; | |
183 } | |
184 | |
185 /************************************************************************/ | |
186 | |
187 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen) | |
188 { | |
189 PRUint32 nb; | |
190 | |
191 if (mon->cvar->lock->owner) { | |
192 nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld", | |
193 mon, mon->cvar->lock->owner->id, | |
194 mon->cvar->lock->owner, mon->entryCount); | |
195 } else { | |
196 nb = PR_snprintf(buf, buflen, "[%p]", mon); | |
197 } | |
198 return nb; | |
199 } | |
OLD | NEW |