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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59192 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2017-06-28 04:49:30 +00:00
parent e42a16190c
commit 249790802d
7 changed files with 131 additions and 192 deletions

7
cont.c
View File

@ -196,9 +196,9 @@ cont_mark(void *ptr)
} }
else { else {
/* fiber */ /* fiber */
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(cont->saved_thread.self);
rb_fiber_t *fib = (rb_fiber_t*)cont; rb_fiber_t *fib = (rb_fiber_t*)cont;
GetThreadPtr(cont->saved_thread.self, th);
if ((th->fiber != fib) && fib->status == FIBER_RUNNING) { if ((th->fiber != fib) && fib->status == FIBER_RUNNING) {
rb_gc_mark_locations(cont->machine.stack, rb_gc_mark_locations(cont->machine.stack,
cont->machine.stack + cont->machine.stack_size); cont->machine.stack + cont->machine.stack_size);
@ -1514,9 +1514,8 @@ rb_fiber_yield(int argc, const VALUE *argv)
void void
rb_fiber_reset_root_local_storage(VALUE thval) 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) { if (th->root_fiber && th->root_fiber != th->fiber) {
th->ec.local_storage = th->root_fiber->cont.saved_thread.ec.local_storage; th->ec.local_storage = th->root_fiber->cont.saved_thread.ec.local_storage;
} }

241
thread.c
View File

@ -712,14 +712,13 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s
static VALUE static VALUE
thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS)) 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; int err;
if (OBJ_FROZEN(current_th->thgroup)) { if (OBJ_FROZEN(current_th->thgroup)) {
rb_raise(rb_eThreadError, rb_raise(rb_eThreadError,
"can't start a new thread (frozen ThreadGroup)"); "can't start a new thread (frozen ThreadGroup)");
} }
GetThreadPtr(thval, th);
/* setup thread environment */ /* setup thread environment */
th->first_func = fn; th->first_func = fn;
@ -781,7 +780,7 @@ thread_s_new(int argc, VALUE *argv, VALUE klass)
rb_raise(rb_eThreadError, "can't alloc thread"); rb_raise(rb_eThreadError, "can't alloc thread");
rb_obj_call_init(thread, argc, argv); rb_obj_call_init(thread, argc, argv);
GetThreadPtr(thread, th); th = rb_thread_ptr(thread);
if (!threadptr_initialized(th)) { if (!threadptr_initialized(th)) {
rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'", rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
klass); klass);
@ -809,21 +808,23 @@ thread_start(VALUE klass, VALUE args)
static VALUE static VALUE
thread_initialize(VALUE thread, VALUE args) thread_initialize(VALUE thread, VALUE args)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(thread);
if (!rb_block_given_p()) { if (!rb_block_given_p()) {
rb_raise(rb_eThreadError, "must be called with a block"); rb_raise(rb_eThreadError, "must be called with a block");
} }
GetThreadPtr(thread, th); else if (th->first_args) {
if (th->first_args) {
VALUE proc = th->first_proc, loc; VALUE proc = th->first_proc, loc;
if (!proc || !RTEST(loc = rb_proc_location(proc))) { if (!proc || !RTEST(loc = rb_proc_location(proc))) {
rb_raise(rb_eThreadError, "already initialized thread"); rb_raise(rb_eThreadError, "already initialized thread");
} }
rb_raise(rb_eThreadError, rb_raise(rb_eThreadError,
"already initialized thread - %"PRIsVALUE":%"PRIsVALUE, "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 VALUE
@ -987,18 +988,15 @@ thread_join(rb_thread_t *target_th, double delay)
static VALUE static VALUE
thread_join_m(int argc, VALUE *argv, VALUE self) thread_join_m(int argc, VALUE *argv, VALUE self)
{ {
rb_thread_t *target_th;
double delay = DELAY_INFTY; double delay = DELAY_INFTY;
VALUE limit; VALUE limit;
GetThreadPtr(self, target_th);
rb_scan_args(argc, argv, "01", &limit); rb_scan_args(argc, argv, "01", &limit);
if (!NIL_P(limit)) { if (!NIL_P(limit)) {
delay = rb_num2dbl(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, VALUE self)
static VALUE static VALUE
thread_value(VALUE self) thread_value(VALUE self)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(self);
GetThreadPtr(self, th);
thread_join(th, DELAY_INFTY); thread_join(th, DELAY_INFTY);
return th->value; return th->value;
} }
@ -1239,9 +1236,7 @@ rb_thread_check_trap_pending(void)
int int
rb_thread_interrupted(VALUE thval) rb_thread_interrupted(VALUE thval)
{ {
rb_thread_t *th; return (int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval));
GetThreadPtr(thval, th);
return (int)RUBY_VM_INTERRUPTED(th);
} }
void void
@ -1918,9 +1913,7 @@ rb_thread_s_handle_interrupt(VALUE self, VALUE mask_arg)
static VALUE static VALUE
rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread) rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread)
{ {
rb_thread_t *target_th; rb_thread_t *target_th = rb_thread_ptr(target_thread);
GetThreadPtr(target_thread, target_th);
if (!target_th->pending_interrupt_queue) { if (!target_th->pending_interrupt_queue) {
return Qfalse; return Qfalse;
@ -2113,9 +2106,7 @@ rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
void void
rb_thread_execute_interrupts(VALUE thval) rb_thread_execute_interrupts(VALUE thval)
{ {
rb_thread_t *th; rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
GetThreadPtr(thval, th);
rb_threadptr_execute_interrupts(th, 1);
} }
static void static void
@ -2254,15 +2245,15 @@ rb_thread_fd_close(int fd)
static VALUE static VALUE
thread_raise_m(int argc, VALUE *argv, VALUE self) thread_raise_m(int argc, VALUE *argv, VALUE self)
{ {
rb_thread_t *target_th; rb_thread_t *target_th = rb_thread_ptr(self);
rb_thread_t *th = GET_THREAD(); const rb_thread_t *current_th = GET_THREAD();
GetThreadPtr(self, target_th);
threadptr_check_pending_interrupt_queue(target_th); threadptr_check_pending_interrupt_queue(target_th);
rb_threadptr_raise(target_th, argc, argv); rb_threadptr_raise(target_th, argc, argv);
/* To perform Thread.current.raise as Kernel.raise */ /* To perform Thread.current.raise as Kernel.raise */
if (th == target_th) { if (current_th == target_th) {
RUBY_VM_CHECK_INTS(th); RUBY_VM_CHECK_INTS(target_th);
} }
return Qnil; return Qnil;
} }
@ -2284,9 +2275,7 @@ thread_raise_m(int argc, VALUE *argv, VALUE self)
VALUE VALUE
rb_thread_kill(VALUE thread) rb_thread_kill(VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(thread);
GetThreadPtr(thread, th);
if (th->to_kill || th->status == THREAD_KILLED) { if (th->to_kill || th->status == THREAD_KILLED) {
return thread; return thread;
@ -2312,9 +2301,7 @@ rb_thread_kill(VALUE thread)
int int
rb_thread_to_be_killed(VALUE thread) rb_thread_to_be_killed(VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(thread);
GetThreadPtr(thread, th);
if (th->to_kill || th->status == THREAD_KILLED) { if (th->to_kill || th->status == THREAD_KILLED) {
return TRUE; return TRUE;
@ -2391,15 +2378,16 @@ rb_thread_wakeup(VALUE thread)
VALUE VALUE
rb_thread_wakeup_alive(VALUE thread) rb_thread_wakeup_alive(VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thread);
GetThreadPtr(thread, th); if (target_th->status == THREAD_KILLED) return Qnil;
if (th->status == THREAD_KILLED) { rb_threadptr_ready(target_th);
return Qnil;
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; return thread;
} }
@ -2626,9 +2614,7 @@ rb_thread_s_abort_exc_set(VALUE self, VALUE val)
static VALUE static VALUE
rb_thread_abort_exc(VALUE thread) rb_thread_abort_exc(VALUE thread)
{ {
rb_thread_t *th; return rb_thread_ptr(thread)->abort_on_exception ? Qtrue : Qfalse;
GetThreadPtr(thread, th);
return th->abort_on_exception ? Qtrue : Qfalse;
} }
@ -2648,10 +2634,7 @@ rb_thread_abort_exc(VALUE thread)
static VALUE static VALUE
rb_thread_abort_exc_set(VALUE thread, VALUE val) rb_thread_abort_exc_set(VALUE thread, VALUE val)
{ {
rb_thread_t *th; rb_thread_ptr(thread)->abort_on_exception = RTEST(val);
GetThreadPtr(thread, th);
th->abort_on_exception = RTEST(val);
return val; return val;
} }
@ -2736,9 +2719,7 @@ rb_thread_s_report_exc_set(VALUE self, VALUE val)
static VALUE static VALUE
rb_thread_report_exc(VALUE thread) rb_thread_report_exc(VALUE thread)
{ {
rb_thread_t *th; return rb_thread_ptr(thread)->report_on_exception ? Qtrue : Qfalse;
GetThreadPtr(thread, th);
return th->report_on_exception ? Qtrue : Qfalse;
} }
@ -2758,10 +2739,7 @@ rb_thread_report_exc(VALUE thread)
static VALUE static VALUE
rb_thread_report_exc_set(VALUE thread, VALUE val) rb_thread_report_exc_set(VALUE thread, VALUE val)
{ {
rb_thread_t *th; rb_thread_ptr(thread)->report_on_exception = RTEST(val);
GetThreadPtr(thread, th);
th->report_on_exception = RTEST(val);
return val; return val;
} }
@ -2779,15 +2757,8 @@ rb_thread_report_exc_set(VALUE thread, VALUE val)
VALUE VALUE
rb_thread_group(VALUE thread) rb_thread_group(VALUE thread)
{ {
rb_thread_t *th; VALUE group = rb_thread_ptr(thread)->thgroup;
VALUE group; return group == 0 ? Qnil : group;
GetThreadPtr(thread, th);
group = th->thgroup;
if (!group) {
group = Qnil;
}
return group;
} }
static const char * static const char *
@ -2795,10 +2766,7 @@ thread_status_name(rb_thread_t *th, int detail)
{ {
switch (th->status) { switch (th->status) {
case THREAD_RUNNABLE: case THREAD_RUNNABLE:
if (th->to_kill) return th->to_kill ? "aborting" : "run";
return "aborting";
else
return "run";
case THREAD_STOPPED_FOREVER: case THREAD_STOPPED_FOREVER:
if (detail) return "sleep_forever"; if (detail) return "sleep_forever";
case THREAD_STOPPED: case THREAD_STOPPED:
@ -2851,17 +2819,20 @@ rb_threadptr_dead(rb_thread_t *th)
static VALUE static VALUE
rb_thread_status(VALUE thread) rb_thread_status(VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thread);
GetThreadPtr(thread, th);
if (rb_threadptr_dead(th)) { if (rb_threadptr_dead(target_th)) {
if (!NIL_P(th->errinfo) && !FIXNUM_P(th->errinfo) if (!NIL_P(target_th->errinfo) &&
/* TODO */ ) { !FIXNUM_P(target_th->errinfo)) {
return Qnil; 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)
static VALUE static VALUE
rb_thread_alive_p(VALUE thread) rb_thread_alive_p(VALUE thread)
{ {
rb_thread_t *th; if (rb_threadptr_dead(rb_thread_ptr(thread))) {
GetThreadPtr(thread, th);
if (rb_threadptr_dead(th))
return Qfalse; return Qfalse;
return Qtrue; }
else {
return Qtrue;
}
} }
/* /*
@ -2907,14 +2878,18 @@ rb_thread_alive_p(VALUE thread)
static VALUE static VALUE
rb_thread_stop_p(VALUE thread) rb_thread_stop_p(VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(thread);
GetThreadPtr(thread, th);
if (rb_threadptr_dead(th)) if (rb_threadptr_dead(th)) {
return Qtrue; 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 Qtrue;
return Qfalse; }
else {
return Qfalse;
}
} }
/* /*
@ -2932,10 +2907,7 @@ rb_thread_stop_p(VALUE thread)
static VALUE static VALUE
rb_thread_safe_level(VALUE thread) rb_thread_safe_level(VALUE thread)
{ {
rb_thread_t *th; return INT2NUM(rb_thread_ptr(thread)->ec.safe_level);
GetThreadPtr(thread, th);
return INT2NUM(th->ec.safe_level);
} }
/* /*
@ -2948,9 +2920,7 @@ rb_thread_safe_level(VALUE thread)
static VALUE static VALUE
rb_thread_getname(VALUE thread) rb_thread_getname(VALUE thread)
{ {
rb_thread_t *th; return rb_thread_ptr(thread)->name;
GetThreadPtr(thread, th);
return th->name;
} }
/* /*
@ -2967,8 +2937,8 @@ rb_thread_setname(VALUE thread, VALUE name)
#ifdef SET_ANOTHER_THREAD_NAME #ifdef SET_ANOTHER_THREAD_NAME
const char *s = ""; const char *s = "";
#endif #endif
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thread);
GetThreadPtr(thread, th);
if (!NIL_P(name)) { if (!NIL_P(name)) {
rb_encoding *enc; rb_encoding *enc;
StringValueCStr(name); StringValueCStr(name);
@ -2982,10 +2952,10 @@ rb_thread_setname(VALUE thread, VALUE name)
s = RSTRING_PTR(name); s = RSTRING_PTR(name);
#endif #endif
} }
th->name = name; target_th->name = name;
#if defined(SET_ANOTHER_THREAD_NAME) #if defined(SET_ANOTHER_THREAD_NAME)
if (threadptr_initialized(th)) { if (threadptr_initialized(target_th)) {
SET_ANOTHER_THREAD_NAME(th->thread_id, s); SET_ANOTHER_THREAD_NAME(target_th->thread_id, s);
} }
#endif #endif
return name; return name;
@ -3002,18 +2972,17 @@ static VALUE
rb_thread_inspect(VALUE thread) rb_thread_inspect(VALUE thread)
{ {
VALUE cname = rb_class_path(rb_obj_class(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; const char *status;
VALUE str; VALUE str;
GetThreadPtr(thread, th); status = thread_status_name(target_th, TRUE);
status = thread_status_name(th, TRUE);
str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread); str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
if (!NIL_P(th->name)) { if (!NIL_P(target_th->name)) {
rb_str_catf(str, "@%"PRIsVALUE, th->name); rb_str_catf(str, "@%"PRIsVALUE, target_th->name);
} }
if (!th->first_func && th->first_proc) { if (!target_th->first_func && target_th->first_proc) {
VALUE loc = rb_proc_location(th->first_proc); VALUE loc = rb_proc_location(target_th->first_proc);
if (!NIL_P(loc)) { if (!NIL_P(loc)) {
const VALUE *ptr = RARRAY_CONST_PTR(loc); const VALUE *ptr = RARRAY_CONST_PTR(loc);
rb_str_catf(str, "@%"PRIsVALUE":%"PRIsVALUE, ptr[0], ptr[1]); rb_str_catf(str, "@%"PRIsVALUE":%"PRIsVALUE, ptr[0], ptr[1]);
@ -3051,9 +3020,7 @@ threadptr_local_aref(rb_thread_t *th, ID id)
VALUE VALUE
rb_thread_local_aref(VALUE thread, ID id) rb_thread_local_aref(VALUE thread, ID id)
{ {
rb_thread_t *th; return threadptr_local_aref(rb_thread_ptr(thread), id);
GetThreadPtr(thread, th);
return threadptr_local_aref(th, id);
} }
/* /*
@ -3129,7 +3096,7 @@ rb_thread_fetch(int argc, VALUE *argv, VALUE self)
{ {
VALUE key, val; VALUE key, val;
ID id; ID id;
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(self);
int block_given; int block_given;
rb_check_arity(argc, 1, 2); rb_check_arity(argc, 1, 2);
@ -3141,12 +3108,12 @@ rb_thread_fetch(int argc, VALUE *argv, VALUE self)
} }
id = rb_check_id(&key); id = rb_check_id(&key);
GetThreadPtr(self, th);
if (id == recursive_key) { if (id == recursive_key) {
return th->ec.local_storage_recursive_hash; return target_th->ec.local_storage_recursive_hash;
} }
else if (id && th->ec.local_storage && st_lookup(th->ec.local_storage, id, &val)) { else if (id && target_th->ec.local_storage &&
st_lookup(target_th->ec.local_storage, id, &val)) {
return val; return val;
} }
else if (block_given) { else if (block_given) {
@ -3188,14 +3155,11 @@ threadptr_local_aset(rb_thread_t *th, ID id, VALUE val)
VALUE VALUE
rb_thread_local_aset(VALUE thread, ID id, VALUE val) rb_thread_local_aset(VALUE thread, ID id, VALUE val)
{ {
rb_thread_t *th;
GetThreadPtr(thread, th);
if (OBJ_FROZEN(thread)) { if (OBJ_FROZEN(thread)) {
rb_error_frozen("thread locals"); rb_error_frozen("thread locals");
} }
return threadptr_local_aset(th, id, val); return threadptr_local_aset(rb_thread_ptr(thread), id, val);
} }
/* /*
@ -3292,12 +3256,8 @@ rb_thread_variable_set(VALUE thread, VALUE id, VALUE val)
static VALUE static VALUE
rb_thread_key_p(VALUE self, VALUE key) rb_thread_key_p(VALUE self, VALUE key)
{ {
rb_thread_t *th;
ID id = rb_check_id(&key); ID id = rb_check_id(&key);
st_table *local_storage; st_table *local_storage = rb_thread_ptr(self)->ec.local_storage;
GetThreadPtr(self, th);
local_storage= th->ec.local_storage;
if (!id || local_storage == NULL) { if (!id || local_storage == NULL) {
return Qfalse; return Qfalse;
@ -3340,12 +3300,11 @@ rb_thread_alone(void)
static VALUE static VALUE
rb_thread_keys(VALUE self) rb_thread_keys(VALUE self)
{ {
rb_thread_t *th; st_table *local_storage = rb_thread_ptr(self)->ec.local_storage;
VALUE ary = rb_ary_new(); VALUE ary = rb_ary_new();
GetThreadPtr(self, th);
if (th->ec.local_storage) { if (local_storage) {
st_foreach(th->ec.local_storage, thread_keys_i, ary); st_foreach(local_storage, thread_keys_i, ary);
} }
return ary; return ary;
} }
@ -3441,9 +3400,7 @@ rb_thread_variable_p(VALUE thread, VALUE key)
static VALUE static VALUE
rb_thread_priority(VALUE thread) rb_thread_priority(VALUE thread)
{ {
rb_thread_t *th; return INT2NUM(rb_thread_ptr(thread)->priority);
GetThreadPtr(thread, th);
return INT2NUM(th->priority);
} }
@ -3476,13 +3433,11 @@ rb_thread_priority(VALUE thread)
static VALUE static VALUE
rb_thread_priority_set(VALUE thread, VALUE prio) rb_thread_priority_set(VALUE thread, VALUE prio)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thread);
int priority; int priority;
GetThreadPtr(thread, th);
#if USE_NATIVE_THREAD_PRIORITY #if USE_NATIVE_THREAD_PRIORITY
th->priority = NUM2INT(prio); target_th->priority = NUM2INT(prio);
native_thread_apply_priority(th); native_thread_apply_priority(th);
#else #else
priority = NUM2INT(prio); priority = NUM2INT(prio);
@ -3492,9 +3447,9 @@ rb_thread_priority_set(VALUE thread, VALUE prio)
else if (priority < RUBY_THREAD_PRIORITY_MIN) { else if (priority < RUBY_THREAD_PRIORITY_MIN) {
priority = RUBY_THREAD_PRIORITY_MIN; priority = RUBY_THREAD_PRIORITY_MIN;
} }
th->priority = priority; target_th->priority = priority;
#endif #endif
return INT2NUM(th->priority); return INT2NUM(target_th->priority);
} }
/* for IO */ /* for IO */
@ -4379,11 +4334,9 @@ thgroup_enclosed_p(VALUE group)
static VALUE static VALUE
thgroup_add(VALUE group, VALUE thread) thgroup_add(VALUE group, VALUE thread)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thread);
struct thgroup *data; struct thgroup *data;
GetThreadPtr(thread, th);
if (OBJ_FROZEN(group)) { if (OBJ_FROZEN(group)) {
rb_raise(rb_eThreadError, "can't move to the frozen thread group"); rb_raise(rb_eThreadError, "can't move to the frozen thread group");
} }
@ -4392,20 +4345,20 @@ thgroup_add(VALUE group, VALUE thread)
rb_raise(rb_eThreadError, "can't move to the enclosed thread group"); rb_raise(rb_eThreadError, "can't move to the enclosed thread group");
} }
if (!th->thgroup) { if (!target_th->thgroup) {
return Qnil; return Qnil;
} }
if (OBJ_FROZEN(th->thgroup)) { if (OBJ_FROZEN(target_th->thgroup)) {
rb_raise(rb_eThreadError, "can't move from the frozen thread group"); rb_raise(rb_eThreadError, "can't move from the frozen thread group");
} }
TypedData_Get_Struct(th->thgroup, struct thgroup, &thgroup_data_type, data); TypedData_Get_Struct(target_th->thgroup, struct thgroup, &thgroup_data_type, data);
if (data->enclosed) { if (data->enclosed) {
rb_raise(rb_eThreadError, rb_raise(rb_eThreadError,
"can't move from the enclosed thread group"); "can't move from the enclosed thread group");
} }
th->thgroup = group; target_th->thgroup = group;
return group; return group;
} }

3
vm.c
View File

@ -2555,9 +2555,8 @@ th_init(rb_thread_t *th, VALUE self)
static VALUE static VALUE
ruby_thread_init(VALUE self) ruby_thread_init(VALUE self)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(self);
rb_vm_t *vm = GET_THREAD()->vm; rb_vm_t *vm = GET_THREAD()->vm;
GetThreadPtr(self, th);
th->vm = vm; th->vm = vm;
th_init(th, self); th_init(th, self);

View File

@ -889,13 +889,12 @@ threadptr_backtrace_to_ary(rb_thread_t *th, int argc, const VALUE *argv, int lev
static VALUE static VALUE
thread_backtrace_to_ary(int argc, const VALUE *argv, VALUE thval, int to_str) thread_backtrace_to_ary(int argc, const VALUE *argv, VALUE thval, int to_str)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thval);
GetThreadPtr(thval, th);
if (th->to_kill || th->status == THREAD_KILLED) if (target_th->to_kill || target_th->status == THREAD_KILLED)
return Qnil; 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 VALUE

View File

@ -672,8 +672,12 @@ typedef struct rb_control_frame_struct {
extern const rb_data_type_t ruby_threadptr_data_type; extern const rb_data_type_t ruby_threadptr_data_type;
#define GetThreadPtr(obj, ptr) \ static inline struct rb_thread_struct *
TypedData_Get_Struct((obj), rb_thread_t, &ruby_threadptr_data_type, (ptr)) 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 { enum rb_thread_status {
THREAD_RUNNABLE, THREAD_RUNNABLE,

View File

@ -212,9 +212,8 @@ rb_vmdebug_proc_dump_raw(rb_proc_t *proc)
void void
rb_vmdebug_stack_dump_th(VALUE thval) rb_vmdebug_stack_dump_th(VALUE thval)
{ {
rb_thread_t *th; rb_thread_t *target_th = rb_thread_ptr(thval);
GetThreadPtr(thval, th); rb_vmdebug_stack_dump_raw(target_th, target_th->ec.cfp);
rb_vmdebug_stack_dump_raw(th, th->ec.cfp);
} }
#if VMDEBUG > 2 #if VMDEBUG > 2
@ -327,9 +326,7 @@ rb_vmdebug_debug_print_register(rb_thread_t *th)
void void
rb_vmdebug_thread_dump_regs(VALUE thval) rb_vmdebug_thread_dump_regs(VALUE thval)
{ {
rb_thread_t *th; rb_vmdebug_debug_print_register(rb_thread_ptr(thval));
GetThreadPtr(thval, th);
rb_vmdebug_debug_print_register(th);
} }
void void
@ -399,10 +396,8 @@ rb_vmdebug_debug_print_post(rb_thread_t *th, rb_control_frame_t *cfp
VALUE VALUE
rb_vmdebug_thread_dump_state(VALUE self) rb_vmdebug_thread_dump_state(VALUE self)
{ {
rb_thread_t *th; rb_thread_t *th = rb_thread_ptr(self);
rb_control_frame_t *cfp; rb_control_frame_t *cfp = th->ec.cfp;
GetThreadPtr(self, th);
cfp = th->ec.cfp;
fprintf(stderr, "Thread state dump:\n"); fprintf(stderr, "Thread state dump:\n");
fprintf(stderr, "pc : %p, sp : %p\n", (void *)cfp->pc, (void *)cfp->sp); fprintf(stderr, "pc : %p, sp : %p\n", (void *)cfp->pc, (void *)cfp->sp);

View File

@ -92,14 +92,6 @@ recalc_remove_ruby_vm_event_flags(rb_event_flag_t events)
/* add/remove hooks */ /* 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 * 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) 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 *th, rb_event_hook_func_t func, rb_event
void void
rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data) 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 void
@ -149,7 +141,7 @@ rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
void 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_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 void
@ -189,13 +181,13 @@ rb_threadptr_remove_event_hook(rb_thread_t *th, rb_event_hook_func_t func, VALUE
int int
rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func) 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 int
rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data) 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 int
@ -519,10 +511,7 @@ thread_add_trace_func(rb_thread_t *th, VALUE trace)
static VALUE static VALUE
thread_add_trace_func_m(VALUE obj, VALUE trace) thread_add_trace_func_m(VALUE obj, VALUE trace)
{ {
rb_thread_t *th; thread_add_trace_func(rb_thread_ptr(obj), trace);
GetThreadPtr(obj, th);
thread_add_trace_func(th, trace);
return trace; return trace;
} }
@ -538,19 +527,19 @@ thread_add_trace_func_m(VALUE obj, VALUE trace)
*/ */
static VALUE 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(target_th, call_trace_func, Qundef);
rb_threadptr_remove_event_hook(th, call_trace_func, Qundef);
if (NIL_P(trace)) { if (NIL_P(trace)) {
return Qnil; return Qnil;
} }
else {
thread_add_trace_func(th, trace); thread_add_trace_func(target_th, trace);
return trace; return trace;
}
} }
static const char * static const char *
@ -1218,9 +1207,10 @@ tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void
VALUE VALUE
rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data) 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)) { if (RTEST(target_thval)) {
GetThreadPtr(target_thval, target_th); target_th = rb_thread_ptr(target_thval);
/* TODO: Test it! /* TODO: Test it!
* Warning: This function is not tested. * Warning: This function is not tested.
*/ */