Merge pull request #5567 from kumpera/fix_59334
[mono.git] / mono / metadata / monitor.c
1 /**
2  * \file
3  * Monitor locking functions
4  *
5  * Author:
6  *      Dick Porter (dick@ximian.com)
7  *
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.
11  */
12
13 #include <config.h>
14 #include <glib.h>
15 #include <string.h>
16
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>
35 #include <mono/utils/mono-os-wait.h>
36
37 /*
38  * Pull the list of opcodes
39  */
40 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
41         a = i,
42
43 enum {
44 #include "mono/cil/opcode.def"
45         LAST = 0xff
46 };
47 #undef OPDEF
48
49 /*#define LOCK_DEBUG(a) do { a; } while (0)*/
50 #define LOCK_DEBUG(a)
51
52 /*
53  * The monitor implementation here is based on
54  * http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf and
55  * http://www.research.ibm.com/people/d/dfb/papers/Bacon98Thin.ps
56  *
57  * The Dice paper describes a technique for saving lock record space
58  * by returning records to a free list when they become unused.  That
59  * sounds like unnecessary complexity to me, though if it becomes
60  * clear that unused lock records are taking up lots of space or we
61  * need to shave more time off by avoiding a malloc then we can always
62  * implement the free list idea later.  The timeout parameter to
63  * try_enter voids some of the assumptions about the reference count
64  * field in Dice's implementation too.  In his version, the thread
65  * attempting to lock a contended object will block until it succeeds,
66  * so the reference count will never be decremented while an object is
67  * locked.
68  *
69  * Bacon's thin locks have a fast path that doesn't need a lock record
70  * for the common case of locking an unlocked or shallow-nested
71  * object.
72  */
73
74
75 typedef struct _MonitorArray MonitorArray;
76
77 struct _MonitorArray {
78         MonitorArray *next;
79         int num_monitors;
80         MonoThreadsSync monitors [MONO_ZERO_LEN_ARRAY];
81 };
82
83 #define mono_monitor_allocator_lock() mono_os_mutex_lock (&monitor_mutex)
84 #define mono_monitor_allocator_unlock() mono_os_mutex_unlock (&monitor_mutex)
85 static mono_mutex_t monitor_mutex;
86 static MonoThreadsSync *monitor_freelist;
87 static MonitorArray *monitor_allocated;
88 static int array_size = 16;
89
90 /* MonoThreadsSync status helpers */
91
92 static inline guint32
93 mon_status_get_owner (guint32 status)
94 {
95         return status & OWNER_MASK;
96 }
97
98 static inline guint32
99 mon_status_set_owner (guint32 status, guint32 owner)
100 {
101         return (status & ENTRY_COUNT_MASK) | owner;
102 }
103
104 static inline gint32
105 mon_status_get_entry_count (guint32 status)
106 {
107         gint32 entry_count = (gint32)((status & ENTRY_COUNT_MASK) >> ENTRY_COUNT_SHIFT);
108         gint32 zero = (gint32)(((guint32)ENTRY_COUNT_ZERO) >> ENTRY_COUNT_SHIFT);
109         return entry_count - zero;
110 }
111
112 static inline guint32
113 mon_status_init_entry_count (guint32 status)
114 {
115         return (status & OWNER_MASK) | ENTRY_COUNT_ZERO;
116 }
117
118 static inline guint32
119 mon_status_increment_entry_count (guint32 status)
120 {
121         return status + (1 << ENTRY_COUNT_SHIFT);
122 }
123
124 static inline guint32
125 mon_status_decrement_entry_count (guint32 status)
126 {
127         return status - (1 << ENTRY_COUNT_SHIFT);
128 }
129
130 static inline gboolean
131 mon_status_have_waiters (guint32 status)
132 {
133         return status & ENTRY_COUNT_WAITERS;
134 }
135
136 /* LockWord helpers */
137
138 static inline MonoThreadsSync*
139 lock_word_get_inflated_lock (LockWord lw)
140 {
141         lw.lock_word &= (~LOCK_WORD_STATUS_MASK);
142         return lw.sync;
143 }
144
145 static inline gboolean
146 lock_word_is_inflated (LockWord lw)
147 {
148         return lw.lock_word & LOCK_WORD_INFLATED;
149 }
150
151 static inline gboolean
152 lock_word_has_hash (LockWord lw)
153 {
154         return lw.lock_word & LOCK_WORD_HAS_HASH;
155 }
156
157 static inline LockWord
158 lock_word_set_has_hash (LockWord lw)
159 {
160         LockWord nlw;
161         nlw.lock_word = lw.lock_word | LOCK_WORD_HAS_HASH;
162         return nlw;
163 }
164
165 static inline gboolean
166 lock_word_is_free (LockWord lw)
167 {
168         return !lw.lock_word;
169 }
170
171 static inline gboolean
172 lock_word_is_flat (LockWord lw)
173 {
174         /* Return whether the lock is flat or free */
175         return (lw.lock_word & LOCK_WORD_STATUS_MASK) == LOCK_WORD_FLAT;
176 }
177
178 static inline gint32
179 lock_word_get_hash (LockWord lw)
180 {
181         return (gint32) (lw.lock_word >> LOCK_WORD_HASH_SHIFT);
182 }
183
184 static inline gint32
185 lock_word_get_nest (LockWord lw)
186 {
187         if (lock_word_is_free (lw))
188                 return 0;
189         /* Inword nest count starts from 0 */
190         return ((lw.lock_word & LOCK_WORD_NEST_MASK) >> LOCK_WORD_NEST_SHIFT) + 1;
191 }
192
193 static inline gboolean
194 lock_word_is_nested (LockWord lw)
195 {
196         return lw.lock_word & LOCK_WORD_NEST_MASK;
197 }
198
199 static inline gboolean
200 lock_word_is_max_nest (LockWord lw)
201 {
202         return (lw.lock_word & LOCK_WORD_NEST_MASK) == LOCK_WORD_NEST_MASK;
203 }
204
205 static inline LockWord
206 lock_word_increment_nest (LockWord lw)
207 {
208         lw.lock_word += 1 << LOCK_WORD_NEST_SHIFT;
209         return lw;
210 }
211
212 static inline LockWord
213 lock_word_decrement_nest (LockWord lw)
214 {
215         lw.lock_word -= 1 << LOCK_WORD_NEST_SHIFT;
216         return lw;
217 }
218
219 static inline gint32
220 lock_word_get_owner (LockWord lw)
221 {
222         return lw.lock_word >> LOCK_WORD_OWNER_SHIFT;
223 }
224
225 static inline LockWord
226 lock_word_new_thin_hash (gint32 hash)
227 {
228         LockWord lw;
229         lw.lock_word = (guint32)hash;
230         lw.lock_word = (lw.lock_word << LOCK_WORD_HASH_SHIFT) | LOCK_WORD_HAS_HASH;
231         return lw;
232 }
233
234 static inline LockWord
235 lock_word_new_inflated (MonoThreadsSync *mon)
236 {
237         LockWord lw;
238         lw.sync = mon;
239         lw.lock_word |= LOCK_WORD_INFLATED;
240         return lw;
241 }
242
243 static inline LockWord
244 lock_word_new_flat (gint32 owner)
245 {
246         LockWord lw;
247         lw.lock_word = owner;
248         lw.lock_word <<= LOCK_WORD_OWNER_SHIFT;
249         return lw;
250 }
251
252 void
253 mono_monitor_init (void)
254 {
255         mono_os_mutex_init_recursive (&monitor_mutex);
256 }
257  
258 void
259 mono_monitor_cleanup (void)
260 {
261         MonoThreadsSync *mon;
262         /* MonitorArray *marray, *next = NULL; */
263
264         /*mono_os_mutex_destroy (&monitor_mutex);*/
265
266         /* The monitors on the freelist don't have weak links - mark them */
267         for (mon = monitor_freelist; mon; mon = (MonoThreadsSync *)mon->data)
268                 mon->wait_list = (GSList *)-1;
269
270         /*
271          * FIXME: This still crashes with sgen (async_read.exe)
272          *
273          * In mini_cleanup() we first call mono_runtime_cleanup(), which calls
274          * mono_monitor_cleanup(), which is supposed to free all monitor memory.
275          *
276          * Later in mini_cleanup(), we call mono_domain_free(), which calls
277          * mono_gc_clear_domain(), which frees all weak links associated with objects.
278          * Those weak links reside in the monitor structures, which we've freed earlier.
279          *
280          * Unless we fix this dependency in the shutdown sequence this code has to remain
281          * disabled, or at least the call to g_free().
282          */
283         /*
284         for (marray = monitor_allocated; marray; marray = next) {
285                 int i;
286
287                 for (i = 0; i < marray->num_monitors; ++i) {
288                         mon = &marray->monitors [i];
289                         if (mon->wait_list != (gpointer)-1)
290                                 mono_gc_weak_link_remove (&mon->data);
291                 }
292
293                 next = marray->next;
294                 g_free (marray);
295         }
296         */
297 }
298
299 static int
300 monitor_is_on_freelist (MonoThreadsSync *mon)
301 {
302         MonitorArray *marray;
303         for (marray = monitor_allocated; marray; marray = marray->next) {
304                 if (mon >= marray->monitors && mon < &marray->monitors [marray->num_monitors])
305                         return TRUE;
306         }
307         return FALSE;
308 }
309
310 /**
311  * mono_locks_dump:
312  * \param include_untaken Whether to list unheld inflated locks.
313  * Print a report on stdout of the managed locks currently held by
314  * threads. If \p include_untaken is specified, list also inflated locks
315  * which are unheld.
316  * This is supposed to be used in debuggers like gdb.
317  */
318 void
319 mono_locks_dump (gboolean include_untaken)
320 {
321         int i;
322         int used = 0, on_freelist = 0, to_recycle = 0, total = 0, num_arrays = 0;
323         MonoThreadsSync *mon;
324         MonitorArray *marray;
325         for (mon = monitor_freelist; mon; mon = (MonoThreadsSync *)mon->data)
326                 on_freelist++;
327         for (marray = monitor_allocated; marray; marray = marray->next) {
328                 total += marray->num_monitors;
329                 num_arrays++;
330                 for (i = 0; i < marray->num_monitors; ++i) {
331                         mon = &marray->monitors [i];
332                         if (mon->data == NULL) {
333                                 if (i < marray->num_monitors - 1)
334                                         to_recycle++;
335                         } else {
336                                 if (!monitor_is_on_freelist ((MonoThreadsSync *)mon->data)) {
337                                         MonoObject *holder = (MonoObject *)mono_gchandle_get_target ((guint32)mon->data);
338                                         if (mon_status_get_owner (mon->status)) {
339                                                 g_print ("Lock %p in object %p held by thread %d, nest level: %d\n",
340                                                         mon, holder, mon_status_get_owner (mon->status), mon->nest);
341                                                 if (mon->entry_sem)
342                                                         g_print ("\tWaiting on semaphore %p: %d\n", mon->entry_sem, mon_status_get_entry_count (mon->status));
343                                         } else if (include_untaken) {
344                                                 g_print ("Lock %p in object %p untaken\n", mon, holder);
345                                         }
346                                         used++;
347                                 }
348                         }
349                 }
350         }
351         g_print ("Total locks (in %d array(s)): %d, used: %d, on freelist: %d, to recycle: %d\n",
352                 num_arrays, total, used, on_freelist, to_recycle);
353 }
354
355 /* LOCKING: this is called with monitor_mutex held */
356 static void 
357 mon_finalize (MonoThreadsSync *mon)
358 {
359         LOCK_DEBUG (g_message ("%s: Finalizing sync %p", __func__, mon));
360
361         if (mon->entry_sem != NULL) {
362                 mono_coop_sem_destroy (mon->entry_sem);
363                 g_free (mon->entry_sem);
364                 mon->entry_sem = NULL;
365         }
366         /* If this isn't empty then something is seriously broken - it
367          * means a thread is still waiting on the object that owned
368          * this lock, but the object has been finalized.
369          */
370         g_assert (mon->wait_list == NULL);
371
372         /* owner and nest are set in mon_new, no need to zero them out */
373
374         mon->data = monitor_freelist;
375         monitor_freelist = mon;
376 #ifndef DISABLE_PERFCOUNTERS
377         InterlockedDecrement (&mono_perfcounters->gc_sync_blocks);
378 #endif
379 }
380
381 /* LOCKING: this is called with monitor_mutex held */
382 static MonoThreadsSync *
383 mon_new (gsize id)
384 {
385         MonoThreadsSync *new_;
386
387         if (!monitor_freelist) {
388                 MonitorArray *marray;
389                 int i;
390                 /* see if any sync block has been collected */
391                 new_ = NULL;
392                 for (marray = monitor_allocated; marray; marray = marray->next) {
393                         for (i = 0; i < marray->num_monitors; ++i) {
394                                 if (mono_gchandle_get_target ((guint32)marray->monitors [i].data) == NULL) {
395                                         new_ = &marray->monitors [i];
396                                         if (new_->wait_list) {
397                                                 /* Orphaned events left by aborted threads */
398                                                 while (new_->wait_list) {
399                                                         LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d): Closing orphaned event %d", mono_thread_info_get_small_id (), new_->wait_list->data));
400                                                         mono_w32event_close (new_->wait_list->data);
401                                                         new_->wait_list = g_slist_remove (new_->wait_list, new_->wait_list->data);
402                                                 }
403                                         }
404                                         mono_gchandle_free ((guint32)new_->data);
405                                         new_->data = monitor_freelist;
406                                         monitor_freelist = new_;
407                                 }
408                         }
409                         /* small perf tweak to avoid scanning all the blocks */
410                         if (new_)
411                                 break;
412                 }
413                 /* need to allocate a new array of monitors */
414                 if (!monitor_freelist) {
415                         MonitorArray *last;
416                         LOCK_DEBUG (g_message ("%s: allocating more monitors: %d", __func__, array_size));
417                         marray = (MonitorArray *)g_malloc0 (MONO_SIZEOF_MONO_ARRAY + array_size * sizeof (MonoThreadsSync));
418                         marray->num_monitors = array_size;
419                         array_size *= 2;
420                         /* link into the freelist */
421                         for (i = 0; i < marray->num_monitors - 1; ++i) {
422                                 marray->monitors [i].data = &marray->monitors [i + 1];
423                         }
424                         marray->monitors [i].data = NULL; /* the last one */
425                         monitor_freelist = &marray->monitors [0];
426                         /* we happend the marray instead of prepending so that
427                          * the collecting loop above will need to scan smaller arrays first
428                          */
429                         if (!monitor_allocated) {
430                                 monitor_allocated = marray;
431                         } else {
432                                 last = monitor_allocated;
433                                 while (last->next)
434                                         last = last->next;
435                                 last->next = marray;
436                         }
437                 }
438         }
439
440         new_ = monitor_freelist;
441         monitor_freelist = (MonoThreadsSync *)new_->data;
442
443         new_->status = mon_status_set_owner (0, id);
444         new_->status = mon_status_init_entry_count (new_->status);
445         new_->nest = 1;
446         new_->data = NULL;
447         
448 #ifndef DISABLE_PERFCOUNTERS
449         InterlockedIncrement (&mono_perfcounters->gc_sync_blocks);
450 #endif
451         return new_;
452 }
453
454 static MonoThreadsSync*
455 alloc_mon (MonoObject *obj, gint32 id)
456 {
457         MonoThreadsSync *mon;
458
459         mono_monitor_allocator_lock ();
460         mon = mon_new (id);
461         mon->data = (void *)(size_t)mono_gchandle_new_weakref (obj, TRUE);
462         mono_monitor_allocator_unlock ();
463
464         return mon;
465 }
466
467
468 static void
469 discard_mon (MonoThreadsSync *mon)
470 {
471         mono_monitor_allocator_lock ();
472         mono_gchandle_free ((guint32)mon->data);
473         mon_finalize (mon);
474         mono_monitor_allocator_unlock ();
475 }
476
477 static void
478 mono_monitor_inflate_owned (MonoObject *obj, int id)
479 {
480         MonoThreadsSync *mon;
481         LockWord nlw, old_lw, tmp_lw;
482         guint32 nest;
483
484         old_lw.sync = obj->synchronisation;
485         LOCK_DEBUG (g_message ("%s: (%d) Inflating owned lock object %p; LW = %p", __func__, id, obj, old_lw.sync));
486
487         if (lock_word_is_inflated (old_lw)) {
488                 /* Someone else inflated the lock in the meantime */
489                 return;
490         }
491
492         mon = alloc_mon (obj, id);
493
494         nest = lock_word_get_nest (old_lw);
495         mon->nest = nest;
496
497         nlw = lock_word_new_inflated (mon);
498
499         mono_memory_write_barrier ();
500         tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
501         if (tmp_lw.sync != old_lw.sync) {
502                 /* Someone else inflated the lock in the meantime */
503                 discard_mon (mon);
504         }
505 }
506
507 static void
508 mono_monitor_inflate (MonoObject *obj)
509 {
510         MonoThreadsSync *mon;
511         LockWord nlw, old_lw;
512
513         LOCK_DEBUG (g_message ("%s: (%d) Inflating lock object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
514
515         mon = alloc_mon (obj, 0);
516
517         nlw = lock_word_new_inflated (mon);
518
519         old_lw.sync = obj->synchronisation;
520
521         for (;;) {
522                 LockWord tmp_lw;
523
524                 if (lock_word_is_inflated (old_lw)) {
525                         break;
526                 }
527 #ifdef HAVE_MOVING_COLLECTOR
528                  else if (lock_word_has_hash (old_lw)) {
529                         mon->hash_code = lock_word_get_hash (old_lw);
530                         mon->status = mon_status_set_owner (mon->status, 0);
531                         nlw = lock_word_set_has_hash (nlw);
532                 }
533 #endif
534                 else if (lock_word_is_free (old_lw)) {
535                         mon->status = mon_status_set_owner (mon->status, 0);
536                         mon->nest = 1;
537                 } else {
538                         /* Lock is flat */
539                         mon->status = mon_status_set_owner (mon->status, lock_word_get_owner (old_lw));
540                         mon->nest = lock_word_get_nest (old_lw);
541                 }
542                 mono_memory_write_barrier ();
543                 tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
544                 if (tmp_lw.sync == old_lw.sync) {
545                         /* Successfully inflated the lock */
546                         return;
547                 }
548
549                 old_lw.sync = tmp_lw.sync;
550         }
551
552         /* Someone else inflated the lock before us */
553         discard_mon (mon);
554 }
555
556 #define MONO_OBJECT_ALIGNMENT_SHIFT     3
557
558 /*
559  * mono_object_hash:
560  * @obj: an object
561  *
562  * Calculate a hash code for @obj that is constant while @obj is alive.
563  */
564 int
565 mono_object_hash (MonoObject* obj)
566 {
567 #ifdef HAVE_MOVING_COLLECTOR
568         LockWord lw;
569         unsigned int hash;
570         if (!obj)
571                 return 0;
572         lw.sync = obj->synchronisation;
573
574         LOCK_DEBUG (g_message("%s: (%d) Get hash for object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
575
576         if (lock_word_has_hash (lw)) {
577                 if (lock_word_is_inflated (lw)) {
578                         return lock_word_get_inflated_lock (lw)->hash_code;
579                 } else {
580                         return lock_word_get_hash (lw);
581                 }
582         }
583         /*
584          * while we are inside this function, the GC will keep this object pinned,
585          * since we are in the unmanaged stack. Thanks to this and to the hash
586          * function that depends only on the address, we can ignore the races if
587          * another thread computes the hash at the same time, because it'll end up
588          * with the same value.
589          */
590         hash = (GPOINTER_TO_UINT (obj) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
591 #if SIZEOF_VOID_P == 4
592         /* clear the top bits as they can be discarded */
593         hash &= ~(LOCK_WORD_STATUS_MASK << (32 - LOCK_WORD_STATUS_BITS));
594 #endif
595         if (lock_word_is_free (lw)) {
596                 LockWord old_lw;
597                 lw = lock_word_new_thin_hash (hash);
598
599                 old_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, NULL);
600                 if (old_lw.sync == NULL) {
601                         return hash;
602                 }
603
604                 if (lock_word_has_hash (old_lw)) {
605                         /* Done by somebody else */
606                         return hash;
607                 }
608                         
609                 mono_monitor_inflate (obj);
610                 lw.sync = obj->synchronisation;
611         } else if (lock_word_is_flat (lw)) {
612                 int id = mono_thread_info_get_small_id ();
613                 if (lock_word_get_owner (lw) == id)
614                         mono_monitor_inflate_owned (obj, id);
615                 else
616                         mono_monitor_inflate (obj);
617                 lw.sync = obj->synchronisation;
618         }
619
620         /* At this point, the lock is inflated */
621         lock_word_get_inflated_lock (lw)->hash_code = hash;
622         lw = lock_word_set_has_hash (lw);
623         mono_memory_write_barrier ();
624         obj->synchronisation = lw.sync;
625         return hash;
626 #else
627 /*
628  * Wang's address-based hash function:
629  *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
630  */
631         return (GPOINTER_TO_UINT (obj) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
632 #endif
633 }
634
635 static gboolean
636 mono_monitor_ensure_owned (LockWord lw, guint32 id)
637 {
638         if (lock_word_is_flat (lw)) {
639                 if (lock_word_get_owner (lw) == id)
640                         return TRUE;
641         } else if (lock_word_is_inflated (lw)) {
642                 if (mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == id)
643                         return TRUE;
644         }
645
646         mono_set_pending_exception (mono_get_exception_synchronization_lock ("Object synchronization method was called from an unsynchronized block of code."));
647         return FALSE;
648 }
649
650 /*
651  * When this function is called it has already been established that the
652  * current thread owns the monitor.
653  */
654 static void
655 mono_monitor_exit_inflated (MonoObject *obj)
656 {
657         LockWord lw;
658         MonoThreadsSync *mon;
659         guint32 nest;
660
661         lw.sync = obj->synchronisation;
662         mon = lock_word_get_inflated_lock (lw);
663
664         nest = mon->nest - 1;
665         if (nest == 0) {
666                 guint32 new_status, old_status, tmp_status;
667
668                 old_status = mon->status;
669
670                 /*
671                  * Release lock and do the wakeup stuff. It's possible that
672                  * the last blocking thread gave up waiting just before we
673                  * release the semaphore resulting in a negative entry count
674                  * and a futile wakeup next time there's contention for this
675                  * object.
676                  */
677                 for (;;) {
678                         gboolean have_waiters = mon_status_have_waiters (old_status);
679         
680                         new_status = mon_status_set_owner (old_status, 0);
681                         if (have_waiters)
682                                 new_status = mon_status_decrement_entry_count (new_status);
683                         tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
684                         if (tmp_status == old_status) {
685                                 if (have_waiters)
686                                         mono_coop_sem_post (mon->entry_sem);
687                                 break;
688                         }
689                         old_status = tmp_status;
690                 }
691                 LOCK_DEBUG (g_message ("%s: (%d) Object %p is now unlocked", __func__, mono_thread_info_get_small_id (), obj));
692         
693                 /* object is now unlocked, leave nest==1 so we don't
694                  * need to set it when the lock is reacquired
695                  */
696         } else {
697                 LOCK_DEBUG (g_message ("%s: (%d) Object %p is now locked %d times", __func__, mono_thread_info_get_small_id (), obj, nest));
698                 mon->nest = nest;
699         }
700 }
701
702 /*
703  * When this function is called it has already been established that the
704  * current thread owns the monitor.
705  */
706 static void
707 mono_monitor_exit_flat (MonoObject *obj, LockWord old_lw)
708 {
709         LockWord new_lw, tmp_lw;
710         if (G_UNLIKELY (lock_word_is_nested (old_lw)))
711                 new_lw = lock_word_decrement_nest (old_lw);
712         else
713                 new_lw.lock_word = 0;
714
715         tmp_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, new_lw.sync, old_lw.sync);
716         if (old_lw.sync != tmp_lw.sync) {
717                 /* Someone inflated the lock in the meantime */
718                 mono_monitor_exit_inflated (obj);
719         }
720
721         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));
722 }
723
724 static void
725 mon_decrement_entry_count (MonoThreadsSync *mon)
726 {
727         guint32 old_status, tmp_status, new_status;
728
729         /* Decrement entry count */
730         old_status = mon->status;
731         for (;;) {
732                 new_status = mon_status_decrement_entry_count (old_status);
733                 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
734                 if (tmp_status == old_status) {
735                         break;
736                 }
737                 old_status = tmp_status;
738         }
739 }
740
741 /* If allow_interruption==TRUE, the method will be interrumped if abort or suspend
742  * is requested. In this case it returns -1.
743  */ 
744 static inline gint32 
745 mono_monitor_try_enter_inflated (MonoObject *obj, guint32 ms, gboolean allow_interruption, guint32 id)
746 {
747         LockWord lw;
748         MonoThreadsSync *mon;
749         HANDLE sem;
750         gint64 then = 0, now, delta;
751         guint32 waitms;
752         guint32 new_status, old_status, tmp_status;
753         MonoSemTimedwaitRet wait_ret;
754         MonoInternalThread *thread;
755         gboolean interrupted = FALSE;
756
757         LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
758
759         if (G_UNLIKELY (!obj)) {
760                 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
761                 return FALSE;
762         }
763
764         lw.sync = obj->synchronisation;
765         mon = lock_word_get_inflated_lock (lw);
766 retry:
767         /* This case differs from Dice's case 3 because we don't
768          * deflate locks or cache unused lock records
769          */
770         old_status = mon->status;
771         if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
772                 /* Try to install our ID in the owner field, nest
773                 * should have been left at 1 by the previous unlock
774                 * operation
775                 */
776                 new_status = mon_status_set_owner (old_status, id);
777                 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
778                 if (G_LIKELY (tmp_status == old_status)) {
779                         /* Success */
780                         g_assert (mon->nest == 1);
781                         return 1;
782                 } else {
783                         /* Trumped again! */
784                         goto retry;
785                 }
786         }
787
788         /* If the object is currently locked by this thread... */
789         if (mon_status_get_owner (old_status) == id) {
790                 mon->nest++;
791                 return 1;
792         }
793
794         /* The object must be locked by someone else... */
795 #ifndef DISABLE_PERFCOUNTERS
796         InterlockedIncrement (&mono_perfcounters->thread_contentions);
797 #endif
798
799         /* If ms is 0 we don't block, but just fail straight away */
800         if (ms == 0) {
801                 LOCK_DEBUG (g_message ("%s: (%d) timed out, returning FALSE", __func__, id));
802                 return 0;
803         }
804
805         MONO_PROFILER_RAISE (monitor_contention, (obj));
806
807         /* The slow path begins here. */
808 retry_contended:
809         /* a small amount of duplicated code, but it allows us to insert the profiler
810          * callbacks without impacting the fast path: from here on we don't need to go back to the
811          * retry label, but to retry_contended. At this point mon is already installed in the object
812          * header.
813          */
814         /* This case differs from Dice's case 3 because we don't
815          * deflate locks or cache unused lock records
816          */
817         old_status = mon->status;
818         if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
819                 /* Try to install our ID in the owner field, nest
820                 * should have been left at 1 by the previous unlock
821                 * operation
822                 */
823                 new_status = mon_status_set_owner (old_status, id);
824                 tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
825                 if (G_LIKELY (tmp_status == old_status)) {
826                         /* Success */
827                         g_assert (mon->nest == 1);
828                         MONO_PROFILER_RAISE (monitor_acquired, (obj));
829                         return 1;
830                 }
831         }
832
833         /* If the object is currently locked by this thread... */
834         if (mon_status_get_owner (old_status) == id) {
835                 mon->nest++;
836                 MONO_PROFILER_RAISE (monitor_acquired, (obj));
837                 return 1;
838         }
839
840         /* We need to make sure there's a semaphore handle (creating it if
841          * necessary), and block on it
842          */
843         if (mon->entry_sem == NULL) {
844                 /* Create the semaphore */
845                 sem = g_new0 (MonoCoopSem, 1);
846                 mono_coop_sem_init (sem, 0);
847                 if (InterlockedCompareExchangePointer ((gpointer*)&mon->entry_sem, sem, NULL) != NULL) {
848                         /* Someone else just put a handle here */
849                         mono_coop_sem_destroy (sem);
850                         g_free (sem);
851                 }
852         }
853
854         /*
855          * We need to register ourselves as waiting if it is the first time we are waiting,
856          * of if we were signaled and failed to acquire the lock.
857          */
858         if (!interrupted) {
859                 old_status = mon->status;
860                 for (;;) {
861                         if (mon_status_get_owner (old_status) == 0)
862                                 goto retry_contended;
863                         new_status = mon_status_increment_entry_count (old_status);
864                         tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
865                         if (tmp_status == old_status) {
866                                 break;
867                         }
868                         old_status = tmp_status;
869                 }
870         }
871
872         if (ms != MONO_INFINITE_WAIT) {
873                 then = mono_msec_ticks ();
874         }
875         waitms = ms;
876         
877 #ifndef DISABLE_PERFCOUNTERS
878         InterlockedIncrement (&mono_perfcounters->thread_queue_len);
879         InterlockedIncrement (&mono_perfcounters->thread_queue_max);
880 #endif
881         thread = mono_thread_internal_current ();
882
883         /*
884          * If we allow interruption, we check the test state for an abort request before going into sleep.
885          * This is a workaround to the fact that Thread.Abort does non-sticky interruption of semaphores.
886          *
887          * Semaphores don't support the sticky interruption with mono_thread_info_install_interrupt.
888          *
889          * A better fix would be to switch to wait with something that allows sticky interrupts together
890          * with wrapping it with abort_protected_block_count for the non-alertable cases.
891          * And somehow make this whole dance atomic and not crazy expensive. Good luck.
892          *
893          */
894         if (allow_interruption) {
895                 if (!mono_thread_test_and_set_state (thread, ThreadState_AbortRequested, ThreadState_WaitSleepJoin)) {
896                         wait_ret = MONO_SEM_TIMEDWAIT_RET_ALERTED;
897                         goto done_waiting;
898                 }
899         } else {
900                 mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
901         }
902
903         /*
904          * We pass ALERTABLE instead of allow_interruption since we have to check for the
905          * StopRequested case below.
906          */
907         wait_ret = mono_coop_sem_timedwait (mon->entry_sem, waitms, MONO_SEM_FLAGS_ALERTABLE);
908
909         mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
910
911 done_waiting:
912 #ifndef DISABLE_PERFCOUNTERS
913         InterlockedDecrement (&mono_perfcounters->thread_queue_len);
914 #endif
915
916         if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED && !allow_interruption) {
917                 interrupted = TRUE;
918                 /* 
919                  * We have to obey a stop/suspend request even if 
920                  * allow_interruption is FALSE to avoid hangs at shutdown.
921                  */
922                 if (!mono_thread_test_state (mono_thread_internal_current (), ThreadState_SuspendRequested | ThreadState_AbortRequested)) {
923                         if (ms != MONO_INFINITE_WAIT) {
924                                 now = mono_msec_ticks ();
925
926                                 /* it should not overflow before ~30k years */
927                                 g_assert (now >= then);
928
929                                 delta = now - then;
930                                 if (delta >= ms) {
931                                         ms = 0;
932                                 } else {
933                                         ms -= delta;
934                                 }
935                         }
936                         /* retry from the top */
937                         goto retry_contended;
938                 }
939         } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
940                 interrupted = FALSE;
941                 /* retry from the top */
942                 goto retry_contended;
943         } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
944                 /* we're done */
945         }
946
947         /* Timed out or interrupted */
948         mon_decrement_entry_count (mon);
949
950         MONO_PROFILER_RAISE (monitor_failed, (obj));
951
952         if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
953                 LOCK_DEBUG (g_message ("%s: (%d) interrupted waiting, returning -1", __func__, id));
954                 return -1;
955         } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
956                 LOCK_DEBUG (g_message ("%s: (%d) timed out waiting, returning FALSE", __func__, id));
957                 return 0;
958         } else {
959                 g_assert_not_reached ();
960                 return 0;
961         }
962 }
963
964 /*
965  * If allow_interruption == TRUE, the method will be interrupted if abort or suspend
966  * is requested. In this case it returns -1.
967  */
968 static inline gint32
969 mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
970 {
971         LockWord lw;
972         int id = mono_thread_info_get_small_id ();
973
974         LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
975
976         lw.sync = obj->synchronisation;
977
978         if (G_LIKELY (lock_word_is_free (lw))) {
979                 LockWord nlw = lock_word_new_flat (id);
980                 if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, NULL) == NULL) {
981                         return 1;
982                 } else {
983                         /* Someone acquired it in the meantime or put a hash */
984                         mono_monitor_inflate (obj);
985                         return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
986                 }
987         } else if (lock_word_is_inflated (lw)) {
988                 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
989         } else if (lock_word_is_flat (lw)) {
990                 if (lock_word_get_owner (lw) == id) {
991                         if (lock_word_is_max_nest (lw)) {
992                                 mono_monitor_inflate_owned (obj, id);
993                                 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
994                         } else {
995                                 LockWord nlw, old_lw;
996                                 nlw = lock_word_increment_nest (lw);
997                                 old_lw.sync = (MonoThreadsSync *)InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, lw.sync);
998                                 if (old_lw.sync != lw.sync) {
999                                         /* Someone else inflated it in the meantime */
1000                                         g_assert (lock_word_is_inflated (old_lw));
1001                                         return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1002                                 }
1003                                 return 1;
1004                         }
1005                 } else {
1006                         mono_monitor_inflate (obj);
1007                         return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1008                 }
1009         } else if (lock_word_has_hash (lw)) {
1010                 mono_monitor_inflate (obj);
1011                 return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
1012         }
1013
1014         g_assert_not_reached ();
1015         return -1;
1016 }
1017
1018 /* This is an icall */
1019 MonoBoolean
1020 mono_monitor_enter_internal (MonoObject *obj)
1021 {
1022         gint32 res;
1023         gboolean allow_interruption = TRUE;
1024         if (G_UNLIKELY (!obj)) {
1025                 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1026                 return FALSE;
1027         }
1028
1029         /*
1030          * An inquisitive mind could ask what's the deal with this loop.
1031          * It exists to deal with interrupting a monitor enter that happened within an abort-protected block, like a .cctor.
1032          *
1033          * 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,
1034          * it will return NULL meaning we can't be aborted right now. Once that happens we switch to non-alertable.
1035          */
1036         do {
1037                 res = mono_monitor_try_enter_internal (obj, MONO_INFINITE_WAIT, allow_interruption);
1038                 /*This means we got interrupted during the wait and didn't got the monitor.*/
1039                 if (res == -1) {
1040                         MonoException *exc = mono_thread_interruption_checkpoint ();
1041                         if (exc) {
1042                                 mono_set_pending_exception (exc);
1043                                 return FALSE;
1044                         } else {
1045                                 //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)
1046                                 allow_interruption = FALSE;
1047                         }
1048                 }
1049         } while (res == -1);
1050         return TRUE;
1051 }
1052
1053 /**
1054  * mono_monitor_enter:
1055  */
1056 gboolean
1057 mono_monitor_enter (MonoObject *obj)
1058 {
1059         return mono_monitor_enter_internal (obj);
1060 }
1061
1062 /* Called from JITted code so we return guint32 instead of gboolean */
1063 guint32
1064 mono_monitor_enter_fast (MonoObject *obj)
1065 {
1066         if (G_UNLIKELY (!obj)) {
1067                 /* don't set pending exn on the fast path, just return
1068                  * FALSE and let the slow path take care of it. */
1069                 return FALSE;
1070         }
1071         return mono_monitor_try_enter_internal (obj, 0, FALSE) == 1;
1072 }
1073
1074 /**
1075  * mono_monitor_try_enter:
1076  */
1077 gboolean
1078 mono_monitor_try_enter (MonoObject *obj, guint32 ms)
1079 {
1080         if (G_UNLIKELY (!obj)) {
1081                 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1082                 return FALSE;
1083         }
1084         return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
1085 }
1086
1087 /**
1088  * mono_monitor_exit:
1089  */
1090 void
1091 mono_monitor_exit (MonoObject *obj)
1092 {
1093         LockWord lw;
1094         
1095         LOCK_DEBUG (g_message ("%s: (%d) Unlocking %p", __func__, mono_thread_info_get_small_id (), obj));
1096
1097         if (G_UNLIKELY (!obj)) {
1098                 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1099                 return;
1100         }
1101
1102         lw.sync = obj->synchronisation;
1103
1104         if (!mono_monitor_ensure_owned (lw, mono_thread_info_get_small_id ()))
1105                 return;
1106
1107         if (G_UNLIKELY (lock_word_is_inflated (lw)))
1108                 mono_monitor_exit_inflated (obj);
1109         else
1110                 mono_monitor_exit_flat (obj, lw);
1111 }
1112
1113 guint32
1114 mono_monitor_get_object_monitor_gchandle (MonoObject *object)
1115 {
1116         LockWord lw;
1117
1118         lw.sync = object->synchronisation;
1119
1120         if (lock_word_is_inflated (lw)) {
1121                 MonoThreadsSync *mon = lock_word_get_inflated_lock (lw);
1122                 return (guint32)mon->data;
1123         }
1124         return 0;
1125 }
1126
1127 /*
1128  * mono_monitor_threads_sync_member_offset:
1129  * @status_offset: returns size and offset of the "status" member
1130  * @nest_offset: returns size and offset of the "nest" member
1131  *
1132  * Returns the offsets and sizes of two members of the
1133  * MonoThreadsSync struct.  The Monitor ASM fastpaths need this.
1134  */
1135 void
1136 mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset)
1137 {
1138         MonoThreadsSync ts;
1139
1140 #define ENCODE_OFF_SIZE(o,s)    (((o) << 8) | ((s) & 0xff))
1141
1142         *status_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, status), sizeof (ts.status));
1143         *nest_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, nest), sizeof (ts.nest));
1144 }
1145
1146 void
1147 ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject *obj, guint32 ms, MonoBoolean *lockTaken)
1148 {
1149         gint32 res;
1150         gboolean allow_interruption = TRUE;
1151         if (G_UNLIKELY (!obj)) {
1152                 mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
1153                 return;
1154         }
1155         do {
1156                 res = mono_monitor_try_enter_internal (obj, ms, allow_interruption);
1157                 /*This means we got interrupted during the wait and didn't got the monitor.*/
1158                 if (res == -1) {
1159                         MonoException *exc = mono_thread_interruption_checkpoint ();
1160                         if (exc) {
1161                                 mono_set_pending_exception (exc);
1162                                 return;
1163                         } else {
1164                                 //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)
1165                                 allow_interruption = FALSE;
1166                         }
1167                 }
1168         } while (res == -1);
1169         /*It's safe to do it from here since interruption would happen only on the wrapper.*/
1170         *lockTaken = res == 1;
1171 }
1172
1173 /**
1174  * mono_monitor_enter_v4:
1175  */
1176 void
1177 mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
1178 {
1179         if (*lock_taken == 1) {
1180                 mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
1181                 return;
1182         }
1183
1184         MonoBoolean taken;
1185
1186         ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, &taken);
1187         *lock_taken = taken;
1188 }
1189
1190 /* Called from JITted code */
1191 void
1192 mono_monitor_enter_v4_internal (MonoObject *obj, MonoBoolean *lock_taken)
1193 {
1194         if (*lock_taken == 1) {
1195                 mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
1196                 return;
1197         }
1198
1199         ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, lock_taken);
1200 }
1201
1202 /*
1203  * mono_monitor_enter_v4_fast:
1204  *
1205  *   Same as mono_monitor_enter_v4, but return immediately if the
1206  * monitor cannot be acquired.
1207  * Returns TRUE if the lock was acquired, FALSE otherwise.
1208  * Called from JITted code so we return guint32 instead of gboolean.
1209  */
1210 guint32
1211 mono_monitor_enter_v4_fast (MonoObject *obj, MonoBoolean *lock_taken)
1212 {
1213         if (*lock_taken == 1)
1214                 return FALSE;
1215         if (G_UNLIKELY (!obj))
1216                 return FALSE;
1217         gint32 res = mono_monitor_try_enter_internal (obj, 0, TRUE);
1218         *lock_taken = res == 1;
1219         return res == 1;
1220 }
1221
1222 MonoBoolean
1223 ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
1224 {
1225         LockWord lw;
1226
1227         LOCK_DEBUG (g_message ("%s: Testing if %p is owned by thread %d", __func__, obj, mono_thread_info_get_small_id()));
1228
1229         lw.sync = obj->synchronisation;
1230
1231         if (lock_word_is_flat (lw)) {
1232                 return lock_word_get_owner (lw) == mono_thread_info_get_small_id ();
1233         } else if (lock_word_is_inflated (lw)) {
1234                 return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == mono_thread_info_get_small_id ();
1235         }
1236         
1237         return(FALSE);
1238 }
1239
1240 MonoBoolean
1241 ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
1242 {
1243         LockWord lw;
1244
1245         LOCK_DEBUG (g_message("%s: (%d) Testing if %p is owned by any thread", __func__, mono_thread_info_get_small_id (), obj));
1246
1247         lw.sync = obj->synchronisation;
1248
1249         if (lock_word_is_flat (lw)) {
1250                 return !lock_word_is_free (lw);
1251         } else if (lock_word_is_inflated (lw)) {
1252                 return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) != 0;
1253         }
1254
1255         return FALSE;
1256 }
1257
1258 /* All wait list manipulation in the pulse, pulseall and wait
1259  * functions happens while the monitor lock is held, so we don't need
1260  * any extra struct locking
1261  */
1262
1263 void
1264 ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
1265 {
1266         int id;
1267         LockWord lw;
1268         MonoThreadsSync *mon;
1269
1270         LOCK_DEBUG (g_message ("%s: (%d) Pulsing %p", __func__, mono_thread_info_get_small_id (), obj));
1271         
1272         id = mono_thread_info_get_small_id ();
1273         lw.sync = obj->synchronisation;
1274
1275         if (!mono_monitor_ensure_owned (lw, id))
1276                 return;
1277
1278         if (!lock_word_is_inflated (lw)) {
1279                 /* No threads waiting. A wait would have inflated the lock */
1280                 return;
1281         }
1282
1283         mon = lock_word_get_inflated_lock (lw);
1284
1285         LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
1286
1287         if (mon->wait_list != NULL) {
1288                 LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
1289         
1290                 mono_w32event_set (mon->wait_list->data);
1291                 mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
1292         }
1293 }
1294
1295 void
1296 ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
1297 {
1298         int id;
1299         LockWord lw;
1300         MonoThreadsSync *mon;
1301         
1302         LOCK_DEBUG (g_message("%s: (%d) Pulsing all %p", __func__, mono_thread_info_get_small_id (), obj));
1303
1304         id = mono_thread_info_get_small_id ();
1305         lw.sync = obj->synchronisation;
1306
1307         if (!mono_monitor_ensure_owned (lw, id))
1308                 return;
1309
1310         if (!lock_word_is_inflated (lw)) {
1311                 /* No threads waiting. A wait would have inflated the lock */
1312                 return;
1313         }
1314
1315         mon = lock_word_get_inflated_lock (lw);
1316
1317         LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
1318
1319         while (mon->wait_list != NULL) {
1320                 LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
1321         
1322                 mono_w32event_set (mon->wait_list->data);
1323                 mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
1324         }
1325 }
1326
1327 MonoBoolean
1328 ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
1329 {
1330         LockWord lw;
1331         MonoThreadsSync *mon;
1332         HANDLE event;
1333         guint32 nest;
1334         MonoW32HandleWaitRet ret;
1335         gboolean success = FALSE;
1336         gint32 regain;
1337         MonoInternalThread *thread = mono_thread_internal_current ();
1338         int id = mono_thread_info_get_small_id ();
1339
1340         LOCK_DEBUG (g_message ("%s: (%d) Trying to wait for %p with timeout %dms", __func__, mono_thread_info_get_small_id (), obj, ms));
1341
1342         lw.sync = obj->synchronisation;
1343
1344         if (!mono_monitor_ensure_owned (lw, id))
1345                 return FALSE;
1346
1347         if (!lock_word_is_inflated (lw)) {
1348                 mono_monitor_inflate_owned (obj, id);
1349                 lw.sync = obj->synchronisation;
1350         }
1351
1352         mon = lock_word_get_inflated_lock (lw);
1353
1354         /* Do this WaitSleepJoin check before creating the event handle */
1355         if (mono_thread_current_check_pending_interrupt ())
1356                 return FALSE;
1357         
1358         event = mono_w32event_create (FALSE, FALSE);
1359         if (event == NULL) {
1360                 mono_set_pending_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
1361                 return FALSE;
1362         }
1363         
1364         LOCK_DEBUG (g_message ("%s: (%d) queuing handle %p", __func__, mono_thread_info_get_small_id (), event));
1365
1366         /* This looks superfluous */
1367         if (mono_thread_current_check_pending_interrupt ()) {
1368                 mono_w32event_close (event);
1369                 return FALSE;
1370         }
1371         
1372         mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
1373
1374         mon->wait_list = g_slist_append (mon->wait_list, event);
1375         
1376         /* Save the nest count, and release the lock */
1377         nest = mon->nest;
1378         mon->nest = 1;
1379         mono_memory_write_barrier ();
1380         mono_monitor_exit_inflated (obj);
1381
1382         LOCK_DEBUG (g_message ("%s: (%d) Unlocked %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
1383
1384         /* There's no race between unlocking mon and waiting for the
1385          * event, because auto reset events are sticky, and this event
1386          * is private to this thread.  Therefore even if the event was
1387          * signalled before we wait, we still succeed.
1388          */
1389         MONO_ENTER_GC_SAFE;
1390 #ifdef HOST_WIN32
1391         ret = mono_w32handle_convert_wait_ret (mono_win32_wait_for_single_object_ex (event, ms, TRUE), 1);
1392 #else
1393         ret = mono_w32handle_wait_one (event, ms, TRUE);
1394 #endif /* HOST_WIN32 */
1395         MONO_EXIT_GC_SAFE;
1396
1397         /* Reset the thread state fairly early, so we don't have to worry
1398          * about the monitor error checking
1399          */
1400         mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
1401
1402         /* Regain the lock with the previous nest count */
1403         do {
1404                 regain = mono_monitor_try_enter_inflated (obj, MONO_INFINITE_WAIT, TRUE, id);
1405                 /* We must regain the lock before handling interruption requests */
1406         } while (regain == -1);
1407
1408         g_assert (regain == 1);
1409
1410         mon->nest = nest;
1411
1412         LOCK_DEBUG (g_message ("%s: (%d) Regained %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
1413
1414         if (ret == MONO_W32HANDLE_WAIT_RET_TIMEOUT) {
1415                 /* Poll the event again, just in case it was signalled
1416                  * while we were trying to regain the monitor lock
1417                  */
1418                 MONO_ENTER_GC_SAFE;
1419 #ifdef HOST_WIN32
1420                 ret = mono_w32handle_convert_wait_ret (mono_win32_wait_for_single_object_ex (event, 0, FALSE), 1);
1421 #else
1422                 ret = mono_w32handle_wait_one (event, 0, FALSE);
1423 #endif /* HOST_WIN32 */
1424                 MONO_EXIT_GC_SAFE;
1425         }
1426
1427         /* Pulse will have popped our event from the queue if it signalled
1428          * us, so we only do it here if the wait timed out.
1429          *
1430          * This avoids a race condition where the thread holding the
1431          * lock can Pulse several times before the WaitForSingleObject
1432          * returns.  If we popped the queue here then this event might
1433          * be signalled more than once, thereby starving another
1434          * thread.
1435          */
1436         
1437         if (ret == MONO_W32HANDLE_WAIT_RET_SUCCESS_0) {
1438                 LOCK_DEBUG (g_message ("%s: (%d) Success", __func__, mono_thread_info_get_small_id ()));
1439                 success = TRUE;
1440         } else {
1441                 LOCK_DEBUG (g_message ("%s: (%d) Wait failed, dequeuing handle %p", __func__, mono_thread_info_get_small_id (), event));
1442                 /* No pulse, so we have to remove ourself from the
1443                  * wait queue
1444                  */
1445                 mon->wait_list = g_slist_remove (mon->wait_list, event);
1446         }
1447         mono_w32event_close (event);
1448         
1449         return success;
1450 }
1451