Skip to content

Commit

Permalink
Allow disabling the use of vthread fibers
Browse files Browse the repository at this point in the history
In case there's an issue with vthread fibers that we have not yet
seen, this allows disabling them and going back to normal native
thread fibers.
  • Loading branch information
headius committed Aug 28, 2023
1 parent 04fbe80 commit 3f4de82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
28 changes: 15 additions & 13 deletions core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.cli.Options;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

Expand All @@ -38,28 +39,29 @@ public class ThreadFiber extends RubyObject implements ExecutionContext {
private static final MethodHandle VTHREAD_START_METHOD;

static {
BiConsumer<Ruby, Runnable> fiberLauncher;
MethodHandle start;
BiConsumer<Ruby, Runnable> fiberLauncher = ThreadFiber::nativeThreadLauncher;
MethodHandle start = null;

try {
// test that API is available
Method ofVirtualMethod = Thread.class.getMethod("ofVirtual");
Object builder = ofVirtualMethod.invoke(null);
Method startMethod = Class.forName("java.lang.Thread$Builder").getMethod("start", Runnable.class);
if (Options.FIBER_VTHREADS.load()) {
try {
// test that API is available
Method ofVirtualMethod = Thread.class.getMethod("ofVirtual");
Object builder = ofVirtualMethod.invoke(null);
Method startMethod = Class.forName("java.lang.Thread$Builder").getMethod("start", Runnable.class);

start = MethodHandles.publicLookup().unreflect(startMethod).bindTo(builder);
start = MethodHandles.publicLookup().unreflect(startMethod).bindTo(builder);

fiberLauncher = new VirtualThreadLauncher();
} catch (Throwable t) {
start = null;
fiberLauncher = ThreadFiber::accept;
fiberLauncher = new VirtualThreadLauncher();
} catch (Throwable t) {
// default impl set below
}
}

VTHREAD_START_METHOD = start;
FIBER_LAUNCHER = fiberLauncher;
}

private static void accept(Ruby runtime, Runnable runnable) {
private static void nativeThreadLauncher(Ruby runtime, Runnable runnable) {
runtime.getFiberExecutor().submit(runnable);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/util/cli/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum Category {
JIT("jit"),
IR("intermediate representation"),
NATIVE("native"),
THREADPOOL("thread pooling"),
THREADPOOL("thread pooling and launching"),
MISCELLANEOUS("miscellaneous"),
DEBUG("debugging and logging"),
JAVA_INTEGRATION("java integration"),
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/util/cli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public class Options {
public static final Option<Integer> THREADPOOL_MAX = integer(THREADPOOL, "thread.pool.max", Integer.MAX_VALUE, "The maximum number of threads to allow in the pool.");
public static final Option<Integer> THREADPOOL_TTL = integer(THREADPOOL, "thread.pool.ttl", 60, "The maximum number of seconds to keep alive an idle thread.");
public static final Option<Integer> FIBER_THREADPOOL_TTL = integer(THREADPOOL, "fiber.thread.pool.ttl", 60, "The maximum number of seconds to keep alive a pooled fiber thread.");
public static final Option<Boolean> FIBER_VTHREADS = bool(THREADPOOL, "fiber.vthread", true, "Use JDK virtual threads for Ruby Fibers.");

public static final Option<Boolean> CLASSLOADER_DELEGATE = bool(MISCELLANEOUS, "classloader.delegate", true, "In some cases of classloader conflicts it might help not to delegate first to the parent classloader but to load first from the jruby-classloader.");
public static final Option<Boolean> OBJECTSPACE_ENABLED = bool(MISCELLANEOUS, "objectspace.enabled", false, "Enable or disable ObjectSpace.each_object.");
Expand Down

0 comments on commit 3f4de82

Please sign in to comment.