diff options
author | Koichi Sasada <ko1@atdot.net> | 2022-04-22 21:19:03 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2022-04-23 03:08:27 +0900 |
commit | 03d21a4fb099da7c52e6591e17704c297871b7db (patch) | |
tree | db2d58907b7c841d8ca15967f063d229bd9e37cc /thread_win32.c | |
parent | 69d41480ec1c91691b79f106f5376a2e2cab3a82 (diff) |
introduce struct `rb_native_thread`
`rb_thread_t` contained `native_thread_data_t` to represent
thread implementation dependent data. This patch separates
them and rename it `rb_native_thread` and point it from
`rb_thraed_t`.
Now, 1 Ruby thread (`rb_thread_t`) has 1 native thread (`rb_native_thread`).
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5836
Diffstat (limited to 'thread_win32.c')
-rw-r--r-- | thread_win32.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/thread_win32.c b/thread_win32.c index d8544af3a3..966f0af5b6 100644 --- a/thread_win32.c +++ b/thread_win32.c @@ -155,7 +155,7 @@ ruby_thread_set_native(rb_thread_t *th) } void -Init_native_thread(rb_thread_t *th) +Init_native_thread(rb_thread_t *main_th) { if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { rb_bug("TlsAlloc() for ruby_current_ec_key fails"); @@ -163,17 +163,21 @@ Init_native_thread(rb_thread_t *th) if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { rb_bug("TlsAlloc() for ruby_native_thread_key fails"); } - ruby_thread_set_native(th); + + // setup main thread + + ruby_thread_set_native(main_th); + main_th->nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0); + DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), - &th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS); - - th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0); + &main_th->nt->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS); thread_debug("initial thread (th: %p, thid: %p, event: %p)\n", - th, GET_THREAD()->thread_id, - th->native_thread_data.interrupt_event); + main_th, + main_th->nt->thread_id, + main_th->nt->interrupt_event); } static int @@ -186,7 +190,7 @@ w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th) thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n", events, count, timeout, th); - if (th && (intr = th->native_thread_data.interrupt_event)) { + if (th && (intr = th->nt->interrupt_event)) { if (ResetEvent(intr) && (!RUBY_VM_INTERRUPTED(th->ec) || SetEvent(intr))) { targets = ALLOCA_N(HANDLE, count + 1); memcpy(targets, events, sizeof(HANDLE) * count); @@ -194,7 +198,7 @@ w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th) targets[count++] = intr; thread_debug(" * handle: %p (count: %d, intr)\n", intr, count); } - else if (intr == th->native_thread_data.interrupt_event) { + else if (intr == th->nt->interrupt_event) { w32_error("w32_wait_events"); } } @@ -592,8 +596,8 @@ native_thread_init_stack(rb_thread_t *th) static void native_thread_destroy(rb_thread_t *th) { - HANDLE intr = InterlockedExchangePointer(&th->native_thread_data.interrupt_event, 0); - thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id); + HANDLE intr = InterlockedExchangePointer(&th->nt->interrupt_event, 0); + thread_debug("close handle - intr: %p, thid: %p\n", intr, th->nt->thread_id); w32_close_handle(intr); } @@ -601,14 +605,14 @@ static unsigned long __stdcall thread_start_func_1(void *th_ptr) { rb_thread_t *th = th_ptr; - volatile HANDLE thread_id = th->thread_id; + volatile HANDLE thread_id = th->nt->thread_id; native_thread_init_stack(th); - th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0); + th->nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0); /* run */ thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th, - th->thread_id, th->native_thread_data.interrupt_event); + th->nt->thread_id, th->nt->interrupt_event); thread_start_func_2(th, th->ec->machine.stack_start); @@ -621,19 +625,20 @@ static int native_thread_create(rb_thread_t *th) { const size_t stack_size = th->vm->default_params.thread_machine_stack_size + th->vm->default_params.thread_vm_stack_size; - th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th); + th->nt = ZALLOC(struct rb_native_thread); + th->nt->thread_id = w32_create_thread(stack_size, thread_start_func_1, th); - if ((th->thread_id) == 0) { + if ((th->nt->thread_id) == 0) { return thread_errno; } - w32_resume_thread(th->thread_id); + w32_resume_thread(th->nt->thread_id); if (THREAD_DEBUG) { Sleep(0); thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %"PRIuSIZE"\n", - th, th->thread_id, - th->native_thread_data.interrupt_event, stack_size); + th, th->nt->thread_id, + th->nt->interrupt_event, stack_size); } return 0; } @@ -660,7 +665,7 @@ native_thread_apply_priority(rb_thread_t *th) priority = THREAD_PRIORITY_NORMAL; } - SetThreadPriority(th->thread_id, priority); + SetThreadPriority(th->nt->thread_id, priority); } #endif /* USE_NATIVE_THREAD_PRIORITY */ @@ -699,7 +704,7 @@ ubf_handle(void *ptr) rb_thread_t *th = (rb_thread_t *)ptr; thread_debug("ubf_handle: %p\n", th); - if (!SetEvent(th->native_thread_data.interrupt_event)) { + if (!SetEvent(th->nt->interrupt_event)) { w32_error("ubf_handle"); } } @@ -848,7 +853,7 @@ native_set_thread_name(rb_thread_t *th) static VALUE native_thread_native_thread_id(rb_thread_t *th) { - DWORD tid = GetThreadId(th->thread_id); + DWORD tid = GetThreadId(th->nt->thread_id); if (tid == 0) rb_sys_fail("GetThreadId"); return ULONG2NUM(tid); } |