[io-layer] Extract wapi_thread_handle_set_exited
authorLudovic Henry <ludovic@xamarin.com>
Thu, 7 Jul 2016 13:59:16 +0000 (15:59 +0200)
committerLudovic Henry <ludovic@xamarin.com>
Thu, 4 Aug 2016 13:40:20 +0000 (15:40 +0200)
mono/io-layer/threads.h
mono/io-layer/wthreads.c
mono/metadata/threads.c
mono/utils/mono-threads-posix.c
mono/utils/mono-threads-windows.c
mono/utils/mono-threads.c
mono/utils/mono-threads.h

index 637aa70f39117b912a8e64ee76729aaaa351fd76..22bec550ce37793f798135b3b1b3ee4571c4c06c 100644 (file)
@@ -42,7 +42,6 @@ typedef enum {
 } WapiThreadPriority;
 
 gpointer wapi_create_thread_handle (void);
-void wapi_thread_handle_set_exited (gpointer handle, guint32 exitstatus);
 void wapi_ref_thread_handle (gpointer handle);
 gpointer wapi_get_current_thread_handle (void);
 
index 9c4e7fe3e581ec15e9a34a86ed3889041c4c84f5..b2d57dbdf044700419359587baaf1150d2e9db33 100644 (file)
@@ -116,51 +116,6 @@ get_current_thread (void)
        return lookup_thread (handle);
 }
 
-void
-wapi_thread_handle_set_exited (gpointer handle, guint32 exitstatus)
-{
-       MonoW32HandleThread *thread_handle;
-       int i, thr_ret;
-       pid_t pid = wapi_getpid ();
-       pthread_t tid = pthread_self ();
-       
-       if (mono_w32handle_issignalled (handle) ||
-           mono_w32handle_get_type (handle) == MONO_W32HANDLE_UNUSED) {
-               /* We must have already deliberately finished with
-                * this thread, so don't do any more now
-                */
-               return;
-       }
-
-       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Thread %p terminating", __func__, handle);
-
-       thread_handle = lookup_thread (handle);
-
-       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Thread %p abandoning held mutexes", __func__, handle);
-
-       for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
-               gpointer mutex = g_ptr_array_index (thread_handle->owned_mutexes, i);
-
-               wapi_mutex_abandon (mutex, pid, tid);
-               wapi_thread_disown_mutex (mutex);
-       }
-       g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
-       
-       thr_ret = mono_w32handle_lock_handle (handle);
-       g_assert (thr_ret == 0);
-
-       mono_w32handle_set_signal_state (handle, TRUE, TRUE);
-
-       thr_ret = mono_w32handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-       
-       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Recording thread handle %p id %ld status as %d",
-                 __func__, handle, thread_handle->id, exitstatus);
-       
-       /* The thread is no longer active, so unref it */
-       mono_w32handle_unref (handle);
-}
-
 /*
  * wapi_create_thread_handle:
  *
index 9b2a401567e5823aa95dc7f96e4fecf9af70db9c..dc307dc601e31fbc76856d95eabd6e4fbc334a82 100644 (file)
@@ -2941,17 +2941,14 @@ void mono_thread_init (MonoThreadStartCB start_cb,
 
 void mono_thread_cleanup (void)
 {
-#if !defined(HOST_WIN32) && !defined(RUN_IN_SUBTHREAD)
-       MonoThreadInfo *info;
-
+#if !defined(RUN_IN_SUBTHREAD)
        /* The main thread must abandon any held mutexes (particularly
         * important for named mutexes as they are shared across
         * processes, see bug 74680.)  This will happen when the
         * thread exits, but if it's not running in a subthread it
         * won't exit in time.
         */
-       info = mono_thread_info_current ();
-       wapi_thread_handle_set_exited (info->handle, mono_environment_exitcode_get ());
+       mono_thread_info_set_exited (mono_thread_info_current ());
 #endif
 
 #if 0
index b12a87ab1248c4526e6816e98ae0f739ad90fab7..1c13fd4b05713d3d46a2e4d74e9979c7cb7bcd15 100644 (file)
@@ -18,6 +18,7 @@
 #include <mono/utils/mono-threads-posix-signals.h>
 #include <mono/utils/mono-coop-semaphore.h>
 #include <mono/metadata/gc-internals.h>
+#include <mono/utils/w32handle.h>
 
 #include <errno.h>
 
@@ -193,7 +194,7 @@ mono_threads_platform_exit (int exit_code)
        nacl_shutdown_gc_thread();
 #endif
 
-       wapi_thread_handle_set_exited (current->handle, exit_code);
+       mono_threads_platform_set_exited (current);
 
        mono_thread_info_detach ();
 
@@ -204,7 +205,7 @@ void
 mono_threads_platform_unregister (MonoThreadInfo *info)
 {
        if (info->handle) {
-               wapi_thread_handle_set_exited (info->handle, 0);
+               mono_threads_platform_set_exited (info);
                info->handle = NULL;
        }
 }
@@ -334,6 +335,49 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
 #endif
 }
 
+void
+mono_threads_platform_set_exited (MonoThreadInfo *info)
+{
+       MonoW32HandleThread *thread_data;
+       gpointer mutex_handle;
+       int i, thr_ret;
+       pid_t pid;
+       pthread_t tid;
+
+       if (!info->handle || mono_w32handle_issignalled (info->handle) || mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED) {
+               /* We must have already deliberately finished
+                * with this thread, so don't do any more now */
+               return;
+       }
+
+       if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
+               g_error ("unknown thread handle %p", info->handle);
+
+       pid = wapi_getpid ();
+       tid = pthread_self ();
+
+       for (i = 0; i < thread_data->owned_mutexes->len; i++) {
+               mutex_handle = g_ptr_array_index (thread_data->owned_mutexes, i);
+               wapi_mutex_abandon (mutex_handle, pid, tid);
+               wapi_thread_disown_mutex (mutex_handle);
+       }
+
+       g_ptr_array_free (thread_data->owned_mutexes, TRUE);
+
+       thr_ret = mono_w32handle_lock_handle (info->handle);
+       g_assert (thr_ret == 0);
+
+       mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
+
+       thr_ret = mono_w32handle_unlock_handle (info->handle);
+       g_assert (thr_ret == 0);
+
+       /* The thread is no longer active, so unref it */
+       mono_w32handle_unref (info->handle);
+
+       info->handle = NULL;
+}
+
 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
 
 #if defined(USE_POSIX_BACKEND)
@@ -375,6 +419,9 @@ mono_threads_suspend_register (MonoThreadInfo *info)
 #if defined (PLATFORM_ANDROID)
        info->native_handle = gettid ();
 #endif
+
+       g_assert (!info->handle);
+       info->handle = wapi_create_thread_handle ();
 }
 
 void
index c00a3155ca554eae9c382c9210a6269cf84c9924..82b3a37312c97d27224c4dfc34e178761fb2d361 100644 (file)
@@ -362,4 +362,9 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
 #endif
 }
 
+void
+mono_threads_platform_set_exited (MonoThreadInfo *info)
+{
+}
+
 #endif
index de2793aa4a6deb67049295cdc4f13cf04aa905d9..2e2b6d56e01848f3d4ac1aab9be73edea5e2bf39 100644 (file)
@@ -1563,3 +1563,10 @@ mono_thread_info_is_current (MonoThreadInfo *info)
 {
        return mono_thread_info_get_tid (info) == mono_native_thread_id_get ();
 }
+
+void
+mono_thread_info_set_exited (THREAD_INFO_TYPE *info)
+{
+       g_assert (mono_thread_info_is_current (info));
+       mono_threads_platform_set_exited (info);
+}
index e6b6e4746efb6af995dce37186244982e8b01f96..1f31a60a55f07d7e136c642d2d530137dea6d56b 100644 (file)
@@ -425,6 +425,9 @@ mono_thread_info_exit (void);
 HANDLE
 mono_thread_info_open_handle (void);
 
+void
+mono_thread_info_set_exited (THREAD_INFO_TYPE *info);
+
 void
 mono_thread_info_install_interrupt (void (*callback) (gpointer data), gpointer data, gboolean *interrupted);
 
@@ -527,6 +530,7 @@ void mono_threads_platform_exit (int exit_code);
 void mono_threads_platform_unregister (THREAD_INFO_TYPE *info);
 HANDLE mono_threads_platform_open_handle (void);
 HANDLE mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid);
+void mono_threads_platform_set_exited (THREAD_INFO_TYPE *info);
 
 void mono_threads_coop_begin_global_suspend (void);
 void mono_threads_coop_end_global_suspend (void);