2009-09-30 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / threads.c
index 400f1b9a06cf683fc72f1a007a762f13cae15dc0..5b6024898b3c7b675d3ce3f4d23c46ebd061b7c8 100644 (file)
@@ -6,7 +6,8 @@
  *     Paolo Molaro (lupus@ximian.com)
  *     Patrik Torstensson (patrik.torstensson@labs2.com)
  *
- * (C) 2001 Ximian, Inc.
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
@@ -27,6 +28,9 @@
 #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>
@@ -58,7 +62,6 @@ struct StartInfo
        MonoThread *obj;
        MonoObject *delegate;
        void *start_arg;
-       MonoDomain *domain;
 };
 
 typedef union {
@@ -111,8 +114,8 @@ static CRITICAL_SECTION contexts_mutex;
 static StaticDataInfo thread_static_info;
 static StaticDataInfo context_static_info;
 
-/* The hash of existing threads (key is thread ID) that need joining
- * before exit
+/* The hash of existing threads (key is thread ID, value is
+ * MonoInternalThread*) that need joining before exit
  */
 static MonoGHashTable *threads=NULL;
 
@@ -122,6 +125,10 @@ static MonoGHashTable *threads=NULL;
  * Protected by mono_threads_lock ().
  */
 static MonoGHashTable *threads_starting_up = NULL;
+/* Maps a MonoThread to its start argument */
+/* Protected by mono_threads_lock () */
+static MonoGHashTable *thread_start_args = NULL;
 
 /* The TLS key that holds the MonoObject assigned to each thread */
 static guint32 current_object_key = -1;
@@ -130,15 +137,15 @@ static guint32 current_object_key = -1;
 /* we need to use both the Tls* functions and __thread because
  * the gc needs to see all the threads 
  */
-static __thread MonoThread * tls_current_object MONO_TLS_FAST;
+static __thread MonoInternalThread * tls_current_object MONO_TLS_FAST;
 #define SET_CURRENT_OBJECT(x) do { \
        tls_current_object = x; \
        TlsSetValue (current_object_key, x); \
 } while (FALSE)
 #define GET_CURRENT_OBJECT() tls_current_object
 #else
-#define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x);
-#define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key);
+#define SET_CURRENT_OBJECT(x) TlsSetValue (current_object_key, x)
+#define GET_CURRENT_OBJECT() (MonoThread*) TlsGetValue (current_object_key)
 #endif
 
 /* function called at thread start */
@@ -157,12 +164,14 @@ static MonoThreadNotifyPendingExcFunc mono_thread_notify_pending_exc_fn = NULL;
 static guint32 default_stacksize = 0;
 #define default_stacksize_for_thread(thread) ((thread)->stack_size? (thread)->stack_size: default_stacksize)
 
-static void thread_adjust_static_data (MonoThread *thread);
+static void thread_adjust_static_data (MonoInternalThread *thread);
 static void mono_init_static_data_info (StaticDataInfo *static_data);
 static guint32 mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32 align);
-static gboolean mono_thread_resume (MonoThread* thread);
+static gboolean mono_thread_resume (MonoInternalThread* thread);
 static void mono_thread_start (MonoThread *thread);
-static void signal_thread_state_change (MonoThread *thread);
+static void signal_thread_state_change (MonoInternalThread *thread);
+
+static MonoException* mono_thread_execute_interruption (MonoInternalThread *thread);
 
 /* Spin lock for InterlockedXXX 64 bit functions */
 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
@@ -180,7 +189,7 @@ 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;
+static MonoInternalThread **small_id_table = NULL;
 
 /* The hazard table */
 #define HAZARD_TABLE_MAX_SIZE  16384 /* There cannot be more threads than this number. */
@@ -230,21 +239,22 @@ static gboolean handle_store(MonoThread *thread)
 
        if(threads==NULL) {
                MONO_GC_REGISTER_ROOT (threads);
-               threads=mono_g_hash_table_new(NULL, NULL);
+               threads=mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
        }
 
        /* We don't need to duplicate thread->handle, because it is
         * only closed when the thread object is finalized by the GC.
         */
-       mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->tid),
-                                thread);
+       g_assert (thread->internal_thread);
+       mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->internal_thread->tid),
+                                thread->internal_thread);
 
        mono_threads_unlock ();
 
        return TRUE;
 }
 
-static gboolean handle_remove(MonoThread *thread)
+static gboolean handle_remove(MonoInternalThread *thread)
 {
        gboolean ret;
        gsize tid = thread->tid;
@@ -295,7 +305,7 @@ static gboolean handle_remove(MonoThread *thread)
  * domain_id_alloc() in domain.c and should be merged.
  */
 static int
-small_id_alloc (MonoThread *thread)
+small_id_alloc (MonoInternalThread *thread)
 {
        int id = -1, i;
 
@@ -303,7 +313,7 @@ small_id_alloc (MonoThread *thread)
 
        if (!small_id_table) {
                small_id_table_size = 2;
-               small_id_table = mono_gc_alloc_fixed (small_id_table_size * sizeof (MonoThread*), NULL);
+               small_id_table = mono_gc_alloc_fixed (small_id_table_size * sizeof (MonoInternalThread*), NULL);
        }
        for (i = small_id_next; i < small_id_table_size; ++i) {
                if (!small_id_table [i]) {
@@ -320,12 +330,12 @@ small_id_alloc (MonoThread *thread)
                }
        }
        if (id == -1) {
-               MonoThread **new_table;
+               MonoInternalThread **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);
+               new_table = mono_gc_alloc_fixed (new_size * sizeof (MonoInternalThread*), 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;
@@ -404,7 +414,7 @@ is_pointer_hazardous (gpointer p)
 MonoThreadHazardPointers*
 mono_hazard_pointer_get (void)
 {
-       MonoThread *current_thread = mono_thread_current ();
+       MonoInternalThread *current_thread = mono_thread_internal_current ();
 
        if (!(current_thread && current_thread->small_id >= 0)) {
                static MonoThreadHazardPointers emerg_hazard_table;
@@ -419,9 +429,8 @@ static void
 try_free_delayed_free_item (int index)
 {
        if (delayed_free_table->len > index) {
-               DelayedFreeItem item;
+               DelayedFreeItem item = { NULL, NULL };
 
-               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
@@ -468,14 +477,19 @@ mono_thread_hazardous_free_or_queue (gpointer p, MonoHazardousFreeFunc free_func
 void
 mono_thread_hazardous_try_free_all (void)
 {
-       int len = delayed_free_table->len;
+       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)
+static void ensure_synch_cs_set (MonoInternalThread *thread)
 {
        CRITICAL_SECTION *synch_cs;
        
@@ -499,10 +513,29 @@ static void ensure_synch_cs_set (MonoThread *thread)
  * 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)
+static void thread_cleanup (MonoInternalThread *thread)
 {
        g_assert (thread != NULL);
 
+       if (thread->abort_state_handle) {
+               g_assert (thread->abort_exc);
+               mono_gchandle_free (thread->abort_state_handle);
+               thread->abort_state_handle = 0;
+       }
+       thread->abort_exc = NULL;
+       thread->current_appcontext = NULL;
+
+       /*
+        * This is necessary because otherwise we might have
+        * cross-domain references which will not get cleaned up when
+        * the target domain is unloaded.
+        */
+       if (thread->cached_culture_info) {
+               int i;
+               for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i)
+                       mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
+       }
+
        /* if the thread is not in the hash it has been removed already */
        if (!handle_remove (thread))
                return;
@@ -517,24 +550,83 @@ static void thread_cleanup (MonoThread *thread)
        
        mono_profiler_thread_end (thread->tid);
 
-       if (thread == mono_thread_current ())
+       if (thread == mono_thread_internal_current ())
                mono_thread_pop_appdomain_ref ();
 
-       if (thread->serialized_culture_info)
-               g_free (thread->serialized_culture_info);
-
        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);
+               mono_thread_cleanup_fn (thread->root_domain_thread);
 
        small_id_free (thread->small_id);
        thread->small_id = -2;
 }
 
+static gpointer
+get_thread_static_data (MonoInternalThread *thread, guint32 offset)
+{
+       int idx;
+       g_assert ((offset & 0x80000000) == 0);
+       offset &= 0x7fffffff;
+       idx = (offset >> 24) - 1;
+       return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
+}
+
+static MonoThread**
+get_current_thread_ptr_for_domain (MonoDomain *domain, MonoInternalThread *thread)
+{
+       static MonoClassField *current_thread_field = NULL;
+
+       guint32 offset;
+
+       if (!current_thread_field) {
+               current_thread_field = mono_class_get_field_from_name (mono_defaults.thread_class, "current_thread");
+               g_assert (current_thread_field);
+       }
+
+       mono_class_vtable (domain, mono_defaults.thread_class);
+       mono_domain_lock (domain);
+       offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, current_thread_field));
+       mono_domain_unlock (domain);
+       g_assert (offset);
+
+       return get_thread_static_data (thread, offset);
+}
+
+static void
+set_current_thread_for_domain (MonoDomain *domain, MonoInternalThread *thread, MonoThread *current)
+{
+       MonoThread **current_thread_ptr = get_current_thread_ptr_for_domain (domain, thread);
+
+       g_assert (current->obj.vtable->domain == domain);
+
+       g_assert (!*current_thread_ptr);
+       *current_thread_ptr = current;
+}
+
+static MonoThread*
+new_thread_with_internal (MonoDomain *domain, MonoInternalThread *internal)
+{
+       MonoThread *thread = (MonoThread*) mono_object_new (domain, mono_defaults.thread_class);
+       thread->internal_thread = internal;
+       return thread;
+}
+
+static void
+init_root_domain_thread (MonoInternalThread *thread, MonoThread *candidate)
+{
+       MonoDomain *domain = mono_get_root_domain ();
+
+       if (!candidate || candidate->obj.vtable->domain != domain)
+               candidate = new_thread_with_internal (domain, thread);
+       set_current_thread_for_domain (domain, thread, candidate);
+       g_assert (!thread->root_domain_thread);
+       thread->root_domain_thread = candidate;
+}
+
 static guint32 WINAPI start_wrapper(void *data)
 {
        struct StartInfo *start_info=(struct StartInfo *)data;
@@ -542,6 +634,7 @@ static guint32 WINAPI start_wrapper(void *data)
        void *start_arg;
        gsize tid;
        MonoThread *thread=start_info->obj;
+       MonoInternalThread *internal = thread->internal_thread;
        MonoObject *start_delegate = start_info->delegate;
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper", __func__, GetCurrentThreadId ()));
@@ -552,14 +645,16 @@ static guint32 WINAPI start_wrapper(void *data)
         * thread resumed
         */
 
-       tid=thread->tid;
+       tid=internal->tid;
 
-       SET_CURRENT_OBJECT (thread);
+       SET_CURRENT_OBJECT (internal);
+
+       mono_monitor_init_tls ();
 
        /* Every thread references the appdomain which created it */
-       mono_thread_push_appdomain_ref (start_info->domain);
+       mono_thread_push_appdomain_ref (thread->obj.vtable->domain);
        
-       if (!mono_domain_set (start_info->domain, FALSE)) {
+       if (!mono_domain_set (thread->obj.vtable->domain, FALSE)) {
                /* No point in raising an appdomain_unloaded exception here */
                /* FIXME: Cleanup here */
                mono_thread_pop_appdomain_ref ();
@@ -569,12 +664,17 @@ static guint32 WINAPI start_wrapper(void *data)
        start_func = start_info->func;
        start_arg = start_info->start_arg;
 
+       /* We have to do this here because mono_thread_new_init()
+          requires that root_domain_thread is set up. */
+       thread_adjust_static_data (internal);
+       init_root_domain_thread (internal, thread);
+
        /* This MUST be called before any managed code can be
         * executed, as it calls the callback function that (for the
         * jit) sets the lmf marker.
         */
        mono_thread_new_init (tid, &tid, start_func);
-       thread->stack_ptr = &tid;
+       internal->stack_ptr = &tid;
 
        LIBGC_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT",%d) Setting thread stack to %p", __func__, GetCurrentThreadId (), getpid (), thread->stack_ptr));
 
@@ -584,29 +684,33 @@ 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 (thread->apartment_state == ThreadApartmentState_Unknown)
-                       thread->apartment_state = ThreadApartmentState_MTA;
+       if (mono_framework_version () != 1) {
+               if (internal->apartment_state == ThreadApartmentState_Unknown)
+                       internal->apartment_state = ThreadApartmentState_MTA;
        }
 
        mono_thread_init_apartment_state ();
 
-       if(thread->start_notify!=NULL) {
+       if(internal->start_notify!=NULL) {
                /* Let the thread that called Start() know we're
                 * ready
                 */
-               ReleaseSemaphore (thread->start_notify, 1, NULL);
+               ReleaseSemaphore (internal->start_notify, 1, NULL);
        }
 
-       MONO_GC_UNREGISTER_ROOT (start_info->start_arg);
-       g_free (start_info);
+       mono_threads_lock ();
+       mono_g_hash_table_remove (thread_start_args, thread);
+       mono_threads_unlock ();
 
-       thread_adjust_static_data (thread);
+       g_free (start_info);
 #ifdef DEBUG
        g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
                   thread->tid);
 #endif
 
+       mono_thread_set_execution_context (thread->ec_to_set);
+       thread->ec_to_set = NULL;
+
        /* start_func is set only for unmanaged start functions */
        if (start_func) {
                start_func (start_arg);
@@ -625,7 +729,7 @@ static guint32 WINAPI start_wrapper(void *data)
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, GetCurrentThreadId ()));
 
-       thread_cleanup (thread);
+       thread_cleanup (internal);
 
        /* Do any cleanup needed for apartment state. This
         * cannot be done in thread_cleanup since thread_cleanup could be 
@@ -664,69 +768,105 @@ guint32 mono_threads_get_default_stacksize (void)
        return default_stacksize;
 }
 
-void mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread)
+/*
+ * mono_create_thread:
+ *
+ *   This is a wrapper around CreateThread which handles differences in the type of
+ * the the 'tid' argument.
+ */
+gpointer mono_create_thread (WapiSecurityAttributes *security,
+                                                        guint32 stacksize, WapiThreadStart start,
+                                                        gpointer param, guint32 create, gsize *tid)
+{
+       gpointer res;
+
+#ifdef PLATFORM_WIN32
+       DWORD real_tid;
+
+       res = CreateThread (security, stacksize, start, param, create, &real_tid);
+       if (tid)
+               *tid = real_tid;
+#else
+       res = CreateThread (security, stacksize, start, param, create, tid);
+#endif
+
+       return res;
+}
+
+MonoInternalThread* mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread)
 {
        MonoThread *thread;
+       MonoInternalThread *internal;
        HANDLE thread_handle;
        struct StartInfo *start_info;
        gsize tid;
 
        thread=(MonoThread *)mono_object_new (domain,
                                              mono_defaults.thread_class);
+       internal = (MonoInternalThread*)mono_object_new (mono_get_root_domain (),
+                       mono_defaults.internal_thread_class);
+       thread->internal_thread = internal;
 
        start_info=g_new0 (struct StartInfo, 1);
        start_info->func = func;
        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;
+               g_free (start_info);
+               return NULL;
        }
        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);
+       if (thread_start_args == NULL) {
+               MONO_GC_REGISTER_ROOT (thread_start_args);
+               thread_start_args = mono_g_hash_table_new (NULL, NULL);
+       }
+       mono_g_hash_table_insert (threads_starting_up, thread, thread);
+       /* 
+        * 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. So
+        * we store it in a GC tracked hash table.
+        */
+       mono_g_hash_table_insert (thread_start_args, thread, start_info->start_arg);
        mono_threads_unlock (); 
 
        /* Create suspended, so we can do some housekeeping before the thread
         * starts
         */
-       thread_handle = CreateThread(NULL, default_stacksize_for_thread (thread), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
+       thread_handle = mono_create_thread (NULL, default_stacksize_for_thread (internal), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
                                     CREATE_SUSPENDED, &tid);
        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;
+               return NULL;
        }
 
-       thread->handle=thread_handle;
-       thread->tid=tid;
-       thread->apartment_state=ThreadApartmentState_Unknown;
-       small_id_alloc (thread);
+       internal->handle=thread_handle;
+       internal->tid=tid;
+       internal->apartment_state=ThreadApartmentState_Unknown;
+       small_id_alloc (internal);
 
-       thread->synch_cs = g_new0 (CRITICAL_SECTION, 1);
-       InitializeCriticalSection (thread->synch_cs);
+       internal->synch_cs = g_new0 (CRITICAL_SECTION, 1);
+       InitializeCriticalSection (internal->synch_cs);
 
-       thread->threadpool_thread = threadpool_thread;
+       internal->threadpool_thread = threadpool_thread;
+       if (threadpool_thread)
+               mono_thread_set_state (internal, ThreadState_Background);
 
        if (handle_store (thread))
                ResumeThread (thread_handle);
+
+       return internal;
 }
 
 void
@@ -738,8 +878,8 @@ mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
 /*
  * 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)
@@ -747,6 +887,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)
@@ -754,51 +895,54 @@ mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
        guint8 *current = (guint8*)&attr;
 
        pthread_attr_init (&attr);
-#ifdef HAVE_PTHREAD_GETATTR_NP
-               pthread_getattr_np (pthread_self(), &attr);
-#else
-#ifdef HAVE_PTHREAD_ATTR_GET_NP
-               pthread_attr_get_np (pthread_self(), &attr);
-#elif defined(sun)
-               *staddr = NULL;
-               pthread_attr_getstacksize (&attr, &stsize);
-#else
-               *staddr = NULL;
-               *stsize = 0;
-               return;
-#endif
-#endif
+#  ifdef HAVE_PTHREAD_GETATTR_NP
+       pthread_getattr_np (pthread_self(), &attr);
+#  else
+#    ifdef HAVE_PTHREAD_ATTR_GET_NP
+       pthread_attr_get_np (pthread_self(), &attr);
+#    elif defined(sun)
+       *staddr = NULL;
+       pthread_attr_getstacksize (&attr, &stsize);
+#    else
+       *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));
-#endif
+#  ifndef sun
+       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 *
 mono_thread_attach (MonoDomain *domain)
 {
-       MonoThread *thread;
+       MonoInternalThread *thread;
+       MonoThread *current_thread;
        HANDLE thread_handle;
        gsize tid;
 
-       if ((thread = mono_thread_current ())) {
+       if ((thread = mono_thread_internal_current ())) {
                if (domain != mono_domain_get ())
                        mono_domain_set (domain, TRUE);
                /* Already attached */
-               return thread;
+               return mono_thread_current ();
        }
 
        if (!mono_gc_register_thread (&domain)) {
                g_error ("Thread %"G_GSIZE_FORMAT" calling into managed code is not registered with the GC. On UNIX, this can be fixed by #include-ing <gc.h> before <pthread.h> in the file containing the thread creation code.", GetCurrentThreadId ());
        }
 
-       thread = (MonoThread *)mono_object_new (domain,
-                                               mono_defaults.thread_class);
+       thread = (MonoInternalThread *)mono_object_new (domain, mono_defaults.internal_thread_class);
 
        thread_handle = GetCurrentThread ();
        g_assert (thread_handle);
@@ -823,7 +967,9 @@ mono_thread_attach (MonoDomain *domain)
 
        THREAD_DEBUG (g_message ("%s: Attached thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
 
-       if (!handle_store (thread)) {
+       current_thread = new_thread_with_internal (domain, thread);
+
+       if (!handle_store (current_thread)) {
                /* Mono is shutting down, so just wait for the end */
                for (;;)
                        Sleep (10000);
@@ -834,8 +980,15 @@ mono_thread_attach (MonoDomain *domain)
        SET_CURRENT_OBJECT (thread);
        mono_domain_set (domain, TRUE);
 
+       mono_monitor_init_tls ();
+
        thread_adjust_static_data (thread);
 
+       init_root_domain_thread (thread, current_thread);
+       if (domain != mono_get_root_domain ())
+               set_current_thread_for_domain (domain, thread, current_thread);
+
+
        if (mono_thread_attach_cb) {
                guint8 *staddr;
                size_t stsize;
@@ -848,7 +1001,7 @@ mono_thread_attach (MonoDomain *domain)
                        mono_thread_attach_cb (tid, staddr + stsize);
        }
 
-       return(thread);
+       return current_thread;
 }
 
 void
@@ -858,7 +1011,7 @@ mono_thread_detach (MonoThread *thread)
 
        THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
        
-       thread_cleanup (thread);
+       thread_cleanup (thread->internal_thread);
 
        SET_CURRENT_OBJECT (NULL);
 
@@ -871,7 +1024,7 @@ mono_thread_detach (MonoThread *thread)
 void
 mono_thread_exit ()
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        THREAD_DEBUG (g_message ("%s: mono_thread_exit for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
 
@@ -879,11 +1032,21 @@ mono_thread_exit ()
        SET_CURRENT_OBJECT (NULL);
 
        /* we could add a callback here for embedders to use. */
-       if (thread == mono_thread_get_main ())
+       if (thread == mono_thread_get_main ()->internal_thread)
                exit (mono_environment_exitcode_get ());
        ExitThread (-1);
 }
 
+void
+ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this)
+{
+       MonoInternalThread *internal = (MonoInternalThread*)mono_object_new (mono_get_root_domain (), mono_defaults.internal_thread_class);
+       internal->state = ThreadState_Unstarted;
+       internal->apartment_state = ThreadApartmentState_Unknown;
+
+       InterlockedCompareExchangePointer ((gpointer)&this->internal_thread, internal, NULL);
+}
+
 HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
                                                         MonoObject *start)
 {
@@ -891,25 +1054,28 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
        struct StartInfo *start_info;
        HANDLE thread;
        gsize tid;
-       
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *internal;
 
        THREAD_DEBUG (g_message("%s: Trying to start a new thread: this (%p) start (%p)", __func__, this, start));
 
-       ensure_synch_cs_set (this);
+       if (!this->internal_thread)
+               ves_icall_System_Threading_Thread_ConstructInternalThread (this);
+       internal = this->internal_thread;
 
-       EnterCriticalSection (this->synch_cs);
+       ensure_synch_cs_set (internal);
 
-       if ((this->state & ThreadState_Unstarted) == 0) {
-               LeaveCriticalSection (this->synch_cs);
+       EnterCriticalSection (internal->synch_cs);
+
+       if ((internal->state & ThreadState_Unstarted) == 0) {
+               LeaveCriticalSection (internal->synch_cs);
                mono_raise_exception (mono_get_exception_thread_state ("Thread has already been started."));
                return NULL;
        }
 
-       this->small_id = -1;
+       internal->small_id = -1;
 
-       if ((this->state & ThreadState_Aborted) != 0) {
-               LeaveCriticalSection (this->synch_cs);
+       if ((internal->state & ThreadState_Aborted) != 0) {
+               LeaveCriticalSection (internal->synch_cs);
                return this;
        }
        start_func = NULL;
@@ -920,12 +1086,13 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
                start_info->start_arg = this->start_obj; /* FIXME: GC object stored in unmanaged memory */
                start_info->delegate = start;
                start_info->obj = this;
-               start_info->domain = mono_domain_get ();
+               g_assert (this->obj.vtable->domain == mono_domain_get ());
 
-               this->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
-               if(this->start_notify==NULL) {
-                       LeaveCriticalSection (this->synch_cs);
+               internal->start_notify=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
+               if (internal->start_notify==NULL) {
+                       LeaveCriticalSection (internal->synch_cs);
                        g_warning ("%s: CreateSemaphore error 0x%x", __func__, GetLastError ());
+                       g_free (start_info);
                        return(NULL);
                }
 
@@ -937,10 +1104,10 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
                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,
+               thread=mono_create_thread(NULL, default_stacksize_for_thread (internal), (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
                                    CREATE_SUSPENDED, &tid);
                if(thread==NULL) {
-                       LeaveCriticalSection (this->synch_cs);
+                       LeaveCriticalSection (internal->synch_cs);
                        mono_threads_lock ();
                        mono_g_hash_table_remove (threads_starting_up, this);
                        mono_threads_unlock ();
@@ -948,9 +1115,9 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
                        return(NULL);
                }
                
-               this->handle=thread;
-               this->tid=tid;
-               small_id_alloc (this);
+               internal->handle=thread;
+               internal->tid=tid;
+               small_id_alloc (internal);
 
                /* Don't call handle_store() here, delay it to Start.
                 * We can't join a thread (trying to will just block
@@ -960,39 +1127,38 @@ HANDLE ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this,
 
                mono_thread_start (this);
                
-               this->state &= ~ThreadState_Unstarted;
+               internal->state &= ~ThreadState_Unstarted;
 
                THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread));
 
-               LeaveCriticalSection (this->synch_cs);
+               LeaveCriticalSection (internal->synch_cs);
                return(thread);
        }
 }
 
-void ves_icall_System_Threading_Thread_Thread_init (MonoThread *this)
+void ves_icall_System_Threading_InternalThread_Thread_free_internal (MonoInternalThread *this, HANDLE thread)
 {
        MONO_ARCH_SAVE_REGS;
 
-       ensure_synch_cs_set (this);
-}
+       THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
 
-void ves_icall_System_Threading_Thread_Thread_free_internal (MonoThread *this,
-                                                            HANDLE thread)
-{
-       MONO_ARCH_SAVE_REGS;
+       if (thread)
+               CloseHandle (thread);
 
-       THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, thread));
+       if (this->synch_cs) {
+               DeleteCriticalSection (this->synch_cs);
+               g_free (this->synch_cs);
+               this->synch_cs = NULL;
+       }
 
-       CloseHandle (thread);
+       g_assert (!this->abort_exc && !this->abort_state_handle);
 
-       DeleteCriticalSection (this->synch_cs);
-       g_free (this->synch_cs);
-       this->synch_cs = NULL;
+       g_free (this->name);
 }
 
 static void mono_thread_start (MonoThread *thread)
 {
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *internal = thread->internal_thread;
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
 
@@ -1003,9 +1169,9 @@ static void mono_thread_start (MonoThread *thread)
        if (!handle_store (thread))
                return;
 
-       ResumeThread (thread->handle);
+       ResumeThread (internal->handle);
 
-       if(thread->start_notify!=NULL) {
+       if(internal->start_notify!=NULL) {
                /* Wait for the thread to set up its TLS data etc, so
                 * theres no potential race condition if someone tries
                 * to look up the data believing the thread has
@@ -1014,9 +1180,9 @@ static void mono_thread_start (MonoThread *thread)
 
                THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for thread %p (%"G_GSIZE_FORMAT") to start", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
 
-               WaitForSingleObjectEx (thread->start_notify, INFINITE, FALSE);
-               CloseHandle (thread->start_notify);
-               thread->start_notify = NULL;
+               WaitForSingleObjectEx (internal->start_notify, INFINITE, FALSE);
+               CloseHandle (internal->start_notify);
+               internal->start_notify = NULL;
        }
 
        THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Done launching thread %p (%"G_GSIZE_FORMAT")", __func__, GetCurrentThreadId (), thread, (gsize)thread->tid));
@@ -1024,9 +1190,8 @@ static void mono_thread_start (MonoThread *thread)
 
 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
 {
-       MonoThread *thread = mono_thread_current ();
-       
-       MONO_ARCH_SAVE_REGS;
+       guint32 res;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
 
@@ -1034,21 +1199,18 @@ void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
        
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
-       SleepEx(ms,TRUE);
+       res = SleepEx(ms,TRUE);
        
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
+
+       if (res == WAIT_IO_COMPLETION) { /* we might have been interrupted */
+               MonoException* exc = mono_thread_execute_interruption (thread);
+               if (exc) mono_raise_exception (exc);
+       }
 }
 
-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
@@ -1060,7 +1222,7 @@ ves_icall_System_Threading_Thread_GetDomainID (void)
 }
 
 MonoString* 
-ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
+ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj)
 {
        MonoString* str;
 
@@ -1079,7 +1241,7 @@ ves_icall_System_Threading_Thread_GetName_internal (MonoThread *this_obj)
 }
 
 void 
-ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoString *name)
+ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
 {
        ensure_synch_cs_set (this_obj);
        
@@ -1103,7 +1265,7 @@ ves_icall_System_Threading_Thread_SetName_internal (MonoThread *this_obj, MonoSt
 }
 
 static MonoObject*
-lookup_cached_culture (MonoThread *this, MonoDomain *domain, int start_idx)
+lookup_cached_culture (MonoInternalThread *this, MonoDomain *domain, int start_idx)
 {
        MonoObject *res;
        int i;
@@ -1120,35 +1282,44 @@ lookup_cached_culture (MonoThread *this, MonoDomain *domain, int start_idx)
        return NULL;
 }
 
-MonoObject*
-ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoThread *this)
+/* If the array is already in the requested domain, we just return it,
+   otherwise we return a copy in that domain. */
+static MonoArray*
+byte_array_to_domain (MonoArray *arr, MonoDomain *domain)
 {
-       return lookup_cached_culture (this, mono_domain_get (), CULTURES_START_IDX);
+       MonoArray *copy;
+
+       if (!arr)
+               return NULL;
+
+       if (mono_object_domain (arr) == domain)
+               return arr;
+
+       copy = mono_array_new (domain, mono_defaults.byte_class, arr->max_length);
+       memcpy (mono_array_addr (copy, guint8, 0), mono_array_addr (arr, guint8, 0), arr->max_length);
+       return copy;
 }
 
 MonoArray*
-ves_icall_System_Threading_Thread_GetSerializedCurrentCulture (MonoThread *this)
+ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr)
 {
-       MonoArray *res;
-
-       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;
-       }
+       return byte_array_to_domain (arr, mono_get_root_domain ());
+}
 
-       LeaveCriticalSection (this->synch_cs);
+MonoArray*
+ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr)
+{
+       return byte_array_to_domain (arr, mono_domain_get ());
+}
 
-       return res;
+MonoObject*
+ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this)
+{
+       return lookup_cached_culture (this, mono_domain_get (), CULTURES_START_IDX);
 }
 
 static void
-cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
+cache_culture (MonoInternalThread *this, MonoObject *culture, int start_idx)
 {
        int i;
        MonoDomain *domain = mono_domain_get ();
@@ -1161,7 +1332,7 @@ cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
        EnterCriticalSection (this->synch_cs);
        
        if (!this->cached_culture_info)
-               MONO_OBJECT_SETREF (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_cached (mono_get_root_domain (), 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);
@@ -1189,91 +1360,56 @@ cache_culture (MonoThread *this, MonoObject *culture, int start_idx)
 void
 ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this, MonoObject *culture)
 {
-       cache_culture (this, culture, CULTURES_START_IDX);
-}
-
-void
-ves_icall_System_Threading_Thread_SetSerializedCurrentCulture (MonoThread *this, MonoArray *arr)
-{
-       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));
-
-       LeaveCriticalSection (this->synch_cs);
+       MonoDomain *domain = mono_object_get_domain (&this->obj);
+       g_assert (domain == mono_domain_get ());
+       cache_culture (this->internal_thread, culture, CULTURES_START_IDX);
 }
 
-
 MonoObject*
-ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoThread *this)
+ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this)
 {
        return lookup_cached_culture (this, mono_domain_get (), UICULTURES_START_IDX);
 }
 
-MonoArray*
-ves_icall_System_Threading_Thread_GetSerializedCurrentUICulture (MonoThread *this)
-{
-       MonoArray *res;
-
-       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;
-       }
-
-       LeaveCriticalSection (this->synch_cs);
-
-       return res;
-}
-
 void
 ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this, MonoObject *culture)
 {
-       cache_culture (this, culture, UICULTURES_START_IDX);
+       MonoDomain *domain = mono_object_get_domain (&this->obj);
+       g_assert (domain == mono_domain_get ());
+       cache_culture (this->internal_thread, culture, UICULTURES_START_IDX);
 }
 
-void
-ves_icall_System_Threading_Thread_SetSerializedCurrentUICulture (MonoThread *this, MonoArray *arr)
+MonoThread *
+mono_thread_current (void)
 {
-       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));
+       MonoDomain *domain = mono_domain_get ();
+       MonoInternalThread *internal = mono_thread_internal_current ();
+       MonoThread **current_thread_ptr;
 
-       LeaveCriticalSection (this->synch_cs);
+       g_assert (internal);
+       current_thread_ptr = get_current_thread_ptr_for_domain (domain, internal);
+
+       if (!*current_thread_ptr) {
+               g_assert (domain != mono_get_root_domain ());
+               *current_thread_ptr = new_thread_with_internal (domain, internal);
+       }
+       return *current_thread_ptr;
 }
 
-/* the jit may read the compiled code of this function */
-MonoThread *
-mono_thread_current (void)
+MonoInternalThread*
+mono_thread_internal_current (void)
 {
-       THREAD_DEBUG (g_message ("%s: returning %p", __func__, GET_CURRENT_OBJECT ()));
-       return GET_CURRENT_OBJECT ();
+       MonoInternalThread *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,
+gboolean ves_icall_System_Threading_Thread_Join_internal(MonoInternalThread *this,
                                                         int ms, HANDLE thread)
 {
-       MonoThread *cur_thread = mono_thread_current ();
+       MonoInternalThread *cur_thread = mono_thread_internal_current ();
        gboolean ret;
-       
-       MONO_ARCH_SAVE_REGS;
-       
+
        mono_thread_current_check_pending_interrupt ();
 
        ensure_synch_cs_set (this);
@@ -1319,9 +1455,7 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
        guint32 ret;
        guint32 i;
        MonoObject *waitHandle;
-       MonoThread *thread = mono_thread_current ();
-               
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* Do this WaitSleepJoin check before creating objects */
        mono_thread_current_check_pending_interrupt ();
@@ -1370,9 +1504,7 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
        guint32 ret;
        guint32 i;
        MonoObject *waitHandle;
-       MonoThread *thread = mono_thread_current ();
-               
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* Do this WaitSleepJoin check before creating objects */
        mono_thread_current_check_pending_interrupt ();
@@ -1417,9 +1549,7 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
 gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this, HANDLE handle, gint32 ms, gboolean exitContext)
 {
        guint32 ret;
-       MonoThread *thread = mono_thread_current ();
-       
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, GetCurrentThreadId (), handle, ms));
        
@@ -1451,6 +1581,28 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this
        return(TRUE);
 }
 
+gboolean
+ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, HANDLE toWait, gint32 ms, gboolean exitContext)
+{
+       guint32 ret;
+       MonoInternalThread *thread = mono_thread_internal_current ();
+
+       MONO_ARCH_SAVE_REGS;
+
+       if (ms == -1)
+               ms = INFINITE;
+
+       mono_thread_current_check_pending_interrupt ();
+
+       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
+       
+       ret = SignalObjectAndWait (toSignal, toWait, ms, TRUE);
+       
+       mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
+
+       return  (!(ret == WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED));
+}
+
 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
 { 
        HANDLE mutex;
@@ -1659,9 +1811,15 @@ gint32 ves_icall_System_Threading_Interlocked_Exchange_Int (gint32 *location, gi
 
 MonoObject * ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject **location, MonoObject *value)
 {
-       MONO_ARCH_SAVE_REGS;
+       MonoObject *res;
+       res = (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
+       mono_gc_wbarrier_generic_nostore (location);
+       return res;
+}
 
-       return (MonoObject *) InterlockedExchangePointer((gpointer *) location, value);
+gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr (gpointer *location, gpointer value)
+{
+       return InterlockedExchangePointer(location, value);
 }
 
 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single (gfloat *location, gfloat value)
@@ -1732,9 +1890,15 @@ gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *locati
 
 MonoObject * ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject **location, MonoObject *value, MonoObject *comparand)
 {
-       MONO_ARCH_SAVE_REGS;
+       MonoObject *res;
+       res = (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
+       mono_gc_wbarrier_generic_nostore (location);
+       return res;
+}
 
-       return (MonoObject *) InterlockedCompareExchangePointer((gpointer *) location, value, comparand);
+gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand)
+{
+       return InterlockedCompareExchangePointer(location, value, comparand);
 }
 
 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single (gfloat *location, gfloat value, gfloat comparand)
@@ -1795,17 +1959,19 @@ ves_icall_System_Threading_Interlocked_CompareExchange_Long (gint64 *location, g
 MonoObject*
 ves_icall_System_Threading_Interlocked_CompareExchange_T (MonoObject **location, MonoObject *value, MonoObject *comparand)
 {
-       MONO_ARCH_SAVE_REGS;
-
-       return InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
+       MonoObject *res;
+       res = InterlockedCompareExchangePointer ((gpointer *)location, value, comparand);
+       mono_gc_wbarrier_generic_nostore (location);
+       return res;
 }
 
 MonoObject*
 ves_icall_System_Threading_Interlocked_Exchange_T (MonoObject **location, MonoObject *value)
 {
-       MONO_ARCH_SAVE_REGS;
-
-       return InterlockedExchangePointer ((gpointer *)location, value);
+       MonoObject *res;
+       res = InterlockedExchangePointer ((gpointer *)location, value);
+       mono_gc_wbarrier_generic_nostore (location);
+       return res;
 }
 
 gint32 
@@ -1871,7 +2037,7 @@ ves_icall_System_Threading_Thread_MemoryBarrier (void)
 }
 
 void
-ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
+ves_icall_System_Threading_Thread_ClrState (MonoInternalThread* this, guint32 state)
 {
        mono_thread_clr_state (this, state);
 
@@ -1885,7 +2051,7 @@ ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
 }
 
 void
-ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
+ves_icall_System_Threading_Thread_SetState (MonoInternalThread* this, guint32 state)
 {
        mono_thread_set_state (this, state);
        
@@ -1899,7 +2065,7 @@ ves_icall_System_Threading_Thread_SetState (MonoThread* this, guint32 state)
 }
 
 guint32
-ves_icall_System_Threading_Thread_GetState (MonoThread* this)
+ves_icall_System_Threading_Thread_GetState (MonoInternalThread* this)
 {
        guint32 state;
 
@@ -1914,11 +2080,14 @@ ves_icall_System_Threading_Thread_GetState (MonoThread* this)
        return state;
 }
 
-void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
+void ves_icall_System_Threading_Thread_Interrupt_internal (MonoInternalThread *this)
 {
        gboolean throw = FALSE;
        
        ensure_synch_cs_set (this);
+
+       if (this == mono_thread_internal_current ())
+               return;
        
        EnterCriticalSection (this->synch_cs);
        
@@ -1937,9 +2106,11 @@ void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
 
 void mono_thread_current_check_pending_interrupt ()
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
        gboolean throw = FALSE;
 
+       mono_debugger_check_interruption ();
+
        ensure_synch_cs_set (thread);
        
        EnterCriticalSection (thread->synch_cs);
@@ -1963,7 +2134,11 @@ mono_thread_get_abort_signal (void)
        return -1;
 #else
 #ifndef        SIGRTMIN
+#ifdef SIGUSR1
        return SIGUSR1;
+#else
+       return -1;
+#endif
 #else
        static int abort_signum = -1;
        int i;
@@ -1998,9 +2173,9 @@ static void CALLBACK interruption_request_apc (ULONG_PTR param)
  * Tells the thread that his state has changed and it has to enter the new
  * state as soon as possible.
  */
-static void signal_thread_state_change (MonoThread *thread)
+static void signal_thread_state_change (MonoInternalThread *thread)
 {
-       if (thread == mono_thread_current ()) {
+       if (thread == mono_thread_internal_current ()) {
                /* Do it synchronously */
                MonoException *exc = mono_thread_request_interruption (FALSE); 
                if (exc)
@@ -2016,14 +2191,21 @@ 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 */
 }
 
 void
-ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
+ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state)
 {
-       MONO_ARCH_SAVE_REGS;
-
        ensure_synch_cs_set (thread);
        
        EnterCriticalSection (thread->synch_cs);
@@ -2043,15 +2225,24 @@ ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
        }
 
        thread->state |= ThreadState_AbortRequested;
-       MONO_OBJECT_SETREF (thread, abort_state, state);
+       if (thread->abort_state_handle)
+               mono_gchandle_free (thread->abort_state_handle);
+       if (state) {
+               thread->abort_state_handle = mono_gchandle_new (state, FALSE);
+               g_assert (thread->abort_state_handle);
+       } else {
+               thread->abort_state_handle = 0;
+       }
        thread->abort_exc = NULL;
 
        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);
 }
@@ -2059,9 +2250,7 @@ ves_icall_System_Threading_Thread_Abort (MonoThread *thread, MonoObject *state)
 void
 ves_icall_System_Threading_Thread_ResetAbort (void)
 {
-       MonoThread *thread = mono_thread_current ();
-
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        ensure_synch_cs_set (thread);
        
@@ -2075,17 +2264,142 @@ ves_icall_System_Threading_Thread_ResetAbort (void)
                mono_raise_exception (mono_get_exception_thread_state (msg));
        } else {
                thread->abort_exc = NULL;
-               thread->abort_state = NULL;
+               if (thread->abort_state_handle) {
+                       mono_gchandle_free (thread->abort_state_handle);
+                       /* This is actually not necessary - the handle
+                          only counts if the exception is set */
+                       thread->abort_state_handle = 0;
+               }
        }
        
        LeaveCriticalSection (thread->synch_cs);
 }
 
-static gboolean
-mono_thread_suspend (MonoThread *thread)
+static MonoObject*
+serialize_object (MonoObject *obj, gboolean *failure, MonoObject **exc)
 {
-       MONO_ARCH_SAVE_REGS;
+       static MonoMethod *serialize_method;
+
+       void *params [1];
+       MonoObject *array;
+
+       if (!serialize_method) {
+               MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting", "RemotingServices");
+               serialize_method = mono_class_get_method_from_name (klass, "SerializeCallData", -1);
+       }
+
+       if (!serialize_method) {
+               *failure = TRUE;
+               return NULL;
+       }
+
+       g_assert (!obj->vtable->klass->marshalbyref);
+
+       params [0] = obj;
+       *exc = NULL;
+       array = mono_runtime_invoke (serialize_method, NULL, params, exc);
+       if (*exc)
+               *failure = TRUE;
+
+       return array;
+}
+
+static MonoObject*
+deserialize_object (MonoObject *obj, gboolean *failure, MonoObject **exc)
+{
+       static MonoMethod *deserialize_method;
 
+       void *params [1];
+       MonoObject *result;
+
+       if (!deserialize_method) {
+               MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting", "RemotingServices");
+               deserialize_method = mono_class_get_method_from_name (klass, "DeserializeCallData", -1);
+       }
+       if (!deserialize_method) {
+               *failure = TRUE;
+               return NULL;
+       }
+
+       params [0] = obj;
+       *exc = NULL;
+       result = mono_runtime_invoke (deserialize_method, NULL, params, exc);
+       if (*exc)
+               *failure = TRUE;
+
+       return result;
+}
+
+static MonoObject*
+make_transparent_proxy (MonoObject *obj, gboolean *failure, MonoObject **exc)
+{
+       static MonoMethod *get_proxy_method;
+
+       MonoDomain *domain = mono_domain_get ();
+       MonoRealProxy *real_proxy;
+       MonoReflectionType *reflection_type;
+       MonoTransparentProxy *transparent_proxy;
+
+       if (!get_proxy_method)
+               get_proxy_method = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
+
+       g_assert (obj->vtable->klass->marshalbyref);
+
+       real_proxy = (MonoRealProxy*) mono_object_new (domain, mono_defaults.real_proxy_class);
+       reflection_type = mono_type_get_object (domain, &obj->vtable->klass->byval_arg);
+
+       real_proxy->class_to_proxy = reflection_type;
+       real_proxy->unwrapped_server = obj;
+
+       *exc = NULL;
+       transparent_proxy = (MonoTransparentProxy*) mono_runtime_invoke (get_proxy_method, real_proxy, NULL, exc);
+       if (*exc)
+               *failure = TRUE;
+
+       return (MonoObject*) transparent_proxy;
+}
+
+MonoObject*
+ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *this)
+{
+       MonoInternalThread *thread = this->internal_thread;
+       MonoObject *state, *serialized, *deserialized = NULL, *exc;
+       MonoDomain *domain;
+       gboolean failure = FALSE;
+
+       if (!thread->abort_state_handle)
+               return NULL;
+
+       state = mono_gchandle_get_target (thread->abort_state_handle);
+       g_assert (state);
+
+       domain = mono_domain_get ();
+       if (state->vtable->domain == domain)
+               return state;
+
+       if (state->vtable->klass->marshalbyref) {
+               deserialized = make_transparent_proxy (state, &failure, &exc);
+       } else {
+               mono_domain_set_internal_with_options (state->vtable->domain, FALSE);
+               serialized = serialize_object (state, &failure, &exc);
+               mono_domain_set_internal_with_options (domain, FALSE);
+               if (!failure)
+                       deserialized = deserialize_object (serialized, &failure, &exc);
+       }
+
+       if (failure) {
+               MonoException *invalid_op_exc = mono_get_exception_invalid_operation ("Thread.ExceptionState cannot access an ExceptionState from a different AppDomain");
+               if (exc)
+                       MONO_OBJECT_SETREF (invalid_op_exc, inner_ex, exc);
+               mono_raise_exception (invalid_op_exc);
+       }
+
+       return deserialized;
+}
+
+static gboolean
+mono_thread_suspend (MonoInternalThread *thread)
+{
        ensure_synch_cs_set (thread);
        
        EnterCriticalSection (thread->synch_cs);
@@ -2115,17 +2429,15 @@ mono_thread_suspend (MonoThread *thread)
 }
 
 void
-ves_icall_System_Threading_Thread_Suspend (MonoThread *thread)
+ves_icall_System_Threading_Thread_Suspend (MonoInternalThread *thread)
 {
        if (!mono_thread_suspend (thread))
                mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
 }
 
 static gboolean
-mono_thread_resume (MonoThread *thread)
+mono_thread_resume (MonoInternalThread *thread)
 {
-       MONO_ARCH_SAVE_REGS;
-
        ensure_synch_cs_set (thread);
        
        EnterCriticalSection (thread->synch_cs);
@@ -2167,7 +2479,7 @@ mono_thread_resume (MonoThread *thread)
 void
 ves_icall_System_Threading_Thread_Resume (MonoThread *thread)
 {
-       if (!mono_thread_resume (thread))
+       if (!thread->internal_thread || !mono_thread_resume (thread->internal_thread))
                mono_raise_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
 }
 
@@ -2195,7 +2507,7 @@ is_running_protected_wrapper (void)
        return found;
 }
 
-void mono_thread_stop (MonoThread *thread)
+void mono_thread_internal_stop (MonoInternalThread *thread)
 {
        ensure_synch_cs_set (thread);
        
@@ -2219,6 +2531,11 @@ void mono_thread_stop (MonoThread *thread)
        signal_thread_state_change (thread);
 }
 
+void mono_thread_stop (MonoThread *thread)
+{
+       mono_thread_internal_stop (thread->internal_thread);
+}
+
 gint8
 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr)
 {
@@ -2279,6 +2596,12 @@ ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value)
        *((volatile void **) ptr) = value;
 }
 
+void
+ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, void *value)
+{
+       mono_gc_wbarrier_generic_store (ptr, value);
+}
+
 void mono_thread_init (MonoThreadStartCB start_cb,
                       MonoThreadAttachCB attach_cb)
 {
@@ -2313,6 +2636,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
@@ -2341,6 +2666,7 @@ void mono_thread_cleanup (void)
 #endif
 
        g_array_free (delayed_free_table, TRUE);
+       delayed_free_table = NULL;
 
        TlsFree (current_object_key);
 }
@@ -2354,7 +2680,7 @@ mono_threads_install_cleanup (MonoThreadCleanupFunc func)
 void
 mono_thread_set_manage_callback (MonoThread *thread, MonoThreadManageCallback func)
 {
-       thread->manage_callback = func;
+       thread->internal_thread->manage_callback = func;
 }
 
 void mono_threads_install_notify_pending_exc (MonoThreadNotifyPendingExcFunc func)
@@ -2377,7 +2703,7 @@ static void print_tids (gpointer key, gpointer value, gpointer user)
 struct wait_data 
 {
        HANDLE handles[MAXIMUM_WAIT_OBJECTS];
-       MonoThread *threads[MAXIMUM_WAIT_OBJECTS];
+       MonoInternalThread *threads[MAXIMUM_WAIT_OBJECTS];
        guint32 num;
 };
 
@@ -2474,7 +2800,7 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
 
        if(wait->num<MAXIMUM_WAIT_OBJECTS) {
                HANDLE handle;
-               MonoThread *thread=(MonoThread *)value;
+               MonoInternalThread *thread=(MonoInternalThread *)value;
 
                /* Ignore background threads, we abort them later */
                /* Do not lock here since it is not needed and the caller holds threads_lock */
@@ -2483,17 +2809,17 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
                        return; /* just leave, ignore */
                }
                
-               if (mono_gc_is_finalizer_thread (thread)) {
+               if (mono_gc_is_finalizer_internal_thread (thread)) {
                        THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
                        return;
                }
 
-               if (thread == mono_thread_current ()) {
+               if (thread == mono_thread_internal_current ()) {
                        THREAD_DEBUG (g_message ("%s: ignoring current thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
                        return;
                }
 
-               if (thread == mono_thread_get_main ()) {
+               if (mono_thread_get_main () && (thread == mono_thread_get_main ()->internal_thread)) {
                        THREAD_DEBUG (g_message ("%s: ignoring main thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
                        return;
                }
@@ -2505,7 +2831,7 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
                }
                
                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)) {
+               if ((thread->manage_callback == NULL) || (thread->manage_callback (thread->root_domain_thread) == TRUE)) {
                        wait->handles[wait->num]=handle;
                        wait->threads[wait->num]=thread;
                        wait->num++;
@@ -2528,7 +2854,7 @@ remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
 {
        struct wait_data *wait=(struct wait_data *)user;
        gsize self = GetCurrentThreadId ();
-       MonoThread *thread = (MonoThread *) value;
+       MonoInternalThread *thread = value;
        HANDLE handle;
 
        if (wait->num >= MAXIMUM_WAIT_OBJECTS)
@@ -2547,15 +2873,13 @@ remove_and_abort_threads (gpointer key, gpointer value, gpointer user)
                wait->num++;
 
                THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
-               mono_thread_stop (thread);
+               mono_thread_internal_stop (thread);
                return TRUE;
        }
 
-       return (thread->tid != self && !mono_gc_is_finalizer_thread (thread)); 
+       return (thread->tid != self && !mono_gc_is_finalizer_internal_thread (thread)); 
 }
 
-static MonoException* mono_thread_execute_interruption (MonoThread *thread);
-
 /** 
  * mono_threads_set_shutting_down:
  *
@@ -2566,7 +2890,7 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread);
 void
 mono_threads_set_shutting_down (void)
 {
-       MonoThread *current_thread = mono_thread_current ();
+       MonoInternalThread *current_thread = mono_thread_internal_current ();
 
        mono_threads_lock ();
 
@@ -2628,6 +2952,7 @@ void mono_thread_manage (void)
        if(threads==NULL) {
                THREAD_DEBUG (g_message("%s: No threads", __func__));
                mono_threads_unlock ();
+               g_free (wait);
                return;
        }
        mono_threads_unlock ();
@@ -2695,7 +3020,7 @@ void mono_thread_manage (void)
 
 static void terminate_thread (gpointer key, gpointer value, gpointer user)
 {
-       MonoThread *thread=(MonoThread *)value;
+       MonoInternalThread *thread=(MonoInternalThread *)value;
        
        if(thread->tid != (gsize)user) {
                /*TerminateThread (thread->handle, -1);*/
@@ -2719,7 +3044,7 @@ void mono_thread_abort_all_other_threads (void)
 static void
 collect_threads_for_suspend (gpointer key, gpointer value, gpointer user_data)
 {
-       MonoThread *thread = (MonoThread*)value;
+       MonoInternalThread *thread = (MonoInternalThread*)value;
        struct wait_data *wait = (struct wait_data*)user_data;
        HANDLE handle;
 
@@ -2792,9 +3117,10 @@ void mono_thread_suspend_all_other_threads (void)
                eventidx = 0;
                /* Get the suspended events that we'll be waiting for */
                for (i = 0; i < wait->num; ++i) {
-                       MonoThread *thread = wait->threads [i];
+                       MonoInternalThread *thread = wait->threads [i];
+                       gboolean signal_suspend = FALSE;
 
-                       if ((thread->tid == self) || mono_gc_is_finalizer_thread (thread)) {
+                       if ((thread->tid == self) || mono_gc_is_finalizer_internal_thread (thread)) {
                                //CloseHandle (wait->handles [i]);
                                wait->threads [i] = NULL; /* ignore this thread in next loop */
                                continue;
@@ -2804,8 +3130,16 @@ void mono_thread_suspend_all_other_threads (void)
                
                        EnterCriticalSection (thread->synch_cs);
 
+                       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 */
+                                       LeaveCriticalSection (thread->synch_cs);
+                                       continue;
+                               }
+                       }
+
                        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);
@@ -2814,32 +3148,28 @@ void mono_thread_suspend_all_other_threads (void)
                                continue;
                        }
 
+                       if ((thread->state & ThreadState_SuspendRequested) == 0)
+                               signal_suspend = TRUE;
+
+                       events [eventidx++] = thread->suspended_event;
+
                        /* Convert abort requests into suspend requests */
                        if ((thread->state & ThreadState_AbortRequested) != 0)
                                thread->state &= ~ThreadState_AbortRequested;
                        
                        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 */
-                                       LeaveCriticalSection (thread->synch_cs);
-                                       continue;
-                               }
-                       }
-
-                       events [eventidx++] = thread->suspended_event;
                        LeaveCriticalSection (thread->synch_cs);
 
                        /* Signal the thread to suspend */
-                       signal_thread_state_change (thread);
+                       if (signal_suspend)
+                               signal_thread_state_change (thread);
                }
 
                if (eventidx > 0) {
                        WaitForMultipleObjectsEx (eventidx, events, TRUE, 100, FALSE);
                        for (i = 0; i < wait->num; ++i) {
-                               MonoThread *thread = wait->threads [i];
+                               MonoInternalThread *thread = wait->threads [i];
 
                                if (thread == NULL)
                                        continue;
@@ -2860,7 +3190,10 @@ void mono_thread_suspend_all_other_threads (void)
                         * FIXME: The finalizer thread can still create new threads.
                         */
                        mono_threads_lock ();
-                       starting = mono_g_hash_table_size (threads_starting_up) > 0;
+                       if (threads_starting_up)
+                               starting = mono_g_hash_table_size (threads_starting_up) > 0;
+                       else
+                               starting = FALSE;
                        mono_threads_unlock ();
                        if (starting)
                                Sleep (100);
@@ -2877,7 +3210,7 @@ void mono_thread_suspend_all_other_threads (void)
 static void
 collect_threads (gpointer key, gpointer value, gpointer user_data)
 {
-       MonoThread *thread = (MonoThread*)value;
+       MonoInternalThread *thread = (MonoInternalThread*)value;
        struct wait_data *wait = (struct wait_data*)user_data;
        HANDLE handle;
 
@@ -2912,9 +3245,11 @@ mono_threads_request_thread_dump (void)
        mono_threads_unlock ();
 
        for (i = 0; i < wait->num; ++i) {
-               MonoThread *thread = wait->threads [i];
+               MonoInternalThread *thread = wait->threads [i];
 
-               if (!mono_gc_is_finalizer_thread (thread) && (thread != mono_thread_current ()) && !thread->thread_dump_requested) {
+               if (!mono_gc_is_finalizer_internal_thread (thread) &&
+                               (thread != mono_thread_internal_current ()) &&
+                               !thread->thread_dump_requested) {
                        thread->thread_dump_requested = TRUE;
 
                        signal_thread_state_change (thread);
@@ -2934,7 +3269,7 @@ mono_threads_request_thread_dump (void)
 void 
 mono_thread_push_appdomain_ref (MonoDomain *domain)
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        if (thread) {
                /* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
@@ -2947,7 +3282,7 @@ mono_thread_push_appdomain_ref (MonoDomain *domain)
 void
 mono_thread_pop_appdomain_ref (void)
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        if (thread) {
                /* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
@@ -2960,7 +3295,7 @@ mono_thread_pop_appdomain_ref (void)
 }
 
 gboolean
-mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
+mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain)
 {
        gboolean res;
        mono_threads_lock ();
@@ -2969,23 +3304,27 @@ mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
        return res;
 }
 
+gboolean
+mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
+{
+       return mono_thread_internal_has_appdomain_ref (thread->internal_thread, domain);
+}
+
 typedef struct abort_appdomain_data {
        struct wait_data wait;
        MonoDomain *domain;
 } abort_appdomain_data;
 
 static void
-abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
+collect_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
 {
-       MonoThread *thread = (MonoThread*)value;
+       MonoInternalThread *thread = (MonoInternalThread*)value;
        abort_appdomain_data *data = (abort_appdomain_data*)user_data;
        MonoDomain *domain = data->domain;
 
-       if (mono_thread_has_appdomain_ref (thread, domain)) {
+       if (mono_thread_internal_has_appdomain_ref (thread, domain)) {
                /* printf ("ABORTING THREAD %p BECAUSE IT REFERENCES DOMAIN %s.\n", thread->tid, domain->friendly_name); */
 
-               ves_icall_System_Threading_Thread_Abort (thread, NULL);
-
                if(data->wait.num<MAXIMUM_WAIT_OBJECTS) {
                        HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
                        if (handle == NULL)
@@ -3011,6 +3350,8 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
 {
        abort_appdomain_data user_data;
        guint32 start_time;
+       int orig_timeout = timeout;
+       int i;
 
        THREAD_DEBUG (g_message ("%s: starting abort", __func__));
 
@@ -3020,21 +3361,27 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
 
                user_data.domain = domain;
                user_data.wait.num = 0;
-               mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
+               /* This shouldn't take any locks */
+               mono_g_hash_table_foreach (threads, collect_appdomain_thread, &user_data);
                mono_threads_unlock ();
 
-               if (user_data.wait.num > 0)
+               if (user_data.wait.num > 0) {
+                       /* Abort the threads outside the threads lock */
+                       for (i = 0; i < user_data.wait.num; ++i)
+                               ves_icall_System_Threading_Thread_Abort (user_data.wait.threads [i], NULL);
+
                        /*
                         * We should wait for the threads either to abort, or to leave the
                         * domain. We can't do the latter, so we wait with a timeout.
                         */
                        wait_for_tids (&user_data.wait, 100);
+               }
 
                /* Update remaining time */
                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);
@@ -3047,7 +3394,7 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
 static void
 clear_cached_culture (gpointer key, gpointer value, gpointer user_data)
 {
-       MonoThread *thread = (MonoThread*)value;
+       MonoInternalThread *thread = (MonoInternalThread*)value;
        MonoDomain *domain = (MonoDomain*)user_data;
        int i;
 
@@ -3086,9 +3433,7 @@ mono_threads_clear_cached_culture (MonoDomain *domain)
 MonoException*
 mono_thread_get_undeniable_exception (void)
 {
-       MonoThread *thread = mono_thread_current ();
-
-       MONO_ARCH_SAVE_REGS;
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
                /*
@@ -3182,7 +3527,7 @@ mono_alloc_static_data_slot (StaticDataInfo *static_data, guint32 size, guint32
  * This function is called when a thread is created or on thread attach.
  */
 static void
-thread_adjust_static_data (MonoThread *thread)
+thread_adjust_static_data (MonoInternalThread *thread)
 {
        guint32 offset;
 
@@ -3198,9 +3543,9 @@ thread_adjust_static_data (MonoThread *thread)
 static void 
 alloc_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
 {
-       MonoThread *thread = value;
+       MonoInternalThread *thread = value;
        guint32 offset = GPOINTER_TO_UINT (user);
-       
+
        mono_alloc_static_data (&(thread->static_data), offset);
 }
 
@@ -3273,13 +3618,9 @@ mono_get_special_static_data (guint32 offset)
        offset &= 0x7fffffff;
        idx = (offset >> 24) - 1;
 
-       if (static_type == 0)
-       {
-               MonoThread *thread = mono_thread_current ();
-               return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
-       }
-       else
-       {
+       if (static_type == 0) {
+               return get_thread_static_data (mono_thread_internal_current (), offset);
+       } else {
                /* Allocate static data block under demand, since we don't have a list
                // of contexts
                */
@@ -3301,7 +3642,7 @@ typedef struct {
 static void 
 free_thread_static_data_helper (gpointer key, gpointer value, gpointer user)
 {
-       MonoThread *thread = value;
+       MonoInternalThread *thread = value;
        TlsOffsetSize *data = user;
        int idx = (data->offset >> 24) - 1;
        char *ptr;
@@ -3360,7 +3701,7 @@ static void
 clear_local_slot (gpointer key, gpointer value, gpointer user_data)
 {
        LocalSlotID *sid = user_data;
-       MonoThread *thread = (MonoThread*)value;
+       MonoInternalThread *thread = (MonoInternalThread*)value;
        MonoArray *slots_array;
        /*
         * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
@@ -3427,23 +3768,32 @@ static guint32 dummy_apc (gpointer param)
  * Performs the operation that the requested thread state requires (abort,
  * suspend or stop)
  */
-static MonoException* mono_thread_execute_interruption (MonoThread *thread)
+static MonoException* mono_thread_execute_interruption (MonoInternalThread *thread)
 {
        ensure_synch_cs_set (thread);
        
        EnterCriticalSection (thread->synch_cs);
 
-       if (thread->interruption_requested) {
+       /* MonoThread::interruption_requested can only be changed with atomics */
+       if (InterlockedCompareExchange (&thread->interruption_requested, FALSE, TRUE)) {
                /* 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 ());
                LeaveCriticalSection (thread->synch_cs);
+               if (thread->abort_exc == NULL) {
+                       /* 
+                        * This might be racy, but it has to be called outside the lock
+                        * since it calls managed code.
+                        */
+                       MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
+               }
                return thread->abort_exc;
        }
        else if ((thread->state & ThreadState_SuspendRequested) != 0) {
@@ -3491,6 +3841,7 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
                return NULL;
        } else if (thread->thread_interrupt_requested) {
 
+               thread->thread_interrupt_requested = FALSE;
                LeaveCriticalSection (thread->synch_cs);
                
                return(mono_get_exception_thread_interrupted ());
@@ -3509,24 +3860,24 @@ 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 ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* The thread may already be stopping */
        if (thread == NULL) 
                return NULL;
-       
-       ensure_synch_cs_set (thread);
 
-       /* FIXME: This is NOT signal safe */
-       EnterCriticalSection (thread->synch_cs);
+#ifdef PLATFORM_WIN32
+       if (thread->interrupt_on_stop && 
+               thread->state & ThreadState_StopRequested && 
+               thread->state & ThreadState_Background)
+               ExitThread (1);
+#endif
        
-       if (thread->interruption_requested) {
-               LeaveCriticalSection (thread->synch_cs);
-               
+       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
@@ -3534,9 +3885,6 @@ MonoException* mono_thread_request_interruption (gboolean running_managed)
                   checked and the thread will be interrupted. */
                
                InterlockedIncrement (&thread_interruption_requested);
-               thread->interruption_requested = TRUE;
-
-               LeaveCriticalSection (thread->synch_cs);
 
                if (mono_thread_notify_pending_exc_fn && !running_managed)
                        /* The JIT will notify the thread about the interruption */
@@ -3550,8 +3898,6 @@ MonoException* mono_thread_request_interruption (gboolean running_managed)
                return NULL;
        }
        else {
-               LeaveCriticalSection (thread->synch_cs);
-               
                return mono_thread_execute_interruption (thread);
        }
 }
@@ -3559,7 +3905,7 @@ MonoException* mono_thread_request_interruption (gboolean running_managed)
 gboolean mono_thread_interruption_requested ()
 {
        if (thread_interruption_requested) {
-               MonoThread *thread = mono_thread_current ();
+               MonoInternalThread *thread = mono_thread_internal_current ();
                /* The thread may already be stopping */
                if (thread != NULL) 
                        return (thread->interruption_requested);
@@ -3569,12 +3915,14 @@ gboolean mono_thread_interruption_requested ()
 
 static void mono_thread_interruption_checkpoint_request (gboolean bypass_abort_protection)
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* The thread may already be stopping */
        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);
@@ -3606,7 +3954,7 @@ void mono_thread_force_interruption_checkpoint ()
 MonoException*
 mono_thread_get_and_clear_pending_exception (void)
 {
-       MonoThread *thread = mono_thread_current ();
+       MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* The thread may already be stopping */
        if (thread == NULL)
@@ -3615,8 +3963,44 @@ mono_thread_get_and_clear_pending_exception (void)
        if (thread->interruption_requested && !is_running_protected_wrapper ()) {
                return mono_thread_execute_interruption (thread);
        }
-       else
-               return NULL;
+       
+       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)
+{
+       MonoInternalThread *thread = mono_thread_internal_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);
+       }
 }
 
 /**
@@ -3636,10 +4020,9 @@ gint32* mono_thread_interruption_request_flag ()
 void 
 mono_thread_init_apartment_state (void)
 {
-       MonoThread* thread;
-       thread = mono_thread_current ();
-
 #ifdef PLATFORM_WIN32
+       MonoInternalThread* thread = mono_thread_internal_current ();
+
        /* Positive return value indicates success, either
         * S_OK if this is first CoInitialize call, or
         * S_FALSE if CoInitialize already called, but with same
@@ -3658,8 +4041,7 @@ void
 mono_thread_cleanup_apartment_state (void)
 {
 #ifdef PLATFORM_WIN32
-       MonoThread* thread;
-       thread = mono_thread_current ();
+       MonoInternalThread* thread = mono_thread_internal_current ();
 
        if (thread && thread->apartment_state != ThreadApartmentState_Unknown) {
                CoUninitialize ();
@@ -3668,7 +4050,7 @@ mono_thread_cleanup_apartment_state (void)
 }
 
 void
-mono_thread_set_state (MonoThread *thread, MonoThreadState state)
+mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state)
 {
        ensure_synch_cs_set (thread);
        
@@ -3678,7 +4060,7 @@ mono_thread_set_state (MonoThread *thread, MonoThreadState state)
 }
 
 void
-mono_thread_clr_state (MonoThread *thread, MonoThreadState state)
+mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state)
 {
        ensure_synch_cs_set (thread);
        
@@ -3688,7 +4070,7 @@ mono_thread_clr_state (MonoThread *thread, MonoThreadState state)
 }
 
 gboolean
-mono_thread_test_state (MonoThread *thread, MonoThreadState test)
+mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test)
 {
        gboolean ret = FALSE;
 
@@ -3704,3 +4086,53 @@ mono_thread_test_state (MonoThread *thread, MonoThreadState test)
        
        return ret;
 }
+
+static MonoClassField *execution_context_field;
+
+static MonoObject**
+get_execution_context_addr (void)
+{
+       MonoDomain *domain = mono_domain_get ();
+       guint32 offset;
+
+       if (!execution_context_field) {
+               execution_context_field = mono_class_get_field_from_name (mono_defaults.thread_class,
+                               "_ec");
+               g_assert (execution_context_field);
+       }
+
+       g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
+
+       mono_domain_lock (domain);
+       offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, execution_context_field));
+       mono_domain_unlock (domain);
+       g_assert (offset);
+
+       return (MonoObject**) mono_get_special_static_data (offset);
+}
+
+MonoObject*
+mono_thread_get_execution_context (void)
+{
+       return *get_execution_context_addr ();
+}
+
+void
+mono_thread_set_execution_context (MonoObject *ec)
+{
+       *get_execution_context_addr () = ec;
+}
+
+static gboolean has_tls_get = FALSE;
+
+void
+mono_runtime_set_has_tls_get (gboolean val)
+{
+       has_tls_get = val;
+}
+
+gboolean
+mono_runtime_has_tls_get (void)
+{
+       return has_tls_get;
+}