From cdf953dd2ef13fd7a74e1cb2da6c23eae066f040 Mon Sep 17 00:00:00 2001 From: Salikh Zakirov Date: Tue, 20 Mar 2007 12:17:56 +0300 Subject: [PATCH] Acquire global thread lock on creating new thread This is needed to prevent race condition when the new thread registers itself before the thread creation call initializes thread->os_handle. --- vm/thread/src/thread_native_basic.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/vm/thread/src/thread_native_basic.c b/vm/thread/src/thread_native_basic.c index f332731..59acc32 100644 --- a/vm/thread/src/thread_native_basic.c +++ b/vm/thread/src/thread_native_basic.c @@ -125,9 +125,13 @@ IDATA VMCALL hythread_create_with_group(hythread_t *ret_thread, hythread_group_t start_proc_data->start_proc = func; start_proc_data->start_proc_args = data; + // we need to make sure thread will not register itself with a thread group + // until os_thread_create returned and initialized thread->os_handle properly + hythread_global_lock(); r = os_thread_create(&new_thread->os_handle, stacksize, priority, thread_start_proc, (void *)start_proc_data); assert(/* error */ r || new_thread->os_handle /* or thread created ok */); + hythread_global_unlock(); return r; } @@ -576,7 +580,9 @@ static IDATA register_to_group(hythread_t thread, hythread_group_t group) { // Acquire global TM lock to prevent concurrent access to thread list status = hythread_global_lock(NULL); - if (status != TM_ERROR_NONE) return status; + assert(status == 0); + + assert(thread->os_handle); thread_set_self(thread); assert(thread == tm_self_tls); -- 1.5.0.33.g1b20