なかだです。

 rubyist MLから転送。Threadのメソッド関連です。

At Sun, 07 Oct 2001 15:18:44 +0900 (JST),
Koji Arai <JCA02266 / nifty.ne.jp> wrote:
> o Thread#status が aborting 状態に対して "run" を返すのは仕様
>   ですか?(意味は通るけど、ソースからは読みとれなかったので確
>   認)

 Thread#inspectでは"aborting"になるので、ヘンな気がします。

> o Thread.exit, Thread.kill, Thread#exit, Thread#kill, がスレッ
>   ドの終了値(th->result)を設定しないのは仕様ですか?

 killで設定できるのはむしろヘンだと思いますが、exitはresultを
渡せるようになっててもいいかも知れません。

> o Thread#priority = val が val を返さず self を返すのはバグですよね?

 でしょう。


Index: eval.c =================================================================== RCS file: /cvs/ruby/src/ruby/eval.c,v retrieving revision 1.209 diff -u -2 -p -r1.209 eval.c --- eval.c 2001/10/05 05:49:02 1.209 +++ eval.c 2001/10/09 04:23:39 @@ -7111,4 +7111,22 @@ struct thread { #define END_FOREACH(x) END_FOREACH_FROM(curr_thread,x) +static const char * +thread_status_name(status) + enum thread_status status; +{ + switch (status) { + case THREAD_RUNNABLE: + return "run"; + case THREAD_STOPPED: + return "sleep"; + case THREAD_TO_KILL: + return "aborting"; + case THREAD_KILLED: + return "dead"; + default: + return "unknown"; + } +} + /* $SAFE accessor */ void @@ -8009,6 +8027,6 @@ rb_thread_run(thread) static VALUE -rb_thread_kill(thread) - VALUE thread; +thread_kill(thread, result) + VALUE thread, result; { rb_thread_t th = rb_thread_check(thread); @@ -8024,4 +8042,5 @@ rb_thread_kill(thread) th->gid = 0; th->status = THREAD_TO_KILL; + th->result = result; if (!rb_thread_critical) rb_thread_schedule(); return thread; @@ -8029,4 +8048,11 @@ rb_thread_kill(thread) static VALUE +rb_thread_kill(th) + VALUE th; +{ + return thread_kill(th, Qfalse); +} + +static VALUE rb_thread_s_kill(obj, th) VALUE obj, th; @@ -8036,7 +8062,21 @@ rb_thread_s_kill(obj, th) static VALUE -rb_thread_exit() +rb_thread_exit(argc, argv, th) + int argc; + VALUE *argv; + VALUE th; +{ + VALUE result = Qfalse; + + rb_scan_args(argc, argv, "01", &result); + return rb_thread_kill(th, result); +} + +static VALUE +rb_thread_s_exit(argc, argv) + int argc; + VALUE *argv; { - return rb_thread_kill(curr_thread->thread); + return rb_thread_exit(argc, argv, curr_thread->thread); } @@ -8135,5 +8175,5 @@ rb_thread_priority_set(thread, prio) th->priority = NUM2INT(prio); rb_thread_schedule(); - return thread; + return prio; } @@ -8487,7 +8527,5 @@ rb_thread_status(thread) } - if (th->status == THREAD_STOPPED) - return rb_str_new2("sleep"); - return rb_str_new2("run"); + return rb_str_new2(thread_status_name(th->status)); } @@ -8778,19 +8816,7 @@ rb_thread_inspect(thread) char *cname = rb_class2name(CLASS_OF(thread)); rb_thread_t th = rb_thread_check(thread); - char *status; + const char *status = thread_status_name(th->status); VALUE str; - switch (th->status) { - case THREAD_RUNNABLE: - status = "run"; break; - case THREAD_STOPPED: - status = "sleep"; break; - case THREAD_TO_KILL: - status = "aborting"; break; - case THREAD_KILLED: - status = "dead"; break; - default: - status = "unknown"; break; - } str = rb_str_new(0, strlen(cname)+7+16+9+1); /* 7:tags 16:addr 9:status 1:nul */ sprintf(RSTRING(str)->ptr, "#<%s:0x%lx %s>", cname, thread, status); @@ -8958,5 +8984,5 @@ Init_Thread() rb_define_singleton_method(rb_cThread, "stop", rb_thread_stop, 0); rb_define_singleton_method(rb_cThread, "kill", rb_thread_s_kill, 1); - rb_define_singleton_method(rb_cThread, "exit", rb_thread_exit, 0); + rb_define_singleton_method(rb_cThread, "exit", rb_thread_s_exit, -1); rb_define_singleton_method(rb_cThread, "pass", rb_thread_pass, 0); rb_define_singleton_method(rb_cThread, "current", rb_thread_current, 0); @@ -8973,5 +8999,5 @@ Init_Thread() rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0); rb_define_method(rb_cThread, "kill", rb_thread_kill, 0); - rb_define_method(rb_cThread, "exit", rb_thread_kill, 0); + rb_define_method(rb_cThread, "exit", rb_thread_exit, -1); rb_define_method(rb_cThread, "value", rb_thread_value, 0); rb_define_method(rb_cThread, "status", rb_thread_status, 0);
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦