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.

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() @trustedYield execution from the current fiber back to the caller.
CoreFiber current() nothrow @nogc @trustedGet the currently executing fiber, if any.
bool inFiber() 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