ruby-changes:40756
From: nobu <ko1@a...>
Date: Tue, 1 Dec 2015 23:37:07 +0900 (JST)
Subject: [ruby-changes:40756] nobu:r52835 (trunk): thread.c: reset name
nobu 2015-12-01 23:36:42 +0900 (Tue, 01 Dec 2015) New Revision: 52835 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52835 Log: thread.c: reset name * thread.c (rb_thread_setname): allow to reset thread name. Modified files: trunk/ChangeLog trunk/test/ruby/test_thread.rb trunk/thread.c Index: ChangeLog =================================================================== --- ChangeLog (revision 52834) +++ ChangeLog (revision 52835) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Dec 1 23:36:39 2015 Nobuyoshi Nakada <nobu@r...> + + * thread.c (rb_thread_setname): allow to reset thread name. + Tue Dec 1 23:14:04 2015 Nobuyoshi Nakada <nobu@r...> * thread.c (rb_thread_setname): check the argument if valid Index: thread.c =================================================================== --- thread.c (revision 52834) +++ thread.c (revision 52835) @@ -2774,18 +2774,23 @@ rb_thread_getname(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2774 static VALUE rb_thread_setname(VALUE thread, VALUE name) { + const char *s = ""; rb_thread_t *th; GetThreadPtr(thread, th); - StringValueCStr(name); - th->name = rb_str_new_frozen(name); + if (!NIL_P(name)) { + StringValueCStr(name); + name = rb_str_new_frozen(name); + s = RSTRING_PTR(name); + } + th->name = name; #if defined(HAVE_PTHREAD_SETNAME_NP) # if defined(__linux__) - pthread_setname_np(th->thread_id, RSTRING_PTR(name)); + pthread_setname_np(th->thread_id, s); # elif defined(__NetBSD__) - pthread_setname_np(th->thread_id, RSTRING_PTR(name), "%s"); + pthread_setname_np(th->thread_id, s, "%s"); # endif #elif defined(HAVE_PTHREAD_SET_NAME_NP) /* FreeBSD */ - pthread_set_name_np(th->thread_id, RSTRING_PTR(name)); + pthread_set_name_np(th->thread_id, s); #endif return name; } Index: test/ruby/test_thread.rb =================================================================== --- test/ruby/test_thread.rb (revision 52834) +++ test/ruby/test_thread.rb (revision 52835) @@ -1050,9 +1050,14 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1050 end def test_thread_name - t = Thread.start { sleep } + t = Thread.start {} + assert_nil t.name + s = t.inspect t.name = 'foo' assert_equal 'foo', t.name + t.name = nil + assert_nil t.name + assert_equal s, t.inspect ensure t.kill t.join @@ -1070,7 +1075,7 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1075 def test_thread_invalid_object bug11756 = '[ruby-core:71774] [Bug #11756]' t = Thread.start {} - assert_raise(TypeError, bug11756) {t.name = nil} + assert_raise(TypeError, bug11756) {t.name = []} ensure t.kill t.join -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/