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

ruby-changes:14736

From: shugo <ko1@a...>
Date: Sat, 6 Feb 2010 21:35:38 +0900 (JST)
Subject: [ruby-changes:14736] Ruby:r26595 (trunk): * lib/monitor.rb (wait): supported timeout.

shugo	2010-02-06 21:31:52 +0900 (Sat, 06 Feb 2010)

  New Revision: 26595

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

  Log:
    * lib/monitor.rb (wait): supported timeout.

  Modified files:
    trunk/lib/monitor.rb
    trunk/test/monitor/test_monitor.rb

Index: lib/monitor.rb
===================================================================
--- lib/monitor.rb	(revision 26594)
+++ lib/monitor.rb	(revision 26595)
@@ -87,46 +87,57 @@
   class ConditionVariable
     class Timeout < Exception; end
 
+    #
+    # Releases the lock held in the associated monitor and waits; reacquires the lock on wakeup.
+    #
+    # If +timeout+ is given, this method returns after +timeout+ seconds passed,
+    # even if no other thread doesn't signal.
+    #
     def wait(timeout = nil)
-      if timeout
-        raise NotImplementedError, "timeout is not implemented yet"
-      end
       @monitor.__send__(:mon_check_owner)
       count = @monitor.__send__(:mon_exit_for_cond)
       begin
-        @cond.wait(@monitor.instance_variable_get("@mon_mutex"))
+        @cond.wait(@monitor.instance_variable_get("@mon_mutex"), timeout)
         return true
       ensure
         @monitor.__send__(:mon_enter_for_cond, count)
       end
     end
 
+    #
+    # Calls wait repeatedly while the given block yields a truthy value.
+    #
     def wait_while
       while yield
 	wait
       end
     end
 
+    #
+    # Calls wait repeatedly until the given block yields a truthy value.
+    #
     def wait_until
       until yield
 	wait
       end
     end
 
+    #
+    # Wakes up the first thread in line waiting for this lock.
+    #
     def signal
       @monitor.__send__(:mon_check_owner)
       @cond.signal
     end
 
+    #
+    # Wakes up all threads waiting for this lock.
+    #
     def broadcast
       @monitor.__send__(:mon_check_owner)
       @cond.broadcast
     end
 
-    def count_waiters
-      raise NotImplementedError
-    end
-
     private
 
     def initialize(monitor)
@@ -195,7 +206,8 @@
   alias synchronize mon_synchronize
 
   #
-  # FIXME: This isn't documented in Nutshell.
+  # Creates a new MonitorMixin::ConditionVariable associated with the
+  # receiver.
   #
   def new_cond
     return ConditionVariable.new(self)
Index: test/monitor/test_monitor.rb
===================================================================
--- test/monitor/test_monitor.rb	(revision 26594)
+++ test/monitor/test_monitor.rb	(revision 26595)
@@ -122,7 +122,7 @@
     end
   end
 
-  def _test_timedwait
+  def test_timedwait
     cond = @monitor.new_cond
     b = "foo"
     queue2 = Queue.new
@@ -153,7 +153,7 @@
     @monitor.synchronize do
       assert_equal("foo", c)
       result3 = cond.wait(0.1)
-      assert_equal(false, result3)
+      assert_equal(true, result3) # wait always returns true in Ruby 1.9
       assert_equal("foo", c)
       queue3.enq(nil)
       result4 = cond.wait

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

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