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