Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 58cd1331cb7b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ff9a618cfb27
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 21, 2017

  1. Copy the full SHA
    0c38d21 View commit details
  2. Add spec for tiny Mutex#sleep times.

    See #4862
    headius committed Nov 21, 2017
    Copy the full SHA
    ff9a618 View commit details
Showing with 30 additions and 1 deletion.
  1. +11 −1 core/src/main/java/org/jruby/ext/thread/Mutex.java
  2. +19 −0 spec/ruby/core/mutex/sleep_spec.rb
12 changes: 11 additions & 1 deletion core/src/main/java/org/jruby/ext/thread/Mutex.java
Original file line number Diff line number Diff line change
@@ -129,15 +129,25 @@ public IRubyObject sleep(ThreadContext context) {
public IRubyObject sleep(ThreadContext context, IRubyObject timeout) {
long beg = System.currentTimeMillis();
double t = timeout.convertToFloat().getDoubleValue();

if (t < 0) throw context.runtime.newArgumentError("negative sleep timeout");

unlock(context);

try {
context.getThread().sleep((long) (t * 1000));
long millis = (long) (t * 1000);

if (Double.compare(t, 0.0d) == 0 || millis == 0) {
// wait time is zero or smaller than 1ms, so we just proceed
} else {
context.getThread().sleep(millis);
}
} catch (InterruptedException ex) {
// ignore interrupted
} finally {
lock(context);
}

return context.runtime.newFixnum((System.currentTimeMillis() - beg) / 1000);
}

19 changes: 19 additions & 0 deletions spec/ruby/core/mutex/sleep_spec.rb
Original file line number Diff line number Diff line change
@@ -71,4 +71,23 @@
m.lock
m.sleep(0.01).should be_kind_of(Integer)
end

it "wakes up when requesting sleep times near or equal to zero" do
times = []
val = 1

# power of two divisor so we eventually get near zero
loop do
val = val / 16.0
times << val
break if val == 0.0
end

m = Mutex.new
m.lock
times.each do |time|
# just testing that sleep completes
m.sleep(time).should_not == nil
end
end
end