[utils/threads] Add and export a mono_native_thread_join () function.
[mono.git] / mono / utils / mono-threads-posix.c
index 4b9f5d393f5e79fb7ee9dc489f7fe8b00a0e7f69..7d656132b5908b35b491f4c439eefa66c83cd243 100644 (file)
@@ -18,7 +18,6 @@
 #include <mono/utils/mono-threads-posix-signals.h>
 #include <mono/utils/mono-coop-semaphore.h>
 #include <mono/metadata/gc-internals.h>
-#include <mono/metadata/w32mutex-utils.h>
 #include <mono/utils/w32handle.h>
 
 #include <errno.h>
@@ -46,8 +45,6 @@ mono_threads_platform_register (MonoThreadInfo *info)
 {
        gpointer thread_handle;
 
-       info->owned_mutexes = g_ptr_array_new ();
-
        thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
        if (thread_handle == INVALID_HANDLE_VALUE)
                g_error ("%s: failed to create handle", __func__);
@@ -160,7 +157,11 @@ mono_threads_platform_exit (int exit_code)
 void
 mono_threads_platform_unregister (MonoThreadInfo *info)
 {
-       mono_threads_platform_set_exited (info);
+       g_assert (info->handle);
+
+       /* The thread is no longer active, so unref it */
+       mono_w32handle_unref (info->handle);
+       info->handle = NULL;
 }
 
 int
@@ -287,73 +288,32 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
 #endif
 }
 
-void
-mono_threads_platform_set_exited (MonoThreadInfo *info)
+gboolean
+mono_native_thread_join (MonoNativeThreadId tid)
 {
-       gpointer mutex_handle;
-       int i, thr_ret;
-       pthread_t tid;
-
-       g_assert (info->handle);
-
-       if (mono_w32handle_issignalled (info->handle))
-               g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, info->handle, mono_thread_info_get_tid (info));
-       if (mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED)
-               g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, info->handle, mono_thread_info_get_tid (info));
-
-       tid = pthread_self ();
-
-       for (i = 0; i < info->owned_mutexes->len; i++) {
-               mutex_handle = g_ptr_array_index (info->owned_mutexes, i);
-               mono_w32mutex_abandon (mutex_handle, tid);
-               mono_thread_info_disown_mutex (info, mutex_handle);
-       }
-
-       g_ptr_array_free (info->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);
+       void *res;
 
-       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;
+       return !pthread_join (tid, &res);
 }
 
 void
-mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
+mono_threads_platform_set_exited (gpointer handle)
 {
-       int i;
-
-       g_string_append_printf (text, "thread handle %p state : ", info->handle);
+       int thr_ret;
 
-       mono_thread_info_describe_interrupt_token (info, text);
-
-       g_string_append_printf (text, ", owns (");
-       for (i = 0; i < info->owned_mutexes->len; i++)
-               g_string_append_printf (text, i > 0 ? ", %p" : "%p", g_ptr_array_index (info->owned_mutexes, i));
-       g_string_append_printf (text, ")");
-}
+       g_assert (handle);
+       if (mono_w32handle_issignalled (handle))
+               g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, handle, mono_native_thread_id_get ());
+       if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_UNUSED)
+               g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, handle, mono_native_thread_id_get ());
 
-void
-mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
-{
-       mono_w32handle_ref (mutex_handle);
-
-       g_ptr_array_add (info->owned_mutexes, mutex_handle);
-}
+       thr_ret = mono_w32handle_lock_handle (handle);
+       g_assert (thr_ret == 0);
 
-void
-mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
-{
-       mono_w32handle_unref (mutex_handle);
+       mono_w32handle_set_signal_state (handle, TRUE, TRUE);
 
-       g_ptr_array_remove (info->owned_mutexes, mutex_handle);
+       thr_ret = mono_w32handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
 }
 
 static const gchar* thread_typename (void)