Skip to content

Commit

Permalink
[mono-threads] Use create_suspended_sem on windows (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
luhenry committed Aug 30, 2016
1 parent 76a6135 commit 57f8cb1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 41 deletions.
11 changes: 0 additions & 11 deletions mono/utils/mono-threads-posix.c
Expand Up @@ -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)
{
Expand Down
38 changes: 10 additions & 28 deletions mono/utils/mono-threads-windows.c
Expand Up @@ -154,7 +154,6 @@ typedef struct {
gint32 priority;
MonoCoopSem registered;
gboolean suspend;
HANDLE suspend_event;
HANDLE handle;
} ThreadStartInfo;

Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mono/utils/mono-threads.c
Expand Up @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion mono/utils/mono-threads.h
Expand Up @@ -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);
Expand Down

0 comments on commit 57f8cb1

Please sign in to comment.