eve.rt.fiber.scheduler

Fiber Scheduler with EventLoop Integration

This module provides a cooperative fiber scheduler that integrates with the EVE event loop. The scheduler manages a collection of fibers and multiplexes their execution, automatically yielding to the event loop when fibers are waiting for I/O or timers.

Types 4

enumTaskState : ubyte

Status of a scheduled task.

READYTask is queued and ready to run.
RUNNINGTask is currently executing.
WAITINGTask is waiting for I/O or a timer.
COMPLETEDTask has completed successfully.
FAILEDTask terminated with an error.
CANCELLEDTask was cancelled.

A handle to a scheduled task.

Task handles allow checking the status of a spawned fiber and optionally cancelling it before completion.

Fields
private size_t id_
private FiberScheduler scheduler_
Methods
size_t id() @property const pure @safe nothrow @nogcGet the unique identifier for this task.
bool isValid() @property const pure @safe nothrow @nogcCheck if this handle refers to a valid task.
TaskState state() @safeQuery the current state of the task.
bool isFinished() @safeCheck if the task has finished (completed, failed, or cancelled).
bool cancel() @safeRequest cancellation of the task.
private structScheduledTask

Internal task representation within the scheduler.

Fields
size_t id
Fiber fiber
TaskState state
string name
size_t parentId
CancelToken cancelToken

A cooperative fiber scheduler with optional event loop integration.

The FiberScheduler manages a collection of fibers (tasks) and executes them cooperatively. When a fiber yields, the scheduler picks the next ready fiber to run.

EventLoop Integration

When constructed with an EventLoop, the scheduler can attach itself to the loop via attachToLoop(). This registers a check-phase watcher that calls tick() on every loop iteration, integrating fiber execution with I/O and timer events. The scheduler borrows (does not own) the EventLoop.

Thread Affinity

One scheduler per EventLoop, one EventLoop per thread. The scheduler must only be used from the loop thread. For cross-thread work, use EventLoop.post() to schedule callbacks on the loop thread.

Example (standalone):

auto scheduler = new FiberScheduler();

scheduler.spawn({
   writeln("Task 1: Step 1");
   Fiber.yield();
   writeln("Task 1: Step 2");
});

scheduler.run();

Example (with EventLoop):

auto loop = EventLoop.create();
scope (exit) loop.dispose();
auto scheduler = new FiberScheduler(loop);
scheduler.attachToLoop();

scheduler.spawn({
   // This fiber runs during event loop iterations
});

loop.run();

Fields
ScheduledTask[] tasks_
size_t[] readyQueue_
size_t nextId_
bool running_
size_t currentTaskId_
size_t completedCount_
EventLoop * loop_
Token checkToken_
bool attached_
Methods
EventLoop * eventLoop() @property pure @safe nothrow @nogcGet the borrowed EventLoop, or `null` if standalone.
bool attachToLoop() @trusted nothrowAttach the scheduler to the EventLoop's prepare phase.
void detachFromLoop() @trusted nothrowDetach the scheduler from the EventLoop.
void park() @safe nothrowPark the current task.
void unpark(size_t taskId) @safe nothrowUnpark a previously parked task.
TaskHandle spawnFiber(Fiber fiber, string name = null) @trustedRegister a pre-created `Fiber` as a new task.
TaskHandle spawn(void delegate() fn, string name = null) @trustedSpawn a new fiber task with the given delegate.
TaskHandle spawnWithToken(void delegate() fn, CancelToken token, string name = null) @trustedSpawn a new fiber task with a cancellation token.
TaskHandle spawn(void function() fn, string name = null) @trustedditto
size_t run() @safeRun the scheduler until all tasks complete.
bool tick() @trustedExecute a single scheduling round.
TaskState taskState(size_t taskId) @safeGet the state of a task by its ID.
bool cancelTask(size_t taskId) @safeCancel a task by its ID.
size_t taskCount() @property const pure @safe nothrow @nogcGet the number of tasks currently managed by the scheduler.
size_t readyCount() @property const pure @safe nothrow @nogcGet the number of tasks in the ready queue.
bool isRunning() @property const pure @safe nothrow @nogcCheck if the scheduler is currently running.
size_t currentTask() @property const pure @safe nothrow @nogcGet the ID of the currently executing task.
Constructors
this()Create a standalone scheduler (no EventLoop integration).
this(ref EventLoop loop)Create a scheduler that borrows an EventLoop.

Functions 1

fnFiberScheduler fiberScheduler() @safeCreate a new fiber scheduler.