[前][次][番号順一覧][スレッド一覧]

ruby-changes:42526

From: nobu <ko1@a...>
Date: Fri, 15 Apr 2016 21:33:27 +0900 (JST)
Subject: [ruby-changes:42526] nobu:r54600 (trunk): thread.c: defer setting name in initialize

nobu	2016-04-15 22:30:03 +0900 (Fri, 15 Apr 2016)

  New Revision: 54600

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54600

  Log:
    thread.c: defer setting name in initialize
    
    * thread.c (rb_thread_setname): defer setting native thread name
      set in initialize until the native thread is created.
      [ruby-core:74963] [Bug #12290]

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_thread.rb
    trunk/thread.c
    trunk/thread_pthread.c
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 54599)
+++ thread_pthread.c	(revision 54600)
@@ -1505,8 +1505,11 @@ native_set_thread_name(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1505
 {
 #ifdef SET_CURRENT_THREAD_NAME
     if (!th->first_func && th->first_proc) {
-	VALUE loc = rb_proc_location(th->first_proc);
-	if (!NIL_P(loc)) {
+	VALUE loc;
+	if (!NIL_P(loc = th->name)) {
+	    SET_CURRENT_THREAD_NAME(RSTRING_PTR(loc));
+	}
+	else if (!NIL_P(loc = rb_proc_location(th->first_proc))) {
 	    const VALUE *ptr = RARRAY_CONST_PTR(loc); /* [ String, Fixnum ] */
 	    char *name, *p;
 	    char buf[16];
Index: thread.c
===================================================================
--- thread.c	(revision 54599)
+++ thread.c	(revision 54600)
@@ -711,17 +711,7 @@ thread_create_core(VALUE thval, VALUE ar https://github.com/ruby/ruby/blob/trunk/thread.c#L711
     return thval;
 }
 
-static rb_thread_t *
-get_initialized_threadptr(VALUE thread, VALUE klass)
-{
-    rb_thread_t *th;
-    GetThreadPtr(thread, th);
-    if (!th->first_args) {
-	rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
-		 klass);
-    }
-    return th;
-}
+#define threadptr_initialized(th) ((th)->first_args != 0)
 
 /*
  * call-seq:
@@ -746,13 +736,18 @@ get_initialized_threadptr(VALUE thread, https://github.com/ruby/ruby/blob/trunk/thread.c#L736
 static VALUE
 thread_s_new(int argc, VALUE *argv, VALUE klass)
 {
+    rb_thread_t *th;
     VALUE thread = rb_thread_alloc(klass);
 
     if (GET_VM()->main_thread->status == THREAD_KILLED)
 	rb_raise(rb_eThreadError, "can't alloc thread");
 
     rb_obj_call_init(thread, argc, argv);
-    get_initialized_threadptr(thread, klass);
+    GetThreadPtr(thread, th);
+    if (!threadptr_initialized(th)) {
+	rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
+		 klass);
+    }
     return thread;
 }
 
@@ -2797,7 +2792,8 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2792
 #ifdef SET_ANOTHER_THREAD_NAME
     const char *s = "";
 #endif
-    rb_thread_t *th = get_initialized_threadptr(thread, RBASIC_CLASS(thread));
+    rb_thread_t *th;
+    GetThreadPtr(thread, th);
     if (!NIL_P(name)) {
 	rb_encoding *enc;
 	StringValueCStr(name);
@@ -2813,7 +2809,9 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2809
     }
     th->name = name;
 #if defined(SET_ANOTHER_THREAD_NAME)
-    SET_ANOTHER_THREAD_NAME(th->thread_id, s);
+    if (threadptr_initialized(th)) {
+	SET_ANOTHER_THREAD_NAME(th->thread_id, s);
+    }
 #endif
     return name;
 }
@@ -2836,7 +2834,7 @@ rb_thread_inspect(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2834
     GetThreadPtr(thread, th);
     status = thread_status_name(th);
     str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
-    if (RTEST(th->name)) {
+    if (!NIL_P(th->name)) {
 	rb_str_catf(str, "@%"PRIsVALUE, th->name);
     }
     if (!th->first_func && th->first_proc) {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54599)
+++ ChangeLog	(revision 54600)
@@ -1,10 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Fri Apr 15 21:12:23 2016  Nobuyoshi Nakada  <nobu@r...>
+Fri Apr 15 22:30:01 2016  Nobuyoshi Nakada  <nobu@r...>
 
-	* thread.c (get_initialized_threadptr): extract ensuring that the
-	  thread is initialized.
-
-	* thread.c (rb_thread_setname): thread must be initialized to set
-	  the name.  [ruby-core:74963] [Bug #12290]
+	* thread.c (rb_thread_setname): defer setting native thread name
+	  set in initialize until the native thread is created.
+	  [ruby-core:74963] [Bug #12290]
 
 Fri Apr 15 20:27:16 2016  SHIBATA Hiroshi  <hsbt@r...>
 
Index: test/ruby/test_thread.rb
===================================================================
--- test/ruby/test_thread.rb	(revision 54599)
+++ test/ruby/test_thread.rb	(revision 54600)
@@ -1099,9 +1099,9 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1099
     t.join
   end
 
-  def test_thread_setname_uninitialized
+  def test_thread_setname_in_initialize
     bug12290 = '[ruby-core:74963] [Bug #12290]'
-    c = Class.new(Thread) {def initialize() self.name = "foo" end}
-    assert_raise(ThreadError, bug12290) {c.new {}}
+    c = Class.new(Thread) {def initialize() self.name = "foo"; super; end}
+    assert_equal("foo", c.new {Thread.current.name}.value)
   end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]