mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/monitor.rb (wait): supported timeout.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cef8a996be
commit
332e8fe51f
2 changed files with 23 additions and 11 deletions
|
@ -87,46 +87,57 @@ module MonitorMixin
|
||||||
class ConditionVariable
|
class ConditionVariable
|
||||||
class Timeout < Exception; end
|
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)
|
def wait(timeout = nil)
|
||||||
if timeout
|
|
||||||
raise NotImplementedError, "timeout is not implemented yet"
|
|
||||||
end
|
|
||||||
@monitor.__send__(:mon_check_owner)
|
@monitor.__send__(:mon_check_owner)
|
||||||
count = @monitor.__send__(:mon_exit_for_cond)
|
count = @monitor.__send__(:mon_exit_for_cond)
|
||||||
begin
|
begin
|
||||||
@cond.wait(@monitor.instance_variable_get("@mon_mutex"))
|
@cond.wait(@monitor.instance_variable_get("@mon_mutex"), timeout)
|
||||||
return true
|
return true
|
||||||
ensure
|
ensure
|
||||||
@monitor.__send__(:mon_enter_for_cond, count)
|
@monitor.__send__(:mon_enter_for_cond, count)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Calls wait repeatedly while the given block yields a truthy value.
|
||||||
|
#
|
||||||
def wait_while
|
def wait_while
|
||||||
while yield
|
while yield
|
||||||
wait
|
wait
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Calls wait repeatedly until the given block yields a truthy value.
|
||||||
|
#
|
||||||
def wait_until
|
def wait_until
|
||||||
until yield
|
until yield
|
||||||
wait
|
wait
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Wakes up the first thread in line waiting for this lock.
|
||||||
|
#
|
||||||
def signal
|
def signal
|
||||||
@monitor.__send__(:mon_check_owner)
|
@monitor.__send__(:mon_check_owner)
|
||||||
@cond.signal
|
@cond.signal
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Wakes up all threads waiting for this lock.
|
||||||
|
#
|
||||||
def broadcast
|
def broadcast
|
||||||
@monitor.__send__(:mon_check_owner)
|
@monitor.__send__(:mon_check_owner)
|
||||||
@cond.broadcast
|
@cond.broadcast
|
||||||
end
|
end
|
||||||
|
|
||||||
def count_waiters
|
|
||||||
raise NotImplementedError
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def initialize(monitor)
|
def initialize(monitor)
|
||||||
|
@ -195,7 +206,8 @@ module MonitorMixin
|
||||||
alias synchronize mon_synchronize
|
alias synchronize mon_synchronize
|
||||||
|
|
||||||
#
|
#
|
||||||
# FIXME: This isn't documented in Nutshell.
|
# Creates a new MonitorMixin::ConditionVariable associated with the
|
||||||
|
# receiver.
|
||||||
#
|
#
|
||||||
def new_cond
|
def new_cond
|
||||||
return ConditionVariable.new(self)
|
return ConditionVariable.new(self)
|
||||||
|
|
|
@ -122,7 +122,7 @@ class TestMonitor < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def _test_timedwait
|
def test_timedwait
|
||||||
cond = @monitor.new_cond
|
cond = @monitor.new_cond
|
||||||
b = "foo"
|
b = "foo"
|
||||||
queue2 = Queue.new
|
queue2 = Queue.new
|
||||||
|
@ -153,7 +153,7 @@ class TestMonitor < Test::Unit::TestCase
|
||||||
@monitor.synchronize do
|
@monitor.synchronize do
|
||||||
assert_equal("foo", c)
|
assert_equal("foo", c)
|
||||||
result3 = cond.wait(0.1)
|
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)
|
assert_equal("foo", c)
|
||||||
queue3.enq(nil)
|
queue3.enq(nil)
|
||||||
result4 = cond.wait
|
result4 = cond.wait
|
||||||
|
|
Loading…
Reference in a new issue