From b24603adff8ec1e93e71358b93b3e30c99ba29d5 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 5 Jun 2019 11:18:50 +1200 Subject: [PATCH] Move vm stack init into thread. --- gc.c | 4 ++++ thread.c | 28 ++++++++++++++++++++++++++-- vm.c | 23 +++++++++++------------ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/gc.c b/gc.c index 531bf29810..345622fd4e 100644 --- a/gc.c +++ b/gc.c @@ -9398,6 +9398,10 @@ rb_memerror(void) rb_objspace_t *objspace = rb_objspace_of(rb_ec_vm_ptr(ec)); VALUE exc; + // Print out pid, sleep, so you can attach debugger to see what went wrong: + // fprintf(stderr, "rb_memerror pid=%d\n", getpid()); + // sleep(60); + if (during_gc) gc_exit(objspace, "rb_memerror"); exc = nomem_error; diff --git a/thread.c b/thread.c index bfe741c908..81018b9721 100644 --- a/thread.c +++ b/thread.c @@ -695,6 +695,17 @@ thread_do_start(rb_thread_t *th) } void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec); +rb_control_frame_t * +rb_vm_push_frame(rb_execution_context_t *sec, + const rb_iseq_t *iseq, + VALUE type, + VALUE self, + VALUE specval, + VALUE cref_or_me, + const VALUE *pc, + VALUE *sp, + int local_size, + int stack_max); static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start) @@ -703,9 +714,22 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s rb_thread_list_t *join_list; rb_thread_t *main_th; VALUE errinfo = Qnil; + size_t vm_stack_size = th->vm->default_params.thread_vm_stack_size; - if (th == th->vm->main_thread) - rb_bug("thread_start_func_2 must not be used for main thread"); + if (th == th->vm->main_thread) { + rb_bug("thread_start_func_2 must not be used for main thread"); + } + + rb_ec_set_vm_stack(th->ec, alloca(vm_stack_size), vm_stack_size / sizeof(VALUE)); + th->ec->cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); + + rb_vm_push_frame(th->ec, + 0 /* dummy iseq */, + VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, + Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, + 0 /* dummy cref/me */, + 0 /* dummy pc */, th->ec->vm_stack, 0, 0 + ); ruby_thread_set_native(th); diff --git a/vm.c b/vm.c index 50e13f40de..cab7f880f6 100644 --- a/vm.c +++ b/vm.c @@ -2691,20 +2691,19 @@ th_init(rb_thread_t *th, VALUE self) th->self = self; rb_threadptr_root_fiber_setup(th); - { - /* vm_stack_size is word number. - * th->vm->default_params.thread_vm_stack_size is byte size. */ - size_t size = th->vm->default_params.thread_vm_stack_size / sizeof(VALUE); - rb_ec_set_vm_stack(th->ec, rb_thread_recycle_stack(size), size); + // Initialize the main thread: + if (self == 0) { + size_t size = th->vm->default_params.thread_vm_stack_size / sizeof(VALUE); + VALUE * vm_stack = ALLOC_N(VALUE, size); + rb_ec_set_vm_stack(th->ec, vm_stack, size); + th->ec->cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); + + vm_push_frame(th->ec, 0 /* dummy iseq */, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, + Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, + 0 /* dummy cref/me */, + 0 /* dummy pc */, th->ec->vm_stack, 0, 0); } - th->ec->cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size); - - vm_push_frame(th->ec, 0 /* dummy iseq */, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */, - Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */, - 0 /* dummy cref/me */, - 0 /* dummy pc */, th->ec->vm_stack, 0, 0); - th->status = THREAD_RUNNABLE; th->last_status = Qnil; th->ec->errinfo = Qnil;