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

ruby-changes:24218

From: akr <ko1@a...>
Date: Mon, 2 Jul 2012 22:15:41 +0900 (JST)
Subject: [ruby-changes:24218] akr:r36269 (trunk): * thread.c (rb_thread_aref): add explanation for why Thread#[] and

akr	2012-07-02 22:15:29 +0900 (Mon, 02 Jul 2012)

  New Revision: 36269

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36269

  Log:
    * thread.c (rb_thread_aref): add explanation for why Thread#[] and
      Thread#[]= are fiber-local and not thread-local.
      reported by Julien A.  [ruby-core:41606] [ruby-trunk - Bug #5750]

  Modified files:
    trunk/ChangeLog
    trunk/thread.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 36268)
+++ ChangeLog	(revision 36269)
@@ -1,3 +1,9 @@
+Mon Jul  2 22:13:04 2012  Tanaka Akira  <akr@f...>
+
+	* thread.c (rb_thread_aref): add explanation for why Thread#[] and
+	  Thread#[]= are fiber-local and not thread-local.
+	  reported by Julien A.  [ruby-core:41606] [ruby-trunk - Bug #5750]
+
 Mon Jul  2 21:25:55 2012  Tanaka Akira  <akr@f...>
 
 	* time.c (timew_out_of_timet_range): specialization for
Index: thread.c
===================================================================
--- thread.c	(revision 36268)
+++ thread.c	(revision 36269)
@@ -2078,6 +2078,39 @@
  *     #<Thread:0x00000002a54220 dead>: A
  *     #<Thread:0x00000002a541a8 dead>: B
  *     #<Thread:0x00000002a54130 dead>: C
+ *
+ *  Thread#[] and Thread#[]= are not thread-local but fiber-local.
+ *  This confusion was not exist until Ruby 1.8 because
+ *  fiber is available since Ruby 1.9.
+ *  Ruby 1.9 chooses that the methods behaves fiber-local to save
+ *  following idiom for dynamic scope.
+ *
+ *    def meth(newvalue)
+ *      begin
+ *        oldvalue = Thread.current[:name]
+ *        Thread.current[:name] = newvalue
+ *        yield
+ *      ensure
+ *        Thread.current[:name] = oldvalue
+ *      end
+ *    end
+ *
+ *  The idiom may not work as dynamic scope if the methods are thread-local
+ *  and a given block switches fiber.
+ *
+ *    f = Fiber.new {
+ *      meth(1) {
+ *        Fiber.yield
+ *      }
+ *    }
+ *    meth(2) {
+ *      f.resume
+ *    }
+ *    f.resume
+ *    p Thread.current[:name]
+ *    #=> nil if fiber-local
+ *    #=> 2 if thread-local (The value 2 is leaked to outside of meth method.)
+ *
  */
 
 static VALUE

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

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