2009-01-12 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mono / metadata / threads.c
index d05ef760808788a993b037d284f0b9f22472a284..96bdefc0bdce864fdc243e8a0fa6e131dcc9be0a 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/gc-internal.h>
 #include <mono/metadata/marshal.h>
 #include <mono/io-layer/io-layer.h>
+#ifndef PLATFORM_WIN32
+#include <mono/io-layer/threads.h>
+#endif
 #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)
@@ -118,6 +119,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;
 
@@ -145,6 +153,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)
@@ -184,6 +195,8 @@ static MonoThreadHazardPointers * volatile hazard_table = NULL;
 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)
 {
@@ -200,13 +213,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);
@@ -219,6 +243,8 @@ static void handle_store(MonoThread *thread)
                                 thread);
 
        mono_threads_unlock ();
+
+       return TRUE;
 }
 
 static gboolean handle_remove(MonoThread *thread)
@@ -383,11 +409,40 @@ mono_hazard_pointer_get (void)
 {
        MonoThread *current_thread = mono_thread_current ();
 
-       g_assert (current_thread && current_thread->small_id >= 0);
+       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)
 {
@@ -395,29 +450,8 @@ mono_thread_hazardous_free_or_queue (gpointer p, MonoHazardousFreeFunc free_func
 
        /* First try to free a few entries in the delayed free
           table. */
-       for (i = 2; i >= 0; --i) {
-               if (delayed_free_table->len > i) {
-                       DelayedFreeItem item;
-
-                       item.p = 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 > i) {
-                               item = g_array_index (delayed_free_table, DelayedFreeItem, i);
-
-                               if (!is_pointer_hazardous (item.p))
-                                       g_array_remove_index_fast (delayed_free_table, i);
-                               else
-                                       item.p = NULL;
-                       }
-                       LeaveCriticalSection (&delayed_free_table_mutex);
-
-                       if (item.p != NULL)
-                               item.free_func (item.p);
-               }
-       }
+       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. */
@@ -433,6 +467,40 @@ mono_thread_hazardous_free_or_queue (gpointer p, MonoHazardousFreeFunc free_func
                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
@@ -447,13 +515,13 @@ static void thread_cleanup (MonoThread *thread)
                return;
        mono_release_type_locks (thread);
 
-       if (!mono_monitor_enter (thread->synch_lock))
-               return;
+       EnterCriticalSection (thread->synch_cs);
 
        thread->state |= ThreadState_Stopped;
        thread->state &= ~ThreadState_Background;
-       mono_monitor_exit (thread->synch_lock);
 
+       LeaveCriticalSection (thread->synch_cs);
+       
        mono_profiler_thread_end (thread->tid);
 
        if (thread == mono_thread_current ())
@@ -462,6 +530,8 @@ static void thread_cleanup (MonoThread *thread)
        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);
@@ -495,6 +565,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);
        
@@ -523,7 +595,7 @@ static guint32 WINAPI start_wrapper(void *data)
 
        /* On 2.0 profile (and higher), set explicitly since state might have been
           Unknown */
-       if (mono_get_runtime_info ()->framework_version [0] != '1') {
+       if (mono_framework_version () != 1) {
                if (thread->apartment_state == ThreadApartmentState_Unknown)
                        thread->apartment_state = ThreadApartmentState_MTA;
        }
@@ -536,7 +608,8 @@ static guint32 WINAPI start_wrapper(void *data)
                 */
                ReleaseSemaphore (thread->start_notify, 1, NULL);
        }
-       
+
+       MONO_GC_UNREGISTER_ROOT (start_info->start_arg);
        g_free (start_info);
 
        thread_adjust_static_data (thread);
@@ -602,7 +675,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;
@@ -617,7 +690,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
         */
@@ -626,6 +717,11 @@ 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;
        }
@@ -635,18 +731,28 @@ void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
        thread->apartment_state=ThreadApartmentState_Unknown;
        small_id_alloc (thread);
 
-       MONO_OBJECT_SETREF (thread, synch_lock, mono_object_new (domain, mono_defaults.object_class));
-                                                 
-       handle_store(thread);
+       thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
+       InitializeCriticalSection (thread->synch_cs);
+
+       thread->threadpool_thread = threadpool_thread;
+       if (threadpool_thread)
+               mono_thread_set_state (thread, ThreadState_Background);
+
+       if (handle_store (thread))
+               ResumeThread (thread_handle);
+}
 
-       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.
  */
 void
 mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
@@ -654,6 +760,7 @@ 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)
@@ -662,28 +769,31 @@ mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
 
        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);
-               if (*staddr)
-                       g_assert ((current > *staddr) && (current < *staddr + *stsize));
+       pthread_attr_getstack (&attr, (void**)staddr, stsize);
+       if (*staddr)
+               g_assert ((current > *staddr) && (current < *staddr + *stsize));
 #endif
 
-               pthread_attr_destroy (&attr); 
+       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 *
@@ -724,17 +834,25 @@ mono_thread_attach (MonoDomain *domain)
        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) {
@@ -797,10 +915,12 @@ 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;
        }
@@ -808,7 +928,7 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
        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;
@@ -823,15 +943,26 @@ 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);
                }
@@ -852,11 +983,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)
 {
@@ -865,6 +1003,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)
@@ -877,7 +1019,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);
 
@@ -908,27 +1051,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
@@ -943,24 +1074,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;
        }
@@ -972,7 +1110,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*
@@ -1004,14 +1142,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;
 }
@@ -1025,9 +1167,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);
@@ -1048,7 +1193,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
@@ -1060,13 +1206,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);
 }
 
 
@@ -1081,14 +1231,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;
 }
@@ -1102,53 +1256,61 @@ 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 */
 MonoThread *
 mono_thread_current (void)
 {
-       THREAD_DEBUG (g_message ("%s: returning %p", __func__, GET_CURRENT_OBJECT ()));
-       return GET_CURRENT_OBJECT ();
+       MonoThread *res = GET_CURRENT_OBJECT ()
+       THREAD_DEBUG (g_message ("%s: returning %p", __func__, res));
+       return res;
 }
 
 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__));
@@ -1188,15 +1350,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);
 
@@ -1243,16 +1401,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));
@@ -1287,16 +1441,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);
@@ -1735,8 +1885,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
@@ -1744,14 +1894,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
@@ -1759,16 +1908,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;
 }
 
@@ -1776,19 +1930,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);
        }
@@ -1798,15 +1954,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 ());
@@ -1873,6 +2033,15 @@ static void signal_thread_state_change (MonoThread *thread)
 #else
        pthread_kill (thread->tid, mono_thread_get_abort_signal ());
 #endif
+
+       /* 
+        * This will cause waits to be broken.
+        * It will also prevent the thread from entering a wait, so if the thread returns
+        * from the wait before it receives the abort signal, it will just spin in the wait
+        * functions in the io-layer until the signal handler calls QueueUserAPC which will
+        * make it return.
+        */
+       wapi_interrupt_thread (thread->handle);
 #endif /* PLATFORM_WIN32 */
 }
 
@@ -1881,19 +2050,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;
        }
 
@@ -1901,12 +2072,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);
 }
@@ -1917,21 +2090,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
@@ -1939,13 +2114,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;
        }
 
@@ -1953,12 +2130,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;
@@ -1976,11 +2154,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;
        }
 
@@ -1989,20 +2169,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);
@@ -2045,12 +2225,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;
        }
        
@@ -2060,7 +2242,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);
 }
@@ -2134,6 +2316,7 @@ void mono_thread_init (MonoThreadStartCB start_cb,
        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);
        
@@ -2158,6 +2341,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
@@ -2186,6 +2371,7 @@ void mono_thread_cleanup (void)
 #endif
 
        g_array_free (delayed_free_table, TRUE);
+       delayed_free_table = NULL;
 
        TlsFree (current_object_key);
 }
@@ -2196,6 +2382,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)
 {
@@ -2338,11 +2535,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
@@ -2367,12 +2571,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;
@@ -2387,10 +2585,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__));
        
@@ -2404,6 +2665,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));
        
@@ -2418,6 +2684,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__));
@@ -2478,12 +2748,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)
@@ -2498,87 +2777,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:
  *
@@ -2698,10 +3042,11 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
 {
        abort_appdomain_data user_data;
        guint32 start_time;
+       int orig_timeout = timeout;
 
        THREAD_DEBUG (g_message ("%s: starting abort", __func__));
 
-       start_time = GetTickCount ();
+       start_time = mono_msec_ticks ();
        do {
                mono_threads_lock ();
 
@@ -2718,10 +3063,10 @@ 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);
@@ -2739,7 +3084,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) {
@@ -2765,13 +3110,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 ();
 
@@ -3116,19 +3461,25 @@ 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 */
                WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
                InterlockedDecrement (&thread_interruption_requested);
                thread->interruption_requested = FALSE;
+#ifndef PLATFORM_WIN32
+               /* Clear the interrupted flag of the thread so it can wait again */
+               wapi_clear_interruption ();
+#endif
        }
 
        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) {
@@ -3136,16 +3487,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;
@@ -3154,20 +3513,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;
 }
 
@@ -3179,7 +3546,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 ();
 
@@ -3187,29 +3555,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);
        }
 }
@@ -3233,6 +3600,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);
@@ -3256,6 +3625,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:
  *
@@ -3303,3 +3729,41 @@ mono_thread_cleanup_apartment_state (void)
        }
 #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;
+}