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  * Notifies just get posted to the monitor. The actual notification is done
12  * when the monitor is fully exited so that MP systems don't contend for a
13  * monitor that they can't enter.
14  */
_PR_PostNotifyToMonitor(PRMonitor * mon,PRBool broadcast)15 static void _PR_PostNotifyToMonitor(PRMonitor *mon, PRBool broadcast)
16 {
17     PR_ASSERT(mon != NULL);
18     PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mon);
19 
20     /* mon->notifyTimes is protected by the monitor, so we don't need to
21      * acquire mon->lock.
22      */
23     if (broadcast) {
24         mon->notifyTimes = -1;
25     }
26     else if (mon->notifyTimes != -1) {
27         mon->notifyTimes += 1;
28     }
29 }
30 
_PR_PostNotifiesFromMonitor(PRCondVar * cv,PRIntn times)31 static void _PR_PostNotifiesFromMonitor(PRCondVar *cv, PRIntn times)
32 {
33     PRStatus rv;
34 
35     /*
36      * Time to actually notify any waits that were affected while the monitor
37      * was entered.
38      */
39     PR_ASSERT(cv != NULL);
40     PR_ASSERT(times != 0);
41     if (times == -1) {
42         rv = PR_NotifyAllCondVar(cv);
43         PR_ASSERT(rv == PR_SUCCESS);
44     } else {
45         while (times-- > 0) {
46             rv = PR_NotifyCondVar(cv);
47             PR_ASSERT(rv == PR_SUCCESS);
48         }
49     }
50 }
51 
52 /*
53 ** Create a new monitor.
54 */
PR_NewMonitor()55 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
56 {
57     PRMonitor *mon;
58     PRStatus rv;
59 
60     if (!_pr_initialized) {
61         _PR_ImplicitInitialization();
62     }
63 
64     mon = PR_NEWZAP(PRMonitor);
65     if (mon == NULL) {
66         PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
67         return NULL;
68     }
69 
70     rv = _PR_InitLock(&mon->lock);
71     PR_ASSERT(rv == PR_SUCCESS);
72     if (rv != PR_SUCCESS) {
73         goto error1;
74     }
75 
76     mon->owner = NULL;
77 
78     rv = _PR_InitCondVar(&mon->entryCV, &mon->lock);
79     PR_ASSERT(rv == PR_SUCCESS);
80     if (rv != PR_SUCCESS) {
81         goto error2;
82     }
83 
84     rv = _PR_InitCondVar(&mon->waitCV, &mon->lock);
85     PR_ASSERT(rv == PR_SUCCESS);
86     if (rv != PR_SUCCESS) {
87         goto error3;
88     }
89 
90     mon->notifyTimes = 0;
91     mon->entryCount = 0;
92     mon->name = NULL;
93     return mon;
94 
95 error3:
96     _PR_FreeCondVar(&mon->entryCV);
97 error2:
98     _PR_FreeLock(&mon->lock);
99 error1:
100     PR_Free(mon);
101     return NULL;
102 }
103 
PR_NewNamedMonitor(const char * name)104 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
105 {
106     PRMonitor* mon = PR_NewMonitor();
107     if (mon) {
108         mon->name = name;
109     }
110     return mon;
111 }
112 
113 /*
114 ** Destroy a monitor. There must be no thread waiting on the monitor's
115 ** condition variable. The caller is responsible for guaranteeing that the
116 ** monitor is no longer in use.
117 */
PR_DestroyMonitor(PRMonitor * mon)118 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
119 {
120     PR_ASSERT(mon != NULL);
121     _PR_FreeCondVar(&mon->waitCV);
122     _PR_FreeCondVar(&mon->entryCV);
123     _PR_FreeLock(&mon->lock);
124 #if defined(DEBUG)
125     memset(mon, 0xaf, sizeof(PRMonitor));
126 #endif
127     PR_Free(mon);
128 }
129 
130 /*
131 ** Enter the lock associated with the monitor.
132 */
PR_EnterMonitor(PRMonitor * mon)133 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
134 {
135     PRThread *me = _PR_MD_CURRENT_THREAD();
136     PRStatus rv;
137 
138     PR_ASSERT(mon != NULL);
139     PR_Lock(&mon->lock);
140     if (mon->entryCount != 0) {
141         if (mon->owner == me) {
142             goto done;
143         }
144         while (mon->entryCount != 0) {
145             rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
146             PR_ASSERT(rv == PR_SUCCESS);
147         }
148     }
149     /* and now I have the monitor */
150     PR_ASSERT(mon->notifyTimes == 0);
151     PR_ASSERT(mon->owner == NULL);
152     mon->owner = me;
153 
154 done:
155     mon->entryCount += 1;
156     rv = PR_Unlock(&mon->lock);
157     PR_ASSERT(rv == PR_SUCCESS);
158 }
159 
160 /*
161 ** Test and then enter the lock associated with the monitor if it's not
162 ** already entered by some other thread. Return PR_FALSE if some other
163 ** thread owned the lock at the time of the call.
164 */
PR_TestAndEnterMonitor(PRMonitor * mon)165 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
166 {
167     PRThread *me = _PR_MD_CURRENT_THREAD();
168     PRStatus rv;
169 
170     PR_ASSERT(mon != NULL);
171     PR_Lock(&mon->lock);
172     if (mon->entryCount != 0) {
173         if (mon->owner == me) {
174             goto done;
175         }
176         rv = PR_Unlock(&mon->lock);
177         PR_ASSERT(rv == PR_SUCCESS);
178         return PR_FALSE;
179     }
180     /* and now I have the monitor */
181     PR_ASSERT(mon->notifyTimes == 0);
182     PR_ASSERT(mon->owner == NULL);
183     mon->owner = me;
184 
185 done:
186     mon->entryCount += 1;
187     rv = PR_Unlock(&mon->lock);
188     PR_ASSERT(rv == PR_SUCCESS);
189     return PR_TRUE;
190 }
191 
192 /*
193 ** Exit the lock associated with the monitor once.
194 */
PR_ExitMonitor(PRMonitor * mon)195 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
196 {
197     PRThread *me = _PR_MD_CURRENT_THREAD();
198     PRStatus rv;
199 
200     PR_ASSERT(mon != NULL);
201     PR_Lock(&mon->lock);
202     /* the entries should be > 0 and we'd better be the owner */
203     PR_ASSERT(mon->entryCount > 0);
204     PR_ASSERT(mon->owner == me);
205     if (mon->entryCount == 0 || mon->owner != me)
206     {
207         rv = PR_Unlock(&mon->lock);
208         PR_ASSERT(rv == PR_SUCCESS);
209         return PR_FAILURE;
210     }
211 
212     mon->entryCount -= 1;  /* reduce by one */
213     if (mon->entryCount == 0)
214     {
215         /* and if it transitioned to zero - notify an entry waiter */
216         /* make the owner unknown */
217         mon->owner = NULL;
218         if (mon->notifyTimes != 0) {
219             _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
220             mon->notifyTimes = 0;
221         }
222         rv = PR_NotifyCondVar(&mon->entryCV);
223         PR_ASSERT(rv == PR_SUCCESS);
224     }
225     rv = PR_Unlock(&mon->lock);
226     PR_ASSERT(rv == PR_SUCCESS);
227     return PR_SUCCESS;
228 }
229 
230 /*
231 ** Return the number of times that the current thread has entered the
232 ** lock. Returns zero if the current thread has not entered the lock.
233 */
PR_GetMonitorEntryCount(PRMonitor * mon)234 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
235 {
236     PRThread *me = _PR_MD_CURRENT_THREAD();
237     PRStatus rv;
238     PRIntn count = 0;
239 
240     PR_Lock(&mon->lock);
241     if (mon->owner == me) {
242         count = mon->entryCount;
243     }
244     rv = PR_Unlock(&mon->lock);
245     PR_ASSERT(rv == PR_SUCCESS);
246     return count;
247 }
248 
PR_AssertCurrentThreadInMonitor(PRMonitor * mon)249 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
250 {
251 #if defined(DEBUG) || defined(FORCE_PR_ASSERT)
252     PRStatus rv;
253 
254     PR_Lock(&mon->lock);
255     PR_ASSERT(mon->entryCount != 0 &&
256               mon->owner == _PR_MD_CURRENT_THREAD());
257     rv = PR_Unlock(&mon->lock);
258     PR_ASSERT(rv == PR_SUCCESS);
259 #endif
260 }
261 
262 /*
263 ** Wait for a notify on the condition variable. Sleep for "ticks" amount
264 ** of time (if "tick" is 0 then the sleep is indefinite). While
265 ** the thread is waiting it exits the monitors lock (as if it called
266 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor).  When
267 ** the wait has finished the thread regains control of the monitors lock
268 ** with the same entry count as before the wait began.
269 **
270 ** The thread waiting on the monitor will be resumed when the monitor is
271 ** notified (assuming the thread is the next in line to receive the
272 ** notify) or when the "ticks" elapses.
273 **
274 ** Returns PR_FAILURE if the caller has not locked the lock associated
275 ** with the condition variable.
276 ** This routine can return PR_PENDING_INTERRUPT_ERROR if the waiting thread
277 ** has been interrupted.
278 */
PR_Wait(PRMonitor * mon,PRIntervalTime ticks)279 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
280 {
281     PRStatus rv;
282     PRUint32 saved_entries;
283     PRThread *saved_owner;
284 
285     PR_ASSERT(mon != NULL);
286     PR_Lock(&mon->lock);
287     /* the entries better be positive */
288     PR_ASSERT(mon->entryCount > 0);
289     /* and it better be owned by us */
290     PR_ASSERT(mon->owner == _PR_MD_CURRENT_THREAD());  /* XXX return failure */
291 
292     /* tuck these away 'till later */
293     saved_entries = mon->entryCount;
294     mon->entryCount = 0;
295     saved_owner = mon->owner;
296     mon->owner = NULL;
297     /* If we have pending notifies, post them now. */
298     if (mon->notifyTimes != 0) {
299         _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
300         mon->notifyTimes = 0;
301     }
302     rv = PR_NotifyCondVar(&mon->entryCV);
303     PR_ASSERT(rv == PR_SUCCESS);
304 
305     rv = PR_WaitCondVar(&mon->waitCV, ticks);
306     PR_ASSERT(rv == PR_SUCCESS);
307 
308     while (mon->entryCount != 0) {
309         rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
310         PR_ASSERT(rv == PR_SUCCESS);
311     }
312     PR_ASSERT(mon->notifyTimes == 0);
313     /* reinstate the interesting information */
314     mon->entryCount = saved_entries;
315     mon->owner = saved_owner;
316 
317     rv = PR_Unlock(&mon->lock);
318     PR_ASSERT(rv == PR_SUCCESS);
319     return rv;
320 }
321 
322 /*
323 ** Notify the highest priority thread waiting on the condition
324 ** variable. If a thread is waiting on the condition variable (using
325 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
326 */
PR_Notify(PRMonitor * mon)327 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
328 {
329     _PR_PostNotifyToMonitor(mon, PR_FALSE);
330     return PR_SUCCESS;
331 }
332 
333 /*
334 ** Notify all of the threads waiting on the condition variable. All of
335 ** threads are notified in turn. The highest priority thread will
336 ** probably acquire the monitor first when the monitor is exited.
337 */
PR_NotifyAll(PRMonitor * mon)338 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
339 {
340     _PR_PostNotifyToMonitor(mon, PR_TRUE);
341     return PR_SUCCESS;
342 }
343 
344 /************************************************************************/
345 
_PR_MonitorToString(PRMonitor * mon,char * buf,PRUint32 buflen)346 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
347 {
348     PRUint32 nb;
349 
350     if (mon->owner) {
351         nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
352                          mon, mon->owner->id, mon->owner, mon->entryCount);
353     } else {
354         nb = PR_snprintf(buf, buflen, "[%p]", mon);
355     }
356     return nb;
357 }
358