New test.
[mono.git] / mono / metadata / threads.c
index 8ff6db17f7a3a3ee20ce2ae48bad30aeaec4bde7..dfbe2f24ddedc3857b43fbd83b7d4aba8faf9209 100644 (file)
@@ -144,6 +144,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)
@@ -600,6 +601,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 +614,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 +829,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);
 
@@ -852,6 +869,9 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
                
        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);
 
@@ -911,6 +931,9 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
                
        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);
 
@@ -971,6 +994,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);
@@ -1456,6 +1481,47 @@ 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)
 {
@@ -1637,6 +1703,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 +1841,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);
@@ -2173,8 +2244,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);
@@ -2686,6 +2763,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 +2790,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);