1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

refactoring thread inits in vm.c

* `th_init` accepts vm and ractor.
* remove `ruby_thread_init` because it is duplicated with `th_init`.
* add some comments.
This commit is contained in:
Koichi Sasada 2022-04-22 22:58:15 +09:00
parent 4b14b2902a
commit 702dc116c4
Notes: git 2022-04-23 00:50:43 +09:00

52
vm.c
View file

@ -3247,9 +3247,12 @@ rb_ec_clear_vm_stack(rb_execution_context_t *ec)
}
static void
th_init(rb_thread_t *th, VALUE self)
th_init(rb_thread_t *th, VALUE self, rb_vm_t *vm, rb_ractor_t *r)
{
th->self = self;
th->vm = vm;
th->ractor = r;
rb_threadptr_root_fiber_setup(th);
/* All threads are blocking until a non-blocking fiber is scheduled */
@ -3257,7 +3260,7 @@ th_init(rb_thread_t *th, VALUE self)
th->scheduler = Qnil;
if (self == 0) {
size_t size = th->vm->default_params.thread_vm_stack_size / sizeof(VALUE);
size_t size = vm->default_params.thread_vm_stack_size / sizeof(VALUE);
rb_ec_initialize_vm_stack(th->ec, ALLOC_N(VALUE, size), size);
}
else {
@ -3268,47 +3271,33 @@ th_init(rb_thread_t *th, VALUE self)
th->status = THREAD_RUNNABLE;
th->last_status = Qnil;
th->ec->errinfo = Qnil;
th->ec->root_svar = Qfalse;
th->ec->local_storage_recursive_hash = Qnil;
th->ec->local_storage_recursive_hash_for_trace = Qnil;
th->top_wrapper = 0;
th->top_self = vm->top_self; // 0 while self == 0
th->value = Qundef;
#ifdef NON_SCALAR_THREAD_ID
th->thread_id_string[0] = '\0';
#endif
th->value = Qundef;
th->ec->errinfo = Qnil;
th->ec->root_svar = Qfalse;
th->ec->local_storage_recursive_hash = Qnil;
th->ec->local_storage_recursive_hash_for_trace = Qnil;
#if OPT_CALL_THREADED_CODE
th->retval = Qundef;
#endif
th->name = Qnil;
th->report_on_exception = th->vm->thread_report_on_exception;
th->report_on_exception = vm->thread_report_on_exception;
th->ext_config.ractor_safe = true;
}
static VALUE
ruby_thread_init(VALUE self)
{
rb_thread_t *th = GET_THREAD();
rb_thread_t *target_th = rb_thread_ptr(self);
rb_vm_t *vm = th->vm;
target_th->vm = vm;
th_init(target_th, self);
target_th->top_wrapper = 0;
target_th->top_self = rb_vm_top_self();
target_th->ec->root_svar = Qfalse;
target_th->ractor = th->ractor;
return self;
}
VALUE
rb_thread_alloc(VALUE klass)
{
VALUE self = thread_alloc(klass);
ruby_thread_init(self);
rb_thread_t *target_th = rb_thread_ptr(self);
th_init(target_th, self, GET_VM(), GET_RACTOR());
return self;
}
@ -3946,6 +3935,8 @@ Init_BareVM(void)
fputs("[FATAL] failed to allocate memory\n", stderr);
exit(EXIT_FAILURE);
}
// setup the VM
MEMZERO(th, rb_thread_t, 1);
vm_init2(vm);
@ -3955,13 +3946,14 @@ Init_BareVM(void)
vm->overloaded_cme_table = st_init_numtable();
vm->constant_cache = rb_id_table_create(0);
// setup main thread
Init_native_thread(th);
th->vm = vm;
th_init(th, 0);
vm->ractor.main_ractor = th->ractor = rb_ractor_main_alloc();
th_init(th, 0, vm, vm->ractor.main_ractor = rb_ractor_main_alloc());
rb_ractor_set_current_ec(th->ractor, th->ec);
ruby_thread_init_stack(th);
// setup ractor system
rb_native_mutex_initialize(&vm->ractor.sync.lock);
rb_native_cond_initialize(&vm->ractor.sync.barrier_cond);
rb_native_cond_initialize(&vm->ractor.sync.terminate_cond);