2009-01-03 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / threads.c
index 83d879040b49d99d020277420c5c365489016bb0..17f56971ac329912fdc2db775c08bd5d491409d4 100644 (file)
@@ -10,9 +10,6 @@
  */
 
 #include <config.h>
-#ifdef PLATFORM_WIN32
-#define _WIN32_WINNT 0x0500
-#endif
 
 #include <glib.h>
 #include <signal.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/utils/mono-compiler.h>
+#include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-membar.h>
+#include <mono/utils/mono-time.h>
 
-#include <mono/os/gc_wrapper.h>
+#include <mono/metadata/gc-internal.h>
 
 /*#define THREAD_DEBUG(a) do { a; } while (0)*/
 #define THREAD_DEBUG(a)
@@ -71,11 +71,24 @@ typedef union {
        gdouble fval;
 } LongDoubleUnion;
  
+typedef struct _MonoThreadDomainTls MonoThreadDomainTls;
+struct _MonoThreadDomainTls {
+       MonoThreadDomainTls *next;
+       guint32 offset;
+       guint32 size;
+};
+
 typedef struct {
        int idx;
        int offset;
+       MonoThreadDomainTls *freelist;
 } StaticDataInfo;
 
+typedef struct {
+       gpointer p;
+       MonoHazardousFreeFunc free_func;
+} DelayedFreeItem;
+
 /* Number of cached culture objects in the MonoThread->cached_culture_info array
  * (per-type): we use the first NUM entries for CultureInfo and the last for
  * UICultureInfo. So the size of the array is really NUM_CACHED_CULTURES * 2.
@@ -103,6 +116,13 @@ static StaticDataInfo context_static_info;
  */
 static MonoGHashTable *threads=NULL;
 
+/*
+ * Threads which are starting up and they are not in the 'threads' hash yet.
+ * When handle_store is called for a thread, it will be removed from this hash table.
+ * Protected by mono_threads_lock ().
+ */
+static MonoGHashTable *threads_starting_up = NULL;
+
 /* The TLS key that holds the MonoObject assigned to each thread */
 static guint32 current_object_key = -1;
 
@@ -130,6 +150,9 @@ static MonoThreadAttachCB mono_thread_attach_cb = NULL;
 /* function called at thread cleanup */
 static MonoThreadCleanupFunc mono_thread_cleanup_fn = NULL;
 
+/* function called to notify the runtime about a pending exception on the current thread */
+static MonoThreadNotifyPendingExcFunc mono_thread_notify_pending_exc_fn = NULL;
+
 /* The default stack size for each thread */
 static guint32 default_stacksize = 0;
 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
@@ -152,6 +175,25 @@ static gint32 thread_interruption_requested = 0;
 /* Event signaled when a thread changes its background mode */
 static HANDLE background_change_event;
 
+/* The table for small ID assignment */
+static CRITICAL_SECTION small_id_mutex;
+static int small_id_table_size = 0;
+static int small_id_next = 0;
+static int highest_small_id = -1;
+static MonoThread **small_id_table = NULL;
+
+/* The hazard table */
+#define HAZARD_TABLE_MAX_SIZE  16384 /* There cannot be more threads than this number. */
+static volatile int hazard_table_size = 0;
+static MonoThreadHazardPointers * volatile hazard_table = NULL;
+
+/* The table where we keep pointers to blocks to be freed but that
+   have to wait because they're guarded by a hazard pointer. */
+static CRITICAL_SECTION delayed_free_table_mutex;
+static GArray *delayed_free_table = NULL;
+
+static gboolean shutting_down = FALSE;
+
 guint32
 mono_thread_get_tls_key (void)
 {
@@ -168,13 +210,24 @@ mono_thread_get_tls_offset (void)
 
 /* handle_store() and handle_remove() manage the array of threads that
  * still need to be waited for when the main thread exits.
+ *
+ * If handle_store() returns FALSE the thread must not be started
+ * because Mono is shutting down.
  */
-static void handle_store(MonoThread *thread)
+static gboolean handle_store(MonoThread *thread)
 {
        mono_threads_lock ();
 
        THREAD_DEBUG (g_message ("%s: thread %p ID %"G_GSIZE_FORMAT, __func__, thread, (gsize)thread->tid));
 
+       if (threads_starting_up)
+               mono_g_hash_table_remove (threads_starting_up, thread);
+
+       if (shutting_down) {
+               mono_threads_unlock ();
+               return FALSE;
+       }
+
        if(threads==NULL) {
                MONO_GC_REGISTER_ROOT (threads);
                threads=mono_g_hash_table_new(NULL, NULL);
@@ -187,17 +240,35 @@ static void handle_store(MonoThread *thread)
                                 thread);
 
        mono_threads_unlock ();
+
+       return TRUE;
 }
 
-static void handle_remove(gsize tid)
+static gboolean handle_remove(MonoThread *thread)
 {
+       gboolean ret;
+       gsize tid = thread->tid;
+
        THREAD_DEBUG (g_message ("%s: thread ID %"G_GSIZE_FORMAT, __func__, tid));
 
        mono_threads_lock ();
-       
 
-       if (threads)
-               mono_g_hash_table_remove (threads, (gpointer)tid);
+       if (threads) {
+               /* We have to check whether the thread object for the
+                * tid is still the same in the table because the
+                * thread might have been destroyed and the tid reused
+                * in the meantime, in which case the tid would be in
+                * the table, but with another thread object.
+                */
+               if (mono_g_hash_table_lookup (threads, (gpointer)tid) == thread) {
+                       mono_g_hash_table_remove (threads, (gpointer)tid);
+                       ret = TRUE;
+               } else {
+                       ret = FALSE;
+               }
+       }
+       else
+               ret = FALSE;
        
        mono_threads_unlock ();
 
@@ -214,32 +285,260 @@ static void handle_remove(gsize tid)
         * thread calling Join() still has a reference to the first
         * thread's object.
         */
+       return ret;
+}
+
+/*
+ * Allocate a small thread id.
+ *
+ * FIXME: The biggest part of this function is very similar to
+ * domain_id_alloc() in domain.c and should be merged.
+ */
+static int
+small_id_alloc (MonoThread *thread)
+{
+       int id = -1, i;
+
+       EnterCriticalSection (&small_id_mutex);
+
+       if (!small_id_table) {
+               small_id_table_size = 2;
+               small_id_table = mono_gc_alloc_fixed (small_id_table_size * sizeof (MonoThread*), NULL);
+       }
+       for (i = small_id_next; i < small_id_table_size; ++i) {
+               if (!small_id_table [i]) {
+                       id = i;
+                       break;
+               }
+       }
+       if (id == -1) {
+               for (i = 0; i < small_id_next; ++i) {
+                       if (!small_id_table [i]) {
+                               id = i;
+                               break;
+                       }
+               }
+       }
+       if (id == -1) {
+               MonoThread **new_table;
+               int new_size = small_id_table_size * 2;
+               if (new_size >= (1 << 16))
+                       g_assert_not_reached ();
+               id = small_id_table_size;
+               new_table = mono_gc_alloc_fixed (new_size * sizeof (MonoThread*), NULL);
+               memcpy (new_table, small_id_table, small_id_table_size * sizeof (void*));
+               mono_gc_free_fixed (small_id_table);
+               small_id_table = new_table;
+               small_id_table_size = new_size;
+       }
+       thread->small_id = id;
+       g_assert (small_id_table [id] == NULL);
+       small_id_table [id] = thread;
+       small_id_next++;
+       if (small_id_next > small_id_table_size)
+               small_id_next = 0;
+
+       if (id >= hazard_table_size) {
+               gpointer page_addr;
+               int pagesize = mono_pagesize ();
+               int num_pages = (hazard_table_size * sizeof (MonoThreadHazardPointers) + pagesize - 1) / pagesize;
+
+               if (hazard_table == NULL) {
+                       hazard_table = mono_valloc (NULL,
+                               sizeof (MonoThreadHazardPointers) * HAZARD_TABLE_MAX_SIZE,
+                               MONO_MMAP_NONE);
+               }
+
+               g_assert (hazard_table != NULL);
+               page_addr = (guint8*)hazard_table + num_pages * pagesize;
+
+               g_assert (id < HAZARD_TABLE_MAX_SIZE);
+
+               mono_mprotect (page_addr, pagesize, MONO_MMAP_READ | MONO_MMAP_WRITE);
+
+               ++num_pages;
+               hazard_table_size = num_pages * pagesize / sizeof (MonoThreadHazardPointers);
+
+               g_assert (id < hazard_table_size);
+
+               hazard_table [id].hazard_pointers [0] = NULL;
+               hazard_table [id].hazard_pointers [1] = NULL;
+       }
+
+       if (id > highest_small_id) {
+               highest_small_id = id;
+               mono_memory_write_barrier ();
+       }
+
+       LeaveCriticalSection (&small_id_mutex);
+
+       return id;
+}
+
+static void
+small_id_free (int id)
+{
+       g_assert (id >= 0 && id < small_id_table_size);
+       g_assert (small_id_table [id] != NULL);
+
+       small_id_table [id] = NULL;
+}
+
+static gboolean
+is_pointer_hazardous (gpointer p)
+{
+       int i;
+       int highest = highest_small_id;
+
+       g_assert (highest < hazard_table_size);
+
+       for (i = 0; i <= highest; ++i) {
+               if (hazard_table [i].hazard_pointers [0] == p
+                               || hazard_table [i].hazard_pointers [1] == p)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
+MonoThreadHazardPointers*
+mono_hazard_pointer_get (void)
+{
+       MonoThread *current_thread = mono_thread_current ();
+
+       if (!(current_thread && current_thread->small_id >= 0)) {
+               static MonoThreadHazardPointers emerg_hazard_table;
+               g_warning ("Thread %p may have been prematurely finalized", current_thread);
+               return &emerg_hazard_table;
+       }
+
+       return &hazard_table [current_thread->small_id];
+}
+
+static void
+try_free_delayed_free_item (int index)
+{
+       if (delayed_free_table->len > index) {
+               DelayedFreeItem item = { NULL, NULL };
+
+               EnterCriticalSection (&delayed_free_table_mutex);
+               /* We have to check the length again because another
+                  thread might have freed an item before we acquired
+                  the lock. */
+               if (delayed_free_table->len > index) {
+                       item = g_array_index (delayed_free_table, DelayedFreeItem, index);
+
+                       if (!is_pointer_hazardous (item.p))
+                               g_array_remove_index_fast (delayed_free_table, index);
+                       else
+                               item.p = NULL;
+               }
+               LeaveCriticalSection (&delayed_free_table_mutex);
+
+               if (item.p != NULL)
+                       item.free_func (item.p);
+       }
+}
+
+void
+mono_thread_hazardous_free_or_queue (gpointer p, MonoHazardousFreeFunc free_func)
+{
+       int i;
+
+       /* First try to free a few entries in the delayed free
+          table. */
+       for (i = 2; i >= 0; --i)
+               try_free_delayed_free_item (i);
+
+       /* Now see if the pointer we're freeing is hazardous.  If it
+          isn't, free it.  Otherwise put it in the delay list. */
+       if (is_pointer_hazardous (p)) {
+               DelayedFreeItem item = { p, free_func };
+
+               ++mono_stats.hazardous_pointer_count;
+
+               EnterCriticalSection (&delayed_free_table_mutex);
+               g_array_append_val (delayed_free_table, item);
+               LeaveCriticalSection (&delayed_free_table_mutex);
+       } else
+               free_func (p);
+}
+
+void
+mono_thread_hazardous_try_free_all (void)
+{
+       int len;
+       int i;
+
+       if (!delayed_free_table)
+               return;
+
+       len = delayed_free_table->len;
+
+       for (i = len - 1; i >= 0; --i)
+               try_free_delayed_free_item (i);
 }
 
+static void ensure_synch_cs_set (MonoThread *thread)
+{
+       CRITICAL_SECTION *synch_cs;
+       
+       if (thread->synch_cs != NULL) {
+               return;
+       }
+       
+       synch_cs = g_new0 (CRITICAL_SECTION, 1);
+       InitializeCriticalSection (synch_cs);
+       
+       if (InterlockedCompareExchangePointer ((gpointer *)&thread->synch_cs,
+                                              synch_cs, NULL) != NULL) {
+               /* Another thread must have installed this CS */
+               DeleteCriticalSection (synch_cs);
+               g_free (synch_cs);
+       }
+}
+
+/*
+ * NOTE: this function can be called also for threads different from the current one:
+ * make sure no code called from it will ever assume it is run on the thread that is
+ * getting cleaned up.
+ */
 static void thread_cleanup (MonoThread *thread)
 {
        g_assert (thread != NULL);
 
+       /* if the thread is not in the hash it has been removed already */
+       if (!handle_remove (thread))
+               return;
        mono_release_type_locks (thread);
 
-       if (!mono_monitor_enter (thread->synch_lock))
-               return;
+       EnterCriticalSection (thread->synch_cs);
 
        thread->state |= ThreadState_Stopped;
-       mono_monitor_exit (thread->synch_lock);
+       thread->state &= ~ThreadState_Background;
 
+       LeaveCriticalSection (thread->synch_cs);
+       
        mono_profiler_thread_end (thread->tid);
-       handle_remove (thread->tid);
 
-       mono_thread_pop_appdomain_ref ();
+       if (thread == mono_thread_current ())
+               mono_thread_pop_appdomain_ref ();
 
        if (thread->serialized_culture_info)
                g_free (thread->serialized_culture_info);
 
+       g_free (thread->name);
+
        thread->cached_culture_info = NULL;
 
+       mono_gc_free_fixed (thread->static_data);
+       thread->static_data = NULL;
+
        if (mono_thread_cleanup_fn)
                mono_thread_cleanup_fn (thread);
+
+       small_id_free (thread->small_id);
+       thread->small_id = -2;
 }
 
 static guint32 WINAPI start_wrapper(void *data)
@@ -263,6 +562,8 @@ static guint32 WINAPI start_wrapper(void *data)
 
        SET_CURRENT_OBJECT (thread);
 
+       mono_monitor_init_tls ();
+
        /* Every thread references the appdomain which created it */
        mono_thread_push_appdomain_ref (start_info->domain);
        
@@ -289,13 +590,23 @@ static guint32 WINAPI start_wrapper(void *data)
 
        mono_profiler_thread_start (tid);
 
+       /* On 2.0 profile (and higher), set explicitly since state might have been
+          Unknown */
+       if (mono_framework_version () != 1) {
+               if (thread->apartment_state == ThreadApartmentState_Unknown)
+                       thread->apartment_state = ThreadApartmentState_MTA;
+       }
+
+       mono_thread_init_apartment_state ();
+
        if(thread->start_notify!=NULL) {
                /* Let the thread that called Start() know we're
                 * ready
                 */
                ReleaseSemaphore (thread->start_notify, 1, NULL);
        }
-       
+
+       MONO_GC_UNREGISTER_ROOT (start_info->start_arg);
        g_free (start_info);
 
        thread_adjust_static_data (thread);
@@ -324,6 +635,13 @@ static guint32 WINAPI start_wrapper(void *data)
 
        thread_cleanup (thread);
 
+       /* Do any cleanup needed for apartment state. This
+        * cannot be done in thread_cleanup since thread_cleanup could be 
+        * called for a thread other than the current thread.
+        * mono_thread_cleanup_apartment_state cleans up apartment
+        * for the current thead */
+       mono_thread_cleanup_apartment_state ();
+
        /* Remove the reference to the thread object in the TLS data,
         * so the thread object can be finalized.  This won't be
         * reached if the thread threw an uncaught exception, so those
@@ -354,7 +672,7 @@ guint32 mono_threads_get_default_stacksize (void)
        return default_stacksize;
 }
 
-void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
+void mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread)
 {
        MonoThread *thread;
        HANDLE thread_handle;
@@ -369,7 +687,25 @@ void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
        start_info->obj = thread;
        start_info->domain = domain;
        start_info->start_arg = arg;
-       
+
+       /* 
+        * The argument may be an object reference, and there is no ref to keep it alive
+        * when the new thread is started but not yet registered with the collector.
+        */
+       MONO_GC_REGISTER_ROOT (start_info->start_arg);
+
+       mono_threads_lock ();
+       if (shutting_down) {
+               mono_threads_unlock ();
+               return;
+       }
+       if (threads_starting_up == NULL) {
+               MONO_GC_REGISTER_ROOT (threads_starting_up);
+               threads_starting_up = mono_g_hash_table_new (NULL, NULL);
+       }
+       mono_g_hash_table_insert (threads_starting_up, thread, thread);
+       mono_threads_unlock (); 
+
        /* Create suspended, so we can do some housekeeping before the thread
         * starts
         */
@@ -378,53 +714,83 @@ void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
        THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
        if (thread_handle == NULL) {
                /* The thread couldn't be created, so throw an exception */
+               MONO_GC_UNREGISTER_ROOT (start_info->start_arg);
+               mono_threads_lock ();
+               mono_g_hash_table_remove (threads_starting_up, thread);
+               mono_threads_unlock ();
+               g_free (start_info);
                mono_raise_exception (mono_get_exception_execution_engine ("Couldn't create thread"));
                return;
        }
 
        thread->handle=thread_handle;
        thread->tid=tid;
+       thread->apartment_state=ThreadApartmentState_Unknown;
+       small_id_alloc (thread);
+
+       thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
+       InitializeCriticalSection (thread->synch_cs);
 
-       MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
-                                                 
-       handle_store(thread);
+       thread->threadpool_thread = threadpool_thread;
+       if (threadpool_thread)
+               mono_thread_set_state (thread, ThreadState_Background);
 
-       ResumeThread (thread_handle);
+       if (handle_store (thread))
+               ResumeThread (thread_handle);
+}
+
+void
+mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
+{
+       mono_thread_create_internal (domain, func, arg, FALSE);
 }
 
 /*
  * mono_thread_get_stack_bounds:
  *
- *   Return the address and size of the current threads stack. Return NULL as the stack
- * address if the stack address cannot be determined.
+ *   Return the address and size of the current threads stack. Return NULL as the 
+ * stack address if the stack address cannot be determined.
  */
-static void
+void
 mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
 {
+#if defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
+       *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self ());
+       *stsize = pthread_get_stacksize_np (pthread_self ());
+       *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
+       return;
+       /* FIXME: simplify the mess below */
+#elif !defined(PLATFORM_WIN32)
        pthread_attr_t attr;
        guint8 *current = (guint8*)&attr;
 
        pthread_attr_init (&attr);
 #ifdef HAVE_PTHREAD_GETATTR_NP
-               pthread_getattr_np (pthread_self(), &attr);
+       pthread_getattr_np (pthread_self(), &attr);
 #else
 #ifdef HAVE_PTHREAD_ATTR_GET_NP
-               pthread_attr_get_np (pthread_self(), &attr);
+       pthread_attr_get_np (pthread_self(), &attr);
 #elif defined(sun)
-               *staddr = NULL;
-               pthread_attr_getstacksize (&attr, &stsize);
+       *staddr = NULL;
+       pthread_attr_getstacksize (&attr, &stsize);
 #else
-               *staddr = NULL;
-               *stsize = 0;
-               return;
+       *staddr = NULL;
+       *stsize = 0;
+       return;
 #endif
 #endif
 
 #ifndef sun
-               pthread_attr_getstack (&attr, (void**)staddr, stsize);
-               g_assert (*staddr);
+       pthread_attr_getstack (&attr, (void**)staddr, stsize);
+       if (*staddr)
                g_assert ((current > *staddr) && (current < *staddr + *stsize));
 #endif
+
+       pthread_attr_destroy (&attr); 
+#endif
+
+       /* When running under emacs, sometimes staddr is not aligned to a page size */
+       *staddr = (guint8*)((gssize)*staddr & ~(mono_pagesize () - 1));
 }      
 
 MonoThread *
@@ -462,18 +828,28 @@ mono_thread_attach (MonoDomain *domain)
 
        thread->handle=thread_handle;
        thread->tid=tid;
+       thread->apartment_state=ThreadApartmentState_Unknown;
+       small_id_alloc (thread);
        thread->stack_ptr = &tid;
-       MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
+
+       thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
+       InitializeCriticalSection (thread->synch_cs);
 
        THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
 
-       handle_store(thread);
+       if (!handle_store (thread)) {
+               /* Mono is shutting down, so just wait for the end */
+               for (;;)
+                       Sleep (10000);
+       }
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Setting current_object_key to %p", __func__, GetCurrentThreadId (), thread));
 
        SET_CURRENT_OBJECT (thread);
        mono_domain_set (domain, TRUE);
 
+       mono_monitor_init_tls ();
+
        thread_adjust_static_data (thread);
 
        if (mono_thread_attach_cb) {
@@ -496,7 +872,7 @@ mono_thread_detach (MonoThread *thread)
 {
        g_return_if_fail (thread != NULL);
 
-       THREAD_DEBUG (g_message ("%s: mono_thread_detach for %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
+       THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
        
        thread_cleanup (thread);
 
@@ -513,6 +889,8 @@ mono_thread_exit ()
 {
        MonoThread *thread = mono_thread_current ();
 
+       THREAD_DEBUG (g_message ("%s: mono_thread_exit for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
+
        thread_cleanup (thread);
        SET_CURRENT_OBJECT (NULL);
 
@@ -534,16 +912,20 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
 
        THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
 
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+
+       EnterCriticalSection (this->synch_cs);
 
        if ((this->state & ThreadState_Unstarted) == 0) {
-               mono_monitor_exit (this->synch_lock);
+               LeaveCriticalSection (this->synch_cs);
                mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
                return NULL;
        }
 
+       this->small_id = -1;
+
        if ((this->state & ThreadState_Aborted) != 0) {
-               mono_monitor_exit (this->synch_lock);
+               LeaveCriticalSection (this->synch_cs);
                return this;
        }
        start_func = NULL;
@@ -558,21 +940,33 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
 
                this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
                if(this->start_notify==NULL) {
-                       mono_monitor_exit (this->synch_lock);
+                       LeaveCriticalSection (this->synch_cs);
                        g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
                        return(NULL);
                }
 
+               mono_threads_lock ();
+               if (threads_starting_up == NULL) {
+                       MONO_GC_REGISTER_ROOT (threads_starting_up);
+                       threads_starting_up = mono_g_hash_table_new (NULL, NULL);
+               }
+               mono_g_hash_table_insert (threads_starting_up, this, this);
+               mono_threads_unlock (); 
+
                thread=CreateThread(NULL, default_stacksize_for_thread (this), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
                                    CREATE_SUSPENDED, &tid);
                if(thread==NULL) {
-                       mono_monitor_exit (this->synch_lock);
+                       LeaveCriticalSection (this->synch_cs);
+                       mono_threads_lock ();
+                       mono_g_hash_table_remove (threads_starting_up, this);
+                       mono_threads_unlock ();
                        g_warning("%s: CreateThread error 0x%x", __func__, GetLastError());
                        return(NULL);
                }
                
                this->handle=thread;
                this->tid=tid;
+               small_id_alloc (this);
 
                /* Don't call handle_store() here, delay it to Start.
                 * We can't join a thread (trying to will just block
@@ -586,11 +980,18 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
 
                THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
 
-               mono_monitor_exit (this->synch_lock);
+               LeaveCriticalSection (this->synch_cs);
                return(thread);
        }
 }
 
+void ves_icall_System_Threading_Thread_Thread_init (MonoThread *this)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       ensure_synch_cs_set (this);
+}
+
 void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
                                                             HANDLE thread)
 {
@@ -599,6 +1000,10 @@ void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
        THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
 
        CloseHandle (thread);
+
+       DeleteCriticalSection (this->synch_cs);
+       g_free (this->synch_cs);
+       this->synch_cs = NULL;
 }
 
 static void mono_thread_start (MonoThread *thread)
@@ -611,7 +1016,8 @@ static void mono_thread_start (MonoThread *thread)
         * launched, to avoid the main thread deadlocking while trying
         * to clean up a thread that will never be signalled.
         */
-       handle_store (thread);
+       if (!handle_store (thread))
+               return;
 
        ResumeThread (thread->handle);
 
@@ -642,27 +1048,15 @@ void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
 
        mono_thread_current_check_pending_interrupt ();
        
-       mono_monitor_enter (thread->synch_lock);
-       thread->state |= ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
+       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
        SleepEx(ms,TRUE);
        
-       mono_monitor_enter (thread->synch_lock);
-       thread->state &= ~ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
+       mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 }
 
-void ves_icall_System_Threading_Thread_SpinWait_internal (gint32 iterations)
+void ves_icall_System_Threading_Thread_SpinWait_nop (void)
 {
-       gint32 i;
-       
-       for(i = 0; i < iterations; i++) {
-               /* We're busy waiting, but at least we can tell the
-                * scheduler to let someone else have a go...
-                */
-               Sleep (0);
-       }
 }
 
 gint32
@@ -677,24 +1071,31 @@ MonoString*
 ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
 {
        MonoString* str;
-       mono_monitor_enter (this_obj->synch_lock);
+
+       ensure_synch_cs_set (this_obj);
+       
+       EnterCriticalSection (this_obj->synch_cs);
        
        if (!this_obj->name)
                str = NULL;
        else
                str = mono_string_new_utf16 (mono_domain_get (), this_obj->name, this_obj->name_len);
        
-       mono_monitor_exit (this_obj->synch_lock);
+       LeaveCriticalSection (this_obj->synch_cs);
+       
        return str;
 }
 
 void 
 ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
 {
-       mono_monitor_enter (this_obj->synch_lock);
+       ensure_synch_cs_set (this_obj);
+       
+       EnterCriticalSection (this_obj->synch_cs);
        
        if (this_obj->name) {
-               mono_monitor_exit (this_obj->synch_lock);
+               LeaveCriticalSection (this_obj->synch_cs);
+               
                mono_raise_exception (mono_get_exception_invalid_operation ("Thread.Name can only be set once."));
                return;
        }
@@ -706,7 +1107,7 @@ ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoSt
        else
                this_obj->name = NULL;
        
-       mono_monitor_exit (this_obj->synch_lock);
+       LeaveCriticalSection (this_obj->synch_cs);
 }
 
 static MonoObject*
@@ -738,14 +1139,18 @@ ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
 {
        MonoArray *res;
 
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        if (this->serialized_culture_info) {
                res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_culture_info_len);
                memcpy (mono_array_addr (res, guint8, 0), this->serialized_culture_info, this->serialized_culture_info_len);
        } else {
                res = NULL;
        }
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 
        return res;
 }
@@ -759,9 +1164,12 @@ cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
        int free_slot = -1;
        int same_domain_slot = -1;
 
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        if (!this->cached_culture_info)
-               this->cached_culture_info = mono_array_new (mono_object_domain (this), mono_defaults.object_class, NUM_CACHED_CULTURES * 2);
+               MONO_OBJECT_SETREF (this, cached_culture_info, mono_array_new (mono_object_domain (this), mono_defaults.object_class, NUM_CACHED_CULTURES * 2));
 
        for (i = start_idx; i < start_idx + NUM_CACHED_CULTURES; ++i) {
                obj = mono_array_get (this->cached_culture_info, MonoObject*, i);
@@ -782,7 +1190,8 @@ cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
        else if (free_slot >= 0)
                mono_array_setref (this->cached_culture_info, free_slot, culture);
        /* we may want to replace an existing entry here, even when no suitable slot is found */
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 }
 
 void
@@ -794,13 +1203,17 @@ ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, Mon
 void
 ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
 {
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        if (this->serialized_culture_info)
                g_free (this->serialized_culture_info);
        this->serialized_culture_info = g_new0 (guint8, mono_array_length (arr));
        this->serialized_culture_info_len = mono_array_length (arr);
        memcpy (this->serialized_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 }
 
 
@@ -815,14 +1228,18 @@ ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *thi
 {
        MonoArray *res;
 
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        if (this->serialized_ui_culture_info) {
                res = mono_array_new (mono_domain_get (), mono_defaults.byte_class, this->serialized_ui_culture_info_len);
                memcpy (mono_array_addr (res, guint8, 0), this->serialized_ui_culture_info, this->serialized_ui_culture_info_len);
        } else {
                res = NULL;
        }
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 
        return res;
 }
@@ -836,13 +1253,17 @@ ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, M
 void
 ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
 {
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        if (this->serialized_ui_culture_info)
                g_free (this->serialized_ui_culture_info);
        this->serialized_ui_culture_info = g_new0 (guint8, mono_array_length (arr));
        this->serialized_ui_culture_info_len = mono_array_length (arr);
        memcpy (this->serialized_ui_culture_info, mono_array_addr (arr, guint8, 0), mono_array_length (arr));
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 }
 
 /* the jit may read the compiled code of this function */
@@ -856,33 +1277,36 @@ mono_thread_current (void)
 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
                                                         int ms, HANDLE thread)
 {
+       MonoThread *cur_thread = mono_thread_current ();
        gboolean ret;
        
        MONO_ARCH_SAVE_REGS;
+       
+       mono_thread_current_check_pending_interrupt ();
 
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
        
        if ((this->state & ThreadState_Unstarted) != 0) {
-               mono_monitor_exit (this->synch_lock);
+               LeaveCriticalSection (this->synch_cs);
+               
                mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started."));
                return FALSE;
        }
-       
-       mono_thread_current_check_pending_interrupt ();
-       
-       this->state |= ThreadState_WaitSleepJoin;
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
 
        if(ms== -1) {
                ms=INFINITE;
        }
        THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, thread, ms));
        
+       mono_thread_set_state (cur_thread, ThreadState_WaitSleepJoin);
+
        ret=WaitForSingleObjectEx (thread, ms, TRUE);
 
-       mono_monitor_enter (this->synch_lock);
-       this->state &= ~ThreadState_WaitSleepJoin;
-       mono_monitor_exit (this->synch_lock);
+       mono_thread_clr_state (cur_thread, ThreadState_WaitSleepJoin);
        
        if(ret==WAIT_OBJECT_0) {
                THREAD_DEBUG (g_message ("%s: join successful", __func__));
@@ -922,15 +1346,11 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
                ms=INFINITE;
        }
 
-       mono_monitor_enter (thread->synch_lock);
-       thread->state |= ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
+       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
        ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
 
-       mono_monitor_enter (thread->synch_lock);
-       thread->state &= ~ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
+       mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
        g_free(handles);
 
@@ -977,16 +1397,12 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
                ms=INFINITE;
        }
 
-       mono_monitor_enter (thread->synch_lock);
-       thread->state |= ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
-
+       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
+       
        ret=WaitForMultipleObjectsEx(numhandles, handles, FALSE, ms, TRUE);
 
-       mono_monitor_enter (thread->synch_lock);
-       thread->state &= ~ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
-
+       mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
+       
        g_free(handles);
 
        THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, GetCurrentThreadId (), ret));
@@ -1021,16 +1437,12 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this
        
        mono_thread_current_check_pending_interrupt ();
 
-       mono_monitor_enter (thread->synch_lock);
-       thread->state |= ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
-
+       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
+       
        ret=WaitForSingleObjectEx (handle, ms, TRUE);
        
-       mono_monitor_enter (thread->synch_lock);
-       thread->state &= ~ThreadState_WaitSleepJoin;
-       mono_monitor_exit (thread->synch_lock);
-
+       mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
+       
        if(ret==WAIT_FAILED) {
                THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
                return(FALSE);
@@ -1469,8 +1881,8 @@ ves_icall_System_Threading_Thread_MemoryBarrier (void)
 void
 ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
 {
-       mono_monitor_enter (this->synch_lock);
-       this->state &= ~state;
+       mono_thread_clr_state (this, state);
+
        if (state & ThreadState_Background) {
                /* If the thread changes the background mode, the main thread has to
                 * be notified, since it has to rebuild the list of threads to
@@ -1478,14 +1890,13 @@ ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
                 */
                SetEvent (background_change_event);
        }
-       mono_monitor_exit (this->synch_lock);
 }
 
 void
 ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
 {
-       mono_monitor_enter (this->synch_lock);
-       this->state |= state;
+       mono_thread_set_state (this, state);
+       
        if (state & ThreadState_Background) {
                /* If the thread changes the background mode, the main thread has to
                 * be notified, since it has to rebuild the list of threads to
@@ -1493,16 +1904,21 @@ ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
                 */
                SetEvent (background_change_event);
        }
-       mono_monitor_exit (this->synch_lock);
 }
 
 guint32
 ves_icall_System_Threading_Thread_GetState (MonoThread* this)
 {
        guint32 state;
-       mono_monitor_enter (this->synch_lock);
+
+       ensure_synch_cs_set (this);
+       
+       EnterCriticalSection (this->synch_cs);
+       
        state = this->state;
-       mono_monitor_exit (this->synch_lock);
+
+       LeaveCriticalSection (this->synch_cs);
+       
        return state;
 }
 
@@ -1510,19 +1926,21 @@ void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
 {
        gboolean throw = FALSE;
        
-       mono_monitor_enter (this->synch_lock);
+       ensure_synch_cs_set (this);
+
+       if (this == mono_thread_current ())
+               return;
+       
+       EnterCriticalSection (this->synch_cs);
        
-       /* Clear out any previous request */
-       this->thread_interrupt_requested = FALSE;
+       this->thread_interrupt_requested = TRUE;
        
        if (this->state & ThreadState_WaitSleepJoin) {
                throw = TRUE;
-       } else {
-               this->thread_interrupt_requested = TRUE;
        }
        
-       mono_monitor_exit (this->synch_lock);
-
+       LeaveCriticalSection (this->synch_cs);
+       
        if (throw) {
                signal_thread_state_change (this);
        }
@@ -1532,15 +1950,19 @@ void mono_thread_current_check_pending_interrupt ()
 {
        MonoThread *thread = mono_thread_current ();
        gboolean throw = FALSE;
-       
-       mono_monitor_enter (thread->synch_lock);
 
+       mono_debugger_check_interruption ();
+
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
+       
        if (thread->thread_interrupt_requested) {
                throw = TRUE;
                thread->thread_interrupt_requested = FALSE;
        }
        
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
 
        if (throw) {
                mono_raise_exception (mono_get_exception_thread_interrupted ());
@@ -1615,19 +2037,21 @@ ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
 {
        MONO_ARCH_SAVE_REGS;
 
-       mono_monitor_enter (thread->synch_lock);
-
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
+       
        if ((thread->state & ThreadState_AbortRequested) != 0 || 
                (thread->state & ThreadState_StopRequested) != 0 ||
                (thread->state & ThreadState_Stopped) != 0)
        {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return;
        }
 
        if ((thread->state & ThreadState_Unstarted) != 0) {
                thread->state |= ThreadState_Aborted;
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return;
        }
 
@@ -1635,12 +2059,14 @@ ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
        MONO_OBJECT_SETREF (thread, abort_state, state);
        thread->abort_exc = NULL;
 
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Abort requested for %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
-       
-       /* Make sure the thread is awake */
-       mono_thread_resume (thread);
+
+       /* During shutdown, we can't wait for other threads */
+       if (!shutting_down)
+               /* Make sure the thread is awake */
+               mono_thread_resume (thread);
        
        signal_thread_state_change (thread);
 }
@@ -1651,21 +2077,23 @@ ves_icall_System_Threading_Thread_ResetAbort (void)
        MonoThread *thread = mono_thread_current ();
 
        MONO_ARCH_SAVE_REGS;
+
+       ensure_synch_cs_set (thread);
        
-       mono_monitor_enter (thread->synch_lock);
+       EnterCriticalSection (thread->synch_cs);
 
        thread->state &= ~ThreadState_AbortRequested;
        
        if (!thread->abort_exc) {
                const char *msg = "Unable to reset abort because no abort was requested";
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                mono_raise_exception (mono_get_exception_thread_state (msg));
        } else {
                thread->abort_exc = NULL;
                thread->abort_state = NULL;
        }
        
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
 }
 
 static gboolean
@@ -1673,13 +2101,15 @@ mono_thread_suspend (MonoThread *thread)
 {
        MONO_ARCH_SAVE_REGS;
 
-       mono_monitor_enter (thread->synch_lock);
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
 
        if ((thread->state & ThreadState_Unstarted) != 0 || 
                (thread->state & ThreadState_Aborted) != 0 || 
                (thread->state & ThreadState_Stopped) != 0)
        {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return FALSE;
        }
 
@@ -1687,12 +2117,13 @@ mono_thread_suspend (MonoThread *thread)
                (thread->state & ThreadState_SuspendRequested) != 0 ||
                (thread->state & ThreadState_StopRequested) != 0) 
        {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return TRUE;
        }
        
        thread->state |= ThreadState_SuspendRequested;
-       mono_monitor_exit (thread->synch_lock);
+
+       LeaveCriticalSection (thread->synch_cs);
 
        signal_thread_state_change (thread);
        return TRUE;
@@ -1710,11 +2141,13 @@ mono_thread_resume (MonoThread *thread)
 {
        MONO_ARCH_SAVE_REGS;
 
-       mono_monitor_enter (thread->synch_lock);
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
 
        if ((thread->state & ThreadState_SuspendRequested) != 0) {
                thread->state &= ~ThreadState_SuspendRequested;
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return TRUE;
        }
 
@@ -1723,20 +2156,20 @@ mono_thread_resume (MonoThread *thread)
                (thread->state & ThreadState_Aborted) != 0 || 
                (thread->state & ThreadState_Stopped) != 0)
        {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return FALSE;
        }
        
        thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
        if (thread->resume_event == NULL) {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return(FALSE);
        }
        
        /* Awake the thread */
        SetEvent (thread->suspend_event);
 
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
 
        /* Wait for the thread to awake */
        WaitForSingleObject (thread->resume_event, INFINITE);
@@ -1779,12 +2212,14 @@ is_running_protected_wrapper (void)
 
 void mono_thread_stop (MonoThread *thread)
 {
-       mono_monitor_enter (thread->synch_lock);
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
 
        if ((thread->state & ThreadState_StopRequested) != 0 ||
                (thread->state & ThreadState_Stopped) != 0)
        {
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return;
        }
        
@@ -1794,7 +2229,7 @@ void mono_thread_stop (MonoThread *thread)
        thread->state |= ThreadState_StopRequested;
        thread->state &= ~ThreadState_AbortRequested;
        
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
        
        signal_thread_state_change (thread);
 }
@@ -1862,9 +2297,13 @@ ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
 void mono_thread_init (MonoThreadStartCB start_cb,
                       MonoThreadAttachCB attach_cb)
 {
+       MONO_GC_REGISTER_ROOT (small_id_table);
        InitializeCriticalSection(&threads_mutex);
        InitializeCriticalSection(&interlocked_mutex);
        InitializeCriticalSection(&contexts_mutex);
+       InitializeCriticalSection(&delayed_free_table_mutex);
+       InitializeCriticalSection(&small_id_mutex);
+       
        background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
        g_assert(background_change_event != NULL);
        
@@ -1877,6 +2316,8 @@ void mono_thread_init (MonoThreadStartCB start_cb,
        mono_thread_start_cb = start_cb;
        mono_thread_attach_cb = attach_cb;
 
+       delayed_free_table = g_array_new (FALSE, FALSE, sizeof (DelayedFreeItem));
+
        /* Get a pseudo handle to the current process.  This is just a
         * kludge so that wapi can build a process handle if needed.
         * As a pseudo handle is returned, we don't need to clean
@@ -1887,6 +2328,8 @@ void mono_thread_init (MonoThreadStartCB start_cb,
 
 void mono_thread_cleanup (void)
 {
+       mono_thread_hazardous_try_free_all ();
+
 #if !defined(PLATFORM_WIN32) && !defined(RUN_IN_SUBTHREAD)
        /* The main thread must abandon any held mutexes (particularly
         * important for named mutexes as they are shared across
@@ -1909,9 +2352,14 @@ void mono_thread_cleanup (void)
        DeleteCriticalSection (&threads_mutex);
        DeleteCriticalSection (&interlocked_mutex);
        DeleteCriticalSection (&contexts_mutex);
+       DeleteCriticalSection (&delayed_free_table_mutex);
+       DeleteCriticalSection (&small_id_mutex);
        CloseHandle (background_change_event);
 #endif
 
+       g_array_free (delayed_free_table, TRUE);
+       delayed_free_table = NULL;
+
        TlsFree (current_object_key);
 }
 
@@ -1921,6 +2369,17 @@ mono_threads_install_cleanup (MonoThreadCleanupFunc func)
        mono_thread_cleanup_fn = func;
 }
 
+void
+mono_thread_set_manage_callback (MonoThread *thread, MonoThreadManageCallback func)
+{
+       thread->manage_callback = func;
+}
+
+void mono_threads_install_notify_pending_exc (MonoThreadNotifyPendingExcFunc func)
+{
+       mono_thread_notify_pending_exc_fn = func;
+}
+
 G_GNUC_UNUSED
 static void print_tids (gpointer key, gpointer value, gpointer user)
 {
@@ -1963,6 +2422,7 @@ static void wait_for_tids (struct wait_data *wait, guint32 timeout)
        for(i=0; i<wait->num; i++) {
                gsize tid = wait->threads[i]->tid;
                
+               mono_threads_lock ();
                if(mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
                        /* This thread must have been killed, because
                         * it hasn't cleaned itself up. (It's just
@@ -1975,8 +2435,11 @@ static void wait_for_tids (struct wait_data *wait, guint32 timeout)
                         * same thread.)
                         */
        
-                       THREAD_DEBUG (g_message ("%s: cleaning up after thread %"G_GSIZE_FORMAT, __func__, tid));
+                       mono_threads_unlock ();
+                       THREAD_DEBUG (g_message ("%s: cleaning up after thread %p (%"G_GSIZE_FORMAT")", __func__, wait->threads[i], tid));
                        thread_cleanup (wait->threads[i]);
+               } else {
+                       mono_threads_unlock ();
                }
        }
 }
@@ -2032,13 +2495,11 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
                MonoThread *thread=(MonoThread *)value;
 
                /* Ignore background threads, we abort them later */
-               mono_monitor_enter (thread->synch_lock);
+               /* Do not lock here since it is not needed and the caller holds threads_lock */
                if (thread->state & ThreadState_Background) {
                        THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
-                       mono_monitor_exit (thread->synch_lock);
                        return; /* just leave, ignore */
                }
-               mono_monitor_exit (thread->synch_lock);
                
                if (mono_gc_is_finalizer_thread (thread)) {
                        THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
@@ -2061,11 +2522,18 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
                        return;
                }
                
-               wait->handles[wait->num]=handle;
-               wait->threads[wait->num]=thread;
-               wait->num++;
+               THREAD_DEBUG (g_message ("%s: Invoking mono_thread_manage callback on thread %p", __func__, thread));
+               if ((thread->manage_callback == NULL) || (thread->manage_callback (thread) == TRUE)) {
+                       wait->handles[wait->num]=handle;
+                       wait->threads[wait->num]=thread;
+                       wait->num++;
 
-               THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
+                       THREAD_DEBUG (g_message ("%s: adding thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
+               } else {
+                       THREAD_DEBUG (g_message ("%s: ignoring (because of callback) thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
+               }
+               
+               
        } else {
                /* Just ignore the rest, we can't do anything with
                 * them yet
@@ -2090,12 +2558,6 @@ remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
                handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
                if (handle == NULL)
                        return FALSE;
-               
-               if(thread->state & ThreadState_AbortRequested ||
-                  thread->state & ThreadState_Aborted) {
-                       THREAD_DEBUG (g_message ("%s: Thread id %"G_GSIZE_FORMAT" already aborting", __func__, (gsize)thread->tid));
-                       return(TRUE);
-               }
 
                /* printf ("A: %d\n", wait->num); */
                wait->handles[wait->num]=thread->handle;
@@ -2110,10 +2572,73 @@ remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
        return (thread->tid != self && !mono_gc_is_finalizer_thread (thread)); 
 }
 
+static MonoException* mono_thread_execute_interruption (MonoThread *thread);
+
+/** 
+ * mono_threads_set_shutting_down:
+ *
+ * Is called by a thread that wants to shut down Mono. If the runtime is already
+ * shutting down, the calling thread is suspended/stopped, and this function never
+ * returns.
+ */
+void
+mono_threads_set_shutting_down (void)
+{
+       MonoThread *current_thread = mono_thread_current ();
+
+       mono_threads_lock ();
+
+       if (shutting_down) {
+               mono_threads_unlock ();
+
+               /* Make sure we're properly suspended/stopped */
+
+               EnterCriticalSection (current_thread->synch_cs);
+
+               if ((current_thread->state & ThreadState_SuspendRequested) ||
+                   (current_thread->state & ThreadState_AbortRequested) ||
+                   (current_thread->state & ThreadState_StopRequested)) {
+                       LeaveCriticalSection (current_thread->synch_cs);
+                       mono_thread_execute_interruption (current_thread);
+               } else {
+                       current_thread->state |= ThreadState_Stopped;
+                       LeaveCriticalSection (current_thread->synch_cs);
+               }
+
+               /* Wake up other threads potentially waiting for us */
+               ExitThread (0);
+       } else {
+               shutting_down = TRUE;
+
+               /* Not really a background state change, but this will
+                * interrupt the main thread if it is waiting for all
+                * the other threads.
+                */
+               SetEvent (background_change_event);
+               
+               mono_threads_unlock ();
+       }
+}
+
+/** 
+ * mono_threads_is_shutting_down:
+ *
+ * Returns whether a thread has commenced shutdown of Mono.  Note that
+ * if the function returns FALSE the caller must not assume that
+ * shutdown is not in progress, because the situation might have
+ * changed since the function returned.  For that reason this function
+ * is of very limited utility.
+ */
+gboolean
+mono_threads_is_shutting_down (void)
+{
+       return shutting_down;
+}
+
 void mono_thread_manage (void)
 {
        struct wait_data *wait=g_new0 (struct wait_data, 1);
-       
+
        /* join each thread that's still running */
        THREAD_DEBUG (g_message ("%s: Joining each running thread...", __func__));
        
@@ -2127,6 +2652,11 @@ void mono_thread_manage (void)
        
        do {
                mono_threads_lock ();
+               if (shutting_down) {
+                       /* somebody else is shutting down */
+                       mono_threads_unlock ();
+                       break;
+               }
                THREAD_DEBUG (g_message ("%s: There are %d threads to join", __func__, mono_g_hash_table_size (threads));
                        mono_g_hash_table_foreach (threads, print_tids, NULL));
        
@@ -2141,6 +2671,10 @@ void mono_thread_manage (void)
                THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
        } while(wait->num>0);
 
+       mono_threads_set_shutting_down ();
+
+       /* No new threads will be created after this point */
+
        mono_runtime_set_shutting_down ();
 
        THREAD_DEBUG (g_message ("%s: threadpool cleanup", __func__));
@@ -2201,12 +2735,21 @@ void mono_thread_abort_all_other_threads (void)
 }
 
 static void
-collect_threads (gpointer key, gpointer value, gpointer user_data)
+collect_threads_for_suspend (gpointer key, gpointer value, gpointer user_data)
 {
        MonoThread *thread = (MonoThread*)value;
        struct wait_data *wait = (struct wait_data*)user_data;
        HANDLE handle;
 
+       /* 
+        * We try to exclude threads early, to avoid running into the MAXIMUM_WAIT_OBJECTS
+        * limitation.
+        * This needs no locking.
+        */
+       if ((thread->state & ThreadState_Suspended) != 0 || 
+               (thread->state & ThreadState_Stopped) != 0)
+               return;
+
        if (wait->num<MAXIMUM_WAIT_OBJECTS) {
                handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
                if (handle == NULL)
@@ -2221,87 +2764,152 @@ collect_threads (gpointer key, gpointer value, gpointer user_data)
 /*
  * mono_thread_suspend_all_other_threads:
  *
- *  Suspend all managed threads except the finalizer thread and this thread.
+ *  Suspend all managed threads except the finalizer thread and this thread. It is
+ * not possible to resume them later.
  */
 void mono_thread_suspend_all_other_threads (void)
 {
        struct wait_data *wait = g_new0 (struct wait_data, 1);
-       int i, waitnum;
+       int i;
        gsize self = GetCurrentThreadId ();
        gpointer *events;
        guint32 eventidx = 0;
+       gboolean starting, finished;
+
+       /*
+        * The other threads could be in an arbitrary state at this point, i.e.
+        * they could be starting up, shutting down etc. This means that there could be
+        * threads which are not even in the threads hash table yet.
+        */
 
        /* 
-        * Make a copy of the hashtable since we can't do anything with
-        * threads while threads_mutex is held.
+        * First we set a barrier which will be checked by all threads before they
+        * are added to the threads hash table, and they will exit if the flag is set.
+        * This ensures that no threads could be added to the hash later.
+        * We will use shutting_down as the barrier for now.
         */
-       mono_threads_lock ();
-       mono_g_hash_table_foreach (threads, collect_threads, wait);
-       mono_threads_unlock ();
+       g_assert (shutting_down);
 
-       events = g_new0 (gpointer, wait->num);
-       waitnum = 0;
-       /* Get the suspended events that we'll be waiting for */
-       for (i = 0; i < wait->num; ++i) {
-               MonoThread *thread = wait->threads [i];
+       /*
+        * We make multiple calls to WaitForMultipleObjects since:
+        * - we can only wait for MAXIMUM_WAIT_OBJECTS threads
+        * - some threads could exit without becoming suspended
+        */
+       finished = FALSE;
+       while (!finished) {
+               /*
+                * Make a copy of the hashtable since we can't do anything with
+                * threads while threads_mutex is held.
+                */
+               wait->num = 0;
+               mono_threads_lock ();
+               mono_g_hash_table_foreach (threads, collect_threads_for_suspend, wait);
+               mono_threads_unlock ();
 
-               if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
-                       //CloseHandle (wait->handles [i]);
-                       wait->threads [i] = NULL; /* ignore this thread in next loop */
-                       continue;
-               }
+               events = g_new0 (gpointer, wait->num);
+               eventidx = 0;
+               /* Get the suspended events that we'll be waiting for */
+               for (i = 0; i < wait->num; ++i) {
+                       MonoThread *thread = wait->threads [i];
 
-               mono_monitor_enter (thread->synch_lock);
+                       if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
+                               //CloseHandle (wait->handles [i]);
+                               wait->threads [i] = NULL; /* ignore this thread in next loop */
+                               continue;
+                       }
 
-               if ((thread->state & ThreadState_Suspended) != 0 || 
-                       (thread->state & ThreadState_SuspendRequested) != 0 ||
-                       (thread->state & ThreadState_StopRequested) != 0 ||
-                       (thread->state & ThreadState_Stopped) != 0) {
-                       mono_monitor_exit (thread->synch_lock);
-                       CloseHandle (wait->handles [i]);
-                       wait->threads [i] = NULL; /* ignore this thread in next loop */
-                       continue;
-               }
+                       ensure_synch_cs_set (thread);
+               
+                       EnterCriticalSection (thread->synch_cs);
+
+                       if ((thread->state & ThreadState_Suspended) != 0 || 
+                               (thread->state & ThreadState_SuspendRequested) != 0 ||
+                               (thread->state & ThreadState_StopRequested) != 0 ||
+                               (thread->state & ThreadState_Stopped) != 0) {
+                               LeaveCriticalSection (thread->synch_cs);
+                               CloseHandle (wait->handles [i]);
+                               wait->threads [i] = NULL; /* ignore this thread in next loop */
+                               continue;
+                       }
 
-               /* Convert abort requests into suspend requests */
-               if ((thread->state & ThreadState_AbortRequested) != 0)
-                       thread->state &= ~ThreadState_AbortRequested;
+                       /* Convert abort requests into suspend requests */
+                       if ((thread->state & ThreadState_AbortRequested) != 0)
+                               thread->state &= ~ThreadState_AbortRequested;
                        
-               thread->state |= ThreadState_SuspendRequested;
+                       thread->state |= ThreadState_SuspendRequested;
 
-               if (thread->suspended_event == NULL) {
-                       thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
                        if (thread->suspended_event == NULL) {
-                               /* Forget this one and go on to the next */
-                               mono_monitor_exit (thread->synch_lock);
-                               continue;
+                               thread->suspended_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+                               if (thread->suspended_event == NULL) {
+                                       /* Forget this one and go on to the next */
+                                       LeaveCriticalSection (thread->synch_cs);
+                                       continue;
+                               }
                        }
-               }
 
-               events [eventidx++] = thread->suspended_event;
-               mono_monitor_exit (thread->synch_lock);
+                       events [eventidx++] = thread->suspended_event;
+                       LeaveCriticalSection (thread->synch_cs);
 
-               /* Signal the thread to suspend */
-               signal_thread_state_change (thread);
-       }
+                       /* Signal the thread to suspend */
+                       signal_thread_state_change (thread);
+               }
 
-       WaitForMultipleObjectsEx (eventidx, events, TRUE, INFINITE, FALSE);
-       for (i = 0; i < wait->num; ++i) {
-               MonoThread *thread = wait->threads [i];
+               if (eventidx > 0) {
+                       WaitForMultipleObjectsEx (eventidx, events, TRUE, 100, FALSE);
+                       for (i = 0; i < wait->num; ++i) {
+                               MonoThread *thread = wait->threads [i];
 
-               if (thread == NULL)
-                       continue;
+                               if (thread == NULL)
+                                       continue;
+                       
+                               EnterCriticalSection (thread->synch_cs);
+                               if ((thread->state & ThreadState_Suspended) != 0) {
+                                       CloseHandle (thread->suspended_event);
+                                       thread->suspended_event = NULL;
+                               }
+                               LeaveCriticalSection (thread->synch_cs);
+                       }
+               } else {
+                       /* 
+                        * If there are threads which are starting up, we wait until they
+                        * are suspended when they try to register in the threads hash.
+                        * This is guaranteed to finish, since the threads which can create new
+                        * threads get suspended after a while.
+                        * FIXME: The finalizer thread can still create new threads.
+                        */
+                       mono_threads_lock ();
+                       starting = mono_g_hash_table_size (threads_starting_up) > 0;
+                       mono_threads_unlock ();
+                       if (starting)
+                               Sleep (100);
+                       else
+                               finished = TRUE;
+               }
 
-               mono_monitor_enter (thread->synch_lock);
-               CloseHandle (thread->suspended_event);
-               thread->suspended_event = NULL;
-               mono_monitor_exit (thread->synch_lock);
+               g_free (events);
        }
 
-       g_free (events);
        g_free (wait);
 }
 
+static void
+collect_threads (gpointer key, gpointer value, gpointer user_data)
+{
+       MonoThread *thread = (MonoThread*)value;
+       struct wait_data *wait = (struct wait_data*)user_data;
+       HANDLE handle;
+
+       if (wait->num<MAXIMUM_WAIT_OBJECTS) {
+               handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
+               if (handle == NULL)
+                       return;
+
+               wait->handles [wait->num] = handle;
+               wait->threads [wait->num] = thread;
+               wait->num++;
+       }
+}
+
 /**
  * mono_threads_request_thread_dump:
  *
@@ -2421,10 +3029,11 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
 {
        abort_appdomain_data user_data;
        guint32 start_time;
+       int orig_timeout = timeout;
 
-       /* printf ("ABORT BEGIN.\n"); */
+       THREAD_DEBUG (g_message ("%s: starting abort", __func__));
 
-       start_time = GetTickCount ();
+       start_time = mono_msec_ticks ();
        do {
                mono_threads_lock ();
 
@@ -2441,15 +3050,15 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
                        wait_for_tids (&user_data.wait, 100);
 
                /* Update remaining time */
-               timeout -= GetTickCount () - start_time;
-               start_time = GetTickCount ();
+               timeout -= mono_msec_ticks () - start_time;
+               start_time = mono_msec_ticks ();
 
-               if (timeout < 0)
+               if (orig_timeout != -1 && timeout < 0)
                        return FALSE;
        }
        while (user_data.wait.num > 0);
 
-       /* printf ("ABORT DONE.\n"); */
+       THREAD_DEBUG (g_message ("%s: abort done", __func__));
 
        return TRUE;
 }
@@ -2462,7 +3071,7 @@ clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
        int i;
 
        /* No locking needed here */
-       /* FIXME: why no locking? writes to the cache are protected with synch_lock above */
+       /* FIXME: why no locking? writes to the cache are protected with synch_cs above */
 
        if (thread->cached_culture_info) {
                for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i) {
@@ -2488,13 +3097,13 @@ mono_threads_clear_cached_culture (MonoDomain *domain)
 }
 
 /*
- * mono_thread_get_pending_exception:
+ * mono_thread_get_undeniable_exception:
  *
  *   Return an exception which needs to be raised when leaving a catch clause.
  * This is used for undeniable exception propagation.
  */
 MonoException*
-mono_thread_get_pending_exception (void)
+mono_thread_get_undeniable_exception (void)
 {
        MonoThread *thread = mono_thread_current ();
 
@@ -2553,6 +3162,7 @@ static void mono_init_static_data_info (StaticDataInfo *static_data)
 {
        static_data->idx = 0;
        static_data->offset = 0;
+       static_data->freelist = NULL;
 }
 
 /*
@@ -2578,15 +3188,6 @@ mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32
        if (static_data->offset + size >= static_data_size [static_data->idx]) {
                static_data->idx ++;
                g_assert (size <= static_data_size [static_data->idx]);
-               /* 
-                * massive unloading and reloading of domains with thread-static
-                * data may eventually exceed the allocated storage...
-                * Need to check what the MS runtime does in that case.
-                * Note that for each appdomain, we need to allocate a separate
-                * thread data slot for security reasons. We could keep track
-                * of the slots per-domain and when the domain is unloaded
-                * out the slots on a sort of free list.
-                */
                g_assert (static_data->idx < NUM_STATIC_DATA_IDX);
                static_data->offset = 0;
        }
@@ -2622,6 +3223,24 @@ alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
        mono_alloc_static_data (&(thread->static_data), offset);
 }
 
+static MonoThreadDomainTls*
+search_tls_slot_in_freelist (StaticDataInfo *static_data, guint32 size, guint32 align)
+{
+       MonoThreadDomainTls* prev = NULL;
+       MonoThreadDomainTls* tmp = static_data->freelist;
+       while (tmp) {
+               if (tmp->size == size) {
+                       if (prev)
+                               prev->next = tmp->next;
+                       else
+                               static_data->freelist = tmp->next;
+                       return tmp;
+               }
+               tmp = tmp->next;
+       }
+       return NULL;
+}
+
 /*
  * The offset for a special static variable is composed of three parts:
  * a bit that indicates the type of static data (0:thread, 1:context),
@@ -2636,8 +3255,16 @@ mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align
        guint32 offset;
        if (static_type == SPECIAL_STATIC_THREAD)
        {
+               MonoThreadDomainTls *item;
                mono_threads_lock ();
-               offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
+               item = search_tls_slot_in_freelist (&thread_static_info, size, align);
+               /*g_print ("TLS alloc: %d in domain %p (total: %d), cached: %p\n", size, mono_domain_get (), thread_static_info.offset, item);*/
+               if (item) {
+                       offset = item->offset;
+                       g_free (item);
+               } else {
+                       offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
+               }
                /* This can be called during startup */
                if (threads != NULL)
                        mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
@@ -2685,6 +3312,59 @@ mono_get_special_static_data (guint32 offset)
        }
 }
 
+typedef struct {
+       guint32 offset;
+       guint32 size;
+} TlsOffsetSize;
+
+static void 
+free_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
+{
+       MonoThread *thread = value;
+       TlsOffsetSize *data = user;
+       int idx = (data->offset >> 24) - 1;
+       char *ptr;
+
+       if (!thread->static_data || !thread->static_data [idx])
+               return;
+       ptr = ((char*) thread->static_data [idx]) + (data->offset & 0xffffff);
+       memset (ptr, 0, data->size);
+}
+
+static void
+do_free_special (gpointer key, gpointer value, gpointer data)
+{
+       MonoClassField *field = key;
+       guint32 offset = GPOINTER_TO_UINT (value);
+       guint32 static_type = (offset & 0x80000000);
+       gint32 align;
+       guint32 size;
+       size = mono_type_size (field->type, &align);
+       /*g_print ("free %s , size: %d, offset: %x\n", field->name, size, offset);*/
+       if (static_type == 0) {
+               TlsOffsetSize data;
+               MonoThreadDomainTls *item = g_new0 (MonoThreadDomainTls, 1);
+               data.offset = offset & 0x7fffffff;
+               data.size = size;
+               if (threads != NULL)
+                       mono_g_hash_table_foreach (threads, free_thread_static_data_helper, &data);
+               item->offset = offset;
+               item->size = size;
+               item->next = thread_static_info.freelist;
+               thread_static_info.freelist = item;
+       } else {
+               /* FIXME: free context static data as well */
+       }
+}
+
+void
+mono_alloc_special_static_data_free (GHashTable *special_static_fields)
+{
+       mono_threads_lock ();
+       g_hash_table_foreach (special_static_fields, do_free_special, NULL);
+       mono_threads_unlock ();
+}
+
 static MonoClassField *local_slots = NULL;
 
 typedef struct {
@@ -2768,7 +3448,9 @@ static guint32 dummy_apc (gpointer param)
  */
 static MonoException* mono_thread_execute_interruption (MonoThread *thread)
 {
-       mono_monitor_enter (thread->synch_lock);
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
 
        if (thread->interruption_requested) {
                /* this will consume pending APC calls */
@@ -2780,7 +3462,7 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
        if ((thread->state & ThreadState_AbortRequested) != 0) {
                if (thread->abort_exc == NULL)
                        MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
-               mono_monitor_exit (thread->synch_lock);
+               LeaveCriticalSection (thread->synch_cs);
                return thread->abort_exc;
        }
        else if ((thread->state & ThreadState_SuspendRequested) != 0) {
@@ -2788,16 +3470,24 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
                thread->state |= ThreadState_Suspended;
                thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
                if (thread->suspend_event == NULL) {
-                       mono_monitor_exit (thread->synch_lock);
+                       LeaveCriticalSection (thread->synch_cs);
                        return(NULL);
                }
                if (thread->suspended_event)
                        SetEvent (thread->suspended_event);
-               mono_monitor_exit (thread->synch_lock);
+
+               LeaveCriticalSection (thread->synch_cs);
+
+               if (shutting_down) {
+                       /* After we left the lock, the runtime might shut down so everything becomes invalid */
+                       for (;;)
+                               Sleep (1000);
+               }
                
                WaitForSingleObject (thread->suspend_event, INFINITE);
                
-               mono_monitor_enter (thread->synch_lock);
+               EnterCriticalSection (thread->synch_cs);
+
                CloseHandle (thread->suspend_event);
                thread->suspend_event = NULL;
                thread->state &= ~ThreadState_Suspended;
@@ -2806,20 +3496,28 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
                 * and will be waiting for it
                 */
                SetEvent (thread->resume_event);
-               mono_monitor_exit (thread->synch_lock);
+
+               LeaveCriticalSection (thread->synch_cs);
+               
                return NULL;
        }
        else if ((thread->state & ThreadState_StopRequested) != 0) {
                /* FIXME: do this through the JIT? */
-               mono_monitor_exit (thread->synch_lock);
+
+               LeaveCriticalSection (thread->synch_cs);
+               
                mono_thread_exit ();
                return NULL;
        } else if (thread->thread_interrupt_requested) {
-               mono_monitor_exit (thread->synch_lock);
+
+               thread->thread_interrupt_requested = FALSE;
+               LeaveCriticalSection (thread->synch_cs);
+               
                return(mono_get_exception_thread_interrupted ());
        }
        
-       mono_monitor_exit (thread->synch_lock);
+       LeaveCriticalSection (thread->synch_cs);
+       
        return NULL;
 }
 
@@ -2831,7 +3529,8 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
  * the thread. If the result is an exception that needs to be throw, it is 
  * provided as return value.
  */
-MonoException* mono_thread_request_interruption (gboolean running_managed)
+MonoException*
+mono_thread_request_interruption (gboolean running_managed)
 {
        MonoThread *thread = mono_thread_current ();
 
@@ -2839,29 +3538,28 @@ MonoException* mono_thread_request_interruption (gboolean running_managed)
        if (thread == NULL) 
                return NULL;
        
-       mono_monitor_enter (thread->synch_lock);
-       
-       if (thread->interruption_requested) {
-               mono_monitor_exit (thread->synch_lock);
+       if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
                return NULL;
-       }
 
        if (!running_managed || is_running_protected_wrapper ()) {
                /* Can't stop while in unmanaged code. Increase the global interruption
                   request count. When exiting the unmanaged method the count will be
                   checked and the thread will be interrupted. */
-
+               
                InterlockedIncrement (&thread_interruption_requested);
-               thread->interruption_requested = TRUE;
-               mono_monitor_exit (thread->synch_lock);
+
+               if (mono_thread_notify_pending_exc_fn && !running_managed)
+                       /* The JIT will notify the thread about the interruption */
+                       /* This shouldn't take any locks */
+                       mono_thread_notify_pending_exc_fn ();
 
                /* this will awake the thread if it is in WaitForSingleObject 
                   or similar */
+               /* Our implementation of this function ignores the func argument */
                QueueUserAPC ((PAPCFUNC)dummy_apc, thread->handle, NULL);
                return NULL;
        }
        else {
-               mono_monitor_exit (thread->synch_lock);
                return mono_thread_execute_interruption (thread);
        }
 }
@@ -2885,6 +3583,8 @@ static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_p
        if (thread == NULL)
                return;
 
+       mono_debugger_check_interruption ();
+
        if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
                MonoException* exc = mono_thread_execute_interruption (thread);
                if (exc) mono_raise_exception (exc);
@@ -2908,6 +3608,63 @@ void mono_thread_force_interruption_checkpoint ()
        mono_thread_interruption_checkpoint_request (TRUE);
 }
 
+/*
+ * mono_thread_get_and_clear_pending_exception:
+ *
+ *   Return any pending exceptions for the current thread and clear it as a side effect.
+ */
+MonoException*
+mono_thread_get_and_clear_pending_exception (void)
+{
+       MonoThread *thread = mono_thread_current ();
+
+       /* The thread may already be stopping */
+       if (thread == NULL)
+               return NULL;
+
+       if (thread->interruption_requested && !is_running_protected_wrapper ()) {
+               return mono_thread_execute_interruption (thread);
+       }
+       
+       if (thread->pending_exception) {
+               MonoException *exc = thread->pending_exception;
+
+               thread->pending_exception = NULL;
+               return exc;
+       }
+
+       return NULL;
+}
+
+/*
+ * mono_set_pending_exception:
+ *
+ *   Set the pending exception of the current thread to EXC. On platforms which 
+ * support it, the exception will be thrown when execution returns to managed code. 
+ * On other platforms, this function is equivalent to mono_raise_exception (). 
+ * Internal calls which report exceptions using this function instead of 
+ * raise_exception () might be called by JITted code using a more efficient calling 
+ * convention.
+ */
+void
+mono_set_pending_exception (MonoException *exc)
+{
+       MonoThread *thread = mono_thread_current ();
+
+       /* The thread may already be stopping */
+       if (thread == NULL)
+               return;
+
+       if (mono_thread_notify_pending_exc_fn) {
+               MONO_OBJECT_SETREF (thread, pending_exception, exc);
+
+               mono_thread_notify_pending_exc_fn ();
+       } else {
+               /* No way to notify the JIT about the exception, have to throw it now */
+               mono_raise_exception (exc);
+       }
+}
+
 /**
  * mono_thread_interruption_request_flag:
  *
@@ -2921,3 +3678,75 @@ gint32* mono_thread_interruption_request_flag ()
 {
        return &thread_interruption_requested;
 }
+
+void 
+mono_thread_init_apartment_state (void)
+{
+       MonoThread* thread;
+       thread = mono_thread_current ();
+
+#ifdef PLATFORM_WIN32
+       /* Positive return value indicates success, either
+        * S_OK if this is first CoInitialize call, or
+        * S_FALSE if CoInitialize already called, but with same
+        * threading model. A negative value indicates failure,
+        * probably due to trying to change the threading model.
+        */
+       if (CoInitializeEx(NULL, (thread->apartment_state == ThreadApartmentState_STA) 
+                       ? COINIT_APARTMENTTHREADED 
+                       : COINIT_MULTITHREADED) < 0) {
+               thread->apartment_state = ThreadApartmentState_Unknown;
+       }
+#endif
+}
+
+void 
+mono_thread_cleanup_apartment_state (void)
+{
+#ifdef PLATFORM_WIN32
+       MonoThread* thread;
+       thread = mono_thread_current ();
+
+       if (thread && thread->apartment_state != ThreadApartmentState_Unknown) {
+               CoUninitialize ();
+       }
+#endif
+}
+
+void
+mono_thread_set_state (MonoThread *thread, MonoThreadState state)
+{
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
+       thread->state |= state;
+       LeaveCriticalSection (thread->synch_cs);
+}
+
+void
+mono_thread_clr_state (MonoThread *thread, MonoThreadState state)
+{
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
+       thread->state &= ~state;
+       LeaveCriticalSection (thread->synch_cs);
+}
+
+gboolean
+mono_thread_test_state (MonoThread *thread, MonoThreadState test)
+{
+       gboolean ret = FALSE;
+
+       ensure_synch_cs_set (thread);
+       
+       EnterCriticalSection (thread->synch_cs);
+
+       if ((thread->state & test) != 0) {
+               ret = TRUE;
+       }
+       
+       LeaveCriticalSection (thread->synch_cs);
+       
+       return ret;
+}