[coop] Switch Posix and Win32 threads locks to coop implementation
authorLudovic Henry <ludovic@xamarin.com>
Mon, 16 Nov 2015 17:23:30 +0000 (17:23 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Tue, 24 Nov 2015 18:24:35 +0000 (13:24 -0500)
mono/metadata/threads.c
mono/utils/mono-threads-posix.c
mono/utils/mono-threads-windows.c
mono/utils/mono-threads.h

index 16252771d725c2aa362c948839558bbba1cd60bc..4d2a92c9e2da45c220365bdfca17f8712a793867 100644 (file)
@@ -813,10 +813,8 @@ create_thread (MonoThread *thread, MonoInternalThread *internal, StartInfo *star
         */
        create_flags = CREATE_SUSPENDED;
 
-       MONO_PREPARE_BLOCKING;
        thread_handle = mono_threads_create_thread ((LPTHREAD_START_ROUTINE)start_wrapper, start_info,
                                                                                                stack_size, create_flags, &tid);
-       MONO_FINISH_BLOCKING;
 
        if (thread_handle == NULL) {
                /* The thread couldn't be created, so throw an exception */
index a763aaafeb4d4d35e0669d6c8701632a3fbd1d58..54f183e58f03659937730f70797d3de5b411c0db 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-threads-posix-signals.h>
+#include <mono/utils/mono-coop-semaphore.h>
 #include <mono/metadata/gc-internals.h>
 
 #include <errno.h>
@@ -40,7 +41,7 @@ typedef struct {
        void *(*start_routine)(void*);
        void *arg;
        int flags;
-       MonoSemType registered;
+       MonoCoopSem registered;
        HANDLE handle;
 } StartInfo;
 
@@ -59,36 +60,34 @@ inner_start_thread (void *arg)
        /* Register the thread with the io-layer */
        handle = wapi_create_thread_handle ();
        if (!handle) {
-               res = mono_os_sem_post (&(start_info->registered));
+               res = mono_coop_sem_post (&(start_info->registered));
                g_assert (!res);
                return NULL;
        }
        start_info->handle = handle;
 
        info = mono_thread_info_attach (&result);
-       MONO_PREPARE_BLOCKING;
 
        info->runtime_thread = TRUE;
        info->handle = handle;
 
        if (flags & CREATE_SUSPENDED) {
                info->create_suspended = TRUE;
-               mono_os_sem_init (&info->create_suspended_sem, 0);
+               mono_coop_sem_init (&info->create_suspended_sem, 0);
        }
 
        /* start_info is not valid after this */
-       res = mono_os_sem_post (&(start_info->registered));
+       res = mono_coop_sem_post (&(start_info->registered));
        g_assert (!res);
        start_info = NULL;
 
        if (flags & CREATE_SUSPENDED) {
-               res = mono_os_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
+               res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
                g_assert (res != -1);
 
-               mono_os_sem_destroy (&info->create_suspended_sem);
+               mono_coop_sem_destroy (&info->create_suspended_sem);
        }
 
-       MONO_FINISH_BLOCKING;
        /* Run the actual main function of the thread */
        result = start_func (t_arg);
 
@@ -132,22 +131,20 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
        start_info.start_routine = (void *(*)(void *)) start_routine;
        start_info.arg = arg;
        start_info.flags = creation_flags;
-       mono_os_sem_init (&(start_info.registered), 0);
+       mono_coop_sem_init (&(start_info.registered), 0);
 
        /* Actually start the thread */
        res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
        if (res) {
-               mono_os_sem_destroy (&(start_info.registered));
+               mono_coop_sem_destroy (&(start_info.registered));
                return NULL;
        }
 
-       MONO_TRY_BLOCKING;
        /* Wait until the thread register itself in various places */
-       res = mono_os_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
+       res = mono_coop_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
        g_assert (res != -1);
-       MONO_FINISH_TRY_BLOCKING;
 
-       mono_os_sem_destroy (&(start_info.registered));
+       mono_coop_sem_destroy (&(start_info.registered));
 
        if (out_tid)
                *out_tid = thread;
@@ -163,7 +160,7 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
 void
 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
 {
-       mono_os_sem_post (&info->create_suspended_sem);
+       mono_coop_sem_post (&info->create_suspended_sem);
 }
 
 gboolean
index 6482e858b21e26b45be0f037edeafad6429cd06f..86275a53833ab5028b682df6386b79015b419ec2 100644 (file)
@@ -139,7 +139,7 @@ mono_threads_platform_free (MonoThreadInfo *info)
 typedef struct {
        LPTHREAD_START_ROUTINE start_routine;
        void *arg;
-       MonoSemType registered;
+       MonoCoopSem registered;
        gboolean suspend;
        HANDLE suspend_event;
 } ThreadStartInfo;
@@ -160,7 +160,7 @@ inner_start_thread (LPVOID arg)
        info->runtime_thread = TRUE;
        info->create_suspended = suspend;
 
-       post_result = mono_os_sem_post (&(start_info->registered));
+       post_result = mono_coop_sem_post (&(start_info->registered));
        g_assert (!post_result);
 
        if (suspend) {
@@ -186,7 +186,7 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
        start_info = g_malloc0 (sizeof (ThreadStartInfo));
        if (!start_info)
                return NULL;
-       mono_os_sem_init (&(start_info->registered), 0);
+       mono_coop_sem_init (&(start_info->registered), 0);
        start_info->arg = arg;
        start_info->start_routine = start_routine;
        start_info->suspend = creation_flags & CREATE_SUSPENDED;
@@ -199,7 +199,7 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
 
        result = CreateThread (NULL, stack_size, inner_start_thread, start_info, creation_flags, &thread_id);
        if (result) {
-               res = mono_os_sem_wait (&(start_info->registered), MONO_SEM_FLAGS_NONE);
+               res = mono_coop_sem_wait (&(start_info->registered), MONO_SEM_FLAGS_NONE);
                g_assert (res != -1);
 
                if (start_info->suspend) {
@@ -211,7 +211,7 @@ mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer
        }
        if (out_tid)
                *out_tid = thread_id;
-       mono_os_sem_destroy (&(start_info->registered));
+       mono_coop_sem_destroy (&(start_info->registered));
        g_free (start_info);
        return result;
 }
index b73d1768bf14f33b6952b12ae0dbb7a94286fc66..367b0bed9daeba1cdd187472f2415bf08b99e96a 100644 (file)
@@ -16,6 +16,7 @@
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/mono-threads-coop.h>
 #include <mono/utils/mono-threads-api.h>
+#include <mono/utils/mono-coop-semaphore.h>
 
 #include <mono/io-layer/io-layer.h>
 
@@ -227,7 +228,7 @@ typedef struct {
        gboolean create_suspended;
 
        /* Semaphore used to implement CREATE_SUSPENDED */
-       MonoSemType create_suspended_sem;
+       MonoCoopSem create_suspended_sem;
 
        /*
         * Values of TLS variables for this thread.