Loom is under active development, which means that information and advice given here might change in the future.
Mac and Linux on x86-64
Download and Build from Source
(Note that you must switch to the
fibers branch before building)
JVM TI support (fiber debugging)
- Yielding while a native VM frame is on the stack in the case of a priviliged action, reflective invocation and
- Cloning continuations
Serialization of fiber/continuation
The primitive continuation construct is that of a scoped (AKA multiple-named-prompt), stackful, one-shot (non-reentrant) delimited continuation. The continuation can be cloned, and thus used to implement reentrant delimited continuations. The construct is exposed via the
java.lang.Continuation class. Continuations are intended as a low-level API, that application authors are not intended to use directly. They will use higher-level constructs built on top of continuations, such as fibers or generators.
A continuation object is constructed by passing two arguments to the constructor: a
Runnable target that serves as the body of the continuation, and a
java.lang.ContinuationScope. The scope is the delimited continuation's prompt, that allows continuations to be nested. One could think of such "scoped continuations" as nested
try/catch blocks, where the scope is the type of the exception thrown, which determines the handler called.
A continuation is started by calling
Continuation.run, which would start executing the body in the continuation's target, and returns either when the continuation terminates (the body runs to completion, and terminates either normally or abnormally), or when it yields on the continuation's scope. To query the reason for
run returning, use
Continuation.isDone, which returns
true if the body has terminated, or
false if it has yielded.
A call to the static
Continuation.yield suspends the current continuation and all enclosing continuations up until the innermost one with the scope passd to
yield, causing the
run method of that continuation to return.
Continuation class does not provide a mechanism of communication between
yield (i.e., neither takes or returns a value that is passed to/received from the other), unlike most implementations of delimited continuations. However, implementing a class that does allow this kind of communication on top of the
Continuation class is straightforward, and will likely be included in the JDK.
The current prototype limits the situation in which a continuation can be yielded: a continuation cannot yield while a native method is on its stack (i.e. on the stack used by the continuation body when it attempts to yield) – this can happen when a VM or a JNI method is called from the continuation body and then calls back into Java code that attempts to yield – or while the body of the continuation holds a native monitor (i.e. a yield is called – directly or indirectly – from inside a synchronized method or a synchronized block). In these situations, the continuation is said to be pinned (to the mounted thread). Attempting to yield while pinned results in a call to the protected method
Continuation.onPinned, which by default throws an exception (fibers override this behavior; see below).
It is likely that we will never support yielding with a JNI method on the stack, but we will support most common cases of VM methods – privileged actions (this may be resolved by implementing that security feature in Java), reflective invocation (this may also be a non-issue, as reflection does support a pure Java path by generating bytecode), and
MethodHandle invocation. It is not yet decided whether or not we will choose to support yielding while holding native monitors.
Current yield/continue performance is far from stellar. The reason is that we focused on getting a working prototype using existing Hotspot mechanisms, some of which have not been designed to be used so frequently. We are now working on improving performance both by optimizing the actual freeze/thaw logic, as well as optimizing those existing VM mechanisms.
One mechanism that is particularly slow is the one used to detect whether a frame is holding a native monitor (synchronized block). Because this mechanism is so slow, the monitor detection can be turned off (which will break fiber code that runs through synchronized blocks) by adding
-XX:-DetectLocksInCompiledFrames to the
java command line.
An important performance feature is lazy-copying of frames. This feature is currently turned off by default because it is missing some important functionality, in particular it does not support the throwing of exceptions, and stack traces will miss some frames (those that have not been thawed). To turn lazy copying on, add
-XX:+UnlockExperimentalVMOptions -XX:+UseNewCode to the
java command line.
Fibers are user-mode lightweight threads that allow synchronous (blocking) code to be efficiently scheduled, so that it performs as well as asynchronous code, but is simpler to read and write, debug, monitor and profile.
There is a tension in the design of fibers between attempting to run as much existing code as possible and our desire to take the opportunity to rethink how modern threads should work, with an eye to new software requirements, programming styles and new/changing hardware. Even if we choose not to run every piece of existing code in fibers, we should provide an easy path to migration.
In the current prototype, fibers are expressed by the
java.lang.Fiber class, and both
Thread extend the
java.lang.Strand class, which will contain the shared portions of the API. Exactly what
Strand should contain, and what mechanisms afforded by
Thread should also be supported by
Fiber are now subject to discussion and experimentation.
Fibers are implemented in the core libraries. A fiber is implemented as a continuation (of fiber scope) that is wrapped as a task and scheduled by a
j.u.c.Executor. Parking (blocking) a fiber results in yielding its continuation, and unparking it results in the continuation being resubmitted to the scheduler. The scheduler worker thread executing a fiber (while its continuation is mounted) is called a carrier thread.
The continuations used in the fiber implementation override
onPinned such that if a fiber attempts to park while its continuation is pinned (see above), it will block the underlying carrier thread.
We envision tail-call elimination that pops one or perhaps even an arbitrary number of stack frames at explicitly marked call-sites. It is not the intention of this project to implement automatic tail-call optimization.
The implementation of this feature requires cross-cutting changes to the VM, VM specification (bytecode), and possibly the front-end Java compiler (javac). As a result, in order not to delay the completion of continuations and fibers, we will only begin specifying and implementing this feature only when the project is at a more advanced phase.