3 * Monitor locking functions
6 * Dick Porter (dick@ximian.com)
8 * Copyright 2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include <mono/metadata/abi-details.h>
18 #include <mono/metadata/monitor.h>
19 #include <mono/metadata/threads-types.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/gc-internals.h>
25 #include <mono/metadata/method-builder.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/metadata/w32event.h>
30 #include <mono/utils/mono-threads.h>
31 #include <mono/metadata/profiler-private.h>
32 #include <mono/utils/mono-time.h>
33 #include <mono/utils/atomic.h>
34 #include <mono/utils/w32api.h>
37 * Pull the list of opcodes
39 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
43 #include "mono/cil/opcode.def"
48 /*#define LOCK_DEBUG(a) do { a; } while (0)*/
52 * The monitor implementation here is based on
53 * http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf and
54 * http://www.research.ibm.com/people/d/dfb/papers/Bacon98Thin.ps
56 * The Dice paper describes a technique for saving lock record space
57 * by returning records to a free list when they become unused. That
58 * sounds like unnecessary complexity to me, though if it becomes
59 * clear that unused lock records are taking up lots of space or we
60 * need to shave more time off by avoiding a malloc then we can always
61 * implement the free list idea later. The timeout parameter to
62 * try_enter voids some of the assumptions about the reference count
63 * field in Dice's implementation too. In his version, the thread
64 * attempting to lock a contended object will block until it succeeds,
65 * so the reference count will never be decremented while an object is
68 * Bacon's thin locks have a fast path that doesn't need a lock record
69 * for the common case of locking an unlocked or shallow-nested
74 typedef struct _MonitorArray MonitorArray;
76 struct _MonitorArray {
79 MonoThreadsSync monitors [MONO_ZERO_LEN_ARRAY];
82 #define mono_monitor_allocator_lock() mono_os_mutex_lock (&monitor_mutex)
83 #define mono_monitor_allocator_unlock() mono_os_mutex_unlock (&monitor_mutex)
84 static mono_mutex_t monitor_mutex;
85 static MonoThreadsSync *monitor_freelist;
86 static MonitorArray *monitor_allocated;
87 static int array_size = 16;
89 /* MonoThreadsSync status helpers */
92 mon_status_get_owner (guint32 status)
94 return status & OWNER_MASK;
98 mon_status_set_owner (guint32 status, guint32 owner)
100 return (status & ENTRY_COUNT_MASK) | owner;
104 mon_status_get_entry_count (guint32 status)
106 gint32 entry_count = (gint32)((status & ENTRY_COUNT_MASK) >> ENTRY_COUNT_SHIFT);
107 gint32 zero = (gint32)(((guint32)ENTRY_COUNT_ZERO) >> ENTRY_COUNT_SHIFT);
108 return entry_count - zero;
111 static inline guint32
112 mon_status_init_entry_count (guint32 status)
114 return (status & OWNER_MASK) | ENTRY_COUNT_ZERO;
117 static inline guint32
118 mon_status_increment_entry_count (guint32 status)
120 return status + (1 << ENTRY_COUNT_SHIFT);
123 static inline guint32
124 mon_status_decrement_entry_count (guint32 status)
126 return status - (1 << ENTRY_COUNT_SHIFT);
129 static inline gboolean
130 mon_status_have_waiters (guint32 status)
132 return status & ENTRY_COUNT_WAITERS;
135 /* LockWord helpers */
137 static inline MonoThreadsSync*
138 lock_word_get_inflated_lock (LockWord lw)
140 lw.lock_word &= (~LOCK_WORD_STATUS_MASK);
144 static inline gboolean
145 lock_word_is_inflated (LockWord lw)
147 return lw.lock_word & LOCK_WORD_INFLATED;
150 static inline gboolean
151 lock_word_has_hash (LockWord lw)
153 return lw.lock_word & LOCK_WORD_HAS_HASH;
156 static inline LockWord
157 lock_word_set_has_hash (LockWord lw)
160 nlw.lock_word = lw.lock_word | LOCK_WORD_HAS_HASH;
164 static inline gboolean
165 lock_word_is_free (LockWord lw)
167 return !lw.lock_word;
170 static inline gboolean
171 lock_word_is_flat (LockWord lw)
173 /* Return whether the lock is flat or free */
174 return (lw.lock_word & LOCK_WORD_STATUS_MASK) == LOCK_WORD_FLAT;
178 lock_word_get_hash (LockWord lw)
180 return (gint32) (lw.lock_word >> LOCK_WORD_HASH_SHIFT);
184 lock_word_get_nest (LockWord lw)
186 if (lock_word_is_free (lw))
188 /* Inword nest count starts from 0 */
189 return ((lw.lock_word & LOCK_WORD_NEST_MASK) >> LOCK_WORD_NEST_SHIFT) + 1;
192 static inline gboolean
193 lock_word_is_nested (LockWord lw)
195 return lw.lock_word & LOCK_WORD_NEST_MASK;
198 static inline gboolean
199 lock_word_is_max_nest (LockWord lw)
201 return (lw.lock_word & LOCK_WORD_NEST_MASK) == LOCK_WORD_NEST_MASK;
204 static inline LockWord
205 lock_word_increment_nest (LockWord lw)
207 lw.lock_word += 1 << LOCK_WORD_NEST_SHIFT;
211 static inline LockWord
212 lock_word_decrement_nest (LockWord lw)
214 lw.lock_word -= 1 << LOCK_WORD_NEST_SHIFT;
219 lock_word_get_owner (LockWord lw)
221 return lw.lock_word >> LOCK_WORD_OWNER_SHIFT;
224 static inline LockWord
225 lock_word_new_thin_hash (gint32 hash)
228 lw.lock_word = (guint32)hash;
229 lw.lock_word = (lw.lock_word << LOCK_WORD_HASH_SHIFT) | LOCK_WORD_HAS_HASH;
233 static inline LockWord
234 lock_word_new_inflated (MonoThreadsSync *mon)
238 lw.lock_word |= LOCK_WORD_INFLATED;
242 static inline LockWord
243 lock_word_new_flat (gint32 owner)
246 lw.lock_word = owner;
247 lw.lock_word <<= LOCK_WORD_OWNER_SHIFT;
252 mono_monitor_init (void)
254 mono_os_mutex_init_recursive (&monitor_mutex);
258 mono_monitor_cleanup (void)
260 MonoThreadsSync *mon;
261 /* MonitorArray *marray, *next = NULL; */
263 /*mono_os_mutex_destroy (&monitor_mutex);*/
265 /* The monitors on the freelist don't have weak links - mark them */
266 for (mon = monitor_freelist; mon; mon = (MonoThreadsSync *)mon->data)
267 mon->wait_list = (GSList *)-1;
270 * FIXME: This still crashes with sgen (async_read.exe)
272 * In mini_cleanup() we first call mono_runtime_cleanup(), which calls
273 * mono_monitor_cleanup(), which is supposed to free all monitor memory.
275 * Later in mini_cleanup(), we call mono_domain_free(), which calls
276 * mono_gc_clear_domain(), which frees all weak links associated with objects.
277 * Those weak links reside in the monitor structures, which we've freed earlier.
279 * Unless we fix this dependency in the shutdown sequence this code has to remain
280 * disabled, or at least the call to g_free().
283 for (marray = monitor_allocated; marray; marray = next) {
286 for (i = 0; i < marray->num_monitors; ++i) {
287 mon = &marray->monitors [i];
288 if (mon->wait_list != (gpointer)-1)
289 mono_gc_weak_link_remove (&mon->data);
299 monitor_is_on_freelist (MonoThreadsSync *mon)
301 MonitorArray *marray;
302 for (marray = monitor_allocated; marray; marray = marray->next) {
303 if (mon >= marray->monitors && mon < &marray->monitors [marray->num_monitors])
311 * \param include_untaken Whether to list unheld inflated locks.
312 * Print a report on stdout of the managed locks currently held by
313 * threads. If \p include_untaken is specified, list also inflated locks
315 * This is supposed to be used in debuggers like gdb.
318 mono_locks_dump (gboolean include_untaken)
321 int used = 0, on_freelist = 0, to_recycle = 0, total = 0, num_arrays = 0;
322 MonoThreadsSync *mon;
323 MonitorArray *marray;
324 for (mon = monitor_freelist; mon; mon = (MonoThreadsSync *)mon->data)
326 for (marray = monitor_allocated; marray; marray = marray->next) {
327 total += marray->num_monitors;
329 for (i = 0; i < marray->num_monitors; ++i) {
330 mon = &marray->monitors [i];
331 if (mon->data == NULL) {
332 if (i < marray->num_monitors - 1)
335 if (!monitor_is_on_freelist ((MonoThreadsSync *)mon->data)) {
336 MonoObject *holder = (MonoObject *)mono_gchandle_get_target ((guint32)mon->data);
337 if (mon_status_get_owner (mon->status)) {
338 g_print ("Lock %p in object %p held by thread %d, nest level: %d\n",
339 mon, holder, mon_status_get_owner (mon->status), mon->nest);
341 g_print ("\tWaiting on semaphore %p: %d\n", mon->entry_sem, mon_status_get_entry_count (mon->status));
342 } else if (include_untaken) {
343 g_print ("Lock %p in object %p untaken\n", mon, holder);
350 g_print ("Total locks (in %d array(s)): %d, used: %d, on freelist: %d, to recycle: %d\n",
351 num_arrays, total, used, on_freelist, to_recycle);
354 /* LOCKING: this is called with monitor_mutex held */
356 mon_finalize (MonoThreadsSync *mon)
358 LOCK_DEBUG (g_message ("%s: Finalizing sync %p", __func__, mon));
360 if (mon->entry_sem != NULL) {
361 mono_coop_sem_destroy (mon->entry_sem);
362 g_free (mon->entry_sem);
363 mon->entry_sem = NULL;
365 /* If this isn't empty then something is seriously broken - it
366 * means a thread is still waiting on the object that owned
367 * this lock, but the object has been finalized.
369 g_assert (mon->wait_list == NULL);
371 /* owner and nest are set in mon_new, no need to zero them out */
373 mon->data = monitor_freelist;
374 monitor_freelist = mon;
375 #ifndef DISABLE_PERFCOUNTERS
376 mono_perfcounters->gc_sync_blocks--;
380 /* LOCKING: this is called with monitor_mutex held */
381 static MonoThreadsSync *
384 MonoThreadsSync *new_;
386 if (!monitor_freelist) {
387 MonitorArray *marray;
389 /* see if any sync block has been collected */
391 for (marray = monitor_allocated; marray; marray = marray->next) {
392 for (i = 0; i < marray->num_monitors; ++i) {
393 if (mono_gchandle_get_target ((guint32)marray->monitors [i].data) == NULL) {
394 new_ = &marray->monitors [i];
395 if (new_->wait_list) {
396 /* Orphaned events left by aborted threads */
397 while (new_->wait_list) {
398 LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d): Closing orphaned event %d", mono_thread_info_get_small_id (), new_->wait_list->data));
399 mono_w32event_close (new_->wait_list->data);
400 new_->wait_list = g_slist_remove (new_->wait_list, new_->wait_list->data);
403 mono_gchandle_free ((guint32)new_->data);
404 new_->data = monitor_freelist;
405 monitor_freelist = new_;
408 /* small perf tweak to avoid scanning all the blocks */
412 /* need to allocate a new array of monitors */
413 if (!monitor_freelist) {
415 LOCK_DEBUG (g_message ("%s: allocating more monitors: %d", __func__, array_size));
416 marray = (MonitorArray *)g_malloc0 (MONO_SIZEOF_MONO_ARRAY + array_size * sizeof (MonoThreadsSync));
417 marray->num_monitors = array_size;
419 /* link into the freelist */
420 for (i = 0; i < marray->num_monitors - 1; ++i) {
421 marray->monitors [i].data = &marray->monitors [i + 1];
423 marray->monitors [i].data = NULL; /* the last one */
424 monitor_freelist = &marray->monitors [0];
425 /* we happend the marray instead of prepending so that
426 * the collecting loop above will need to scan smaller arrays first
428 if (!monitor_allocated) {
429 monitor_allocated = marray;
431 last = monitor_allocated;
439 new_ = monitor_freelist;
440 monitor_freelist = (MonoThreadsSync *)new_->data;
442 new_->status = mon_status_set_owner (0, id);
443 new_->status = mon_status_init_entry_count (new_->status);
447 #ifndef DISABLE_PERFCOUNTERS
448 mono_perfcounters->gc_sync_blocks++;
453 static MonoThreadsSync*
454 alloc_mon (MonoObject *obj, gint32 id)
456 MonoThreadsSync *mon;
458 mono_monitor_allocator_lock ();
460 mon->data = (void *)(size_t)mono_gchandle_new_weakref (obj, TRUE);
461 mono_monitor_allocator_unlock ();
468 discard_mon (MonoThreadsSync *mon)
470 mono_monitor_allocator_lock ();
471 mono_gchandle_free ((guint32)mon->data);
473 mono_monitor_allocator_unlock ();
477 mono_monitor_inflate_owned (MonoObject *obj, int id)
479 MonoThreadsSync *mon;
480 LockWord nlw, old_lw, tmp_lw;
483 old_lw.sync = obj->synchronisation;
484 LOCK_DEBUG (g_message ("%s: (%d) Inflating owned lock object %p; LW = %p", __func__, id, obj, old_lw.sync));
486 if (lock_word_is_inflated (old_lw)) {
487 /* Someone else inflated the lock in the meantime */
491 mon = alloc_mon (obj, id);
493 nest = lock_word_get_nest (old_lw);
496 nlw = lock_word_new_inflated (mon);
498 mono_memory_write_barrier ();
499 tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
500 if (tmp_lw.sync != old_lw.sync) {
501 /* Someone else inflated the lock in the meantime */
507 mono_monitor_inflate (MonoObject *obj)
509 MonoThreadsSync *mon;
510 LockWord nlw, old_lw;
512 LOCK_DEBUG (g_message ("%s: (%d) Inflating lock object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
514 mon = alloc_mon (obj, 0);
516 nlw = lock_word_new_inflated (mon);
518 old_lw.sync = obj->synchronisation;
523 if (lock_word_is_inflated (old_lw)) {
526 #ifdef HAVE_MOVING_COLLECTOR
527 else if (lock_word_has_hash (old_lw)) {
528 mon->hash_code = lock_word_get_hash (old_lw);
529 mon->status = mon_status_set_owner (mon->status, 0);
530 nlw = lock_word_set_has_hash (nlw);
533 else if (lock_word_is_free (old_lw)) {
534 mon->status = mon_status_set_owner (mon->status, 0);
538 mon->status = mon_status_set_owner (mon->status, lock_word_get_owner (old_lw));
539 mon->nest = lock_word_get_nest (old_lw);
541 mono_memory_write_barrier ();
542 tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
543 if (tmp_lw.sync == old_lw.sync) {
544 /* Successfully inflated the lock */
548 old_lw.sync = tmp_lw.sync;
551 /* Someone else inflated the lock before us */
555 #define MONO_OBJECT_ALIGNMENT_SHIFT 3
561 * Calculate a hash code for @obj that is constant while @obj is alive.
564 mono_object_hash (MonoObject* obj)
566 #ifdef HAVE_MOVING_COLLECTOR
571 lw.sync = obj->synchronisation;
573 LOCK_DEBUG (g_message("%s: (%d) Get hash for object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
575 if (lock_word_has_hash (lw)) {
576 if (lock_word_is_inflated (lw)) {
577 return lock_word_get_inflated_lock (lw)->hash_code;
579 return lock_word_get_hash (lw);
583 * while we are inside this function, the GC will keep this object pinned,
584 * since we are in the unmanaged stack. Thanks to this and to the hash
585 * function that depends only on the address, we can ignore the races if
586 * another thread computes the hash at the same time, because it'll end up
587 * with the same value.
589 hash = (GPOINTER_TO_UINT (obj) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
590 #if SIZEOF_VOID_P == 4
591 /* clear the top bits as they can be discarded */
592 hash &= ~(LOCK_WORD_STATUS_MASK << (32 - LOCK_WORD_STATUS_BITS));
594 if (lock_word_is_free (lw)) {
596 lw = lock_word_new_thin_hash (hash);
598 old_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, NULL);
599 if (old_lw.sync == NULL) {
603 if (lock_word_has_hash (old_lw)) {
604 /* Done by somebody else */
608 mono_monitor_inflate (obj);
609 lw.sync = obj->synchronisation;
610 } else if (lock_word_is_flat (lw)) {
611 int id = mono_thread_info_get_small_id ();
612 if (lock_word_get_owner (lw) == id)
613 mono_monitor_inflate_owned (obj, id);
615 mono_monitor_inflate (obj);
616 lw.sync = obj->synchronisation;
619 /* At this point, the lock is inflated */
620 lock_word_get_inflated_lock (lw)->hash_code = hash;
621 lw = lock_word_set_has_hash (lw);
622 mono_memory_write_barrier ();
623 obj->synchronisation = lw.sync;
627 * Wang's address-based hash function:
628 * http://www.concentric.net/~Ttwang/tech/addrhash.htm
630 return (GPOINTER_TO_UINT (obj) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
635 mono_monitor_ensure_owned (LockWord lw, guint32 id)
637 if (lock_word_is_flat (lw)) {
638 if (lock_word_get_owner (lw) == id)
640 } else if (lock_word_is_inflated (lw)) {
641 if (mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == id)
645 mono_set_pending_exception (mono_get_exception_synchronization_lock ("Object synchronization method was called from an unsynchronized block of code."));
650 * When this function is called it has already been established that the
651 * current thread owns the monitor.
654 mono_monitor_exit_inflated (MonoObject *obj)
657 MonoThreadsSync *mon;
660 lw.sync = obj->synchronisation;
661 mon = lock_word_get_inflated_lock (lw);
663 nest = mon->nest - 1;
665 guint32 new_status, old_status, tmp_status;
667 old_status = mon->status;
670 * Release lock and do the wakeup stuff. It's possible that
671 * the last blocking thread gave up waiting just before we
672 * release the semaphore resulting in a negative entry count
673 * and a futile wakeup next time there's contention for this
677 gboolean have_waiters = mon_status_have_waiters (old_status);
679 new_status = mon_status_set_owner (old_status, 0);
681 new_status = mon_status_decrement_entry_count (new_status);
682 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
683 if (tmp_status == old_status) {
685 mono_coop_sem_post (mon->entry_sem);
688 old_status = tmp_status;
690 LOCK_DEBUG (g_message ("%s: (%d) Object %p is now unlocked", __func__, mono_thread_info_get_small_id (), obj));
692 /* object is now unlocked, leave nest==1 so we don't
693 * need to set it when the lock is reacquired
696 LOCK_DEBUG (g_message ("%s: (%d) Object %p is now locked %d times", __func__, mono_thread_info_get_small_id (), obj, nest));
702 * When this function is called it has already been established that the
703 * current thread owns the monitor.
706 mono_monitor_exit_flat (MonoObject *obj, LockWord old_lw)
708 LockWord new_lw, tmp_lw;
709 if (G_UNLIKELY (lock_word_is_nested (old_lw)))
710 new_lw = lock_word_decrement_nest (old_lw);
712 new_lw.lock_word = 0;
714 tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, new_lw.sync, old_lw.sync);
715 if (old_lw.sync != tmp_lw.sync) {
716 /* Someone inflated the lock in the meantime */
717 mono_monitor_exit_inflated (obj);
720 LOCK_DEBUG (g_message ("%s: (%d) Object %p is now locked %d times; LW = %p", __func__, mono_thread_info_get_small_id (), obj, lock_word_get_nest (new_lw), obj->synchronisation));
724 mon_decrement_entry_count (MonoThreadsSync *mon)
726 guint32 old_status, tmp_status, new_status;
728 /* Decrement entry count */
729 old_status = mon->status;
731 new_status = mon_status_decrement_entry_count (old_status);
732 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
733 if (tmp_status == old_status) {
736 old_status = tmp_status;
740 /* If allow_interruption==TRUE, the method will be interrumped if abort or suspend
741 * is requested. In this case it returns -1.
744 mono_monitor_try_enter_inflated (MonoObject *obj, guint32 ms, gboolean allow_interruption, guint32 id)
747 MonoThreadsSync *mon;
749 gint64 then = 0, now, delta;
751 guint32 new_status, old_status, tmp_status;
752 MonoSemTimedwaitRet wait_ret;
753 MonoInternalThread *thread;
754 gboolean interrupted = FALSE;
756 LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
758 if (G_UNLIKELY (!obj)) {
759 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
763 lw.sync = obj->synchronisation;
764 mon = lock_word_get_inflated_lock (lw);
766 /* This case differs from Dice's case 3 because we don't
767 * deflate locks or cache unused lock records
769 old_status = mon->status;
770 if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
771 /* Try to install our ID in the owner field, nest
772 * should have been left at 1 by the previous unlock
775 new_status = mon_status_set_owner (old_status, id);
776 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
777 if (G_LIKELY (tmp_status == old_status)) {
779 g_assert (mon->nest == 1);
787 /* If the object is currently locked by this thread... */
788 if (mon_status_get_owner (old_status) == id) {
793 /* The object must be locked by someone else... */
794 #ifndef DISABLE_PERFCOUNTERS
795 mono_perfcounters->thread_contentions++;
798 /* If ms is 0 we don't block, but just fail straight away */
800 LOCK_DEBUG (g_message ("%s: (%d) timed out, returning FALSE", __func__, id));
804 mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_CONTENTION);
806 /* The slow path begins here. */
808 /* a small amount of duplicated code, but it allows us to insert the profiler
809 * callbacks without impacting the fast path: from here on we don't need to go back to the
810 * retry label, but to retry_contended. At this point mon is already installed in the object
813 /* This case differs from Dice's case 3 because we don't
814 * deflate locks or cache unused lock records
816 old_status = mon->status;
817 if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
818 /* Try to install our ID in the owner field, nest
819 * should have been left at 1 by the previous unlock
822 new_status = mon_status_set_owner (old_status, id);
823 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
824 if (G_LIKELY (tmp_status == old_status)) {
826 g_assert (mon->nest == 1);
827 mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
832 /* If the object is currently locked by this thread... */
833 if (mon_status_get_owner (old_status) == id) {
835 mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
839 /* We need to make sure there's a semaphore handle (creating it if
840 * necessary), and block on it
842 if (mon->entry_sem == NULL) {
843 /* Create the semaphore */
844 sem = g_new0 (MonoCoopSem, 1);
845 mono_coop_sem_init (sem, 0);
846 if (InterlockedCompareExchangePointer ((gpointer*)&mon->entry_sem, sem, NULL) != NULL) {
847 /* Someone else just put a handle here */
848 mono_coop_sem_destroy (sem);
854 * We need to register ourselves as waiting if it is the first time we are waiting,
855 * of if we were signaled and failed to acquire the lock.
858 old_status = mon->status;
860 if (mon_status_get_owner (old_status) == 0)
861 goto retry_contended;
862 new_status = mon_status_increment_entry_count (old_status);
863 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
864 if (tmp_status == old_status) {
867 old_status = tmp_status;
871 if (ms != MONO_INFINITE_WAIT) {
872 then = mono_msec_ticks ();
876 #ifndef DISABLE_PERFCOUNTERS
877 mono_perfcounters->thread_queue_len++;
878 mono_perfcounters->thread_queue_max++;
880 thread = mono_thread_internal_current ();
883 * If we allow interruption, we check the test state for an abort request before going into sleep.
884 * This is a workaround to the fact that Thread.Abort does non-sticky interruption of semaphores.
886 * Semaphores don't support the sticky interruption with mono_thread_info_install_interrupt.
888 * A better fix would be to switch to wait with something that allows sticky interrupts together
889 * with wrapping it with abort_protected_block_count for the non-alertable cases.
890 * And somehow make this whole dance atomic and not crazy expensive. Good luck.
893 if (allow_interruption) {
894 if (!mono_thread_test_and_set_state (thread, ThreadState_AbortRequested, ThreadState_WaitSleepJoin)) {
895 wait_ret = MONO_SEM_TIMEDWAIT_RET_ALERTED;
899 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
903 * We pass ALERTABLE instead of allow_interruption since we have to check for the
904 * StopRequested case below.
906 wait_ret = mono_coop_sem_timedwait (mon->entry_sem, waitms, MONO_SEM_FLAGS_ALERTABLE);
908 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
911 #ifndef DISABLE_PERFCOUNTERS
912 mono_perfcounters->thread_queue_len--;
915 if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED && !allow_interruption) {
918 * We have to obey a stop/suspend request even if
919 * allow_interruption is FALSE to avoid hangs at shutdown.
921 if (!mono_thread_test_state (mono_thread_internal_current (), ThreadState_SuspendRequested | ThreadState_AbortRequested)) {
922 if (ms != MONO_INFINITE_WAIT) {
923 now = mono_msec_ticks ();
925 /* it should not overflow before ~30k years */
926 g_assert (now >= then);
935 /* retry from the top */
936 goto retry_contended;
938 } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
940 /* retry from the top */
941 goto retry_contended;
942 } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
946 /* Timed out or interrupted */
947 mon_decrement_entry_count (mon);
949 mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_FAIL);
951 if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
952 LOCK_DEBUG (g_message ("%s: (%d) interrupted waiting, returning -1", __func__, id));
954 } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
955 LOCK_DEBUG (g_message ("%s: (%d) timed out waiting, returning FALSE", __func__, id));
958 g_assert_not_reached ();
964 * If allow_interruption == TRUE, the method will be interrupted if abort or suspend
965 * is requested. In this case it returns -1.
968 mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
971 int id = mono_thread_info_get_small_id ();
973 LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
975 lw.sync = obj->synchronisation;
977 if (G_LIKELY (lock_word_is_free (lw))) {
978 LockWord nlw = lock_word_new_flat (id);
979 if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, NULL) == NULL) {
982 /* Someone acquired it in the meantime or put a hash */
983 mono_monitor_inflate (obj);
984 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
986 } else if (lock_word_is_inflated (lw)) {
987 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
988 } else if (lock_word_is_flat (lw)) {
989 if (lock_word_get_owner (lw) == id) {
990 if (lock_word_is_max_nest (lw)) {
991 mono_monitor_inflate_owned (obj, id);
992 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
994 LockWord nlw, old_lw;
995 nlw = lock_word_increment_nest (lw);
996 old_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, lw.sync);
997 if (old_lw.sync != lw.sync) {
998 /* Someone else inflated it in the meantime */
999 g_assert (lock_word_is_inflated (old_lw));
1000 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1005 mono_monitor_inflate (obj);
1006 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1008 } else if (lock_word_has_hash (lw)) {
1009 mono_monitor_inflate (obj);
1010 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1013 g_assert_not_reached ();
1017 /* This is an icall */
1019 mono_monitor_enter_internal (MonoObject *obj)
1022 gboolean allow_interruption = TRUE;
1023 if (G_UNLIKELY (!obj)) {
1024 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1029 * An inquisitive mind could ask what's the deal with this loop.
1030 * It exists to deal with interrupting a monitor enter that happened within an abort-protected block, like a .cctor.
1032 * The thread will be set with a pending abort and the wait might even be interrupted. Either way, once we call mono_thread_interruption_checkpoint,
1033 * it will return NULL meaning we can't be aborted right now. Once that happens we switch to non-alertable.
1036 res = mono_monitor_try_enter_internal (obj, MONO_INFINITE_WAIT, allow_interruption);
1037 /*This means we got interrupted during the wait and didn't got the monitor.*/
1039 MonoException *exc = mono_thread_interruption_checkpoint ();
1041 mono_set_pending_exception (exc);
1044 //we detected a pending interruption but it turned out to be a false positive, we ignore it from now on (this feels like a hack, right?, threads.c should give us less confusing directions)
1045 allow_interruption = FALSE;
1048 } while (res == -1);
1053 mono_monitor_enter (MonoObject *obj)
1055 return mono_monitor_enter_internal (obj);
1058 /* Called from JITted code so we return guint32 instead of gboolean */
1060 mono_monitor_enter_fast (MonoObject *obj)
1062 if (G_UNLIKELY (!obj)) {
1063 /* don't set pending exn on the fast path, just return
1064 * FALSE and let the slow path take care of it. */
1067 return mono_monitor_try_enter_internal (obj, 0, FALSE) == 1;
1071 mono_monitor_try_enter (MonoObject *obj, guint32 ms)
1073 if (G_UNLIKELY (!obj)) {
1074 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1077 return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
1081 mono_monitor_exit (MonoObject *obj)
1085 LOCK_DEBUG (g_message ("%s: (%d) Unlocking %p", __func__, mono_thread_info_get_small_id (), obj));
1087 if (G_UNLIKELY (!obj)) {
1088 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1092 lw.sync = obj->synchronisation;
1094 if (!mono_monitor_ensure_owned (lw, mono_thread_info_get_small_id ()))
1097 if (G_UNLIKELY (lock_word_is_inflated (lw)))
1098 mono_monitor_exit_inflated (obj);
1100 mono_monitor_exit_flat (obj, lw);
1104 mono_monitor_get_object_monitor_gchandle (MonoObject *object)
1108 lw.sync = object->synchronisation;
1110 if (lock_word_is_inflated (lw)) {
1111 MonoThreadsSync *mon = lock_word_get_inflated_lock (lw);
1112 return (guint32)mon->data;
1118 * mono_monitor_threads_sync_member_offset:
1119 * @status_offset: returns size and offset of the "status" member
1120 * @nest_offset: returns size and offset of the "nest" member
1122 * Returns the offsets and sizes of two members of the
1123 * MonoThreadsSync struct. The Monitor ASM fastpaths need this.
1126 mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset)
1130 #define ENCODE_OFF_SIZE(o,s) (((o) << 8) | ((s) & 0xff))
1132 *status_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, status), sizeof (ts.status));
1133 *nest_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, nest), sizeof (ts.nest));
1137 ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject *obj, guint32 ms, MonoBoolean *lockTaken)
1140 gboolean allow_interruption = TRUE;
1141 if (G_UNLIKELY (!obj)) {
1142 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1146 res = mono_monitor_try_enter_internal (obj, ms, allow_interruption);
1147 /*This means we got interrupted during the wait and didn't got the monitor.*/
1149 MonoException *exc = mono_thread_interruption_checkpoint ();
1151 mono_set_pending_exception (exc);
1154 //we detected a pending interruption but it turned out to be a false positive, we ignore it from now on (this feels like a hack, right?, threads.c should give us less confusing directions)
1155 allow_interruption = FALSE;
1158 } while (res == -1);
1159 /*It's safe to do it from here since interruption would happen only on the wrapper.*/
1160 *lockTaken = res == 1;
1164 mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
1166 if (*lock_taken == 1) {
1167 mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
1173 ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, &taken);
1174 *lock_taken = taken;
1177 /* Called from JITted code */
1179 mono_monitor_enter_v4_internal (MonoObject *obj, MonoBoolean *lock_taken)
1181 if (*lock_taken == 1) {
1182 mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
1186 ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, lock_taken);
1190 * mono_monitor_enter_v4_fast:
1192 * Same as mono_monitor_enter_v4, but return immediately if the
1193 * monitor cannot be acquired.
1194 * Returns TRUE if the lock was acquired, FALSE otherwise.
1195 * Called from JITted code so we return guint32 instead of gboolean.
1198 mono_monitor_enter_v4_fast (MonoObject *obj, MonoBoolean *lock_taken)
1200 if (*lock_taken == 1)
1202 if (G_UNLIKELY (!obj))
1204 gint32 res = mono_monitor_try_enter_internal (obj, 0, TRUE);
1205 *lock_taken = res == 1;
1210 ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
1214 LOCK_DEBUG (g_message ("%s: Testing if %p is owned by thread %d", __func__, obj, mono_thread_info_get_small_id()));
1216 lw.sync = obj->synchronisation;
1218 if (lock_word_is_flat (lw)) {
1219 return lock_word_get_owner (lw) == mono_thread_info_get_small_id ();
1220 } else if (lock_word_is_inflated (lw)) {
1221 return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == mono_thread_info_get_small_id ();
1228 ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
1232 LOCK_DEBUG (g_message("%s: (%d) Testing if %p is owned by any thread", __func__, mono_thread_info_get_small_id (), obj));
1234 lw.sync = obj->synchronisation;
1236 if (lock_word_is_flat (lw)) {
1237 return !lock_word_is_free (lw);
1238 } else if (lock_word_is_inflated (lw)) {
1239 return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) != 0;
1245 /* All wait list manipulation in the pulse, pulseall and wait
1246 * functions happens while the monitor lock is held, so we don't need
1247 * any extra struct locking
1251 ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
1255 MonoThreadsSync *mon;
1257 LOCK_DEBUG (g_message ("%s: (%d) Pulsing %p", __func__, mono_thread_info_get_small_id (), obj));
1259 id = mono_thread_info_get_small_id ();
1260 lw.sync = obj->synchronisation;
1262 if (!mono_monitor_ensure_owned (lw, id))
1265 if (!lock_word_is_inflated (lw)) {
1266 /* No threads waiting. A wait would have inflated the lock */
1270 mon = lock_word_get_inflated_lock (lw);
1272 LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
1274 if (mon->wait_list != NULL) {
1275 LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
1277 mono_w32event_set (mon->wait_list->data);
1278 mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
1283 ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
1287 MonoThreadsSync *mon;
1289 LOCK_DEBUG (g_message("%s: (%d) Pulsing all %p", __func__, mono_thread_info_get_small_id (), obj));
1291 id = mono_thread_info_get_small_id ();
1292 lw.sync = obj->synchronisation;
1294 if (!mono_monitor_ensure_owned (lw, id))
1297 if (!lock_word_is_inflated (lw)) {
1298 /* No threads waiting. A wait would have inflated the lock */
1302 mon = lock_word_get_inflated_lock (lw);
1304 LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
1306 while (mon->wait_list != NULL) {
1307 LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
1309 mono_w32event_set (mon->wait_list->data);
1310 mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
1315 ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
1318 MonoThreadsSync *mon;
1321 MonoW32HandleWaitRet ret;
1322 gboolean success = FALSE;
1324 MonoInternalThread *thread = mono_thread_internal_current ();
1325 int id = mono_thread_info_get_small_id ();
1327 LOCK_DEBUG (g_message ("%s: (%d) Trying to wait for %p with timeout %dms", __func__, mono_thread_info_get_small_id (), obj, ms));
1329 lw.sync = obj->synchronisation;
1331 if (!mono_monitor_ensure_owned (lw, id))
1334 if (!lock_word_is_inflated (lw)) {
1335 mono_monitor_inflate_owned (obj, id);
1336 lw.sync = obj->synchronisation;
1339 mon = lock_word_get_inflated_lock (lw);
1341 /* Do this WaitSleepJoin check before creating the event handle */
1342 if (mono_thread_current_check_pending_interrupt ())
1345 event = mono_w32event_create (FALSE, FALSE);
1346 if (event == NULL) {
1347 mono_set_pending_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
1351 LOCK_DEBUG (g_message ("%s: (%d) queuing handle %p", __func__, mono_thread_info_get_small_id (), event));
1353 /* This looks superfluous */
1354 if (mono_thread_current_check_pending_interrupt ()) {
1355 mono_w32event_close (event);
1359 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1361 mon->wait_list = g_slist_append (mon->wait_list, event);
1363 /* Save the nest count, and release the lock */
1366 mono_memory_write_barrier ();
1367 mono_monitor_exit_inflated (obj);
1369 LOCK_DEBUG (g_message ("%s: (%d) Unlocked %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
1371 /* There's no race between unlocking mon and waiting for the
1372 * event, because auto reset events are sticky, and this event
1373 * is private to this thread. Therefore even if the event was
1374 * signalled before we wait, we still succeed.
1378 ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, ms, TRUE), 1);
1380 ret = mono_w32handle_wait_one (event, ms, TRUE);
1381 #endif /* HOST_WIN32 */
1384 /* Reset the thread state fairly early, so we don't have to worry
1385 * about the monitor error checking
1387 mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1389 /* Regain the lock with the previous nest count */
1391 regain = mono_monitor_try_enter_inflated (obj, MONO_INFINITE_WAIT, TRUE, id);
1392 /* We must regain the lock before handling interruption requests */
1393 } while (regain == -1);
1395 g_assert (regain == 1);
1399 LOCK_DEBUG (g_message ("%s: (%d) Regained %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
1401 if (ret == MONO_W32HANDLE_WAIT_RET_TIMEOUT) {
1402 /* Poll the event again, just in case it was signalled
1403 * while we were trying to regain the monitor lock
1407 ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, 0, FALSE), 1);
1409 ret = mono_w32handle_wait_one (event, 0, FALSE);
1410 #endif /* HOST_WIN32 */
1414 /* Pulse will have popped our event from the queue if it signalled
1415 * us, so we only do it here if the wait timed out.
1417 * This avoids a race condition where the thread holding the
1418 * lock can Pulse several times before the WaitForSingleObject
1419 * returns. If we popped the queue here then this event might
1420 * be signalled more than once, thereby starving another
1424 if (ret == MONO_W32HANDLE_WAIT_RET_SUCCESS_0) {
1425 LOCK_DEBUG (g_message ("%s: (%d) Success", __func__, mono_thread_info_get_small_id ()));
1428 LOCK_DEBUG (g_message ("%s: (%d) Wait failed, dequeuing handle %p", __func__, mono_thread_info_get_small_id (), event));
1429 /* No pulse, so we have to remove ourself from the
1432 mon->wait_list = g_slist_remove (mon->wait_list, event);
1434 mono_w32event_close (event);