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 semanticsstruct Fiber matches EVE's

    stack-allocated, GC-optional style used throughout Layers 0–2. D's built-in Fiber is a class and always GC-allocated.

  • Domain-specific stateFiberState distinguishes

    CREATED, RUNNING, SUSPENDED, COMPLETED, and FAILED, with convenience queries isTerminated() and isResumable(). D's Fiber.State only offers HOLD and TERM.

  • Error capture — on unhandled exception the fiber stores the

    Throwable so a scheduler or caller can inspect it later via failure(), rather than losing the error or crashing the event loop.

  • Scheduler integrationFiberScheduler manages a run queue

    of these fibers and drains it in the event loop's check phase. D's core.thread.Fiber provides no scheduler; it is only the primitive context-switch mechanism.

  • Safe API boundary — the wrapper exposes @safe methods

    (e.g.\ state(), isTerminated(), yield()) even though D's Fiber is @system.

struct Fiber

Types 2

enumFiberState : ubyte

Fiber execution state.

CREATEDFiber has been created but never started.
RUNNINGFiber is currently running.
SUSPENDEDFiber has yielded and is waiting to be resumed.
COMPLETEDFiber has completed execution normally.
FAILEDFiber terminated due to an unhandled exception.
structFiber

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"

Fields
private CoreFiber impl
private FiberState currentState
private Throwable error
Methods
FiberState call() @trustedResume or start fiber execution.
FiberState state() const pure nothrow @nogc @safeQuery the current execution state of the fiber.
bool isTerminated() const pure nothrow @nogc @safeCheck if the fiber has finished execution (either completed or failed).
bool isResumable() const pure nothrow @nogc @safeCheck if the fiber can be resumed.
Throwable failure() pure nothrow @nogc @safeRetrieve the error if the fiber failed.
bool reset() @trustedReset the fiber to its initial state for reuse.
void yield() static @trustedYield execution from the current fiber back to the caller.
CoreFiber current() static nothrow @nogc @trustedGet the currently executing fiber, if any.
bool inFiber() static nothrow @nogc @trustedCheck if code is currently executing inside a fiber context.
Constructors
this(void delegate() fn, size_t stackSize = 0)Construct a fiber with the given callable.
this(void function() fn, size_t stackSize = 0)ditto

Functions 2

fnFiber fiber(void delegate() fn, size_t stackSize = 0) @safeCreate a fiber from a delegate.
fnFiber fiber(void function() fn, size_t stackSize = 0) @safeditto