refactoring r63073.

* cont.c (root_fiber_alloc): call `ConvertThreadToFiber()` here.

  `rb_fiber_t` for root_fiber is allocated before running Threads.
  Fiber objects wrapping this rb_fiber_t for root_fiber are created
  when root Fiber object is required explicitly (for example, Fiber
  switching and so on). We can put calling `ConvertThreadToFiber()`.
  In other words, we can pending `ConvertThreadToFiber()`
  until Fiber objects are created.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63090 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2018-04-04 08:19:28 +00:00
parent 746a88e199
commit 51d227e3a5
3 changed files with 13 additions and 34 deletions

41
cont.c
View File

@ -1464,18 +1464,6 @@ rb_fiber_start(void)
VM_UNREACHABLE(rb_fiber_start); VM_UNREACHABLE(rb_fiber_start);
} }
#ifdef _WIN32
static HANDLE
win32_convert_thread_to_fiber(void)
{
HANDLE fib_handle = ConvertThreadToFiber(0);
if (!fib_handle) {
rb_bug("rb_threadptr_root_fiber_setup_by_child: ConvertThreadToFiber() failed - %s\n", rb_w32_strerror(-1));
}
return fib_handle;
}
#endif
static rb_fiber_t * static rb_fiber_t *
root_fiber_alloc(rb_thread_t *th) root_fiber_alloc(rb_thread_t *th)
{ {
@ -1489,18 +1477,26 @@ root_fiber_alloc(rb_thread_t *th)
th->root_fiber = fib; th->root_fiber = fib;
DATA_PTR(fibval) = fib; DATA_PTR(fibval) = fib;
fib->cont.self = fibval; fib->cont.self = fibval;
#if FIBER_USE_NATIVE #if FIBER_USE_NATIVE
#ifdef _WIN32 #ifdef _WIN32
/* setup fib_handle for root Fiber */
if (fib->fib_handle == 0) { if (fib->fib_handle == 0) {
fib->fib_handle = win32_convert_thread_to_fiber(); if ((fib->fib_handle = ConvertThreadToFiber(0)) == 0) {
rb_bug("root_fiber_alloc: ConvertThreadToFiber() failed - %s\n", rb_w32_strerror(-1));
}
}
else {
rb_bug("root_fiber_alloc: fib_handle is not NULL.");
} }
#endif #endif
#endif #endif
return fib; return fib;
} }
void void
rb_threadptr_root_fiber_setup_by_parent(rb_thread_t *th) rb_threadptr_root_fiber_setup(rb_thread_t *th)
{ {
rb_fiber_t *fib = ruby_mimmalloc(sizeof(rb_fiber_t)); rb_fiber_t *fib = ruby_mimmalloc(sizeof(rb_fiber_t));
MEMZERO(fib, rb_fiber_t, 1); MEMZERO(fib, rb_fiber_t, 1);
@ -1509,23 +1505,8 @@ rb_threadptr_root_fiber_setup_by_parent(rb_thread_t *th)
fib->cont.saved_ec.thread_ptr = th; fib->cont.saved_ec.thread_ptr = th;
fiber_status_set(fib, FIBER_RESUMED); /* skip CREATED */ fiber_status_set(fib, FIBER_RESUMED); /* skip CREATED */
th->ec = &fib->cont.saved_ec; th->ec = &fib->cont.saved_ec;
}
void /* NOTE: On WIN32, fib_handle is not allocated yet. */
rb_threadptr_root_fiber_setup_by_child(rb_thread_t *th)
{
#if FIBER_USE_NATIVE
#ifdef _WIN32
rb_fiber_t *fib = th->ec->fiber_ptr;
if (fib->fib_handle == 0) {
fib->fib_handle = win32_convert_thread_to_fiber();
}
else {
rb_bug("rb_threadptr_root_fiber_setup_by_child: fib_handle is not NULL.");
}
#endif
#endif
} }
void void

View File

@ -643,7 +643,6 @@ thread_do_start(rb_thread_t *th, VALUE args)
} }
void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec); void rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec);
void rb_threadptr_root_fiber_setup_by_child(rb_thread_t *th);
static int static int
thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start) thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start)
@ -663,7 +662,6 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s
rb_bug("thread_start_func_2 must not be used for main thread"); rb_bug("thread_start_func_2 must not be used for main thread");
ruby_thread_set_native(th); ruby_thread_set_native(th);
rb_threadptr_root_fiber_setup_by_child(th);
th->ec->machine.stack_start = stack_start; th->ec->machine.stack_start = stack_start;
#ifdef __ia64 #ifdef __ia64

4
vm.c
View File

@ -2425,7 +2425,7 @@ rb_execution_context_mark(const rb_execution_context_t *ec)
} }
void rb_fiber_mark_self(rb_fiber_t *fib); void rb_fiber_mark_self(rb_fiber_t *fib);
void rb_threadptr_root_fiber_setup_by_parent(rb_thread_t *th); void rb_threadptr_root_fiber_setup(rb_thread_t *th);
void rb_threadptr_root_fiber_release(rb_thread_t *th); void rb_threadptr_root_fiber_release(rb_thread_t *th);
static void static void
@ -2533,7 +2533,7 @@ static void
th_init(rb_thread_t *th, VALUE self) th_init(rb_thread_t *th, VALUE self)
{ {
th->self = self; th->self = self;
rb_threadptr_root_fiber_setup_by_parent(th); rb_threadptr_root_fiber_setup(th);
/* allocate thread stack */ /* allocate thread stack */
#ifdef USE_SIGALTSTACK #ifdef USE_SIGALTSTACK