Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/java/org/jruby/FiberScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.cli.Options;
import org.jruby.util.io.OpenFile;

import java.nio.ByteBuffer;
Expand All @@ -13,6 +14,15 @@
import static org.jruby.api.Error.argumentError;

public class FiberScheduler {
// MRI: rb_fiber_scheduler_current, null when there is no scheduler to defer to
public static IRubyObject current(ThreadContext context) {
if (!Options.FIBER_SCHEDULER.load()) return null;

IRubyObject scheduler = context.getFiberCurrentThread().getSchedulerCurrent();

return scheduler.isNil() ? null : scheduler;
}

// MRI: rb_fiber_scheduler_kernel_sleep
public static IRubyObject kernelSleep(ThreadContext context, IRubyObject scheduler, IRubyObject timeout) {
return scheduler.callMethod(context, "kernel_sleep", timeout);
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,9 @@ private int userTeardown(ThreadContext context) {
context.pushScope(new ManyVarsDynamicScope(topStaticScope, null));
}

// MRI: rb_ec_teardown. Run out this thread's scheduler before any at-exit handler
context.getFiberCurrentThread().setFiberScheduler(context, context.nil);

// Run all exit functions from user hooks like at_exit
while (!exitBlocks.isEmpty()) {
ExitFunction fun = exitBlocks.remove(0);
Expand Down
30 changes: 29 additions & 1 deletion core/src/main/java/org/jruby/RubyKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,23 @@ public static IRubyObject select(ThreadContext context, IRubyObject recv, IRubyO
return RubyIO.select(context, recv, args);
}

// MRI: rb_f_sleep
@JRubyMethod(module = true, visibility = PRIVATE)
public static IRubyObject sleep(ThreadContext context, IRubyObject recv) {
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return schedulerSleep(context, scheduler);

// Zero sleeps forever
return sleepCommon(context, 0);
}

// MRI: rb_f_sleep
@JRubyMethod(module = true, visibility = PRIVATE)
public static IRubyObject sleep(ThreadContext context, IRubyObject recv, IRubyObject timeout) {
if (timeout.isNil()) return sleep(context, recv);
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return schedulerSleep(context, scheduler, timeout);

if (timeout.isNil()) return sleepCommon(context, 0);

long nanoseconds = (long) (RubyTime.convertTimeInterval(context, timeout) * 1_000_000_000);

Expand All @@ -881,6 +889,22 @@ public static IRubyObject sleep(ThreadContext context, IRubyObject recv, IRubyOb
return sleepCommon(context, nanoseconds);
}

private static RubyFixnum schedulerSleep(ThreadContext context, IRubyObject scheduler) {
final long startTime = System.nanoTime();

FiberScheduler.kernelSleep(context, scheduler, IRubyObject.NULL_ARRAY);

return sleptSeconds(context, startTime);
}

private static RubyFixnum schedulerSleep(ThreadContext context, IRubyObject scheduler, IRubyObject timeout) {
final long startTime = System.nanoTime();

FiberScheduler.kernelSleep(context, scheduler, timeout);

return sleptSeconds(context, startTime);
}

private static RubyFixnum sleepCommon(ThreadContext context, long nanoseconds) {
final long startTime = System.nanoTime();
final RubyThread rubyThread = context.getThread();
Expand All @@ -904,6 +928,10 @@ private static RubyFixnum sleepCommon(ThreadContext context, long nanoseconds) {
if (interrupted) Thread.currentThread().interrupt();
}

return sleptSeconds(context, startTime);
}

private static RubyFixnum sleptSeconds(ThreadContext context, long startTime) {
return asFixnum(context, Math.round((System.nanoTime() - startTime) / 1_000_000_000.0));
}

Expand Down
2 changes: 0 additions & 2 deletions spec/tags/ruby/core/kernel/sleep_tags.txt

This file was deleted.

2 changes: 2 additions & 0 deletions test/mri/excludes/TestFiberMutex.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exclude :test_condition_variable, "hangs on macos m1"
exclude :test_mutex_deadlock, "subprocess times out probably not raising deadlock error"
exclude :test_mutex_fiber_raise, "hangs on macos m1"
exclude :test_mutex_interleaved_locking, "Thread::Mutex#lock does not defer to Fiber::Scheduler, so a fiber sleeping under the lock blocks its siblings"
exclude :test_queue, "Thread::Queue#pop does not defer to Fiber::Scheduler, so it blocks the thread that has to run the scheduler"
exclude :test_queue_pop_waits, "wonky subprocess launching in test"
1 change: 1 addition & 0 deletions test/mri/excludes/TestFiberScheduler.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude :test_autoload, "autoload holds a Java monitor across Fiber switches, so a sibling fiber hangs instead of deferring to the scheduler"
exclude :test_condition_variable, "hangs on macos M1"
exclude :test_current_scheduler, "fails intermittently on Linux CI on GHA"
exclude :test_io_read_error, "not hooked up to Scheduler yet, https://github.com/jruby/jruby/issues/9275"
Expand Down
2 changes: 0 additions & 2 deletions test/mri/excludes/TestFiberSleep.rb

This file was deleted.

Loading