From 79df14c04b452411b9d17e26a398e491bca1a811 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Tue, 10 Mar 2020 02:22:11 +0900 Subject: Introduce Ractor mechanism for parallel execution This commit introduces Ractor mechanism to run Ruby program in parallel. See doc/ractor.md for more details about Ractor. See ticket [Feature #17100] to see the implementation details and discussions. [Feature #17100] This commit does not complete the implementation. You can find many bugs on using Ractor. Also the specification will be changed so that this feature is experimental. You will see a warning when you make the first Ractor with `Ractor.new`. I hope this feature can help programmers from thread-safety issues. --- thread_win32.c | 59 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'thread_win32.c') diff --git a/thread_win32.c b/thread_win32.c index d8eb2b1ee6..842a9efc85 100644 --- a/thread_win32.c +++ b/thread_win32.c @@ -28,8 +28,6 @@ static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES; static int w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th); -void rb_native_mutex_lock(rb_nativethread_lock_t *lock); -void rb_native_mutex_unlock(rb_nativethread_lock_t *lock); static void w32_error(const char *func) @@ -97,38 +95,38 @@ w32_mutex_create(void) #define GVL_DEBUG 0 static void -gvl_acquire(rb_vm_t *vm, rb_thread_t *th) +gvl_acquire(rb_global_vm_lock_t *gvl, rb_thread_t *th) { - w32_mutex_lock(vm->gvl.lock); + w32_mutex_lock(gvl->lock); if (GVL_DEBUG) fprintf(stderr, "gvl acquire (%p): acquire\n", th); } static void -gvl_release(rb_vm_t *vm) +gvl_release(rb_global_vm_lock_t *gvl) { - ReleaseMutex(vm->gvl.lock); + ReleaseMutex(gvl->lock); } static void -gvl_yield(rb_vm_t *vm, rb_thread_t *th) +gvl_yield(rb_global_vm_lock_t *gvl, rb_thread_t *th) { - gvl_release(th->vm); + gvl_release(gvl); native_thread_yield(); - gvl_acquire(vm, th); + gvl_acquire(gvl, th); } -static void -gvl_init(rb_vm_t *vm) +void +rb_gvl_init(rb_global_vm_lock_t *gvl) { if (GVL_DEBUG) fprintf(stderr, "gvl init\n"); - vm->gvl.lock = w32_mutex_create(); + gvl->lock = w32_mutex_create(); } static void -gvl_destroy(rb_vm_t *vm) +gvl_destroy(rb_global_vm_lock_t *gvl) { if (GVL_DEBUG) fprintf(stderr, "gvl destroy\n"); - CloseHandle(vm->gvl.lock); + CloseHandle(gvl->lock); } static rb_thread_t * @@ -140,13 +138,21 @@ ruby_thread_from_native(void) static int ruby_thread_set_native(rb_thread_t *th) { + if (th && th->ec) { + rb_ractor_set_current_ec(th->ractor, th->ec); + } return TlsSetValue(ruby_native_thread_key, th); } void Init_native_thread(rb_thread_t *th) { - ruby_native_thread_key = TlsAlloc(); + if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { + rb_bug("TlsAlloc() for ruby_current_ec_key fails"); + } + if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { + rb_bug("TlsAlloc() for ruby_native_thread_key fails"); + } ruby_thread_set_native(th); DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), @@ -458,7 +464,6 @@ rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex) native_cond_timedwait_ms(cond, mutex, INFINITE); } -#if 0 static unsigned long abs_timespec_to_timeout_ms(const struct timespec *ts) { @@ -487,6 +492,19 @@ native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mute return native_cond_timedwait_ms(cond, mutex, timeout_ms); } +static struct timespec native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel); + +void +rb_native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec) +{ + struct timespec rel = { + .tv_sec = msec / 1000, + .tv_nsec = (msec % 1000) * 1000 * 1000, + }; + struct timespec ts = native_cond_timeout(cond, rel); + native_cond_timedwait(cond, mutex, &ts); +} + static struct timespec native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel) { @@ -516,7 +534,6 @@ native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel) return timeout; } -#endif void rb_native_cond_initialize(rb_nativethread_cond_t *cond) @@ -694,9 +711,13 @@ timer_thread_func(void *dummy) rb_w32_set_thread_description(GetCurrentThread(), L"ruby-timer-thread"); while (WaitForSingleObject(timer_thread.lock, TIME_QUANTUM_USEC/1000) == WAIT_TIMEOUT) { - timer_thread_function(); + rb_execution_context_t *running_ec = vm->ractor.main_ractor->threads.running_ec; + + if (running_ec) { + timer_thread_function(running_ec); + } ruby_sigchld_handler(vm); /* probably no-op */ - rb_threadptr_check_signal(vm->main_thread); + rb_threadptr_check_signal(vm->ractor.main_thread); } thread_debug("timer killed\n"); return 0; -- cgit v1.2.1