eve.rt.fiber
EVE Runtime Fiber Support
This module provides lightweight fiber/coroutine primitives that integrate with the EVE event loop. Fibers enable cooperative multitasking, allowing multiple logical execution contexts to run on a single thread.
Fibers can yield execution voluntarily and be resumed later, making them ideal for async I/O operations where blocking would waste resources.
This module does not reimplement fibers. It wraps D's own
core.thread.Fiber as a value-type struct with EVE-specific state tracking and @safe ergonomics. The underlying context-switch mechanism is entirely D's.
Why a wrapper instead of using core.thread.Fiber directly?
- Value-type semantics —
struct Fibermatches EVE'sstack-allocated, GC-optional style used throughout Layers 0–2. D's built-in
Fiberis aclassand always GC-allocated. - Domain-specific state —
FiberStatedistinguishesCREATED, RUNNING, SUSPENDED, COMPLETED, and FAILED, with convenience queries
isTerminated()andisResumable(). D'sFiber.Stateonly offers HOLD and TERM. - Error capture — on unhandled exception the fiber stores the
Throwableso a scheduler or caller can inspect it later viafailure(), rather than losing the error or crashing the event loop. - Scheduler integration —
FiberSchedulermanages a run queueof these fibers and drains it in the event loop's check phase. D's
core.thread.Fiberprovides no scheduler; it is only the primitive context-switch mechanism. - Safe API boundary — the wrapper exposes
@safemethods(e.g.\
state(),isTerminated(),yield()) even though D'sFiberis@system.
Copyright
Types 2
Fiber execution state.
A lightweight fiber that integrates with the EVE event loop.
Fibers provide cooperative multitasking by allowing code to yield execution at specific points and be resumed later. This is more efficient than threads for I/O-bound workloads as there's no kernel-level context switching overhead.
Example:
auto fiber = Fiber({
writeln("Step 1");
Fiber.yield();
writeln("Step 2");
});
fiber.call(); // Prints "Step 1"
fiber.call(); // Prints "Step 2"bool isTerminated() const pure nothrow @nogc @safeCheck if the fiber has finished execution (either completed or failed).bool inFiber() static nothrow @nogc @trustedCheck if code is currently executing inside a fiber context.