|
fiber
|
This page describes the core architecture behind coroutine-based task scheduling in Fiber. The coroutine system is designed for real-time embedded environments, supporting deeply nested coroutine chains with strict memory control, deterministic behavior, and minimal runtime overhead.
Fiber uses C++20 coroutines to model cooperative tasks. Each task consists of one or more coroutines connected via co_await chains. These coroutines form a reverse-linked list, where:
Task::main() or similar) The list structure supports:co_await, co_yield, or co_return)co_await delay(10ms) or co_await futureawait_transformresume() calls the current leaf coroutine)co_await pointThis means that this coroutine architecture has a memory complexity of O[1] - stack depth does not grow with the number or nested coroutines.
Task::resume() is calledco_await another coroutine → creates a new coroutine and sets it as the new leafco_await another awaitable → (optionally transforms it via await_transfrom to integrate into the scheduler) and sets a new awaitable leaf.co_return or returns to final_suspend()final_suspend(), control returns to TaskTask registers that as the new leaf and resumes it immediatelyTask marks the task as Exit::Success All control flow and state transition pass through Task. The coroutine chain never resumes itself.co_await Delay or co_await NextCycle sends a CoSignal signal to the Task that forwards it to the Scheduler allowing different scheduling strategies.try/catch calls promise_type::unhandled_exception()std::current_exception() inside the coroutine's promiseTask::handle_exception()Task::handle_exception():kill_chain() to destroy all coroutine frames (from leaf to root)Exit::Failure.destroy() is calledTask::kill_chain() iterates from the leaf up to the root, calling .destroy() on each coroutine frameTask is allowed to resume coroutinesco_await and co_return returns control to Taskleaf)Think of the coroutine chain as a linked list:
Coroutine is a nodeTask is the owner and controller of the listco_await links nodes in reverse, so the list grows toward the leafTask traverses and manipulates the list