2007-04-28 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / threads.c
index 8ff6db17f7a3a3ee20ce2ae48bad30aeaec4bde7..733fe28fc845cbe34271dee495b07e44a4fca924 100644 (file)
@@ -84,11 +84,6 @@ typedef struct {
 #define CULTURES_START_IDX 0
 #define UICULTURES_START_IDX NUM_CACHED_CULTURES
 
-/*
- * The "os_handle" field of the WaitHandle class.
- */
-static MonoClassField *wait_handle_os_handle_field = NULL;
-
 /* Controls access to the 'threads' hash table */
 #define mono_threads_lock() EnterCriticalSection (&threads_mutex)
 #define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
@@ -144,6 +139,7 @@ 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 void mono_thread_start (MonoThread *thread);
+static void signal_thread_state_change (MonoThread *thread);
 
 /* Spin lock for InterlockedXXX 64 bit functions */
 #define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
@@ -266,10 +262,14 @@ static guint32 WINAPI start_wrapper(void *data)
        tid=thread->tid;
 
        SET_CURRENT_OBJECT (thread);
+
+       /* Every thread references the appdomain which created it */
+       mono_thread_push_appdomain_ref (start_info->domain);
        
        if (!mono_domain_set (start_info->domain, FALSE)) {
                /* No point in raising an appdomain_unloaded exception here */
                /* FIXME: Cleanup here */
+               mono_thread_pop_appdomain_ref ();
                return 0;
        }
 
@@ -298,9 +298,6 @@ static guint32 WINAPI start_wrapper(void *data)
        
        g_free (start_info);
 
-       /* Every thread references the appdomain which created it */
-       mono_thread_push_appdomain_ref (mono_domain_get ());
-
        thread_adjust_static_data (thread);
 #ifdef DEBUG
        g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
@@ -395,6 +392,43 @@ void mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
        ResumeThread (thread_handle);
 }
 
+/*
+ * 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.
+ */
+static void
+mono_thread_get_stack_bounds (guint8 **staddr, size_t *stsize)
+{
+#ifndef PLATFORM_WIN32
+       pthread_attr_t attr;
+       guint8 *current = (guint8*)&attr;
+
+       pthread_attr_init (&attr);
+#ifdef HAVE_PTHREAD_GETATTR_NP
+               pthread_getattr_np (pthread_self(), &attr);
+#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
+#endif
+}      
+
 MonoThread *
 mono_thread_attach (MonoDomain *domain)
 {
@@ -445,7 +479,15 @@ mono_thread_attach (MonoDomain *domain)
        thread_adjust_static_data (thread);
 
        if (mono_thread_attach_cb) {
-               mono_thread_attach_cb (tid, &tid);
+               guint8 *staddr;
+               size_t stsize;
+
+               mono_thread_get_stack_bounds (&staddr, &stsize);
+
+               if (staddr == NULL)
+                       mono_thread_attach_cb (tid, &tid);
+               else
+                       mono_thread_attach_cb (tid, staddr + stsize);
        }
 
        return(thread);
@@ -600,6 +642,8 @@ void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
 
        THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
 
+       mono_thread_current_check_pending_interrupt ();
+       
        mono_monitor_enter (thread->synch_lock);
        thread->state |= ThreadState_WaitSleepJoin;
        mono_monitor_exit (thread->synch_lock);
@@ -611,6 +655,18 @@ void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
        mono_monitor_exit (thread->synch_lock);
 }
 
+void ves_icall_System_Threading_Thread_SpinWait_internal (gint32 iterations)
+{
+       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
 ves_icall_System_Threading_Thread_GetDomainID (void) 
 {
@@ -814,6 +870,8 @@ gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this,
                return FALSE;
        }
        
+       mono_thread_current_check_pending_interrupt ();
+       
        this->state |= ThreadState_WaitSleepJoin;
        mono_monitor_exit (this->synch_lock);
 
@@ -847,23 +905,19 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
        guint32 ret;
        guint32 i;
        MonoObject *waitHandle;
-       MonoClass *klass;
        MonoThread *thread = mono_thread_current ();
                
        MONO_ARCH_SAVE_REGS;
 
+       /* Do this WaitSleepJoin check before creating objects */
+       mono_thread_current_check_pending_interrupt ();
+
        numhandles = mono_array_length(mono_handles);
        handles = g_new0(HANDLE, numhandles);
 
-       if (wait_handle_os_handle_field == 0) {
-               /* Get the field os_handle which will contain the actual handle */
-               klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");   
-               wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
-       }
-               
        for(i = 0; i < numhandles; i++) {       
-               waitHandle = mono_array_get(mono_handles, MonoObject*, i);              
-               mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
+               waitHandle = mono_array_get(mono_handles, MonoObject*, i);
+               handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
        }
        
        if(ms== -1) {
@@ -906,23 +960,19 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
        guint32 ret;
        guint32 i;
        MonoObject *waitHandle;
-       MonoClass *klass;
        MonoThread *thread = mono_thread_current ();
                
        MONO_ARCH_SAVE_REGS;
 
+       /* Do this WaitSleepJoin check before creating objects */
+       mono_thread_current_check_pending_interrupt ();
+
        numhandles = mono_array_length(mono_handles);
        handles = g_new0(HANDLE, numhandles);
 
-       if (wait_handle_os_handle_field == 0) {
-               /* Get the field os_handle which will contain the actual handle */
-               klass = mono_class_from_name(mono_defaults.corlib, "System.Threading", "WaitHandle");   
-               wait_handle_os_handle_field = mono_class_get_field_from_name(klass, "os_handle");
-       }
-               
        for(i = 0; i < numhandles; i++) {       
-               waitHandle = mono_array_get(mono_handles, MonoObject*, i);              
-               mono_field_get_value(waitHandle, wait_handle_os_handle_field, &handles[i]);
+               waitHandle = mono_array_get(mono_handles, MonoObject*, i);
+               handles [i] = mono_wait_handle_get_handle ((MonoWaitHandle *) waitHandle);
        }
        
        if(ms== -1) {
@@ -971,6 +1021,8 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this
                ms=INFINITE;
        }
        
+       mono_thread_current_check_pending_interrupt ();
+
        mono_monitor_enter (thread->synch_lock);
        thread->state |= ThreadState_WaitSleepJoin;
        mono_monitor_exit (thread->synch_lock);
@@ -1018,10 +1070,10 @@ HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned,
        return(mutex);
 }                                                                   
 
-void ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) { 
+MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle ) { 
        MONO_ARCH_SAVE_REGS;
 
-       ReleaseMutex(handle);
+       return(ReleaseMutex (handle));
 }
 
 HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name,
@@ -1412,8 +1464,8 @@ ves_icall_System_Threading_Interlocked_Read_Long (gint64 *location)
 void
 ves_icall_System_Threading_Thread_MemoryBarrier (void)
 {
-       /* Should be implemented as a JIT intrinsic */
-       mono_raise_exception (mono_get_exception_not_implemented (NULL));
+       mono_threads_lock ();
+       mono_threads_unlock ();
 }
 
 void
@@ -1456,10 +1508,51 @@ ves_icall_System_Threading_Thread_GetState (MonoThread* this)
        return state;
 }
 
+void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
+{
+       gboolean throw = FALSE;
+       
+       mono_monitor_enter (this->synch_lock);
+       
+       /* Clear out any previous request */
+       this->thread_interrupt_requested = FALSE;
+       
+       if (this->state & ThreadState_WaitSleepJoin) {
+               throw = TRUE;
+       } else {
+               this->thread_interrupt_requested = TRUE;
+       }
+       
+       mono_monitor_exit (this->synch_lock);
+
+       if (throw) {
+               signal_thread_state_change (this);
+       }
+}
+
+void mono_thread_current_check_pending_interrupt ()
+{
+       MonoThread *thread = mono_thread_current ();
+       gboolean throw = FALSE;
+       
+       mono_monitor_enter (thread->synch_lock);
+
+       if (thread->thread_interrupt_requested) {
+               throw = TRUE;
+               thread->thread_interrupt_requested = FALSE;
+       }
+       
+       mono_monitor_exit (thread->synch_lock);
+
+       if (throw) {
+               mono_raise_exception (mono_get_exception_thread_interrupted ());
+       }
+}
+
 int  
 mono_thread_get_abort_signal (void)
 {
-#if defined (__MINGW32__) || defined (_MSC_VER)
+#ifdef PLATFORM_WIN32
        return -1;
 #else
 #ifndef        SIGRTMIN
@@ -1481,16 +1574,16 @@ mono_thread_get_abort_signal (void)
        /* fallback to the old way */
        return SIGRTMIN;
 #endif
-#endif /*defined (__MINGW32__) || defined (_MSC_VER) */
+#endif /* PLATFORM_WIN32 */
 }
 
-#if defined (__MINGW32__) || defined (_MSC_VER)
+#ifdef PLATFORM_WIN32
 static void CALLBACK interruption_request_apc (ULONG_PTR param)
 {
        MonoException* exc = mono_thread_request_interruption (FALSE);
        if (exc) mono_raise_exception (exc);
 }
-#endif /* defined (__MINGW32__) || defined (_MSC_VER) */
+#endif /* PLATFORM_WIN32 */
 
 /*
  * signal_thread_state_change
@@ -1507,7 +1600,7 @@ static void signal_thread_state_change (MonoThread *thread)
                        mono_raise_exception (exc);
        }
 
-#if defined (__MINGW32__) || defined (_MSC_VER)
+#ifdef PLATFORM_WIN32
        QueueUserAPC ((PAPCFUNC)interruption_request_apc, thread->handle, NULL);
 #else
        /* fixme: store the state somewhere */
@@ -1516,7 +1609,7 @@ static void signal_thread_state_change (MonoThread *thread)
 #else
        pthread_kill (thread->tid, mono_thread_get_abort_signal ());
 #endif
-#endif /* defined (__MINGW32__) || defined (__MSC_VER) */
+#endif /* PLATFORM_WIN32 */
 }
 
 void
@@ -1637,6 +1730,10 @@ mono_thread_resume (MonoThread *thread)
        }
        
        thread->resume_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+       if (thread->resume_event == NULL) {
+               mono_monitor_exit (thread->synch_lock);
+               return(FALSE);
+       }
        
        /* Awake the thread */
        SetEvent (thread->suspend_event);
@@ -1771,6 +1868,7 @@ void mono_thread_init (MonoThreadStartCB start_cb,
        InitializeCriticalSection(&interlocked_mutex);
        InitializeCriticalSection(&contexts_mutex);
        background_change_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+       g_assert(background_change_event != NULL);
        
        mono_init_static_data_info (&thread_static_info);
        mono_init_static_data_info (&context_static_info);
@@ -1936,13 +2034,11 @@ static void build_wait_tids (gpointer key, gpointer value, gpointer user)
                MonoThread *thread=(MonoThread *)value;
 
                /* Ignore background threads, we abort them later */
-               mono_monitor_enter (thread->synch_lock);
+               /* Do not lock here since it is not needed and the caller holds threads_lock */
                if (thread->state & ThreadState_Background) {
                        THREAD_DEBUG (g_message ("%s: ignoring background thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
-                       mono_monitor_exit (thread->synch_lock);
                        return; /* just leave, ignore */
                }
-               mono_monitor_exit (thread->synch_lock);
                
                if (mono_gc_is_finalizer_thread (thread)) {
                        THREAD_DEBUG (g_message ("%s: ignoring finalizer thread %"G_GSIZE_FORMAT, __func__, (gsize)thread->tid));
@@ -2173,8 +2269,14 @@ void mono_thread_suspend_all_other_threads (void)
                        
                thread->state |= ThreadState_SuspendRequested;
 
-               if (thread->suspended_event == NULL)
+               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;
+                       }
+               }
 
                events [eventidx++] = thread->suspended_event;
                mono_monitor_exit (thread->synch_lock);
@@ -2290,15 +2392,14 @@ abort_appdomain_thread (gpointer key, gpointer value, gpointer user_data)
        MonoDomain *domain = data->domain;
 
        if (mono_thread_has_appdomain_ref (thread, domain)) {
-               HANDLE handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
-               if (handle == NULL)
-                       return;
-
                /* 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)
+                               return;
                        data->wait.handles [data->wait.num] = handle;
                        data->wait.threads [data->wait.num] = thread;
                        data->wait.num++;
@@ -2648,8 +2749,8 @@ mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
        }
 }
 
-#ifdef __MINGW32__
-static CALLBACK void dummy_apc (ULONG_PTR param)
+#ifdef PLATFORM_WIN32
+static void CALLBACK dummy_apc (ULONG_PTR param)
 {
 }
 #else
@@ -2686,6 +2787,10 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
                thread->state &= ~ThreadState_SuspendRequested;
                thread->state |= ThreadState_Suspended;
                thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+               if (thread->suspend_event == NULL) {
+                       mono_monitor_exit (thread->synch_lock);
+                       return(NULL);
+               }
                if (thread->suspended_event)
                        SetEvent (thread->suspended_event);
                mono_monitor_exit (thread->synch_lock);
@@ -2709,6 +2814,9 @@ static MonoException* mono_thread_execute_interruption (MonoThread *thread)
                mono_monitor_exit (thread->synch_lock);
                mono_thread_exit ();
                return NULL;
+       } else if (thread->thread_interrupt_requested) {
+               mono_monitor_exit (thread->synch_lock);
+               return(mono_get_exception_thread_interrupted ());
        }
        
        mono_monitor_exit (thread->synch_lock);