[io-layer] Extract wapi_create_thread_handle
authorLudovic Henry <ludovic@xamarin.com>
Thu, 7 Jul 2016 16:26:17 +0000 (18:26 +0200)
committerLudovic Henry <ludovic@xamarin.com>
Thu, 4 Aug 2016 13:41:44 +0000 (15:41 +0200)
mono/io-layer/threads.h
mono/io-layer/wthreads.c
mono/utils/mono-threads-posix.c

index 37948812064849d6595a72a7a6c696c5974f1064..a293ff22d792103b1099139848d01d456d1256ff 100644 (file)
@@ -41,8 +41,6 @@ typedef enum {
        THREAD_PRIORITY_HIGHEST = 2
 } WapiThreadPriority;
 
-gpointer wapi_create_thread_handle (void);
-
 extern gint32 GetThreadPriority (gpointer handle);
 extern gboolean SetThreadPriority (gpointer handle, gint32 priority);
 
index 2cbb4ba0c700621c8617d3febbf3962d6b354628..e7f818f0e627c626e5659971d6db5eca33288fbd 100644 (file)
@@ -84,54 +84,6 @@ _wapi_thread_cleanup (void)
 {
 }
 
-static MonoW32HandleThread*
-lookup_thread (HANDLE handle)
-{
-       MonoW32HandleThread *thread;
-       gboolean ok;
-
-       ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_THREAD,
-                                                         (gpointer *)&thread);
-       g_assert (ok);
-       return thread;
-}
-
-/*
- * wapi_create_thread_handle:
- *
- *   Create a thread handle for the current thread.
- */
-gpointer
-wapi_create_thread_handle (void)
-{
-       MonoW32HandleThread thread_handle = {0}, *thread;
-       gpointer handle;
-
-       thread_handle.owned_mutexes = g_ptr_array_new ();
-
-       handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, &thread_handle);
-       if (handle == INVALID_HANDLE_VALUE) {
-               g_warning ("%s: error creating thread handle", __func__);
-               SetLastError (ERROR_GEN_FAILURE);
-               
-               return NULL;
-       }
-
-       thread = lookup_thread (handle);
-
-       thread->id = pthread_self ();
-
-       /*
-        * Hold a reference while the thread is active, because we use
-        * the handle to store thread exit information
-        */
-       mono_w32handle_ref (handle);
-
-       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: started thread id %ld", __func__, thread->id);
-       
-       return handle;
-}
-
 /**
  * wapi_init_thread_info_priority:
  * @param handle: The thread handle to set.
index 309980231277334333da94e214b0123386d944f4..51c85e0a79f3dc7be1b76ee39c5bcd5d9b184bfc 100644 (file)
@@ -32,12 +32,36 @@ extern int tkill (pid_t tid, int signal);
 
 #if defined(_POSIX_VERSION) || defined(__native_client__)
 
+#include <pthread.h>
+
 #include <sys/resource.h>
 
 #if defined(__native_client__)
 void nacl_shutdown_gc_thread(void);
 #endif
 
+static gpointer
+thread_handle_create (void)
+{
+       MonoW32HandleThread thread_data;
+       gpointer thread_handle;
+
+       thread_data.id = pthread_self ();
+       thread_data.owned_mutexes = g_ptr_array_new ();
+       thread_data.priority = THREAD_PRIORITY_NORMAL;
+
+       thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, (gpointer) &thread_data);
+       if (thread_handle == INVALID_HANDLE_VALUE)
+               return NULL;
+
+       /* We need to keep the handle alive, as long as the corresponding managed
+        * thread object is alive. The handle is going to be unref when calling
+        * the finalizer on the MonoThreadInternal object */
+       mono_w32handle_ref (thread_handle);
+
+       return thread_handle;
+}
+
 typedef struct {
        void *(*start_routine)(void*);
        void *arg;
@@ -60,7 +84,7 @@ inner_start_thread (void *arg)
        MonoThreadInfo *info;
 
        /* Register the thread with the io-layer */
-       handle = wapi_create_thread_handle ();
+       handle = thread_handle_create ();
        if (!handle) {
                mono_coop_sem_post (&(start_info->registered));
                return NULL;
@@ -219,7 +243,7 @@ mono_threads_platform_open_handle (void)
        g_assert (info);
 
        if (!info->handle)
-               info->handle = wapi_create_thread_handle ();
+               info->handle = thread_handle_create ();
        else
                mono_w32handle_ref (info->handle);
        return info->handle;
@@ -472,7 +496,7 @@ mono_threads_suspend_register (MonoThreadInfo *info)
 #endif
 
        g_assert (!info->handle);
-       info->handle = wapi_create_thread_handle ();
+       info->handle = thread_handle_create ();
 }
 
 void