Skip to content

Commit 41daaf6

Browse files
committed
Fix rounding error
1 parent 2e2a464 commit 41daaf6

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/concurrent/atomic/event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def wait(timeout = nil)
6767
synchronize do
6868
unless @set
6969
iteration = @iteration
70-
ns_wait_until(timeout) { iteration != @iteration || @set }
70+
ns_wait_until(timeout) { iteration < @iteration || @set }
7171
else
7272
true
7373
end

lib/concurrent/synchronized_object.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def synchronize
2828
raise NotImplementedError
2929
end
3030

31+
private
32+
3133
# wait until another thread calls #signal or #broadcast,
3234
# spurious wake-ups can happen.
3335
# @param [Numeric, nil] timeout in seconds, `nil` means no timeout
@@ -54,16 +56,16 @@ def broadcast
5456
synchronize { ns_broadcast }
5557
end
5658

57-
private
58-
5959
# @yield condition
6060
def ns_wait_until(timeout, &condition)
6161
if timeout
6262
wait_until = Concurrent.monotonic_time + timeout
6363
while true
6464
now = Concurrent.monotonic_time
6565
condition_result = condition.call
66-
return condition_result if now >= wait_until || condition_result
66+
# 0.001 correction to avoid error when `wait_until - now` is smaller than 0.0005 and rounded to 0
67+
# when passed to java #wait(long timeout)
68+
return condition_result if (now + 0.001) >= wait_until || condition_result
6769
ns_wait wait_until - now
6870
end
6971
else
@@ -123,7 +125,7 @@ def ns_signal
123125
class RubySynchronizedObject < AbstractSynchronizedObject
124126
def initialize
125127
@__lock__do_not_use_directly = Mutex.new
126-
@__condition__do_not_use_directly = ConditionVariable.new
128+
@__condition__do_not_use_directly = ::ConditionVariable.new
127129
end
128130

129131
def synchronize

0 commit comments

Comments
 (0)