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

ruby-changes:26052

From: kosaki <ko1@a...>
Date: Sat, 1 Dec 2012 17:28:55 +0900 (JST)
Subject: [ruby-changes:26052] kosaki:r38109 (trunk): * lib/thread.rb (ConditionVariable): use hash instead of array for

kosaki	2012-12-01 17:28:44 +0900 (Sat, 01 Dec 2012)

  New Revision: 38109

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

  Log:
    * lib/thread.rb (ConditionVariable): use hash instead of array for
      @waiters.
    * test/thread/test_queue.rb (test_sized_queue_and_wakeup): remove
      a test because @waiters no longer have a chance to duplicated. Now it's
      a hash.

  Modified files:
    trunk/ChangeLog
    trunk/lib/thread.rb
    trunk/test/thread/test_queue.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38108)
+++ ChangeLog	(revision 38109)
@@ -1,3 +1,11 @@
+Sat Dec  1 14:23:33 2012  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* lib/thread.rb (ConditionVariable): use hash instead of array for
+	  @waiters.
+	* test/thread/test_queue.rb (test_sized_queue_and_wakeup): remove
+	  a test because @waiters no longer have a chance to duplicated. Now it's
+	  a hash.
+
 Sat Dec  1 17:16:54 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* misc/ruby-electric.el (ruby-electric-curlies): use kill-region
Index: lib/thread.rb
===================================================================
--- lib/thread.rb	(revision 38108)
+++ lib/thread.rb	(revision 38109)
@@ -52,7 +52,7 @@
   # Creates a new ConditionVariable
   #
   def initialize
-    @waiters = []
+    @waiters = {}
     @waiters_mutex = Mutex.new
   end
 
@@ -67,7 +67,7 @@
       begin
         Thread.async_interrupt_timing(StandardError => :on_blocking) do
           @waiters_mutex.synchronize do
-            @waiters.push(Thread.current)
+            @waiters[Thread.current] = true
           end
           mutex.sleep timeout
         end
@@ -86,10 +86,10 @@
   def signal
     Thread.async_interrupt_timing(StandardError => :on_blocking) do
       begin
-        t = @waiters_mutex.synchronize {@waiters.shift}
+        t, _ = @waiters_mutex.synchronize { @waiters.shift }
         t.run if t
       rescue ThreadError
-        retry # t was alread dead?
+        retry # t was already dead?
       end
     end
     self
@@ -100,12 +100,12 @@
   #
   def broadcast
     Thread.async_interrupt_timing(StandardError => :on_blocking) do
-      waiters0 = nil
+      threads = nil
       @waiters_mutex.synchronize do
-        waiters0 = @waiters.dup
+        threads = @waiters.keys
         @waiters.clear
       end
-      for t in waiters0
+      for t in threads
         begin
           t.run
         rescue ThreadError
Index: test/thread/test_queue.rb
===================================================================
--- test/thread/test_queue.rb	(revision 38108)
+++ test/thread/test_queue.rb	(revision 38109)
@@ -56,24 +56,6 @@
     assert_equal(1, q.max)
   end
 
-  def test_sized_queue_and_wakeup
-    sq = SizedQueue.new(1)
-    sq.push(0)
-
-    t1 = Thread.start { sq.push(1) ; sleep }
-
-    sleep 0.1 until t1.stop?
-    t1.wakeup
-    sleep 0.1 until t1.stop?
-
-    t2 = Thread.start { sq.push(2) }
-    sleep 0.1 until t1.stop? && t2.stop?
-
-    enque_cond = sq.instance_eval{ @enque_cond }
-    queue_wait = enque_cond.instance_eval { @waiters }
-    assert_equal(queue_wait.uniq, queue_wait)
-  end
-
   def test_queue_pop_interrupt
     q = Queue.new
     t1 = Thread.new { q.pop }

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

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