Revert "[coop] Remove mono_raise_exception from threads.c"
authorRodrigo Kumpera <kumpera@gmail.com>
Sun, 22 May 2016 23:09:43 +0000 (16:09 -0700)
committerRodrigo Kumpera <kumpera@gmail.com>
Sun, 22 May 2016 23:09:43 +0000 (16:09 -0700)
mono/metadata/monitor.c
mono/metadata/threads-types.h
mono/metadata/threads.c
mono/metadata/threads.h
mono/mini/debugger-agent.c

index b9a305121641975286467b84392ceba8856a4111..6cf9691777ea783631f39c7cac24c239cd180a7f 100644 (file)
@@ -1250,8 +1250,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
        mon = lock_word_get_inflated_lock (lw);
 
        /* Do this WaitSleepJoin check before creating the event handle */
-       if (mono_thread_current_check_pending_interrupt ())
-               return FALSE;
+       mono_thread_current_check_pending_interrupt ();
        
        event = CreateEvent (NULL, FALSE, FALSE, NULL);
        if (event == NULL) {
@@ -1261,11 +1260,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
        
        LOCK_DEBUG (g_message ("%s: (%d) queuing handle %p", __func__, mono_thread_info_get_small_id (), event));
 
-       /* This looks superfluous */
-       if (mono_thread_current_check_pending_interrupt ()) {
-               CloseHandle (event);
-               return FALSE;
-       }
+       mono_thread_current_check_pending_interrupt ();
        
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
 
index 57d76b425478e79953f9ce11feac035bc8939967..3face85f7794f472c4cb1a4e93ba1e3d05173911 100644 (file)
@@ -193,7 +193,6 @@ MonoInternalThread *mono_thread_internal_current (void);
 void mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread);
 
 void mono_thread_internal_stop (MonoInternalThread *thread);
-void mono_thread_internal_abort (MonoInternalThread *thread);
 
 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
 
@@ -202,7 +201,7 @@ void mono_thread_internal_reset_abort (MonoInternalThread *thread);
 void mono_thread_internal_unhandled_exception (MonoObject* exc);
 
 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
-gboolean mono_thread_current_check_pending_interrupt (void);
+void mono_thread_current_check_pending_interrupt (void);
 
 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
index e800c869241dbd4a2ebd69aee8ef2dfd2f157e32..be914f427e81390e4fbe6115fe001e12e0ca939d 100644 (file)
@@ -206,7 +206,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 (MonoInternalThread* thread);
 static void async_abort_internal (MonoInternalThread *thread, gboolean install_async_abort);
-static void self_abort_internal (MonoError *error);
+static void self_abort_internal (void);
 static void async_suspend_internal (MonoInternalThread *thread, gboolean interrupt);
 static void self_suspend_internal (void);
 
@@ -1237,8 +1237,7 @@ ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
 
        THREAD_DEBUG (g_message ("%s: Sleeping for %d ms", __func__, ms));
 
-       if (mono_thread_current_check_pending_interrupt ())
-               return;
+       mono_thread_current_check_pending_interrupt ();
 
        while (TRUE) {
                gboolean alerted = FALSE;
@@ -1252,8 +1251,7 @@ ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms)
                if (alerted) {
                        MonoException* exc = mono_thread_execute_interruption ();
                        if (exc) {
-                               mono_set_pending_exception (exc);
-                               return;
+                               mono_raise_exception (exc);
                        } else {
                                // FIXME: !INFINITE
                                if (ms != INFINITE)
@@ -1553,8 +1551,7 @@ ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms)
        MonoInternalThread *cur_thread = mono_thread_internal_current ();
        gboolean ret;
 
-       if (mono_thread_current_check_pending_interrupt ())
-               return FALSE;
+       mono_thread_current_check_pending_interrupt ();
 
        LOCK_THREAD (thread);
        
@@ -1591,17 +1588,8 @@ ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms)
        return(FALSE);
 }
 
-#define MANAGED_WAIT_FAILED 0x7fffffff
-
-static gint32
-map_native_wait_result_to_managed (gint32 val)
-{
-       /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
-       return val == WAIT_FAILED ? MANAGED_WAIT_FAILED : val;
-}
-
 static gint32
-mono_wait_uninterrupted (MonoInternalThread *thread, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, MonoError *error)
+mono_wait_uninterrupted (MonoInternalThread *thread, gboolean multiple, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, gboolean alertable)
 {
        MonoException *exc;
        guint32 ret;
@@ -1609,25 +1597,21 @@ mono_wait_uninterrupted (MonoInternalThread *thread, guint32 numhandles, gpointe
        gint32 diff_ms;
        gint32 wait = ms;
 
-       mono_error_init (error);
-
        start = (ms == -1) ? 0 : mono_100ns_ticks ();
        do {
                MONO_ENTER_GC_SAFE;
-               if (numhandles != 1)
-                       ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, TRUE);
+                       if (multiple)
+                       ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, alertable);
                else
-                       ret = WaitForSingleObjectEx (handles [0], ms, TRUE);
+                       ret = WaitForSingleObjectEx (handles [0], ms, alertable);
                MONO_EXIT_GC_SAFE;
 
                if (ret != WAIT_IO_COMPLETION)
                        break;
 
                exc = mono_thread_execute_interruption ();
-               if (exc) {
-                       mono_error_set_exception_instance (error, exc);
-                       break;
-               }
+               if (exc)
+                       mono_raise_exception (exc);
 
                if (ms == -1)
                        continue;
@@ -1646,7 +1630,6 @@ mono_wait_uninterrupted (MonoInternalThread *thread, guint32 numhandles, gpointe
 
 gint32 ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms)
 {
-       MonoError error;
        HANDLE *handles;
        guint32 numhandles;
        guint32 ret;
@@ -1655,8 +1638,7 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_ha
        MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* Do this WaitSleepJoin check before creating objects */
-       if (mono_thread_current_check_pending_interrupt ())
-               return map_native_wait_result_to_managed (WAIT_FAILED);
+       mono_thread_current_check_pending_interrupt ();
 
        /* We fail in managed if the array has more than 64 elements */
        numhandles = (guint32)mono_array_length(mono_handles);
@@ -1672,22 +1654,19 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_ha
        }
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
-
-       ret = mono_wait_uninterrupted (thread, numhandles, handles, TRUE, ms, &error);
+       
+       ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, TRUE, ms, TRUE);
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
        g_free(handles);
 
-       mono_error_set_pending_exception (&error);
-
        /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
-       return map_native_wait_result_to_managed (ret);
+       return ret == WAIT_FAILED ? 0x7fffffff : ret;
 }
 
 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms)
 {
-       MonoError error;
        HANDLE handles [MAXIMUM_WAIT_OBJECTS];
        uintptr_t numhandles;
        guint32 ret;
@@ -1696,12 +1675,11 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
        MonoInternalThread *thread = mono_thread_internal_current ();
 
        /* Do this WaitSleepJoin check before creating objects */
-       if (mono_thread_current_check_pending_interrupt ())
-               return map_native_wait_result_to_managed (WAIT_FAILED);
+       mono_thread_current_check_pending_interrupt ();
 
        numhandles = mono_array_length(mono_handles);
        if (numhandles > MAXIMUM_WAIT_OBJECTS)
-               return map_native_wait_result_to_managed (WAIT_FAILED);
+               return WAIT_FAILED;
 
        for(i = 0; i < numhandles; i++) {       
                waitHandle = mono_array_get(mono_handles, MonoObject*, i);
@@ -1714,31 +1692,29 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
 
-       ret = mono_wait_uninterrupted (thread, numhandles, handles, FALSE, ms, &error);
+       ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, FALSE, ms, TRUE);
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
        THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") returning %d", __func__, mono_native_thread_id_get (), ret));
 
-       mono_error_set_pending_exception (&error);
        /*
         * These need to be here.  See MSDN dos on WaitForMultipleObjects.
         */
        if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 + numhandles - 1) {
-               return map_native_wait_result_to_managed (ret - WAIT_OBJECT_0);
+               return ret - WAIT_OBJECT_0;
        }
        else if (ret >= WAIT_ABANDONED_0 && ret <= WAIT_ABANDONED_0 + numhandles - 1) {
-               return map_native_wait_result_to_managed (ret - WAIT_ABANDONED_0);
+               return ret - WAIT_ABANDONED_0;
        }
        else {
                /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
-               return map_native_wait_result_to_managed (ret);
+               return ret == WAIT_FAILED ? 0x7fffffff : ret;
        }
 }
 
 gint32 ves_icall_System_Threading_WaitHandle_WaitOne_internal(HANDLE handle, gint32 ms)
 {
-       MonoError error;
        guint32 ret;
        MonoInternalThread *thread = mono_thread_internal_current ();
 
@@ -1748,17 +1724,16 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitOne_internal(HANDLE handle, gin
                ms=INFINITE;
        }
        
-       if (mono_thread_current_check_pending_interrupt ())
-               return map_native_wait_result_to_managed (WAIT_FAILED);
+       mono_thread_current_check_pending_interrupt ();
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
-       ret = mono_wait_uninterrupted (thread, 1, &handle, FALSE, ms, &error);
+       ret = mono_wait_uninterrupted (thread, FALSE, 1, &handle, FALSE, ms, TRUE);
        
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
-
-       mono_error_set_pending_exception (&error);
-       return map_native_wait_result_to_managed (ret == WAIT_FAILED);
+       
+       /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
+       return ret == WAIT_FAILED ? 0x7fffffff : ret;
 }
 
 gint32
@@ -1770,8 +1745,7 @@ ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, H
        if (ms == -1)
                ms = INFINITE;
 
-       if (mono_thread_current_check_pending_interrupt ())
-               return map_native_wait_result_to_managed (WAIT_FAILED);
+       mono_thread_current_check_pending_interrupt ();
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
@@ -1781,7 +1755,8 @@ ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, H
        
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
-       return map_native_wait_result_to_managed (ret);
+       /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
+       return ret == WAIT_FAILED ? 0x7fffffff : ret;
 }
 
 HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
@@ -2212,15 +2187,7 @@ void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this_obj)
        }
 }
 
-/**
- * mono_thread_current_check_pending_interrupt:
- *
- * Checks if there's a interruption request and set the pending exception if so.
- *
- * @returns true if a pending exception was set
- */
-gboolean
-mono_thread_current_check_pending_interrupt (void)
+void mono_thread_current_check_pending_interrupt ()
 {
        MonoInternalThread *thread = mono_thread_internal_current ();
        gboolean throw_ = FALSE;
@@ -2234,13 +2201,13 @@ mono_thread_current_check_pending_interrupt (void)
        
        UNLOCK_THREAD (thread);
 
-       if (throw_)
-               mono_set_pending_exception (mono_get_exception_thread_interrupted ());
-       return throw_;
+       if (throw_) {
+               mono_raise_exception (mono_get_exception_thread_interrupted ());
+       }
 }
 
-static gboolean
-request_thread_abort (MonoInternalThread *thread, MonoObject *state)
+void
+ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state)
 {
        LOCK_THREAD (thread);
        
@@ -2249,13 +2216,13 @@ request_thread_abort (MonoInternalThread *thread, MonoObject *state)
                (thread->state & ThreadState_Stopped) != 0)
        {
                UNLOCK_THREAD (thread);
-               return FALSE;
+               return;
        }
 
        if ((thread->state & ThreadState_Unstarted) != 0) {
                thread->state |= ThreadState_Aborted;
                UNLOCK_THREAD (thread);
-               return FALSE;
+               return;
        }
 
        thread->state |= ThreadState_AbortRequested;
@@ -2277,39 +2244,11 @@ request_thread_abort (MonoInternalThread *thread, MonoObject *state)
                mono_thread_resume (thread);
 
        UNLOCK_THREAD (thread);
-       return TRUE;
-}
-
-void
-ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state)
-{
-       if (!request_thread_abort (thread, state))
-               return;
 
-       if (thread == mono_thread_internal_current ()) {
-               MonoError error;
-               self_abort_internal (&error);
-               mono_error_set_pending_exception (&error);
-       } else {
+       if (thread == mono_thread_internal_current ())
+               self_abort_internal ();
+       else
                async_abort_internal (thread, TRUE);
-       }
-}
-
-/**
- * mono_thread_internal_abort:
- *
- * Request thread @thread to be aborted.
- *
- * @thread MUST NOT be the current thread.
- */
-void
-mono_thread_internal_abort (MonoInternalThread *thread)
-{
-       g_assert (thread != mono_thread_internal_current ());
-
-       if (!request_thread_abort (thread, NULL))
-               return;
-       async_abort_internal (thread, TRUE);
 }
 
 void
@@ -2509,8 +2448,7 @@ is_running_protected_wrapper (void)
        return found;
 }
 
-static gboolean
-request_thread_stop (MonoInternalThread *thread)
+void mono_thread_internal_stop (MonoInternalThread *thread)
 {
        LOCK_THREAD (thread);
 
@@ -2518,7 +2456,7 @@ request_thread_stop (MonoInternalThread *thread)
                (thread->state & ThreadState_Stopped) != 0)
        {
                UNLOCK_THREAD (thread);
-               return FALSE;
+               return;
        }
        
        /* Make sure the thread is awake */
@@ -2528,45 +2466,16 @@ request_thread_stop (MonoInternalThread *thread)
        thread->state &= ~ThreadState_AbortRequested;
        
        UNLOCK_THREAD (thread);
-       return TRUE;
-}
-
-/**
- * mono_thread_internal_stop:
- *
- * Request thread @thread to stop.
- *
- * @thread MUST NOT be the current thread.
- */
-void
-mono_thread_internal_stop (MonoInternalThread *thread)
-{
-       g_assert (thread != mono_thread_internal_current ());
-
-       if (!request_thread_stop (thread))
-               return;
        
-       async_abort_internal (thread, TRUE);
+       if (thread == mono_thread_internal_current ())
+               self_abort_internal ();
+       else
+               async_abort_internal (thread, TRUE);
 }
 
 void mono_thread_stop (MonoThread *thread)
 {
-       MonoInternalThread *internal = thread->internal_thread;
-
-       if (!request_thread_stop (internal))
-               return;
-       
-       if (internal == mono_thread_internal_current ()) {
-               MonoError error;
-               self_abort_internal (&error);
-               /*
-               This function is part of the embeding API and has no way to return the exception
-               to be thrown. So what we do is keep the old behavior and raise the exception.
-               */
-               mono_error_raise_exception (&error);
-       } else {
-               async_abort_internal (internal, TRUE);
-       }
+       mono_thread_internal_stop (thread->internal_thread);
 }
 
 gint8
@@ -3914,7 +3823,7 @@ mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout)
                if (user_data.wait.num > 0) {
                        /* Abort the threads outside the threads lock */
                        for (i = 0; i < user_data.wait.num; ++i)
-                               mono_thread_internal_abort (user_data.wait.threads [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
@@ -4871,20 +4780,18 @@ async_abort_internal (MonoInternalThread *thread, gboolean install_async_abort)
 }
 
 static void
-self_abort_internal (MonoError *error)
+self_abort_internal (void)
 {
        MonoException *exc;
 
-       mono_error_init (error);
-
        /* FIXME this is insanely broken, it doesn't cause interruption to happen synchronously
         * since passing FALSE to mono_thread_request_interruption makes sure it returns NULL */
 
        exc = mono_thread_request_interruption (TRUE);
        if (exc)
-               mono_error_set_exception_instance (error, exc);
-       else
-               mono_thread_info_self_interrupt ();
+               mono_raise_exception (exc);
+
+       mono_thread_info_self_interrupt ();
 }
 
 typedef struct {
index b72072f3246ca5e69329de13afb80229699d5699..5a0c79c7cca732b1ffeff80292a6ad943d1114e5 100644 (file)
@@ -30,7 +30,7 @@ extern MONO_API MonoThread *mono_thread_current (void);
 extern MONO_API void        mono_thread_set_main (MonoThread *thread);
 extern MONO_API MonoThread *mono_thread_get_main (void);
 
-extern MONO_RT_EXTERNAL_ONLY MONO_API void mono_thread_stop (MonoThread *thread);
+extern MONO_API void mono_thread_stop (MonoThread *thread);
 
 extern MONO_API void mono_thread_new_init (intptr_t tid, void* stack_start,
                                  void* func);
index 21fa1d4fc8ffa6b08cb2a41c8fba72e525984030..60126395bc67b2d304de82b975dd777989858ccd 100644 (file)
@@ -2678,7 +2678,7 @@ notify_thread (gpointer key, gpointer value, gpointer user_data)
 
        DEBUG_PRINTF (1, "[%p] Interrupting %p...\n", (gpointer) (gsize) mono_native_thread_id_get (), (gpointer)tid);
 
-       /* This is _not_ equivalent to mono_thread_internal_abort () */
+       /* This is _not_ equivalent to ves_icall_System_Threading_Thread_Abort () */
        InterruptData interrupt_data = { 0 };
        interrupt_data.tls = tls;
 
@@ -6802,7 +6802,7 @@ invoke_method (void)
        /*
         * Take the loader lock to avoid race conditions with CMD_VM_ABORT_INVOKE:
         *
-        * It is possible that mono_thread_internal_abort () was called
+        * It is possible that ves_icall_System_Threading_Thread_Abort () was called
         * after the mono_runtime_invoke_checked() already returned, but it doesn't matter
         * because we reset the abort here.
         */
@@ -7114,7 +7114,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
 
                tls->abort_requested = TRUE;
 
-               mono_thread_internal_abort (THREAD_TO_INTERNAL (thread));
+               ves_icall_System_Threading_Thread_Abort (THREAD_TO_INTERNAL (thread), NULL);
                mono_loader_unlock ();
                break;
        }