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.
Copyright
Types 4
Status of a scheduled task.
A handle to a scheduled task.
Task handles allow checking the status of a spawned fiber and optionally cancelling it before completion.
Internal task representation within the scheduler.
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();ScheduledTask[] tasks_size_t[] readyQueue_size_t nextId_bool running_size_t currentTaskId_size_t completedCount_EventLoop * loop_Token checkToken_bool attached_EventLoop * eventLoop() @property pure @safe nothrow @nogcGet the borrowed EventLoop, or `null` if standalone.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.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.