* vm_core.h (rb_thread_t): add th->name.
* vm.c (th_init): initialize th->name. * thread.c (Init_Thread): add Thread.name and Thread.name=. * thread.c (rb_thread_inspect): show thread's name if set. * thread.c (rb_thread_getname): defined. * thread.c (rb_thread_setname): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
654b90d31a
commit
712a570f56
14
ChangeLog
14
ChangeLog
@ -1,3 +1,17 @@
|
|||||||
|
Sat Jun 13 17:35:11 2015 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* vm_core.h (rb_thread_t): add th->name.
|
||||||
|
|
||||||
|
* vm.c (th_init): initialize th->name.
|
||||||
|
|
||||||
|
* thread.c (Init_Thread): add Thread.name and Thread.name=.
|
||||||
|
|
||||||
|
* thread.c (rb_thread_inspect): show thread's name if set.
|
||||||
|
|
||||||
|
* thread.c (rb_thread_getname): defined.
|
||||||
|
|
||||||
|
* thread.c (rb_thread_setname): ditto.
|
||||||
|
|
||||||
Sat Jun 13 11:39:43 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
Sat Jun 13 11:39:43 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||||
|
|
||||||
* lib/tempfile.rb: Fix typo. [fix GH-933] Patch by @Zorbash
|
* lib/tempfile.rb: Fix typo. [fix GH-933] Patch by @Zorbash
|
||||||
|
@ -1046,4 +1046,11 @@ q.pop
|
|||||||
t.new {}
|
t.new {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_thread_name
|
||||||
|
t = Thread.start { }
|
||||||
|
t.name = 'foo'
|
||||||
|
assert_equal 'foo', t.name
|
||||||
|
t.join
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
57
thread.c
57
thread.c
@ -2715,6 +2715,58 @@ rb_thread_safe_level(VALUE thread)
|
|||||||
return INT2NUM(th->safe_level);
|
return INT2NUM(th->safe_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* thr.name -> string
|
||||||
|
*
|
||||||
|
* Dump the name, id, and status of _thr_ to a string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_thread_getname(VALUE thread)
|
||||||
|
{
|
||||||
|
rb_thread_t *th;
|
||||||
|
GetThreadPtr(thread, th);
|
||||||
|
return th->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* thr.name=(name) -> string
|
||||||
|
*
|
||||||
|
* Dump the name, id, and status of _thr_ to a string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_thread_setname(VALUE thread, VALUE name)
|
||||||
|
{
|
||||||
|
rb_thread_t *th;
|
||||||
|
GetThreadPtr(thread, th);
|
||||||
|
th->name = rb_str_new_frozen(name);
|
||||||
|
#if defined(HAVE_PTHREAD_SETNAME_NP)
|
||||||
|
# if defined(__linux__)
|
||||||
|
pthread_setname_np(th->thread_id, RSTRING_PTR(name));
|
||||||
|
# elif defined(__NetBSD__)
|
||||||
|
pthread_setname_np(th->thread_id, RSTRING_PTR(name), "%s");
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
{
|
||||||
|
int mib[2] = {CTL_KERN, KERN_THREADNAME};
|
||||||
|
int r;
|
||||||
|
# ifndef MAXTHREADNAMESIZE
|
||||||
|
# define MAXTHREADNAMESIZE 64
|
||||||
|
# endif
|
||||||
|
int size_t len = RSTRING_LEN(name);
|
||||||
|
if (len > MAXTHREADNAMESIZE-1) len = MAXTHREADNAMESIZE-1;
|
||||||
|
r = sysctl(mib, 2, NULL, 0, RSTRING_PTR(name), len);
|
||||||
|
if (!r) rb_sys_fail("sysctl([CTL_KERN, KERN_THREADNAME],..)");
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
#elif defined(HAVE_PTHREAD_SET_NAME_NP) /* FreeBSD */
|
||||||
|
pthread_set_name_np(th->thread_id, RSTRING_PTR(name));
|
||||||
|
#endif
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* thr.inspect -> string
|
* thr.inspect -> string
|
||||||
@ -2733,6 +2785,9 @@ rb_thread_inspect(VALUE thread)
|
|||||||
GetThreadPtr(thread, th);
|
GetThreadPtr(thread, th);
|
||||||
status = thread_status_name(th);
|
status = thread_status_name(th);
|
||||||
str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
|
str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
|
||||||
|
if (!NIL_P(th->name)) {
|
||||||
|
rb_str_catf(str, "@%"PRIsVALUE, th->name);
|
||||||
|
}
|
||||||
if (!th->first_func && th->first_proc) {
|
if (!th->first_func && th->first_proc) {
|
||||||
VALUE loc = rb_proc_location(th->first_proc);
|
VALUE loc = rb_proc_location(th->first_proc);
|
||||||
if (!NIL_P(loc)) {
|
if (!NIL_P(loc)) {
|
||||||
@ -5039,6 +5094,8 @@ Init_Thread(void)
|
|||||||
rb_define_method(rb_cThread, "backtrace", rb_thread_backtrace_m, -1);
|
rb_define_method(rb_cThread, "backtrace", rb_thread_backtrace_m, -1);
|
||||||
rb_define_method(rb_cThread, "backtrace_locations", rb_thread_backtrace_locations_m, -1);
|
rb_define_method(rb_cThread, "backtrace_locations", rb_thread_backtrace_locations_m, -1);
|
||||||
|
|
||||||
|
rb_define_method(rb_cThread, "name", rb_thread_getname, 0);
|
||||||
|
rb_define_method(rb_cThread, "name=", rb_thread_setname, 1);
|
||||||
rb_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);
|
rb_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);
|
||||||
|
|
||||||
rb_vm_register_special_exception(ruby_error_closed_stream, rb_eIOError, "stream closed");
|
rb_vm_register_special_exception(ruby_error_closed_stream, rb_eIOError, "stream closed");
|
||||||
|
1
vm.c
1
vm.c
@ -2262,6 +2262,7 @@ th_init(rb_thread_t *th, VALUE self)
|
|||||||
#if OPT_CALL_THREADED_CODE
|
#if OPT_CALL_THREADED_CODE
|
||||||
th->retval = Qundef;
|
th->retval = Qundef;
|
||||||
#endif
|
#endif
|
||||||
|
th->name = Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user