Skip to content

Commit c85b162

Browse files
committed
Merge pull request #253 from tenderlove/monotonic
use a monotonic clock for timeouts
2 parents c3d47b2 + c498ed6 commit c85b162

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/concurrent/atomic/condition.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def initialize
4242
# @param [Object] timeout nil means no timeout
4343
# @return [Result]
4444
def wait(mutex, timeout = nil)
45-
start_time = Time.now.to_f
45+
start_time = clock_time
4646
@condition.wait(mutex, timeout)
4747

4848
if timeout.nil?
4949
Result.new(nil)
5050
else
51-
Result.new(start_time + timeout - Time.now.to_f)
51+
Result.new(start_time + timeout - clock_time)
5252
end
5353
end
5454

@@ -66,5 +66,17 @@ def broadcast
6666
true
6767
end
6868

69+
private
70+
71+
if defined?(Process::CLOCK_MONOTONIC)
72+
def clock_time
73+
Process.clock_gettime Process::CLOCK_MONOTONIC
74+
end
75+
else
76+
def clock_time
77+
Time.now.to_f
78+
end
79+
end
80+
6981
end
7082
end

0 commit comments

Comments
 (0)