eve.rt.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

A cooperative fiber scheduler with 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. The scheduler can optionally integrate with an EventLoop to handle I/O-bound fibers efficiently.

Example:

auto scheduler = new FiberScheduler();

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

scheduler.spawn({
  writeln("Task 2: Running");
});

scheduler.run();
// Output:
// Task 1: Step 1
// Task 2: Running
// Task 1: Step 2

Fields
ScheduledTask[] tasks_
size_t[] readyQueue_
size_t nextId_
bool running_
size_t currentTaskId_
size_t completedCount_
Methods
TaskHandle spawn(void delegate() fn, string name = null) @trustedSpawn a new fiber task with the given delegate.
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.

Functions 1

fnFiberScheduler fiberScheduler() @safeCreate a new fiber scheduler.