[mono-threads] Use create_suspended_sem on windows (#3482)
authorLudovic Henry <ludovic@xamarin.com>
Tue, 30 Aug 2016 10:45:53 +0000 (12:45 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Aug 2016 10:45:53 +0000 (12:45 +0200)
mono/utils/mono-threads-posix.c
mono/utils/mono-threads-windows.c
mono/utils/mono-threads.c
mono/utils/mono-threads.h

index 60f5585e1823dbb35454d0a5197cc0886d02efc0..c92f7bb316f814400e151a10f59a798ca5a74697 100644 (file)
@@ -228,17 +228,6 @@ mono_threads_platform_create_thread (MonoThreadStart start_routine, gpointer arg
        return start_info.handle;
 }
 
-/*
- * mono_threads_platform_resume_created:
- *
- *   Resume a newly created thread created using CREATE_SUSPENDED.
- */
-void
-mono_threads_platform_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
-{
-       mono_coop_sem_post (&info->create_suspended_sem);
-}
-
 gboolean
 mono_threads_platform_yield (void)
 {
index f66e6936b6dc1df123ab5060db34eb65a74ef86f..97f75f0db1e5544155933bf8846256723c9f8b36 100644 (file)
@@ -154,7 +154,6 @@ typedef struct {
        gint32 priority;
        MonoCoopSem registered;
        gboolean suspend;
-       HANDLE suspend_event;
        HANDLE handle;
 } ThreadStartInfo;
 
@@ -166,22 +165,28 @@ inner_start_thread (LPVOID arg)
        LPTHREAD_START_ROUTINE start_func = start_info->start_routine;
        DWORD result;
        gboolean suspend = start_info->suspend;
-       HANDLE suspend_event = start_info->suspend_event;
        MonoThreadInfo *info;
+       int res;
 
        info = mono_thread_info_attach (&result);
        info->runtime_thread = TRUE;
-       info->create_suspended = suspend;
 
        start_info->handle = info->handle;
 
        mono_threads_platform_set_priority(info, start_info->priority);
 
+       if (suspend) {
+               info->create_suspended = TRUE;
+               mono_coop_sem_init (&info->create_suspended_sem, 0);
+       }
+
        mono_coop_sem_post (&(start_info->registered));
 
        if (suspend) {
-               WaitForSingleObject (suspend_event, INFINITE); /* caller will suspend the thread before setting the event. */
-               CloseHandle (suspend_event);
+               res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
+               g_assert (res != -1);
+
+               mono_coop_sem_destroy (&info->create_suspended_sem);
        }
 
        result = start_func (t_arg);
@@ -207,11 +212,6 @@ mono_threads_platform_create_thread (MonoThreadStart start_routine, gpointer arg
        start_info.suspend = creation_flags & CREATE_SUSPENDED;
        start_info.priority = tp->priority;
        creation_flags &= ~CREATE_SUSPENDED;
-       if (start_info.suspend) {
-               start_info.suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
-               if (!start_info.suspend_event)
-                       return NULL;
-       }
 
        result = CreateThread (NULL, tp->stack_size, inner_start_thread, &start_info, creation_flags, &thread_id);
        if (result) {
@@ -221,13 +221,6 @@ mono_threads_platform_create_thread (MonoThreadStart start_routine, gpointer arg
                /* A new handle has been opened when attaching
                 * the thread, so we don't need this one */
                CloseHandle (result);
-
-               if (start_info.suspend) {
-                       g_assert (SuspendThread (start_info.handle) != (DWORD)-1);
-                       SetEvent (start_info.suspend_event);
-               }
-       } else if (start_info.suspend) {
-               CloseHandle (start_info.suspend_event);
        }
        if (out_tid)
                *out_tid = thread_id;
@@ -254,17 +247,6 @@ mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
        return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;
 }
 
-void
-mono_threads_platform_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
-{
-       HANDLE handle;
-
-       handle = OpenThread (THREAD_ALL_ACCESS, TRUE, tid);
-       g_assert (handle);
-       ResumeThread (handle);
-       CloseHandle (handle);
-}
-
 #if HAVE_DECL___READFSDWORD==0
 static MONO_ALWAYS_INLINE unsigned long long
 __readfsdword (unsigned long offset)
index 5add7fe0f86f32f82803d805762bd4d865e4d78e..fce261ee47b5dac8fdbacf8653719381b2d6535b 100644 (file)
@@ -770,7 +770,7 @@ mono_thread_info_core_resume (MonoThreadInfo *info)
                MonoNativeThreadId tid = mono_thread_info_get_tid (info);
                /* Have to special case this, as the normal suspend/resume pair are racy, they don't work if he resume is received before the suspend */
                info->create_suspended = FALSE;
-               mono_threads_platform_resume_created (info, tid);
+               mono_coop_sem_post (&info->create_suspended_sem);
                return TRUE;
        }
 
index 96e197980a05fce513369c12755c83b74a836c9d..00162929b8bd63b22191e6dd595b9bf8f6695fcc 100644 (file)
@@ -536,7 +536,6 @@ gboolean mono_threads_suspend_needs_abort_syscall (void);
 void mono_threads_platform_register (THREAD_INFO_TYPE *info);
 void mono_threads_platform_unregister (THREAD_INFO_TYPE *info);
 HANDLE mono_threads_platform_create_thread (MonoThreadStart start, gpointer arg, MonoThreadParm *, MonoNativeThreadId *out_tid);
-void mono_threads_platform_resume_created (THREAD_INFO_TYPE *info, MonoNativeThreadId tid);
 void mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize);
 gboolean mono_threads_platform_yield (void);
 void mono_threads_platform_exit (int exit_code);