2 * monitor.c: Monitor locking functions
5 * Dick Porter (dick@ximian.com)
7 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/monitor.h>
14 #include <mono/metadata/threads-types.h>
15 #include <mono/metadata/exception.h>
16 #include <mono/metadata/threads.h>
17 #include <mono/io-layer/io-layer.h>
18 #include <mono/metadata/object-internals.h>
20 #include <mono/os/gc_wrapper.h>
22 #undef THREAD_LOCK_DEBUG
25 * The monitor implementation here is based on
26 * http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf and
27 * http://www.research.ibm.com/people/d/dfb/papers/Bacon98Thin.ps
29 * The Dice paper describes a technique for saving lock record space
30 * by returning records to a free list when they become unused. That
31 * sounds like unnecessary complexity to me, though if it becomes
32 * clear that unused lock records are taking up lots of space or we
33 * need to shave more time off by avoiding a malloc then we can always
34 * implement the free list idea later. The timeout parameter to
35 * try_enter voids some of the assumptions about the reference count
36 * field in Dice's implementation too. In his version, the thread
37 * attempting to lock a contended object will block until it succeeds,
38 * so the reference count will never be decremented while an object is
41 * Bacon's thin locks have a fast path that doesn't need a lock record
42 * for the common case of locking an unlocked or shallow-nested
43 * object, but the technique relies on encoding the thread ID in 15
44 * bits (to avoid too much per-object space overhead.) Unfortunately
45 * I don't think it's possible to reliably encode a pthread_t into 15
46 * bits. (The JVM implementation used seems to have a 15-bit
47 * per-thread identifier available.)
49 * This implementation then combines Dice's basic lock model with
50 * Bacon's simplification of keeping a lock record for the lifetime of
55 static void mon_finalize (void *o, void *unused)
57 MonoThreadsSync *mon=(MonoThreadsSync *)o;
59 #ifdef THREAD_LOCK_DEBUG
60 g_message (G_GNUC_PRETTY_FUNCTION ": Finalizing sync %p", mon);
63 if(mon->entry_sem!=NULL) {
64 CloseHandle (mon->entry_sem);
66 /* If this isn't empty then something is seriously broken - it
67 * means a thread is still waiting on the object that owned
68 * this lock, but the object has been finalized.
70 g_assert (mon->wait_list==NULL);
73 static MonoThreadsSync *mon_new(guint32 id)
78 new=(MonoThreadsSync *)GC_MALLOC (sizeof(MonoThreadsSync));
79 GC_REGISTER_FINALIZER (new, mon_finalize, NULL, NULL, NULL);
81 /* This should be freed when the object that owns it is
84 new=(MonoThreadsSync *)g_new0 (MonoThreadsSync, 1);
92 /* If allow_interruption==TRUE, the method will be interrumped if abort or suspend
93 * is requested. In this case it returns -1.
95 static gint32 mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
98 guint32 id=GetCurrentThreadId ();
100 guint32 then=0, now, delta;
104 #ifdef THREAD_LOCK_DEBUG
105 g_message(G_GNUC_PRETTY_FUNCTION
106 ": (%d) Trying to lock object %p (%d ms)", id, obj, ms);
110 mon=obj->synchronisation;
112 /* If the object has never been locked... */
115 if(InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, mon, NULL)==NULL) {
116 /* Successfully locked */
119 /* Another thread got in first, so try again.
120 * GC will take care of the monitor record
122 #ifndef HAVE_BOEHM_GC
123 mon_finalize (mon, NULL);
129 /* If the object is currently locked by this thread... */
135 /* If the object has previously been locked but isn't now... */
137 /* This case differs from Dice's case 3 because we don't
138 * deflate locks or cache unused lock records
141 /* Try to install our ID in the owner field, nest
142 * should have been left at 1 by the previous unlock
145 if(InterlockedCompareExchange (&mon->owner, id, 0)==0) {
147 g_assert (mon->nest==1);
155 /* The object must be locked by someone else... */
157 /* If ms is 0 we don't block, but just fail straight away */
159 #ifdef THREAD_LOCK_DEBUG
160 g_message (G_GNUC_PRETTY_FUNCTION
161 ": (%d) timed out, returning FALSE", id);
167 /* The slow path begins here. We need to make sure theres a
168 * semaphore handle (creating it if necessary), and block on
171 if(mon->entry_sem==NULL) {
172 /* Create the semaphore */
173 sem=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
174 if(InterlockedCompareExchangePointer ((gpointer*)&mon->entry_sem, sem, NULL)!=NULL) {
175 /* Someone else just put a handle here */
180 /* If we need to time out, record a timestamp and adjust ms,
181 * because WaitForSingleObject doesn't tell us how long it
184 * Don't block forever here, because theres a chance the owner
185 * thread released the lock while we were creating the
186 * semaphore: we would not get the wakeup. Using the event
187 * handle technique from pulse/wait would involve locking the
188 * lock struct and therefore slowing down the fast path.
191 then=GetTickCount ();
201 InterlockedIncrement (&mon->entry_count);
202 ret=WaitForSingleObjectEx (mon->entry_sem, waitms, allow_interruption);
203 InterlockedDecrement (&mon->entry_count);
209 /* The counter must have wrapped around */
210 #ifdef THREAD_LOCK_DEBUG
211 g_message (G_GNUC_PRETTY_FUNCTION
212 ": wrapped around! now=0x%x then=0x%x",
216 now+=(0xffffffff - then);
219 #ifdef THREAD_LOCK_DEBUG
220 g_message (G_GNUC_PRETTY_FUNCTION ": wrap rejig: now=0x%x then=0x%x delta=0x%x", now, then, now-then);
231 if((ret==WAIT_TIMEOUT || (ret==WAIT_IO_COMPLETION && !allow_interruption)) && ms>0) {
236 if(ret==WAIT_TIMEOUT || (ret==WAIT_IO_COMPLETION && !allow_interruption)) {
237 /* Infinite wait, so just try again */
242 if(ret==WAIT_OBJECT_0) {
243 /* retry from the top */
247 /* We must have timed out */
248 #ifdef THREAD_LOCK_DEBUG
249 g_message (G_GNUC_PRETTY_FUNCTION
250 ": (%d) timed out waiting, returning FALSE", id);
253 if (ret==WAIT_IO_COMPLETION) return(-1);
257 gboolean mono_monitor_enter (MonoObject *obj)
259 return mono_monitor_try_enter_internal (obj, INFINITE, FALSE) == 1;
262 gboolean mono_monitor_try_enter (MonoObject *obj, guint32 ms)
264 return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
267 void mono_monitor_exit (MonoObject *obj)
269 MonoThreadsSync *mon;
272 #ifdef THREAD_LOCK_DEBUG
273 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Unlocking %p",
274 GetCurrentThreadId (), obj);
277 mon=obj->synchronisation;
280 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
283 if(mon->owner!=GetCurrentThreadId ()) {
289 #ifdef THREAD_LOCK_DEBUG
290 g_message(G_GNUC_PRETTY_FUNCTION
291 ": (%d) Object %p is now unlocked",
292 GetCurrentThreadId (), obj);
295 /* object is now unlocked, leave nest==1 so we don't
296 * need to set it when the lock is reacquired
300 /* Do the wakeup stuff. It's possible that the last
301 * blocking thread gave up waiting just before we
302 * release the semaphore resulting in a futile wakeup
303 * next time there's contention for this object, but
304 * it means we don't have to waste time locking the
307 if(mon->entry_count>0) {
308 ReleaseSemaphore (mon->entry_sem, 1, NULL);
311 #ifdef THREAD_LOCK_DEBUG
312 g_message(G_GNUC_PRETTY_FUNCTION
313 ": (%d) Object %p is now locked %d times",
314 GetCurrentThreadId (), obj,
321 gboolean ves_icall_System_Threading_Monitor_Monitor_try_enter(MonoObject *obj,
328 res = mono_monitor_try_enter_internal (obj, ms, TRUE);
330 mono_thread_interruption_checkpoint ();
337 void ves_icall_System_Threading_Monitor_Monitor_exit(MonoObject *obj)
341 mono_monitor_exit (obj);
344 gboolean ves_icall_System_Threading_Monitor_Monitor_test_owner(MonoObject *obj)
346 MonoThreadsSync *mon;
350 #ifdef THREAD_LOCK_DEBUG
351 g_message(G_GNUC_PRETTY_FUNCTION
352 ": Testing if %p is owned by thread %d", obj,
353 GetCurrentThreadId());
356 mon=obj->synchronisation;
361 if(mon->owner==GetCurrentThreadId ()) {
368 gboolean ves_icall_System_Threading_Monitor_Monitor_test_synchronised(MonoObject *obj)
370 MonoThreadsSync *mon;
374 #ifdef THREAD_LOCK_DEBUG
375 g_message(G_GNUC_PRETTY_FUNCTION
376 ": (%d) Testing if %p is owned by any thread",
377 GetCurrentThreadId (), obj);
380 mon=obj->synchronisation;
392 /* All wait list manipulation in the pulse, pulseall and wait
393 * functions happens while the monitor lock is held, so we don't need
394 * any extra struct locking
397 void ves_icall_System_Threading_Monitor_Monitor_pulse(MonoObject *obj)
399 MonoThreadsSync *mon;
403 #ifdef THREAD_LOCK_DEBUG
404 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing %p",
405 GetCurrentThreadId (), obj);
408 mon=obj->synchronisation;
410 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
413 if(mon->owner!=GetCurrentThreadId ()) {
414 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
418 #ifdef THREAD_LOCK_DEBUG
419 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
420 GetCurrentThreadId (), g_slist_length (mon->wait_list));
423 if(mon->wait_list!=NULL) {
424 #ifdef THREAD_LOCK_DEBUG
425 g_message(G_GNUC_PRETTY_FUNCTION
426 ": (%d) signalling and dequeuing handle %p",
427 GetCurrentThreadId (), mon->wait_list->data);
430 SetEvent (mon->wait_list->data);
431 mon->wait_list=g_slist_remove (mon->wait_list,
432 mon->wait_list->data);
436 void ves_icall_System_Threading_Monitor_Monitor_pulse_all(MonoObject *obj)
438 MonoThreadsSync *mon;
442 #ifdef THREAD_LOCK_DEBUG
443 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing all %p",
444 GetCurrentThreadId (), obj);
447 mon=obj->synchronisation;
449 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
452 if(mon->owner!=GetCurrentThreadId ()) {
453 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
457 #ifdef THREAD_LOCK_DEBUG
458 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
459 GetCurrentThreadId (), g_slist_length (mon->wait_list));
462 while(mon->wait_list!=NULL) {
463 #ifdef THREAD_LOCK_DEBUG
464 g_message(G_GNUC_PRETTY_FUNCTION
465 ": (%d) signalling and dequeuing handle %p",
466 GetCurrentThreadId (), mon->wait_list->data);
469 SetEvent (mon->wait_list->data);
470 mon->wait_list=g_slist_remove (mon->wait_list,
471 mon->wait_list->data);
475 gboolean ves_icall_System_Threading_Monitor_Monitor_wait(MonoObject *obj,
478 MonoThreadsSync *mon;
482 gboolean success=FALSE;
484 MonoThread *thread = mono_thread_current ();
488 #ifdef THREAD_LOCK_DEBUG
489 g_message(G_GNUC_PRETTY_FUNCTION
490 ": (%d) Trying to wait for %p with timeout %dms",
491 GetCurrentThreadId (), obj, ms);
494 mon=obj->synchronisation;
496 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
499 if(mon->owner!=GetCurrentThreadId ()) {
500 mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
504 event=CreateEvent (NULL, FALSE, FALSE, NULL);
506 mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
510 #ifdef THREAD_LOCK_DEBUG
511 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) queuing handle %p",
512 GetCurrentThreadId (), event);
515 mono_monitor_enter (thread->synch_lock);
516 thread->state |= ThreadState_WaitSleepJoin;
517 mono_monitor_exit (thread->synch_lock);
519 mon->wait_list=g_slist_append (mon->wait_list, event);
521 /* Save the nest count, and release the lock */
524 mono_monitor_exit (obj);
526 #ifdef THREAD_LOCK_DEBUG
527 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Unlocked %p lock %p",
528 GetCurrentThreadId (), obj, mon);
531 /* There's no race between unlocking mon and waiting for the
532 * event, because auto reset events are sticky, and this event
533 * is private to this thread. Therefore even if the event was
534 * signalled before we wait, we still succeed.
536 ret=WaitForSingleObjectEx (event, ms, TRUE);
538 /* Reset the thread state fairly early, so we don't have to worry
539 * about the monitor error checking
541 mono_monitor_enter (thread->synch_lock);
542 thread->state &= ~ThreadState_WaitSleepJoin;
543 mono_monitor_exit (thread->synch_lock);
545 if (mono_thread_interruption_requested ()) {
550 /* Regain the lock with the previous nest count */
552 regain=mono_monitor_try_enter_internal (obj, INFINITE, TRUE);
554 mono_thread_interruption_checkpoint ();
556 while (regain == -1);
559 /* Something went wrong, so throw a
560 * SynchronizationLockException
563 mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to regain lock"));
569 #ifdef THREAD_LOCK_DEBUG
570 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Regained %p lock %p",
571 GetCurrentThreadId (), obj, mon);
574 if(ret==WAIT_TIMEOUT) {
575 /* Poll the event again, just in case it was signalled
576 * while we were trying to regain the monitor lock
578 ret=WaitForSingleObjectEx (event, 0, FALSE);
581 /* Pulse will have popped our event from the queue if it signalled
582 * us, so we only do it here if the wait timed out.
584 * This avoids a race condition where the thread holding the
585 * lock can Pulse several times before the WaitForSingleObject
586 * returns. If we popped the queue here then this event might
587 * be signalled more than once, thereby starving another
591 if(ret==WAIT_OBJECT_0) {
592 #ifdef THREAD_LOCK_DEBUG
593 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Success",
594 GetCurrentThreadId ());
598 #ifdef THREAD_LOCK_DEBUG
599 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
600 GetCurrentThreadId ());
601 g_message(G_GNUC_PRETTY_FUNCTION ": (%d) dequeuing handle %p",
602 GetCurrentThreadId (), event);
604 /* No pulse, so we have to remove ourself from the
607 mon->wait_list=g_slist_remove (mon->wait_list, event);