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. --- cont.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'cont.c') diff --git a/cont.c b/cont.c index 654fc55774..efff86fe84 100644 --- a/cont.c +++ b/cont.c @@ -28,6 +28,7 @@ #include "mjit.h" #include "vm_core.h" #include "id_table.h" +#include "ractor.h" static const int DEBUG = 0; @@ -808,14 +809,15 @@ static inline void ec_switch(rb_thread_t *th, rb_fiber_t *fiber) { rb_execution_context_t *ec = &fiber->cont.saved_ec; - - ruby_current_execution_context_ptr = th->ec = ec; + rb_ractor_set_current_ec(th->ractor, th->ec = ec); + // ruby_current_execution_context_ptr = th->ec = ec; /* * timer-thread may set trap interrupt on previous th->ec at any time; * ensure we do not delay (or lose) the trap interrupt handling. */ - if (th->vm->main_thread == th && rb_signal_buff_size() > 0) { + if (th->vm->ractor.main_thread == th && + rb_signal_buff_size() > 0) { RUBY_VM_SET_TRAP_INTERRUPT(ec); } @@ -1873,7 +1875,7 @@ rb_fiber_start(void) enum ruby_tag_type state; int need_interrupt = TRUE; - VM_ASSERT(th->ec == ruby_current_execution_context_ptr); + VM_ASSERT(th->ec == GET_EC()); VM_ASSERT(FIBER_RESUMED_P(fiber)); if (fiber->blocking) { @@ -1964,13 +1966,15 @@ rb_threadptr_root_fiber_release(rb_thread_t *th) /* ignore. A root fiber object will free th->ec */ } else { + rb_execution_context_t *ec = GET_EC(); + VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT); VM_ASSERT(th->ec->fiber_ptr->cont.self == 0); - fiber_free(th->ec->fiber_ptr); - if (th->ec == ruby_current_execution_context_ptr) { - ruby_current_execution_context_ptr = NULL; + if (th->ec == ec) { + rb_ractor_set_current_ec(th->ractor, NULL); } + fiber_free(th->ec->fiber_ptr); th->ec = NULL; } } -- cgit v1.2.3