/** * Creates a {@code Val} whose value will be the one returned by the given {@link SuspendableCallable}, which will be spawned * into a new fiber. * <p> * @param f The function that will compute this {@code Val}'s value in a newly spawned fiber */ public Val(final SuspendableCallable<V> f) { this(DefaultFiberScheduler.getInstance(), f); }
/** * Runs an action in a new fiber and awaits the fiber's termination. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * . * * @param target the operation * @throws ExecutionException * @throws InterruptedException */ public static void runInFiber(SuspendableRunnable target) throws ExecutionException, InterruptedException { runInFiber(DefaultFiberScheduler.getInstance(), target); }
/** * Locates a registered actor by name, or, if not actor by that name is currently registered, spawns and registers it. * This method atomically checks if an actor by the given name is registers, and if so, returns it; otherwise it spawns the actor * returned by the given factory using the default fiber scheduler, and registers it. * * @param name the actor's name. * @param actorFactory returns an actor that will be registered if one isn't currently registered. * @return the actor. */ public static <Message> ActorRef<Message> getOrRegisterActor(String name, Callable<Actor<Message, ?>> actorFactory) throws SuspendExecution { return getOrRegisterActor(name, actorFactory, DefaultFiberScheduler.getInstance()); }
/** * Runs an action in a new fiber and awaits the fiber's termination. * Unlike {@link #runInFiber(SuspendableRunnable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * * @param target the operation * @throws InterruptedException */ public static void runInFiberRuntime(SuspendableRunnable target) throws InterruptedException { runInFiberRuntime(DefaultFiberScheduler.getInstance(), target); }
/** * Runs an action in a new fiber and awaits the fiber's termination. * Unlike {@link #runInFiber(SuspendableRunnable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}, unless it is of the given {@code exception type}, in * which case the checked exception is thrown as-is. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * * @param target the operation * @param exceptionType a checked exception type that will not be wrapped if thrown by the operation, but thrown as-is. * @throws InterruptedException */ public static <X extends Exception> void runInFiberChecked(SuspendableRunnable target, Class<X> exceptionType) throws X, InterruptedException { runInFiberChecked(DefaultFiberScheduler.getInstance(), target, exceptionType); }
@Override public final String getName() { if (name == null) // benign race this.name = "fiber-" + ((scheduler != null && scheduler != DefaultFiberScheduler.getInstance()) ? (scheduler.getName() + '-') : "") + fid; return name; }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * Unlike {@link #runInFiber(SuspendableCallable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * * @param <V> * @param target the operation * @return the operations return value * @throws InterruptedException */ public static <V> V runInFiberRuntime(SuspendableCallable<V> target) throws InterruptedException { return runInFiberRuntime(DefaultFiberScheduler.getInstance(), target); }
/** * Hires and resumes/restarts a migrated actor. * * @param ref the {@link ActorRef} of the migrated actor. * @return the ref */ public static <M> ActorRef<M> hire(ActorRef<M> ref) throws SuspendExecution { return hire(ref, DefaultFiberScheduler.getInstance()); }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * * @param <V> * @param target the operation * @return the operations return value * @throws ExecutionException * @throws InterruptedException */ public static <V> V runInFiber(SuspendableCallable<V> target) throws ExecutionException, InterruptedException { return runInFiber(DefaultFiberScheduler.getInstance(), target); }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * Unlike {@link #runInFiber(SuspendableCallable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}. * The new fiber is scheduled by the {@link DefaultFiberScheduler default scheduler}. * * @param <V> * @param target the operation * @return the operations return value * @throws InterruptedException */ public static <V, X extends Exception> V runInFiberChecked(SuspendableCallable<V> target, Class<X> exceptionType) throws X, InterruptedException { return runInFiberChecked(DefaultFiberScheduler.getInstance(), target, exceptionType); }
private static FiberScheduler defaultScheduler() { final Fiber parent = currentFiber(); if (parent == null) return DefaultFiberScheduler.getInstance(); else return parent.getScheduler(); }
/** * Creates a new {@code Var}, whose value is set to the value returned by the given function {@code f}; the * function will be re-applied, and the {@code Var}'s value re-set, whenever any of the {@code Var}s referenced * by {@code f} change value. * * @param history how many historical values to maintain for each strand reading the var. * @param scheduler the {@link FiberScheduler} to use to schedule the fiber that will run, and re-run {@code f}. * @param f this var's value is set to the return value of {@code f} * @see #get() */ public Var(int history, FiberScheduler scheduler, SuspendableCallable<T> f) { if (history < 0) throw new IllegalArgumentException("history must be >= 0, but is " + history); this.ch = Channels.newChannel(1 + history, Channels.OverflowPolicy.DISPLACE); this.f = f; if (f != null) new VarFiber<T>(scheduler != null ? scheduler : DefaultFiberScheduler.getInstance(), this).start(); }
final Recorder<T>... rs) return new Fiber<Void>(fiberName, fe != null ? fe : DefaultFiberScheduler.getInstance(), () -> { while (true) { final TimingEvent<T> event = rp.receive();