Simplify function interface for rework of image.c hashes, based on PR comments
[mono.git] / mono / metadata / monitor.c
index 734f3f2de768403e32b6fc308cfa56ac68a80f63..151c1d382c91033269361ce8ff1fcdbb2da617f2 100644 (file)
@@ -25,6 +25,7 @@
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/marshal.h>
+#include <mono/utils/mono-threads.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/utils/mono-time.h>
 #include <mono/utils/atomic.h>
@@ -63,15 +64,7 @@ enum {
  *
  * Bacon's thin locks have a fast path that doesn't need a lock record
  * for the common case of locking an unlocked or shallow-nested
- * object, but the technique relies on encoding the thread ID in 15
- * bits (to avoid too much per-object space overhead.)  Unfortunately
- * I don't think it's possible to reliably encode a pthread_t into 15
- * bits. (The JVM implementation used seems to have a 15-bit
- * per-thread identifier available.)
- *
- * This implementation then combines Dice's basic lock model with
- * Bacon's simplification of keeping a lock record for the lifetime of
- * an object.
+ * object.
  */
 
 
@@ -90,21 +83,167 @@ static MonoThreadsSync *monitor_freelist;
 static MonitorArray *monitor_allocated;
 static int array_size = 16;
 
-#ifdef HAVE_KW_THREAD
-static __thread gsize tls_pthread_self MONO_TLS_FAST;
-#endif
+/* MonoThreadsSync status helpers */
 
-#ifndef HOST_WIN32
-#ifdef HAVE_KW_THREAD
-#define GetCurrentThreadId() tls_pthread_self
-#else
-/* 
- * The usual problem: we can't replace GetCurrentThreadId () with a macro because
- * it is in a public header.
- */
-#define GetCurrentThreadId() ((gsize)pthread_self ())
-#endif
-#endif
+static inline guint32
+mon_status_get_owner (guint32 status)
+{
+       return status & OWNER_MASK;
+}
+
+static inline guint32
+mon_status_set_owner (guint32 status, guint32 owner)
+{
+       return (status & ENTRY_COUNT_MASK) | owner;
+}
+
+static inline gint32
+mon_status_get_entry_count (guint32 status)
+{
+       gint32 entry_count = (gint32)((status & ENTRY_COUNT_MASK) >> ENTRY_COUNT_SHIFT);
+       gint32 zero = (gint32)(((guint32)ENTRY_COUNT_ZERO) >> ENTRY_COUNT_SHIFT);
+       return entry_count - zero;
+}
+
+static inline guint32
+mon_status_init_entry_count (guint32 status)
+{
+       return (status & OWNER_MASK) | ENTRY_COUNT_ZERO;
+}
+
+static inline guint32
+mon_status_increment_entry_count (guint32 status)
+{
+       return status + (1 << ENTRY_COUNT_SHIFT);
+}
+
+static inline guint32
+mon_status_decrement_entry_count (guint32 status)
+{
+       return status - (1 << ENTRY_COUNT_SHIFT);
+}
+
+static inline gboolean
+mon_status_have_waiters (guint32 status)
+{
+       return status & ENTRY_COUNT_WAITERS;
+}
+
+/* LockWord helpers */
+
+static inline MonoThreadsSync*
+lock_word_get_inflated_lock (LockWord lw)
+{
+       lw.lock_word &= (~LOCK_WORD_STATUS_MASK);
+       return lw.sync;
+}
+
+static inline gboolean
+lock_word_is_inflated (LockWord lw)
+{
+       return lw.lock_word & LOCK_WORD_INFLATED;
+}
+
+static inline gboolean
+lock_word_has_hash (LockWord lw)
+{
+       return lw.lock_word & LOCK_WORD_HAS_HASH;
+}
+
+static inline LockWord
+lock_word_set_has_hash (LockWord lw)
+{
+       LockWord nlw;
+       nlw.lock_word = lw.lock_word | LOCK_WORD_HAS_HASH;
+       return nlw;
+}
+
+static inline gboolean
+lock_word_is_free (LockWord lw)
+{
+       return !lw.lock_word;
+}
+
+static inline gboolean
+lock_word_is_flat (LockWord lw)
+{
+       /* Return whether the lock is flat or free */
+       return (lw.lock_word & LOCK_WORD_STATUS_MASK) == LOCK_WORD_FLAT;
+}
+
+static inline gint32
+lock_word_get_hash (LockWord lw)
+{
+       return (gint32) (lw.lock_word >> LOCK_WORD_HASH_SHIFT);
+}
+
+static inline gint32
+lock_word_get_nest (LockWord lw)
+{
+       if (lock_word_is_free (lw))
+               return 0;
+       /* Inword nest count starts from 0 */
+       return ((lw.lock_word & LOCK_WORD_NEST_MASK) >> LOCK_WORD_NEST_SHIFT) + 1;
+}
+
+static inline gboolean
+lock_word_is_nested (LockWord lw)
+{
+       return lw.lock_word & LOCK_WORD_NEST_MASK;
+}
+
+static inline gboolean
+lock_word_is_max_nest (LockWord lw)
+{
+       return (lw.lock_word & LOCK_WORD_NEST_MASK) == LOCK_WORD_NEST_MASK;
+}
+
+static inline LockWord
+lock_word_increment_nest (LockWord lw)
+{
+       lw.lock_word += 1 << LOCK_WORD_NEST_SHIFT;
+       return lw;
+}
+
+static inline LockWord
+lock_word_decrement_nest (LockWord lw)
+{
+       lw.lock_word -= 1 << LOCK_WORD_NEST_SHIFT;
+       return lw;
+}
+
+static inline gint32
+lock_word_get_owner (LockWord lw)
+{
+       return lw.lock_word >> LOCK_WORD_OWNER_SHIFT;
+}
+
+static inline LockWord
+lock_word_new_thin_hash (gint32 hash)
+{
+       LockWord lw;
+       lw.lock_word = (guint32)hash;
+       lw.lock_word = (lw.lock_word << LOCK_WORD_HASH_SHIFT) | LOCK_WORD_HAS_HASH;
+       return lw;
+}
+
+static inline LockWord
+lock_word_new_inflated (MonoThreadsSync *mon)
+{
+       LockWord lw;
+       lw.sync = mon;
+       lw.lock_word |= LOCK_WORD_INFLATED;
+       return lw;
+}
+
+static inline LockWord
+lock_word_new_flat (gint32 owner)
+{
+       LockWord lw;
+       lw.lock_word = owner;
+       lw.lock_word <<= LOCK_WORD_OWNER_SHIFT;
+       return lw;
+}
 
 void
 mono_monitor_init (void)
@@ -124,7 +263,19 @@ mono_monitor_cleanup (void)
        for (mon = monitor_freelist; mon; mon = mon->data)
                mon->wait_list = (gpointer)-1;
 
-       /* FIXME: This still crashes with sgen (async_read.exe) */
+       /*
+        * FIXME: This still crashes with sgen (async_read.exe)
+        *
+        * In mini_cleanup() we first call mono_runtime_cleanup(), which calls
+        * mono_monitor_cleanup(), which is supposed to free all monitor memory.
+        *
+        * Later in mini_cleanup(), we call mono_domain_free(), which calls
+        * mono_gc_clear_domain(), which frees all weak links associated with objects.
+        * Those weak links reside in the monitor structures, which we've freed earlier.
+        *
+        * Unless we fix this dependency in the shutdown sequence this code has to remain
+        * disabled, or at least the call to g_free().
+        */
        /*
        for (marray = monitor_allocated; marray; marray = next) {
                int i;
@@ -141,19 +292,6 @@ mono_monitor_cleanup (void)
        */
 }
 
-/*
- * mono_monitor_init_tls:
- *
- *   Setup TLS variables used by the monitor code for the current thread.
- */
-void
-mono_monitor_init_tls (void)
-{
-#if !defined(HOST_WIN32) && defined(HAVE_KW_THREAD)
-       tls_pthread_self = (gsize) pthread_self ();
-#endif
-}
-
 static int
 monitor_is_on_freelist (MonoThreadsSync *mon)
 {
@@ -193,12 +331,12 @@ mono_locks_dump (gboolean include_untaken)
                                        to_recycle++;
                        } else {
                                if (!monitor_is_on_freelist (mon->data)) {
-                                       MonoObject *holder = mono_gc_weak_link_get (&mon->data);
-                                       if (mon->owner) {
-                                               g_print ("Lock %p in object %p held by thread %p, nest level: %d\n",
-                                                       mon, holder, (void*)mon->owner, mon->nest);
+                                       MonoObject *holder = (MonoObject *)mono_gchandle_get_target ((guint32)mon->data);
+                                       if (mon_status_get_owner (mon->status)) {
+                                               g_print ("Lock %p in object %p held by thread %d, nest level: %d\n",
+                                                       mon, holder, mon_status_get_owner (mon->status), mon->nest);
                                                if (mon->entry_sem)
-                                                       g_print ("\tWaiting on semaphore %p: %d\n", mon->entry_sem, mon->entry_count);
+                                                       g_print ("\tWaiting on semaphore %p: %d\n", mon->entry_sem, mon_status_get_entry_count (mon->status));
                                        } else if (include_untaken) {
                                                g_print ("Lock %p in object %p untaken\n", mon, holder);
                                        }
@@ -227,7 +365,6 @@ mon_finalize (MonoThreadsSync *mon)
         */
        g_assert (mon->wait_list == NULL);
 
-       mon->entry_count = 0;
        /* owner and nest are set in mon_new, no need to zero them out */
 
        mon->data = monitor_freelist;
@@ -250,17 +387,17 @@ mon_new (gsize id)
                new = NULL;
                for (marray = monitor_allocated; marray; marray = marray->next) {
                        for (i = 0; i < marray->num_monitors; ++i) {
-                               if (marray->monitors [i].data == NULL) {
+                               if (mono_gchandle_get_target ((guint32)marray->monitors [i].data) == NULL) {
                                        new = &marray->monitors [i];
                                        if (new->wait_list) {
                                                /* Orphaned events left by aborted threads */
                                                while (new->wait_list) {
-                                                       LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d): Closing orphaned event %d", GetCurrentThreadId (), new->wait_list->data));
+                                                       LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d): Closing orphaned event %d", mono_thread_info_get_small_id (), new->wait_list->data));
                                                        CloseHandle (new->wait_list->data);
                                                        new->wait_list = g_slist_remove (new->wait_list, new->wait_list->data);
                                                }
                                        }
-                                       mono_gc_weak_link_remove (&new->data, TRUE);
+                                       mono_gchandle_free ((guint32)new->data);
                                        new->data = monitor_freelist;
                                        monitor_freelist = new;
                                }
@@ -299,7 +436,8 @@ mon_new (gsize id)
        new = monitor_freelist;
        monitor_freelist = new->data;
 
-       new->owner = id;
+       new->status = mon_status_set_owner (0, id);
+       new->status = mon_status_init_entry_count (new->status);
        new->nest = 1;
        new->data = NULL;
        
@@ -309,26 +447,107 @@ mon_new (gsize id)
        return new;
 }
 
-/*
- * Format of the lock word:
- * thinhash | fathash | data
- *
- * thinhash is the lower bit: if set data is the shifted hashcode of the object.
- * fathash is another bit: if set the hash code is stored in the MonoThreadsSync
- *   struct pointed to by data
- * if neither bit is set and data is non-NULL, data is a MonoThreadsSync
- */
-typedef union {
-       gsize lock_word;
-       MonoThreadsSync *sync;
-} LockWord;
+static MonoThreadsSync*
+alloc_mon (MonoObject *obj, gint32 id)
+{
+       MonoThreadsSync *mon;
 
-enum {
-       LOCK_WORD_THIN_HASH = 1,
-       LOCK_WORD_FAT_HASH = 1 << 1,
-       LOCK_WORD_BITS_MASK = 0x3,
-       LOCK_WORD_HASH_SHIFT = 2
-};
+       mono_monitor_allocator_lock ();
+       mon = mon_new (id);
+       mon->data = (void *)(size_t)mono_gchandle_new_weakref (obj, TRUE);
+       mono_monitor_allocator_unlock ();
+
+       return mon;
+}
+
+
+static void
+discard_mon (MonoThreadsSync *mon)
+{
+       mono_monitor_allocator_lock ();
+       mono_gchandle_free ((guint32)mon->data);
+       mon_finalize (mon);
+       mono_monitor_allocator_unlock ();
+}
+
+static void
+mono_monitor_inflate_owned (MonoObject *obj, int id)
+{
+       MonoThreadsSync *mon;
+       LockWord nlw, old_lw, tmp_lw;
+       guint32 nest;
+
+       old_lw.sync = obj->synchronisation;
+       LOCK_DEBUG (g_message ("%s: (%d) Inflating owned lock object %p; LW = %p", __func__, id, obj, old_lw.sync));
+
+       if (lock_word_is_inflated (old_lw)) {
+               /* Someone else inflated the lock in the meantime */
+               return;
+       }
+
+       mon = alloc_mon (obj, id);
+
+       nest = lock_word_get_nest (old_lw);
+       mon->nest = nest;
+
+       nlw = lock_word_new_inflated (mon);
+
+       mono_memory_write_barrier ();
+       tmp_lw.sync = InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
+       if (tmp_lw.sync != old_lw.sync) {
+               /* Someone else inflated the lock in the meantime */
+               discard_mon (mon);
+       }
+}
+
+static void
+mono_monitor_inflate (MonoObject *obj)
+{
+       MonoThreadsSync *mon;
+       LockWord nlw, old_lw;
+
+       LOCK_DEBUG (g_message ("%s: (%d) Inflating lock object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
+
+       mon = alloc_mon (obj, 0);
+
+       nlw = lock_word_new_inflated (mon);
+
+       old_lw.sync = obj->synchronisation;
+
+       for (;;) {
+               LockWord tmp_lw;
+
+               if (lock_word_is_inflated (old_lw)) {
+                       break;
+               }
+#ifdef HAVE_MOVING_COLLECTOR
+                else if (lock_word_has_hash (old_lw)) {
+                       mon->hash_code = lock_word_get_hash (old_lw);
+                       mon->status = mon_status_set_owner (mon->status, 0);
+                       nlw = lock_word_set_has_hash (nlw);
+               }
+#endif
+               else if (lock_word_is_free (old_lw)) {
+                       mon->status = mon_status_set_owner (mon->status, 0);
+                       mon->nest = 1;
+               } else {
+                       /* Lock is flat */
+                       mon->status = mon_status_set_owner (mon->status, lock_word_get_owner (old_lw));
+                       mon->nest = lock_word_get_nest (old_lw);
+               }
+               mono_memory_write_barrier ();
+               tmp_lw.sync = InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, old_lw.sync);
+               if (tmp_lw.sync == old_lw.sync) {
+                       /* Successfully inflated the lock */
+                       return;
+               }
+
+               old_lw.sync = tmp_lw.sync;
+       }
+
+       /* Someone else inflated the lock before us */
+       discard_mon (mon);
+}
 
 #define MONO_OBJECT_ALIGNMENT_SHIFT    3
 
@@ -347,14 +566,15 @@ mono_object_hash (MonoObject* obj)
        if (!obj)
                return 0;
        lw.sync = obj->synchronisation;
-       if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-               /*g_print ("fast thin hash %d for obj %p store\n", (unsigned int)lw.lock_word >> LOCK_WORD_HASH_SHIFT, obj);*/
-               return (unsigned int)lw.lock_word >> LOCK_WORD_HASH_SHIFT;
-       }
-       if (lw.lock_word & LOCK_WORD_FAT_HASH) {
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               /*g_print ("fast fat hash %d for obj %p store\n", lw.sync->hash_code, obj);*/
-               return lw.sync->hash_code;
+
+       LOCK_DEBUG (g_message("%s: (%d) Get hash for object %p; LW = %p", __func__, mono_thread_info_get_small_id (), obj, obj->synchronisation));
+
+       if (lock_word_has_hash (lw)) {
+               if (lock_word_is_inflated (lw)) {
+                       return lock_word_get_inflated_lock (lw)->hash_code;
+               } else {
+                       return lock_word_get_hash (lw);
+               }
        }
        /*
         * while we are inside this function, the GC will keep this object pinned,
@@ -364,31 +584,40 @@ mono_object_hash (MonoObject* obj)
         * with the same value.
         */
        hash = (GPOINTER_TO_UINT (obj) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
+#if SIZEOF_VOID_P == 4
        /* clear the top bits as they can be discarded */
-       hash &= ~(LOCK_WORD_BITS_MASK << 30);
-       /* no hash flags were set, so it must be a MonoThreadsSync pointer if not NULL */
-       if (lw.sync) {
-               lw.sync->hash_code = hash;
-               /*g_print ("storing hash code %d for obj %p in sync %p\n", hash, obj, lw.sync);*/
-               lw.lock_word |= LOCK_WORD_FAT_HASH;
-               /* this is safe since we don't deflate locks */
-               obj->synchronisation = lw.sync;
-       } else {
-               /*g_print ("storing thin hash code %d for obj %p\n", hash, obj);*/
-               lw.lock_word = LOCK_WORD_THIN_HASH | (hash << LOCK_WORD_HASH_SHIFT);
-               if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, NULL) == NULL)
+       hash &= ~(LOCK_WORD_STATUS_MASK << (32 - LOCK_WORD_STATUS_BITS));
+#endif
+       if (lock_word_is_free (lw)) {
+               LockWord old_lw;
+               lw = lock_word_new_thin_hash (hash);
+
+               old_lw.sync = InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, NULL);
+               if (old_lw.sync == NULL) {
                        return hash;
-               /*g_print ("failed store\n");*/
-               /* someone set the hash flag or someone inflated the object */
-               lw.sync = obj->synchronisation;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH)
+               }
+
+               if (lock_word_has_hash (old_lw)) {
+                       /* Done by somebody else */
                        return hash;
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               lw.sync->hash_code = hash;
-               lw.lock_word |= LOCK_WORD_FAT_HASH;
-               /* this is safe since we don't deflate locks */
-               obj->synchronisation = lw.sync;
+               }
+                       
+               mono_monitor_inflate (obj);
+               lw.sync = obj->synchronisation;
+       } else if (lock_word_is_flat (lw)) {
+               int id = mono_thread_info_get_small_id ();
+               if (lock_word_get_owner (lw) == id)
+                       mono_monitor_inflate_owned (obj, id);
+               else
+                       mono_monitor_inflate (obj);
+               lw.sync = obj->synchronisation;
        }
+
+       /* At this point, the lock is inflated */
+       lock_word_get_inflated_lock (lw)->hash_code = hash;
+       lw = lock_word_set_has_hash (lw);
+       mono_memory_write_barrier ();
+       obj->synchronisation = lw.sync;
        return hash;
 #else
 /*
@@ -399,122 +628,149 @@ mono_object_hash (MonoObject* obj)
 #endif
 }
 
+static void
+mono_monitor_ensure_owned (LockWord lw, guint32 id)
+{
+       if (lock_word_is_flat (lw)) {
+               if (lock_word_get_owner (lw) == id)
+                       return;
+       } else if (lock_word_is_inflated (lw)) {
+               if (mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == id)
+                       return;
+       }
+
+       mono_set_pending_exception (mono_get_exception_synchronization_lock ("Object synchronization method was called from an unsynchronized block of code."));
+}
+
+/*
+ * When this function is called it has already been established that the
+ * current thread owns the monitor.
+ */
+static void
+mono_monitor_exit_inflated (MonoObject *obj)
+{
+       LockWord lw;
+       MonoThreadsSync *mon;
+       guint32 nest;
+
+       lw.sync = obj->synchronisation;
+       mon = lock_word_get_inflated_lock (lw);
+
+       nest = mon->nest - 1;
+       if (nest == 0) {
+               guint32 new_status, old_status, tmp_status;
+
+               old_status = mon->status;
+
+               /*
+                * Release lock and do the wakeup stuff. It's possible that
+                * the last blocking thread gave up waiting just before we
+                * release the semaphore resulting in a negative entry count
+                * and a futile wakeup next time there's contention for this
+                * object.
+                */
+               for (;;) {
+                       gboolean have_waiters = mon_status_have_waiters (old_status);
+       
+                       new_status = mon_status_set_owner (old_status, 0);
+                       if (have_waiters)
+                               new_status = mon_status_decrement_entry_count (new_status);
+                       tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
+                       if (tmp_status == old_status) {
+                               if (have_waiters)
+                                       ReleaseSemaphore (mon->entry_sem, 1, NULL);
+                               break;
+                       }
+                       old_status = tmp_status;
+               }
+               LOCK_DEBUG (g_message ("%s: (%d) Object %p is now unlocked", __func__, mono_thread_info_get_small_id (), obj));
+       
+               /* object is now unlocked, leave nest==1 so we don't
+                * need to set it when the lock is reacquired
+                */
+       } else {
+               LOCK_DEBUG (g_message ("%s: (%d) Object %p is now locked %d times", __func__, mono_thread_info_get_small_id (), obj, nest));
+               mon->nest = nest;
+       }
+}
+
+/*
+ * When this function is called it has already been established that the
+ * current thread owns the monitor.
+ */
+static void
+mono_monitor_exit_flat (MonoObject *obj, LockWord old_lw)
+{
+       LockWord new_lw, tmp_lw;
+       if (G_UNLIKELY (lock_word_is_nested (old_lw)))
+               new_lw = lock_word_decrement_nest (old_lw);
+       else
+               new_lw.lock_word = 0;
+
+       tmp_lw.sync = InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, new_lw.sync, old_lw.sync);
+       if (old_lw.sync != tmp_lw.sync) {
+               /* Someone inflated the lock in the meantime */
+               mono_monitor_exit_inflated (obj);
+       }
+
+       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));
+}
+
+static void
+mon_decrement_entry_count (MonoThreadsSync *mon)
+{
+       guint32 old_status, tmp_status, new_status;
+
+       /* Decrement entry count */
+       old_status = mon->status;
+       for (;;) {
+               new_status = mon_status_decrement_entry_count (old_status);
+               tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
+               if (tmp_status == old_status) {
+                       break;
+               }
+               old_status = tmp_status;
+       }
+}
+
 /* If allow_interruption==TRUE, the method will be interrumped if abort or suspend
  * is requested. In this case it returns -1.
  */ 
 static inline gint32 
-mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
+mono_monitor_try_enter_inflated (MonoObject *obj, guint32 ms, gboolean allow_interruption, guint32 id)
 {
+       LockWord lw;
        MonoThreadsSync *mon;
-       gsize id = GetCurrentThreadId ();
        HANDLE sem;
        guint32 then = 0, now, delta;
        guint32 waitms;
        guint32 ret;
+       guint32 new_status, old_status, tmp_status;
        MonoInternalThread *thread;
+       gboolean interrupted = FALSE;
 
        LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
 
        if (G_UNLIKELY (!obj)) {
-               mono_raise_exception (mono_get_exception_argument_null ("obj"));
+               mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
                return FALSE;
        }
 
+       lw.sync = obj->synchronisation;
+       mon = lock_word_get_inflated_lock (lw);
 retry:
-       mon = obj->synchronisation;
-
-       /* If the object has never been locked... */
-       if (G_UNLIKELY (mon == NULL)) {
-               mono_monitor_allocator_lock ();
-               mon = mon_new (id);
-               if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, mon, NULL) == NULL) {
-                       mono_gc_weak_link_add (&mon->data, obj, TRUE);
-                       mono_monitor_allocator_unlock ();
-                       /* Successfully locked */
-                       return 1;
-               } else {
-#ifdef HAVE_MOVING_COLLECTOR
-                       LockWord lw;
-                       lw.sync = obj->synchronisation;
-                       if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-                               MonoThreadsSync *oldlw = lw.sync;
-                               /* move the already calculated hash */
-                               mon->hash_code = lw.lock_word >> LOCK_WORD_HASH_SHIFT;
-                               lw.sync = mon;
-                               lw.lock_word |= LOCK_WORD_FAT_HASH;
-                               if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, oldlw) == oldlw) {
-                                       mono_gc_weak_link_add (&mon->data, obj, TRUE);
-                                       mono_monitor_allocator_unlock ();
-                                       /* Successfully locked */
-                                       return 1;
-                               } else {
-                                       mon_finalize (mon);
-                                       mono_monitor_allocator_unlock ();
-                                       goto retry;
-                               }
-                       } else if (lw.lock_word & LOCK_WORD_FAT_HASH) {
-                               mon_finalize (mon);
-                               mono_monitor_allocator_unlock ();
-                               /* get the old lock without the fat hash bit */
-                               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-                               mon = lw.sync;
-                       } else {
-                               mon_finalize (mon);
-                               mono_monitor_allocator_unlock ();
-                               mon = obj->synchronisation;
-                       }
-#else
-                       mon_finalize (mon);
-                       mono_monitor_allocator_unlock ();
-                       mon = obj->synchronisation;
-#endif
-               }
-       } else {
-#ifdef HAVE_MOVING_COLLECTOR
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-                       MonoThreadsSync *oldlw = lw.sync;
-                       mono_monitor_allocator_lock ();
-                       mon = mon_new (id);
-                       /* move the already calculated hash */
-                       mon->hash_code = lw.lock_word >> LOCK_WORD_HASH_SHIFT;
-                       lw.sync = mon;
-                       lw.lock_word |= LOCK_WORD_FAT_HASH;
-                       if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, lw.sync, oldlw) == oldlw) {
-                               mono_gc_weak_link_add (&mon->data, obj, TRUE);
-                               mono_monitor_allocator_unlock ();
-                               /* Successfully locked */
-                               return 1;
-                       } else {
-                               mon_finalize (mon);
-                               mono_monitor_allocator_unlock ();
-                               goto retry;
-                       }
-               }
-#endif
-       }
-
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-
-       /* If the object has previously been locked but isn't now... */
-
        /* This case differs from Dice's case 3 because we don't
         * deflate locks or cache unused lock records
         */
-       if (G_LIKELY (mon->owner == 0)) {
+       old_status = mon->status;
+       if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
                /* Try to install our ID in the owner field, nest
-                * should have been left at 1 by the previous unlock
-                * operation
-                */
-               if (G_LIKELY (InterlockedCompareExchangePointer ((gpointer *)&mon->owner, (gpointer)id, 0) == 0)) {
+               * should have been left at 1 by the previous unlock
+               * operation
+               */
+               new_status = mon_status_set_owner (old_status, id);
+               tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
+               if (G_LIKELY (tmp_status == old_status)) {
                        /* Success */
                        g_assert (mon->nest == 1);
                        return 1;
@@ -525,7 +781,7 @@ retry:
        }
 
        /* If the object is currently locked by this thread... */
-       if (mon->owner == id) {
+       if (mon_status_get_owner (old_status) == id) {
                mon->nest++;
                return 1;
        }
@@ -553,12 +809,15 @@ retry_contended:
        /* This case differs from Dice's case 3 because we don't
         * deflate locks or cache unused lock records
         */
-       if (G_LIKELY (mon->owner == 0)) {
+       old_status = mon->status;
+       if (G_LIKELY (mon_status_get_owner (old_status) == 0)) {
                /* Try to install our ID in the owner field, nest
                * should have been left at 1 by the previous unlock
                * operation
                */
-               if (G_LIKELY (InterlockedCompareExchangePointer ((gpointer *)&mon->owner, (gpointer)id, 0) == 0)) {
+               new_status = mon_status_set_owner (old_status, id);
+               tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
+               if (G_LIKELY (tmp_status == old_status)) {
                        /* Success */
                        g_assert (mon->nest == 1);
                        mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
@@ -567,7 +826,7 @@ retry_contended:
        }
 
        /* If the object is currently locked by this thread... */
-       if (mon->owner == id) {
+       if (mon_status_get_owner (old_status) == id) {
                mon->nest++;
                mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
                return 1;
@@ -585,30 +844,30 @@ retry_contended:
                        CloseHandle (sem);
                }
        }
-       
-       /* If we need to time out, record a timestamp and adjust ms,
-        * because WaitForSingleObject doesn't tell us how long it
-        * waited for.
-        *
-        * Don't block forever here, because theres a chance the owner
-        * thread released the lock while we were creating the
-        * semaphore: we would not get the wakeup.  Using the event
-        * handle technique from pulse/wait would involve locking the
-        * lock struct and therefore slowing down the fast path.
+
+       /*
+        * We need to register ourselves as waiting if it is the first time we are waiting,
+        * of if we were signaled and failed to acquire the lock.
         */
+       if (!interrupted) {
+               old_status = mon->status;
+               for (;;) {
+                       if (mon_status_get_owner (old_status) == 0)
+                               goto retry_contended;
+                       new_status = mon_status_increment_entry_count (old_status);
+                       tmp_status = InterlockedCompareExchange ((gint32*)&mon->status, new_status, old_status);
+                       if (tmp_status == old_status) {
+                               break;
+                       }
+                       old_status = tmp_status;
+               }
+       }
+
        if (ms != INFINITE) {
                then = mono_msec_ticks ();
-               if (ms < 100) {
-                       waitms = ms;
-               } else {
-                       waitms = 100;
-               }
-       } else {
-               waitms = 100;
        }
+       waitms = ms;
        
-       InterlockedIncrement (&mon->entry_count);
-
 #ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->thread_queue_len++;
        mono_perfcounters->thread_queue_max++;
@@ -621,617 +880,193 @@ retry_contended:
         * We pass TRUE instead of allow_interruption since we have to check for the
         * StopRequested case below.
         */
+       MONO_PREPARE_BLOCKING;
        ret = WaitForSingleObjectEx (mon->entry_sem, waitms, TRUE);
+       MONO_FINISH_BLOCKING;
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
        
-       InterlockedDecrement (&mon->entry_count);
 #ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->thread_queue_len--;
 #endif
 
-       if (ms != INFINITE) {
-               now = mono_msec_ticks ();
-               
-               if (now < then) {
-                       /* The counter must have wrapped around */
-                       LOCK_DEBUG (g_message ("%s: wrapped around! now=0x%x then=0x%x", __func__, now, then));
-                       
-                       now += (0xffffffff - then);
-                       then = 0;
+       if (ret == WAIT_IO_COMPLETION && !allow_interruption) {
+               interrupted = TRUE;
+               /* 
+                * We have to obey a stop/suspend request even if 
+                * allow_interruption is FALSE to avoid hangs at shutdown.
+                */
+               if (!mono_thread_test_state (mono_thread_internal_current (), (ThreadState_StopRequested|ThreadState_SuspendRequested))) {
+                       if (ms != INFINITE) {
+                               now = mono_msec_ticks ();
+                               if (now < then) {
+                                       LOCK_DEBUG (g_message ("%s: wrapped around! now=0x%x then=0x%x", __func__, now, then));
 
-                       LOCK_DEBUG (g_message ("%s: wrap rejig: now=0x%x then=0x%x delta=0x%x", __func__, now, then, now-then));
-               }
-               
-               delta = now - then;
-               if (delta >= ms) {
-                       ms = 0;
-               } else {
-                       ms -= delta;
-               }
+                                       now += (0xffffffff - then);
+                                       then = 0;
 
-               if ((ret == WAIT_TIMEOUT || (ret == WAIT_IO_COMPLETION && !allow_interruption)) && ms > 0) {
-                       /* More time left */
-                       goto retry_contended;
-               }
-       } else {
-               if (ret == WAIT_TIMEOUT || (ret == WAIT_IO_COMPLETION && !allow_interruption)) {
-                       if (ret == WAIT_IO_COMPLETION && (mono_thread_test_state (mono_thread_internal_current (), (ThreadState_StopRequested|ThreadState_SuspendRequested)))) {
-                               /* 
-                                * We have to obey a stop/suspend request even if 
-                                * allow_interruption is FALSE to avoid hangs at shutdown.
-                                */
-                               mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_FAIL);
-                               return -1;
+                                       LOCK_DEBUG (g_message ("%s: wrap rejig: now=0x%x then=0x%x delta=0x%x", __func__, now, then, now-then));
+                               }
+
+                               delta = now - then;
+                               if (delta >= ms) {
+                                       ms = 0;
+                               } else {
+                                       ms -= delta;
+                               }
                        }
-                       /* Infinite wait, so just try again */
+                       /* retry from the top */
                        goto retry_contended;
                }
-       }
-       
-       if (ret == WAIT_OBJECT_0) {
+       } else if (ret == WAIT_OBJECT_0) {
+               interrupted = FALSE;
                /* retry from the top */
                goto retry_contended;
+       } else if (ret == WAIT_TIMEOUT) {
+               /* we're done */
        }
 
-       /* We must have timed out */
-       LOCK_DEBUG (g_message ("%s: (%d) timed out waiting, returning FALSE", __func__, id));
+       /* Timed out or interrupted */
+       mon_decrement_entry_count (mon);
 
        mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_FAIL);
 
-       if (ret == WAIT_IO_COMPLETION)
+       if (ret == WAIT_IO_COMPLETION) {
+               LOCK_DEBUG (g_message ("%s: (%d) interrupted waiting, returning -1", __func__, id));
                return -1;
-       else 
+       } else if (ret == WAIT_TIMEOUT) {
+               LOCK_DEBUG (g_message ("%s: (%d) timed out waiting, returning FALSE", __func__, id));
                return 0;
-}
-
-gboolean 
-mono_monitor_enter (MonoObject *obj)
-{
-       return mono_monitor_try_enter_internal (obj, INFINITE, FALSE) == 1;
-}
-
-gboolean 
-mono_monitor_try_enter (MonoObject *obj, guint32 ms)
-{
-       return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
-}
-
-void
-mono_monitor_exit (MonoObject *obj)
-{
-       MonoThreadsSync *mon;
-       guint32 nest;
-       
-       LOCK_DEBUG (g_message ("%s: (%d) Unlocking %p", __func__, GetCurrentThreadId (), obj));
-
-       if (G_UNLIKELY (!obj)) {
-               mono_raise_exception (mono_get_exception_argument_null ("obj"));
-               return;
-       }
-
-       mon = obj->synchronisation;
-
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH)
-                       return;
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (G_UNLIKELY (mon == NULL)) {
-               /* No one ever used Enter. Just ignore the Exit request as MS does */
-               return;
-       }
-       if (G_UNLIKELY (mon->owner != GetCurrentThreadId ())) {
-               return;
-       }
-       
-       nest = mon->nest - 1;
-       if (nest == 0) {
-               LOCK_DEBUG (g_message ("%s: (%d) Object %p is now unlocked", __func__, GetCurrentThreadId (), obj));
-       
-               /* object is now unlocked, leave nest==1 so we don't
-                * need to set it when the lock is reacquired
-                */
-               mon->owner = 0;
-
-               /* Do the wakeup stuff.  It's possible that the last
-                * blocking thread gave up waiting just before we
-                * release the semaphore resulting in a futile wakeup
-                * next time there's contention for this object, but
-                * it means we don't have to waste time locking the
-                * struct.
-                */
-               if (mon->entry_count > 0) {
-                       ReleaseSemaphore (mon->entry_sem, 1, NULL);
-               }
        } else {
-               LOCK_DEBUG (g_message ("%s: (%d) Object %p is now locked %d times", __func__, GetCurrentThreadId (), obj, nest));
-               mon->nest = nest;
+               g_assert_not_reached ();
+               return 0;
        }
 }
 
-void**
-mono_monitor_get_object_monitor_weak_link (MonoObject *object)
+/*
+ * If allow_interruption == TRUE, the method will be interrupted if abort or suspend
+ * is requested. In this case it returns -1.
+ */
+static inline gint32
+mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
 {
        LockWord lw;
-       MonoThreadsSync *sync = NULL;
-
-       lw.sync = object->synchronisation;
-       if (lw.lock_word & LOCK_WORD_FAT_HASH) {
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               sync = lw.sync;
-       } else if (!(lw.lock_word & LOCK_WORD_THIN_HASH)) {
-               sync = lw.sync;
-       }
+       int id = mono_thread_info_get_small_id ();
 
-       if (sync && sync->data)
-               return &sync->data;
-       return NULL;
-}
-
-#ifndef DISABLE_JIT
-
-static void
-emit_obj_syncp_check (MonoMethodBuilder *mb, int syncp_loc, int *obj_null_branch, int *true_locktaken_branch, int *syncp_true_false_branch,
-       int *thin_hash_branch, gboolean branch_on_true)
-{
-       /*
-         ldarg         0                                                       obj
-         brfalse.s     obj_null
-       */
-
-       mono_mb_emit_byte (mb, CEE_LDARG_0);
-       *obj_null_branch = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
+       LOCK_DEBUG (g_message("%s: (%d) Trying to lock object %p (%d ms)", __func__, id, obj, ms));
 
-       /*
-         ldarg.1
-         ldind.i1
-         brtrue.s true_locktaken
-       */
-       if (true_locktaken_branch) {
-               mono_mb_emit_byte (mb, CEE_LDARG_1);
-               mono_mb_emit_byte (mb, CEE_LDIND_I1);
-               *true_locktaken_branch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
+       if (G_UNLIKELY (!obj)) {
+               mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
+               return FALSE;
        }
 
-       /*
-         ldarg         0                                                       obj
-         conv.i                                                                objp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoObject, synchronisation)         objp off
-         add                                                                   &syncp
-         ldind.i                                                               syncp
-         stloc         syncp
-         ldloc         syncp                                                   syncp
-         brtrue/false.s        syncp_true_false
-       */
-
-       mono_mb_emit_byte (mb, CEE_LDARG_0);
-       mono_mb_emit_byte (mb, CEE_CONV_I);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoObject, synchronisation));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I);
-       mono_mb_emit_stloc (mb, syncp_loc);
+       lw.sync = obj->synchronisation;
 
-       
-       if (mono_gc_is_moving ()) {
-               /*check for a thin hash*/
-               mono_mb_emit_ldloc (mb, syncp_loc);
-               mono_mb_emit_icon (mb, 0x01);
-               mono_mb_emit_byte (mb, CEE_CONV_I);
-               mono_mb_emit_byte (mb, CEE_AND);
-               *thin_hash_branch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-               /*clear gc bits*/
-               mono_mb_emit_ldloc (mb, syncp_loc);
-               mono_mb_emit_icon (mb, ~0x3);
-               mono_mb_emit_byte (mb, CEE_CONV_I);
-               mono_mb_emit_byte (mb, CEE_AND);
-               mono_mb_emit_stloc (mb, syncp_loc);
-       } else {
-               *thin_hash_branch = 0;
+       if (G_LIKELY (lock_word_is_free (lw))) {
+               LockWord nlw = lock_word_new_flat (id);
+               if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, NULL) == NULL) {
+                       return 1;
+               } else {
+                       /* Someone acquired it in the meantime or put a hash */
+                       mono_monitor_inflate (obj);
+                       return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
+               }
+       } else if (lock_word_is_inflated (lw)) {
+               return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
+       } else if (lock_word_is_flat (lw)) {
+               if (lock_word_get_owner (lw) == id) {
+                       if (lock_word_is_max_nest (lw)) {
+                               mono_monitor_inflate_owned (obj, id);
+                               return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
+                       } else {
+                               LockWord nlw, old_lw;
+                               nlw = lock_word_increment_nest (lw);
+                               old_lw.sync = InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, nlw.sync, lw.sync);
+                               if (old_lw.sync != lw.sync) {
+                                       /* Someone else inflated it in the meantime */
+                                       g_assert (lock_word_is_inflated (old_lw));
+                                       return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
+                               }
+                               return 1;
+                       }
+               } else {
+                       mono_monitor_inflate (obj);
+                       return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
+               }
+       } else if (lock_word_has_hash (lw)) {
+               mono_monitor_inflate (obj);
+               return mono_monitor_try_enter_inflated (obj, ms, allow_interruption, id);
        }
 
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       *syncp_true_false_branch = mono_mb_emit_short_branch (mb, branch_on_true ? CEE_BRTRUE_S : CEE_BRFALSE_S);
+       g_assert_not_reached ();
+       return -1;
 }
 
-#endif
-
-static MonoMethod* monitor_il_fastpaths[3];
-
-gboolean
-mono_monitor_is_il_fastpath_wrapper (MonoMethod *method)
+gboolean 
+mono_monitor_enter (MonoObject *obj)
 {
-       int i;
-       for (i = 0; i < 3; ++i) {
-               if (monitor_il_fastpaths [i] == method)
-                       return TRUE;
-       }
-       return FALSE;
+       return mono_monitor_try_enter_internal (obj, INFINITE, FALSE) == 1;
 }
 
-enum {
-       FASTPATH_ENTER,
-       FASTPATH_ENTERV4,
-       FASTPATH_EXIT
-};
-
-
-static MonoMethod*
-register_fastpath (MonoMethod *method, int idx)
+gboolean 
+mono_monitor_try_enter (MonoObject *obj, guint32 ms)
 {
-       mono_memory_barrier ();
-       monitor_il_fastpaths [idx] = method;
-       return method;
+       return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
 }
 
-static MonoMethod*
-mono_monitor_get_fast_enter_method (MonoMethod *monitor_enter_method)
+void
+mono_monitor_exit (MonoObject *obj)
 {
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       static MonoMethod *compare_exchange_method;
-       int obj_null_branch, true_locktaken_branch = 0, syncp_null_branch, has_owner_branch, other_owner_branch, tid_branch, thin_hash_branch;
-       int tid_loc, syncp_loc, owner_loc;
-       gboolean is_v4 = mono_method_signature (monitor_enter_method)->param_count == 2;
-       int fast_path_idx = is_v4 ? FASTPATH_ENTERV4 : FASTPATH_ENTER;
-       WrapperInfo *info;
-
-       /* The !is_v4 version is not used/tested */
-       g_assert (is_v4);
-
-       if (monitor_il_fastpaths [fast_path_idx])
-               return monitor_il_fastpaths [fast_path_idx];
-
-       if (!mono_get_runtime_callbacks ()->tls_key_supported (TLS_KEY_THREAD))
-               return NULL;
-
-       if (!compare_exchange_method) {
-               MonoMethodDesc *desc;
-               MonoClass *class;
-
-               desc = mono_method_desc_new ("Interlocked:CompareExchange(intptr&,intptr,intptr)", FALSE);
-               class = mono_class_from_name (mono_defaults.corlib, "System.Threading", "Interlocked");
-               compare_exchange_method = mono_method_desc_search_in_class (desc, class);
-               mono_method_desc_free (desc);
-
-               if (!compare_exchange_method)
-                       return NULL;
-       }
-
-       mb = mono_mb_new (mono_defaults.monitor_class, is_v4 ? "FastMonitorEnterV4" : "FastMonitorEnter", MONO_WRAPPER_UNKNOWN);
-
-       mb->method->slot = -1;
-       mb->method->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC |
-               METHOD_ATTRIBUTE_HIDE_BY_SIG | METHOD_ATTRIBUTE_FINAL;
-
-#ifndef DISABLE_JIT
-       tid_loc = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-       syncp_loc = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-       owner_loc = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
-       emit_obj_syncp_check (mb, syncp_loc, &obj_null_branch, is_v4 ? &true_locktaken_branch : NULL, &syncp_null_branch, &thin_hash_branch, FALSE);
-
-       /*
-         mono. tls     thread_tls_offset                                       threadp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThread, tid)                     threadp off
-         add                                                                   &tid
-         ldind.i                                                               tid
-         stloc         tid
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, owner)                      syncp off
-         add                                                                   &owner
-         ldind.i                                                               owner
-         stloc         owner
-         ldloc         owner                                                   owner
-         brtrue.s      tid
-       */
-
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_TLS);
-       mono_mb_emit_i4 (mb, TLS_KEY_THREAD);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoInternalThread, tid));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I);
-       mono_mb_emit_stloc (mb, tid_loc);
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, owner));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I);
-       mono_mb_emit_stloc (mb, owner_loc);
-       mono_mb_emit_ldloc (mb, owner_loc);
-       tid_branch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-       /*
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, owner)                      syncp off
-         add                                                                   &owner
-         ldloc         tid                                                     &owner tid
-         ldc.i4        0                                                       &owner tid 0
-         call          System.Threading.Interlocked.CompareExchange            oldowner
-         brtrue.s      has_owner
-         ret
-       */
-
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, owner));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_ldloc (mb, tid_loc);
-       mono_mb_emit_byte (mb, CEE_LDC_I4_0);
-       mono_mb_emit_managed_call (mb, compare_exchange_method, NULL);
-       has_owner_branch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-       if (is_v4) {
-               mono_mb_emit_byte (mb, CEE_LDARG_1);
-               mono_mb_emit_byte (mb, CEE_LDC_I4_1);
-               mono_mb_emit_byte (mb, CEE_STIND_I1);
-       }
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        tid:
-         ldloc         owner                                                   owner
-         ldloc         tid                                                     owner tid
-         brne.s        other_owner
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, nest)                       syncp off
-         add                                                                   &nest
-         dup                                                                   &nest &nest
-         ldind.i4                                                              &nest nest
-         ldc.i4        1                                                       &nest nest 1
-         add                                                                   &nest nest+
-         stind.i4
-         ret
-       */
+       LockWord lw;
+       
+       LOCK_DEBUG (g_message ("%s: (%d) Unlocking %p", __func__, mono_thread_info_get_small_id (), obj));
 
-       mono_mb_patch_short_branch (mb, tid_branch);
-       mono_mb_emit_ldloc (mb, owner_loc);
-       mono_mb_emit_ldloc (mb, tid_loc);
-       other_owner_branch = mono_mb_emit_short_branch (mb, CEE_BNE_UN_S);
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, nest));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_DUP);
-       mono_mb_emit_byte (mb, CEE_LDIND_I4);
-       mono_mb_emit_byte (mb, CEE_LDC_I4_1);
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_STIND_I4);
-
-       if (is_v4) {
-               mono_mb_emit_byte (mb, CEE_LDARG_1);
-               mono_mb_emit_byte (mb, CEE_LDC_I4_1);
-               mono_mb_emit_byte (mb, CEE_STIND_I1);
+       if (G_UNLIKELY (!obj)) {
+               mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
+               return;
        }
 
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        obj_null, syncp_null, has_owner, other_owner:
-         ldarg         0                                                       obj
-         call          System.Threading.Monitor.Enter
-         ret
-       */
-
-       if (thin_hash_branch)
-               mono_mb_patch_short_branch (mb, thin_hash_branch);
-       mono_mb_patch_short_branch (mb, obj_null_branch);
-       mono_mb_patch_short_branch (mb, syncp_null_branch);
-       mono_mb_patch_short_branch (mb, has_owner_branch);
-       mono_mb_patch_short_branch (mb, other_owner_branch);
-       if (true_locktaken_branch)
-               mono_mb_patch_short_branch (mb, true_locktaken_branch);
-       mono_mb_emit_byte (mb, CEE_LDARG_0);
-       if (is_v4)
-               mono_mb_emit_byte (mb, CEE_LDARG_1);
-       mono_mb_emit_managed_call (mb, monitor_enter_method, NULL);
-       mono_mb_emit_byte (mb, CEE_RET);
-#endif
-
-       res = register_fastpath (mono_mb_create_method (mb, mono_signature_no_pinvoke (monitor_enter_method), 5), fast_path_idx);
+       lw.sync = obj->synchronisation;
 
-       info = mono_image_alloc0 (mono_defaults.corlib, sizeof (WrapperInfo));
-       info->subtype = is_v4 ? WRAPPER_SUBTYPE_FAST_MONITOR_ENTER_V4 : WRAPPER_SUBTYPE_FAST_MONITOR_ENTER;
-       mono_marshal_set_wrapper_info (res, info);
+       mono_monitor_ensure_owned (lw, mono_thread_info_get_small_id ());
 
-       mono_mb_free (mb);
-       return res;
+       if (G_UNLIKELY (lock_word_is_inflated (lw)))
+               mono_monitor_exit_inflated (obj);
+       else
+               mono_monitor_exit_flat (obj, lw);
 }
 
-static MonoMethod*
-mono_monitor_get_fast_exit_method (MonoMethod *monitor_exit_method)
+guint32
+mono_monitor_get_object_monitor_gchandle (MonoObject *object)
 {
-       MonoMethodBuilder *mb;
-       MonoMethod *res;
-       int obj_null_branch, has_waiting_branch, has_syncp_branch, owned_branch, nested_branch, thin_hash_branch;
-       int syncp_loc;
-       WrapperInfo *info;
-
-       if (monitor_il_fastpaths [FASTPATH_EXIT])
-               return monitor_il_fastpaths [FASTPATH_EXIT];
-
-       if (!mono_get_runtime_callbacks ()->tls_key_supported (TLS_KEY_THREAD))
-               return NULL;
-
-       mb = mono_mb_new (mono_defaults.monitor_class, "FastMonitorExit", MONO_WRAPPER_UNKNOWN);
-
-       mb->method->slot = -1;
-       mb->method->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC |
-               METHOD_ATTRIBUTE_HIDE_BY_SIG | METHOD_ATTRIBUTE_FINAL;
-
-#ifndef DISABLE_JIT
-       syncp_loc = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
-       emit_obj_syncp_check (mb, syncp_loc, &obj_null_branch, NULL, &has_syncp_branch, &thin_hash_branch, TRUE);
-
-       /*
-         ret
-       */
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        has_syncp:
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, owner)                      syncp off
-         add                                                                   &owner
-         ldind.i                                                               owner
-         mono. tls     thread_tls_offset                                       owner threadp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThread, tid)                     owner threadp off
-         add                                                                   owner &tid
-         ldind.i                                                               owner tid
-         beq.s         owned
-       */
-
-       mono_mb_patch_short_branch (mb, has_syncp_branch);
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, owner));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I);
-       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-       mono_mb_emit_byte (mb, CEE_MONO_TLS);
-       mono_mb_emit_i4 (mb, TLS_KEY_THREAD);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoInternalThread, tid));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I);
-       owned_branch = mono_mb_emit_short_branch (mb, CEE_BEQ_S);
-
-       /*
-         ret
-       */
-
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        owned:
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, nest)                       syncp off
-         add                                                                   &nest
-         dup                                                                   &nest &nest
-         ldind.i4                                                              &nest nest
-         dup                                                                   &nest nest nest
-         ldc.i4        1                                                       &nest nest nest 1
-         bgt.un.s      nested                                                  &nest nest
-       */
-
-       mono_mb_patch_short_branch (mb, owned_branch);
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, nest));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_DUP);
-       mono_mb_emit_byte (mb, CEE_LDIND_I4);
-       mono_mb_emit_byte (mb, CEE_DUP);
-       mono_mb_emit_byte (mb, CEE_LDC_I4_1);
-       nested_branch = mono_mb_emit_short_branch (mb, CEE_BGT_UN_S);
-
-       /*
-         pop                                                                   &nest
-         pop
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, entry_count)                syncp off
-         add                                                                   &count
-         ldind.i4                                                              count
-         brtrue.s      has_waiting
-       */
-
-       mono_mb_emit_byte (mb, CEE_POP);
-       mono_mb_emit_byte (mb, CEE_POP);
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, entry_count));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDIND_I4);
-       has_waiting_branch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-       /*
-         ldloc         syncp                                                   syncp
-         ldc.i4        MONO_STRUCT_OFFSET(MonoThreadsSync, owner)                      syncp off
-         add                                                                   &owner
-         ldnull                                                                &owner 0
-         stind.i
-         ret
-       */
-
-       mono_mb_emit_ldloc (mb, syncp_loc);
-       mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoThreadsSync, owner));
-       mono_mb_emit_byte (mb, CEE_ADD);
-       mono_mb_emit_byte (mb, CEE_LDNULL);
-       mono_mb_emit_byte (mb, CEE_STIND_I);
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        nested:
-         ldc.i4        1                                                       &nest nest 1
-         sub                                                                   &nest nest-
-         stind.i4
-         ret
-       */
-
-       mono_mb_patch_short_branch (mb, nested_branch);
-       mono_mb_emit_byte (mb, CEE_LDC_I4_1);
-       mono_mb_emit_byte (mb, CEE_SUB);
-       mono_mb_emit_byte (mb, CEE_STIND_I4);
-       mono_mb_emit_byte (mb, CEE_RET);
-
-       /*
-        obj_null, has_waiting:
-         ldarg         0                                                       obj
-         call          System.Threading.Monitor.Exit
-         ret
-        */
-
-       if (thin_hash_branch)
-               mono_mb_patch_short_branch (mb, thin_hash_branch);
-       mono_mb_patch_short_branch (mb, obj_null_branch);
-       mono_mb_patch_short_branch (mb, has_waiting_branch);
-       mono_mb_emit_byte (mb, CEE_LDARG_0);
-       mono_mb_emit_managed_call (mb, monitor_exit_method, NULL);
-       mono_mb_emit_byte (mb, CEE_RET);
-#endif
-
-       res = register_fastpath (mono_mb_create_method (mb, mono_signature_no_pinvoke (monitor_exit_method), 5), FASTPATH_EXIT);
-       mono_mb_free (mb);
-
-       info = mono_image_alloc0 (mono_defaults.corlib, sizeof (WrapperInfo));
-       info->subtype = WRAPPER_SUBTYPE_FAST_MONITOR_EXIT;
-       mono_marshal_set_wrapper_info (res, info);
+       LockWord lw;
 
-       return res;
-}
+       lw.sync = object->synchronisation;
 
-MonoMethod*
-mono_monitor_get_fast_path (MonoMethod *enter_or_exit)
-{
-       if (strcmp (enter_or_exit->name, "Enter") == 0)
-               return mono_monitor_get_fast_enter_method (enter_or_exit);
-       if (strcmp (enter_or_exit->name, "Exit") == 0)
-               return mono_monitor_get_fast_exit_method (enter_or_exit);
-       g_assert_not_reached ();
-       return NULL;
+       if (lock_word_is_inflated (lw)) {
+               MonoThreadsSync *mon = lock_word_get_inflated_lock (lw);
+               return (guint32)mon->data;
+       }
+       return 0;
 }
 
 /*
  * mono_monitor_threads_sync_member_offset:
- * @owner_offset: returns size and offset of the "owner" member
+ * @status_offset: returns size and offset of the "status" member
  * @nest_offset: returns size and offset of the "nest" member
- * @entry_count_offset: returns size and offset of the "entry_count" member
  *
- * Returns the offsets and sizes of three members of the
+ * Returns the offsets and sizes of two members of the
  * MonoThreadsSync struct.  The Monitor ASM fastpaths need this.
  */
 void
-mono_monitor_threads_sync_members_offset (int *owner_offset, int *nest_offset, int *entry_count_offset)
+mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset)
 {
        MonoThreadsSync ts;
 
 #define ENCODE_OFF_SIZE(o,s)   (((o) << 8) | ((s) & 0xff))
 
-       *owner_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, owner), sizeof (ts.owner));
+       *status_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, status), sizeof (ts.status));
        *nest_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, nest), sizeof (ts.nest));
-       *entry_count_offset = ENCODE_OFF_SIZE (MONO_STRUCT_OFFSET (MonoThreadsSync, entry_count), sizeof (ts.entry_count));
 }
 
 gboolean 
@@ -1262,30 +1097,31 @@ ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject
        *lockTaken = res == 1;
 }
 
+void
+mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
+{
+
+       if (*lock_taken == 1) {
+               mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
+               return;
+       }
+
+       ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, INFINITE, lock_taken);
+}
+
 gboolean 
 ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
 {
-       MonoThreadsSync *mon;
-       
-       LOCK_DEBUG (g_message ("%s: Testing if %p is owned by thread %d", __func__, obj, GetCurrentThreadId()));
+       LockWord lw;
 
-       mon = obj->synchronisation;
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH)
-                       return FALSE;
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (mon == NULL) {
-               return FALSE;
-       }
-       
-       if(mon->owner==GetCurrentThreadId ()) {
-               return(TRUE);
+       LOCK_DEBUG (g_message ("%s: Testing if %p is owned by thread %d", __func__, obj, mono_thread_info_get_small_id()));
+
+       lw.sync = obj->synchronisation;
+
+       if (lock_word_is_flat (lw)) {
+               return lock_word_get_owner (lw) == mono_thread_info_get_small_id ();
+       } else if (lock_word_is_inflated (lw)) {
+               return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == mono_thread_info_get_small_id ();
        }
        
        return(FALSE);
@@ -1294,29 +1130,18 @@ ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
 gboolean 
 ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
 {
-       MonoThreadsSync *mon;
+       LockWord lw;
 
-       LOCK_DEBUG (g_message("%s: (%d) Testing if %p is owned by any thread", __func__, GetCurrentThreadId (), obj));
-       
-       mon = obj->synchronisation;
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH)
-                       return FALSE;
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (mon == NULL) {
-               return FALSE;
-       }
-       
-       if (mon->owner != 0) {
-               return TRUE;
+       LOCK_DEBUG (g_message("%s: (%d) Testing if %p is owned by any thread", __func__, mono_thread_info_get_small_id (), obj));
+
+       lw.sync = obj->synchronisation;
+
+       if (lock_word_is_flat (lw)) {
+               return !lock_word_is_free (lw);
+       } else if (lock_word_is_inflated (lw)) {
+               return mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) != 0;
        }
-       
+
        return FALSE;
 }
 
@@ -1328,36 +1153,28 @@ ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
 void
 ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
 {
+       int id;
+       LockWord lw;
        MonoThreadsSync *mon;
+
+       LOCK_DEBUG (g_message ("%s: (%d) Pulsing %p", __func__, mono_thread_info_get_small_id (), obj));
        
-       LOCK_DEBUG (g_message ("%s: (%d) Pulsing %p", __func__, GetCurrentThreadId (), obj));
-       
-       mon = obj->synchronisation;
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-                       mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-                       return;
-               }
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (mon == NULL) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-               return;
-       }
-       if (mon->owner != GetCurrentThreadId ()) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
+       id = mono_thread_info_get_small_id ();
+       lw.sync = obj->synchronisation;
+
+       mono_monitor_ensure_owned (lw, id);
+
+       if (!lock_word_is_inflated (lw)) {
+               /* No threads waiting. A wait would have inflated the lock */
                return;
        }
 
-       LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, GetCurrentThreadId (), g_slist_length (mon->wait_list)));
-       
+       mon = lock_word_get_inflated_lock (lw);
+
+       LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
+
        if (mon->wait_list != NULL) {
-               LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, GetCurrentThreadId (), mon->wait_list->data));
+               LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
        
                SetEvent (mon->wait_list->data);
                mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
@@ -1367,36 +1184,28 @@ ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
 void
 ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
 {
+       int id;
+       LockWord lw;
        MonoThreadsSync *mon;
        
-       LOCK_DEBUG (g_message("%s: (%d) Pulsing all %p", __func__, GetCurrentThreadId (), obj));
+       LOCK_DEBUG (g_message("%s: (%d) Pulsing all %p", __func__, mono_thread_info_get_small_id (), obj));
 
-       mon = obj->synchronisation;
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-                       mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-                       return;
-               }
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (mon == NULL) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-               return;
-       }
-       if (mon->owner != GetCurrentThreadId ()) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
+       id = mono_thread_info_get_small_id ();
+       lw.sync = obj->synchronisation;
+
+       mono_monitor_ensure_owned (lw, id);
+
+       if (!lock_word_is_inflated (lw)) {
+               /* No threads waiting. A wait would have inflated the lock */
                return;
        }
 
-       LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, GetCurrentThreadId (), g_slist_length (mon->wait_list)));
+       mon = lock_word_get_inflated_lock (lw);
+
+       LOCK_DEBUG (g_message ("%s: (%d) %d threads waiting", __func__, mono_thread_info_get_small_id (), g_slist_length (mon->wait_list)));
 
        while (mon->wait_list != NULL) {
-               LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, GetCurrentThreadId (), mon->wait_list->data));
+               LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
        
                SetEvent (mon->wait_list->data);
                mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
@@ -1406,6 +1215,7 @@ ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
 gboolean
 ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 {
+       LockWord lw;
        MonoThreadsSync *mon;
        HANDLE event;
        guint32 nest;
@@ -1413,41 +1223,31 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
        gboolean success = FALSE;
        gint32 regain;
        MonoInternalThread *thread = mono_thread_internal_current ();
+       int id = mono_thread_info_get_small_id ();
 
-       LOCK_DEBUG (g_message ("%s: (%d) Trying to wait for %p with timeout %dms", __func__, GetCurrentThreadId (), obj, ms));
-       
-       mon = obj->synchronisation;
-#ifdef HAVE_MOVING_COLLECTOR
-       {
-               LockWord lw;
-               lw.sync = mon;
-               if (lw.lock_word & LOCK_WORD_THIN_HASH) {
-                       mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-                       return FALSE;
-               }
-               lw.lock_word &= ~LOCK_WORD_BITS_MASK;
-               mon = lw.sync;
-       }
-#endif
-       if (mon == NULL) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
-               return FALSE;
-       }
-       if (mon->owner != GetCurrentThreadId ()) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
-               return FALSE;
+       LOCK_DEBUG (g_message ("%s: (%d) Trying to wait for %p with timeout %dms", __func__, mono_thread_info_get_small_id (), obj, ms));
+
+       lw.sync = obj->synchronisation;
+
+       mono_monitor_ensure_owned (lw, id);
+
+       if (!lock_word_is_inflated (lw)) {
+               mono_monitor_inflate_owned (obj, id);
+               lw.sync = obj->synchronisation;
        }
 
+       mon = lock_word_get_inflated_lock (lw);
+
        /* Do this WaitSleepJoin check before creating the event handle */
        mono_thread_current_check_pending_interrupt ();
        
        event = CreateEvent (NULL, FALSE, FALSE, NULL);
        if (event == NULL) {
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
+               mono_set_pending_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
                return FALSE;
        }
        
-       LOCK_DEBUG (g_message ("%s: (%d) queuing handle %p", __func__, GetCurrentThreadId (), event));
+       LOCK_DEBUG (g_message ("%s: (%d) queuing handle %p", __func__, mono_thread_info_get_small_id (), event));
 
        mono_thread_current_check_pending_interrupt ();
        
@@ -1458,16 +1258,19 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
        /* Save the nest count, and release the lock */
        nest = mon->nest;
        mon->nest = 1;
-       mono_monitor_exit (obj);
+       mono_memory_write_barrier ();
+       mono_monitor_exit_inflated (obj);
 
-       LOCK_DEBUG (g_message ("%s: (%d) Unlocked %p lock %p", __func__, GetCurrentThreadId (), obj, mon));
+       LOCK_DEBUG (g_message ("%s: (%d) Unlocked %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
 
        /* There's no race between unlocking mon and waiting for the
         * event, because auto reset events are sticky, and this event
         * is private to this thread.  Therefore even if the event was
         * signalled before we wait, we still succeed.
         */
+       MONO_PREPARE_BLOCKING;
        ret = WaitForSingleObjectEx (event, ms, TRUE);
+       MONO_FINISH_BLOCKING;
 
        /* Reset the thread state fairly early, so we don't have to worry
         * about the monitor error checking
@@ -1488,7 +1291,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 
        /* Regain the lock with the previous nest count */
        do {
-               regain = mono_monitor_try_enter_internal (obj, INFINITE, TRUE);
+               regain = mono_monitor_try_enter_inflated (obj, INFINITE, TRUE, id);
                if (regain == -1) 
                        mono_thread_interruption_checkpoint ();
        } while (regain == -1);
@@ -1498,19 +1301,21 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
                 * SynchronizationLockException
                 */
                CloseHandle (event);
-               mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to regain lock"));
+               mono_set_pending_exception (mono_get_exception_synchronization_lock ("Failed to regain lock"));
                return FALSE;
        }
 
        mon->nest = nest;
 
-       LOCK_DEBUG (g_message ("%s: (%d) Regained %p lock %p", __func__, GetCurrentThreadId (), obj, mon));
+       LOCK_DEBUG (g_message ("%s: (%d) Regained %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
 
        if (ret == WAIT_TIMEOUT) {
                /* Poll the event again, just in case it was signalled
                 * while we were trying to regain the monitor lock
                 */
+               MONO_PREPARE_BLOCKING;
                ret = WaitForSingleObjectEx (event, 0, FALSE);
+               MONO_FINISH_BLOCKING;
        }
 
        /* Pulse will have popped our event from the queue if it signalled
@@ -1524,10 +1329,10 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
         */
        
        if (ret == WAIT_OBJECT_0) {
-               LOCK_DEBUG (g_message ("%s: (%d) Success", __func__, GetCurrentThreadId ()));
+               LOCK_DEBUG (g_message ("%s: (%d) Success", __func__, mono_thread_info_get_small_id ()));
                success = TRUE;
        } else {
-               LOCK_DEBUG (g_message ("%s: (%d) Wait failed, dequeuing handle %p", __func__, GetCurrentThreadId (), event));
+               LOCK_DEBUG (g_message ("%s: (%d) Wait failed, dequeuing handle %p", __func__, mono_thread_info_get_small_id (), event));
                /* No pulse, so we have to remove ourself from the
                 * wait queue
                 */