ruby-changes:47077
From: ko1 <ko1@a...>
Date: Wed, 28 Jun 2017 13:49:37 +0900 (JST)
Subject: [ruby-changes:47077] ko1:r59192 (trunk): introduce rb_thread_ptr() to replace GetThreadPtr().
ko1 2017-06-28 13:49:30 +0900 (Wed, 28 Jun 2017) New Revision: 59192 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59192 Log: introduce rb_thread_ptr() to replace GetThreadPtr(). * vm_core.h (rb_thread_ptr): added to replace GetThreadPtr() macro. * thread.c (in some functions: use "target_th" instead of "th" to make clear that it is not a current thread. Modified files: trunk/cont.c trunk/thread.c trunk/vm.c trunk/vm_backtrace.c trunk/vm_core.h trunk/vm_dump.c trunk/vm_trace.c Index: vm_trace.c =================================================================== --- vm_trace.c (revision 59191) +++ vm_trace.c (revision 59192) @@ -92,14 +92,6 @@ recalc_remove_ruby_vm_event_flags(rb_eve https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L92 /* add/remove hooks */ -static rb_thread_t * -thval2thread_t(VALUE thval) -{ - rb_thread_t *th; - GetThreadPtr(thval, th); - return th; -} - static rb_event_hook_t * alloc_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags) { @@ -136,7 +128,7 @@ rb_threadptr_add_event_hook(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L128 void rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data) { - rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE); + rb_threadptr_add_event_hook(rb_thread_ptr(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE); } void @@ -149,7 +141,7 @@ rb_add_event_hook(rb_event_hook_func_t f https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L141 void rb_thread_add_event_hook2(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags) { - rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, hook_flags); + rb_threadptr_add_event_hook(rb_thread_ptr(thval), func, events, data, hook_flags); } void @@ -189,13 +181,13 @@ rb_threadptr_remove_event_hook(rb_thread https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L181 int rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func) { - return rb_threadptr_remove_event_hook(thval2thread_t(thval), func, Qundef); + return rb_threadptr_remove_event_hook(rb_thread_ptr(thval), func, Qundef); } int rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data) { - return rb_threadptr_remove_event_hook(thval2thread_t(thval), func, data); + return rb_threadptr_remove_event_hook(rb_thread_ptr(thval), func, data); } int @@ -519,10 +511,7 @@ thread_add_trace_func(rb_thread_t *th, V https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L511 static VALUE thread_add_trace_func_m(VALUE obj, VALUE trace) { - rb_thread_t *th; - - GetThreadPtr(obj, th); - thread_add_trace_func(th, trace); + thread_add_trace_func(rb_thread_ptr(obj), trace); return trace; } @@ -538,19 +527,19 @@ thread_add_trace_func_m(VALUE obj, VALUE https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L527 */ static VALUE -thread_set_trace_func_m(VALUE obj, VALUE trace) +thread_set_trace_func_m(VALUE target_thread, VALUE trace) { - rb_thread_t *th; + rb_thread_t *target_th = rb_thread_ptr(target_thread); - GetThreadPtr(obj, th); - rb_threadptr_remove_event_hook(th, call_trace_func, Qundef); + rb_threadptr_remove_event_hook(target_th, call_trace_func, Qundef); if (NIL_P(trace)) { return Qnil; } - - thread_add_trace_func(th, trace); - return trace; + else { + thread_add_trace_func(target_th, trace); + return trace; + } } static const char * @@ -1218,9 +1207,10 @@ tracepoint_new(VALUE klass, rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L1207 VALUE rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data) { - rb_thread_t *target_th = 0; + rb_thread_t *target_th = NULL; + if (RTEST(target_thval)) { - GetThreadPtr(target_thval, target_th); + target_th = rb_thread_ptr(target_thval); /* TODO: Test it! * Warning: This function is not tested. */ Index: vm_dump.c =================================================================== --- vm_dump.c (revision 59191) +++ vm_dump.c (revision 59192) @@ -212,9 +212,8 @@ rb_vmdebug_proc_dump_raw(rb_proc_t *proc https://github.com/ruby/ruby/blob/trunk/vm_dump.c#L212 void rb_vmdebug_stack_dump_th(VALUE thval) { - rb_thread_t *th; - GetThreadPtr(thval, th); - rb_vmdebug_stack_dump_raw(th, th->ec.cfp); + rb_thread_t *target_th = rb_thread_ptr(thval); + rb_vmdebug_stack_dump_raw(target_th, target_th->ec.cfp); } #if VMDEBUG > 2 @@ -327,9 +326,7 @@ rb_vmdebug_debug_print_register(rb_threa https://github.com/ruby/ruby/blob/trunk/vm_dump.c#L326 void rb_vmdebug_thread_dump_regs(VALUE thval) { - rb_thread_t *th; - GetThreadPtr(thval, th); - rb_vmdebug_debug_print_register(th); + rb_vmdebug_debug_print_register(rb_thread_ptr(thval)); } void @@ -399,10 +396,8 @@ rb_vmdebug_debug_print_post(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_dump.c#L396 VALUE rb_vmdebug_thread_dump_state(VALUE self) { - rb_thread_t *th; - rb_control_frame_t *cfp; - GetThreadPtr(self, th); - cfp = th->ec.cfp; + rb_thread_t *th = rb_thread_ptr(self); + rb_control_frame_t *cfp = th->ec.cfp; fprintf(stderr, "Thread state dump:\n"); fprintf(stderr, "pc : %p, sp : %p\n", (void *)cfp->pc, (void *)cfp->sp); Index: vm_backtrace.c =================================================================== --- vm_backtrace.c (revision 59191) +++ vm_backtrace.c (revision 59192) @@ -889,13 +889,12 @@ threadptr_backtrace_to_ary(rb_thread_t * https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L889 static VALUE thread_backtrace_to_ary(int argc, const VALUE *argv, VALUE thval, int to_str) { - rb_thread_t *th; - GetThreadPtr(thval, th); + rb_thread_t *target_th = rb_thread_ptr(thval); - if (th->to_kill || th->status == THREAD_KILLED) - return Qnil; + if (target_th->to_kill || target_th->status == THREAD_KILLED) + return Qnil; - return threadptr_backtrace_to_ary(th, argc, argv, 0, 0, to_str); + return threadptr_backtrace_to_ary(target_th, argc, argv, 0, 0, to_str); } VALUE Index: vm.c =================================================================== --- vm.c (revision 59191) +++ vm.c (revision 59192) @@ -2555,9 +2555,8 @@ th_init(rb_thread_t *th, VALUE self) https://github.com/ruby/ruby/blob/trunk/vm.c#L2555 static VALUE ruby_thread_init(VALUE self) { - rb_thread_t *th; + rb_thread_t *th = rb_thread_ptr(self); rb_vm_t *vm = GET_THREAD()->vm; - GetThreadPtr(self, th); th->vm = vm; th_init(th, self); Index: vm_core.h =================================================================== --- vm_core.h (revision 59191) +++ vm_core.h (revision 59192) @@ -672,8 +672,12 @@ typedef struct rb_control_frame_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L672 extern const rb_data_type_t ruby_threadptr_data_type; -#define GetThreadPtr(obj, ptr) \ - TypedData_Get_Struct((obj), rb_thread_t, &ruby_threadptr_data_type, (ptr)) +static inline struct rb_thread_struct * +rb_thread_ptr(VALUE thval) +{ + VM_ASSERT(rb_check_typeddata(obj, &ruby_threadptr_data_type) != NULL); + return (struct rb_thread_struct *)DATA_PTR(thval); +} enum rb_thread_status { THREAD_RUNNABLE, Index: cont.c =================================================================== --- cont.c (revision 59191) +++ cont.c (revision 59192) @@ -196,9 +196,9 @@ cont_mark(void *ptr) https://github.com/ruby/ruby/blob/trunk/cont.c#L196 } else { /* fiber */ - rb_thread_t *th; + rb_thread_t *th = rb_thread_ptr(cont->saved_thread.self); rb_fiber_t *fib = (rb_fiber_t*)cont; - GetThreadPtr(cont->saved_thread.self, th); + if ((th->fiber != fib) && fib->status == FIBER_RUNNING) { rb_gc_mark_locations(cont->machine.stack, cont->machine.stack + cont->machine.stack_size); @@ -1514,9 +1514,8 @@ rb_fiber_yield(int argc, const VALUE *ar https://github.com/ruby/ruby/blob/trunk/cont.c#L1514 void rb_fiber_reset_root_local_storage(VALUE thval) { - rb_thread_t *th; + rb_thread_t *th = rb_thread_ptr(thval); - GetThreadPtr(thval, th); if (th->root_fiber && th->root_fiber != th->fiber) { th->ec.local_storage = th->root_fiber->cont.saved_thread.ec.local_storage; } Index: thread.c =================================================================== --- thread.c (revision 59191) +++ thread.c (revision 59192) @@ -712,14 +712,13 @@ thread_start_func_2(rb_thread_t *th, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L712 static VALUE thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS)) { - rb_thread_t *th, *current_th = GET_THREAD(); + rb_thread_t *th = rb_thread_ptr(thval), *current_th = GET_THREAD(); int err; if (OBJ_FROZEN(current_th->thgroup)) { rb_raise(rb_eThreadError, "can't start a new thread (frozen ThreadGroup)"); } - GetThreadPtr(thval, th); /* setup thread environment */ th->first_func = fn; @@ -781,7 +780,7 @@ thread_s_new(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/thread.c#L780 rb_raise(rb_eThreadError, "can't alloc thread"); rb_obj_call_init(thread, argc, argv); - GetThreadPtr(thread, th); + th = rb_thread_ptr(thread); if (!threadptr_initialized(th)) { rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'", klass); @@ -809,21 +808,23 @@ thread_start(VALUE klass, VALUE args) https://github.com/ruby/ruby/blob/trunk/thread.c#L808 static VALUE thread_initialize(VALUE thread, VALUE args) { - rb_thread_t *th; + rb_thread_t *th = rb_thread_ptr(thread); + if (!rb_block_given_p()) { rb_raise(rb_eThreadError, "must be called with a block"); } - GetThreadPtr(thread, th); - if (th->first_args) { + else if (th->first_args) { VALUE proc = th->first_proc, loc; - if (!proc || !RTEST(loc = rb_proc_location(proc))) { - rb_raise(rb_eThreadError, "already initialized thread"); - } - rb_raise(rb_eThreadError, + if (!proc || !RTEST(loc = rb_proc_location(proc))) { + rb_raise(rb_eThreadError, "already initialized thread"); + } + rb_raise(rb_eThreadError, "already initialized thread - %"PRIsVALUE":%"PRIsVALUE, - RARRAY_AREF(loc, 0), RARRAY_AREF(loc, 1)); + RARRAY_AREF(loc, 0), RARRAY_AREF(loc, 1)); + } + else { + return thread_create_core(thread, args, 0); } - return thread_create_core(thread, args, 0); } VALUE @@ -987,18 +988,15 @@ thread_join(rb_thread_t *target_th, doub https://github.com/ruby/ruby/blob/trunk/thread.c#L988 static VALUE thread_join_m(int argc, VALUE *argv, VALUE self) { - rb_thread_t *target_th; double delay = DELAY_INFTY; VALUE limit; - GetThreadPtr(self, target_th); - rb_scan_args(argc, argv, "01", &limit); if (!NIL_P(limit)) { delay = rb_num2dbl(limit); } - return thread_join(target_th, delay); + return thread_join(rb_thread_ptr(self), delay); } /* @@ -1018,8 +1016,7 @@ thread_join_m(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L1016 static VALUE thread_value(VALUE self) { - rb_thread_t *th; - GetThreadPtr(self, th); + rb_thread_t *th = rb_thread_ptr(self); thread_join(th, DELAY_INFTY); return th->value; } @@ -1239,9 +1236,7 @@ rb_thread_check_trap_pending(void) https://github.com/ruby/ruby/blob/trunk/thread.c#L1236 int rb_thread_interrupted(VALUE thval) { - rb_thread_t *th; - GetThreadPtr(thval, th); - return (int)RUBY_VM_INTERRUPTED(th); + return (int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)); } void @@ -1918,9 +1913,7 @@ rb_thread_s_handle_interrupt(VALUE self, https://github.com/ruby/ruby/blob/trunk/thread.c#L1913 static VALUE rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread) { - rb_thread_t *target_th; - - GetThreadPtr(target_thread, target_th); + rb_thread_t *target_th = rb_thread_ptr(target_thread); if (!target_th->pending_interrupt_queue) { return Qfalse; @@ -2113,9 +2106,7 @@ rb_threadptr_execute_interrupts(rb_threa https://github.com/ruby/ruby/blob/trunk/thread.c#L2106 void rb_thread_execute_interrupts(VALUE thval) { - rb_thread_t *th; - GetThreadPtr(thval, th); - rb_threadptr_execute_interrupts(th, 1); + rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1); } static void @@ -2254,15 +2245,15 @@ rb_thread_fd_close(int fd) https://github.com/ruby/ruby/blob/trunk/thread.c#L2245 static VALUE thread_raise_m(int argc, VALUE *argv, VALUE self) { - rb_thread_t *target_th; - rb_thread_t *th = GET_THREAD(); - GetThreadPtr(self, target_th); + rb_thread_t *target_th = rb_thread_ptr(self); + const rb_thread_t *current_th = GET_THREAD(); + threadptr_check_pending_interrupt_queue(target_th); rb_threadptr_raise(target_th, argc, argv); /* To perform Thread.current.raise as Kernel.raise */ - if (th == target_th) { - RUBY_VM_CHECK_INTS(th); + if (current_th == target_th) { + RUBY_VM_CHECK_INTS(target_th); } return Qnil; } @@ -2284,9 +2275,7 @@ thread_raise_m(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/thread.c#L2275 VALUE rb_thread_kill(VALUE thread) { - rb_thread_t *th; - - GetThreadPtr(thread, th); + rb_thread_t *th = rb_thread_ptr(thread); if (th->to_kill || th->status == THREAD_KILLED) { return thread; @@ -2312,9 +2301,7 @@ rb_thread_kill(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2301 int rb_thread_to_be_killed(VALUE thread) { - rb_thread_t *th; - - GetThreadPtr(thread, th); + rb_thread_t *th = rb_thread_ptr(thread); if (th->to_kill || th->status == THREAD_KILLED) { return TRUE; @@ -2391,15 +2378,16 @@ rb_thread_wakeup(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2378 VALUE rb_thread_wakeup_alive(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); + rb_thread_t *target_th = rb_thread_ptr(thread); + if (target_th->status == THREAD_KILLED) return Qnil; - if (th->status == THREAD_KILLED) { - return Qnil; + rb_threadptr_ready(target_th); + + if (target_th->status == THREAD_STOPPED || + target_th->status == THREAD_STOPPED_FOREVER) { + target_th->status = THREAD_RUNNABLE; } - rb_threadptr_ready(th); - if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER) - th->status = THREAD_RUNNABLE; + return thread; } @@ -2626,9 +2614,7 @@ rb_thread_s_abort_exc_set(VALUE self, VA https://github.com/ruby/ruby/blob/trunk/thread.c#L2614 static VALUE rb_thread_abort_exc(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); - return th->abort_on_exception ? Qtrue : Qfalse; + return rb_thread_ptr(thread)->abort_on_exception ? Qtrue : Qfalse; } @@ -2648,10 +2634,7 @@ rb_thread_abort_exc(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2634 static VALUE rb_thread_abort_exc_set(VALUE thread, VALUE val) { - rb_thread_t *th; - - GetThreadPtr(thread, th); - th->abort_on_exception = RTEST(val); + rb_thread_ptr(thread)->abort_on_exception = RTEST(val); return val; } @@ -2736,9 +2719,7 @@ rb_thread_s_report_exc_set(VALUE self, V https://github.com/ruby/ruby/blob/trunk/thread.c#L2719 static VALUE rb_thread_report_exc(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); - return th->report_on_exception ? Qtrue : Qfalse; + return rb_thread_ptr(thread)->report_on_exception ? Qtrue : Qfalse; } @@ -2758,10 +2739,7 @@ rb_thread_report_exc(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2739 static VALUE rb_thread_report_exc_set(VALUE thread, VALUE val) { - rb_thread_t *th; - - GetThreadPtr(thread, th); - th->report_on_exception = RTEST(val); + rb_thread_ptr(thread)->report_on_exception = RTEST(val); return val; } @@ -2779,15 +2757,8 @@ rb_thread_report_exc_set(VALUE thread, V https://github.com/ruby/ruby/blob/trunk/thread.c#L2757 VALUE rb_thread_group(VALUE thread) { - rb_thread_t *th; - VALUE group; - GetThreadPtr(thread, th); - group = th->thgroup; - - if (!group) { - group = Qnil; - } - return group; + VALUE group = rb_thread_ptr(thread)->thgroup; + return group == 0 ? Qnil : group; } static const char * @@ -2795,10 +2766,7 @@ thread_status_name(rb_thread_t *th, int https://github.com/ruby/ruby/blob/trunk/thread.c#L2766 { switch (th->status) { case THREAD_RUNNABLE: - if (th->to_kill) - return "aborting"; - else - return "run"; + return th->to_kill ? "aborting" : "run"; case THREAD_STOPPED_FOREVER: if (detail) return "sleep_forever"; case THREAD_STOPPED: @@ -2851,17 +2819,20 @@ rb_threadptr_dead(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread.c#L2819 static VALUE rb_thread_status(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); + rb_thread_t *target_th = rb_thread_ptr(thread); - if (rb_threadptr_dead(th)) { - if (!NIL_P(th->errinfo) && !FIXNUM_P(th->errinfo) - /* TODO */ ) { + if (rb_threadptr_dead(target_th)) { + if (!NIL_P(target_th->errinfo) && + !FIXNUM_P(target_th->errinfo)) { return Qnil; } - return Qfalse; + else { + return Qfalse; + } + } + else { + return rb_str_new2(thread_status_name(target_th, FALSE)); } - return rb_str_new2(thread_status_name(th, FALSE)); } @@ -2882,12 +2853,12 @@ rb_thread_status(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2853 static VALUE rb_thread_alive_p(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); - - if (rb_threadptr_dead(th)) + if (rb_threadptr_dead(rb_thread_ptr(thread))) { return Qfalse; - return Qtrue; + } + else { + return Qtrue; + } } /* @@ -2907,14 +2878,18 @@ rb_thread_alive_p(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2878 static VALUE rb_thread_stop_p(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); + rb_thread_t *th = rb_thread_ptr(thread); - if (rb_threadptr_dead(th)) + if (rb_threadptr_dead(th)) { return Qtrue; - if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER) + } + else if (th->status == THREAD_STOPPED || + th->status == THREAD_STOPPED_FOREVER) { return Qtrue; - return Qfalse; + } + else { + return Qfalse; + } } /* @@ -2932,10 +2907,7 @@ rb_thread_stop_p(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2907 static VALUE rb_thread_safe_level(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); - - return INT2NUM(th->ec.safe_level); + return INT2NUM(rb_thread_ptr(thread)->ec.safe_level); } /* @@ -2948,9 +2920,7 @@ rb_thread_safe_level(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2920 static VALUE rb_thread_getname(VALUE thread) { - rb_thread_t *th; - GetThreadPtr(thread, th); - return th->name; + return rb_thread_ptr(thread)->name; } /* @@ -2967,8 +2937,8 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2937 #ifdef SET_ANOTHER_THREAD_NAME const char *s = ""; #endif - rb_thread_t *th; - GetThreadPtr(thread, th); + rb_thread_t *target_th = rb_thread_ptr(thread); + if (!NIL_P(name)) { rb_encoding *enc; StringValueCStr(name); @@ -2982,10 +2952,10 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2952 s = RSTRING_PTR(name); #endif } - th->name = name; + target_th->name = name; #if defined(SET_ANOTHER_THREAD_NAME) - if (threadptr_initialized(th)) { - SET_ANOTHER_THREAD_NAME(th->thread_id, s); + if (threadptr_initialized(target_th)) { + SET_ANOTHER_THREAD_NAME(target_th->thread_id, s); } #endif return name; @@ -3002,18 +2972,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/thread.c#L2972 rb_thread_inspect(VALUE thread) { VALUE cname = rb_class_path(rb_obj_class(thread)); - rb_thread_t *th; + rb_thread_t *target_th = rb_thread_ptr(thread); const char *status; VALUE str; - GetThreadPtr(thread, th); - status = thread_status_name(th, (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/