How sakurs2/tinycorolab works
Purpose and Build Surface
tinyCoroLab is a Linux x86_64 C++20 instructional implementation of a high-performance coroutine runtime. It combines stackless C++ coroutines with io_uring, progressively organized as labs for task handling, execution, I/O, and coroutine-safe synchronization. CMake is the build entry point: CMakeLists.txt, src/CMakeLists.txt, and generated configuration from config/config.h.in.
Public Runtime API and Task Model
task is the fundamental user-visible coroutine unit; it encapsulates coroutine lifetime, promise/awaiter behavior, and result propagation. include/coro/coro.hpp is the umbrella-facing API, while include/coro/task.hpp and coroutine constraints in include/coro/concepts/awaitable.hpp define the core awaitable contract. Applications enter through independently built example executables, notably examples/tcp_echo_server.cpp and examples/parallel_calc.cpp.
Scheduling and Worker Execution
The scheduling tier assigns submitted coroutine tasks to worker contexts. The singleton scheduler creates, runs, and stops a set of contexts; dispatching uses a separate policy layer, documented as round-robin. Each context owns a worker thread and wraps an engine, which executes ready coroutine work and coordinates asynchronous I/O:
• include/coro/scheduler.hpp, src/scheduler.cpp
• include/coro/context.hpp, src/context.cpp
• include/coro/engine.hpp, src/engine.cpp
• include/coro/dispatcher.hpp
Control flow is generally: application submits task → scheduler/dispatcher selects context → context wakes or queues work for its engine → engine resumes coroutine until completion or suspension. Scheduler shutdown is coordinated after all contexts finish, rather than allowing idle contexts to exit independently.
Async I/O Boundary
The I/O subsystem bridges coroutine suspension/resumption with Linux io_uring. I/O awaiters package operation metadata, submit it through the liburing proxy, suspend the current task, then resume it after completion. This is the OS-facing boundary and ties completion processing back into engine execution:
• include/coro/io/io_awaiter.hpp, src/io/io_awaiter.cpp
• include/coro/io/base_awaiter.hpp, include/coro/io/io_info.hpp
• include/coro/uring_proxy.hpp
• third_party/liburing
Networking Layer
Networking is a thin coroutine-oriented TCP layer built on the I/O subsystem. TCP operations are exposed as awaitable flows rather than blocking socket calls; an HTTP namespace exists as an API area, but the tree shows concrete implementation only for TCP.
• include/coro/io/net/tcp/tcp.hpp, src/io/net/tcp/tcp.cpp
• include/coro/io/net/http/http.hpp
• examples/tcp_echo_client.cpp, examples/tcp_echo_server.cpp
Coroutine Synchronization and Composition
Synchronization primitives suspend coroutines instead of blocking worker threads. Basic primitives cover mutexes, condition variables, events, latches, and wait groups; higher-level coordination includes channels and when_all. These components form the concurrency boundary above scheduler/engine execution:
• include/coro/comp/mutex.hpp, src/comp/mutex.cpp
• include/coro/comp/event.hpp, src/comp/event.cpp
• include/coro/comp/channel.hpp, src/comp/channel.cpp
• include/coro/comp/when_all.hpp
Runtime Dependencies and Validation
The runtime targets Linux with a modern kernel, C++20-capable GCC, and liburing. It vendors atomic_queue for lock-free queueing and spdlog for logging; GoogleTest and Google Benchmark support correctness and performance comparison. CI runs on Ubuntu through .github/workflows/ci-ubuntu.yml; lab-oriented functional coverage lives under tests/, while synchronization performance checks live under benchtests/.
Main entry points: task.hpp task coroutine · CMakeLists.txt CMake build · io_awaiter.hpp I/O awaiter · scheduler.hpp Scheduler
Components 19
Public API
- task coroutine · Owns coroutine lifetime, awaiting, and result propagation.
include/coro/task.hpp - Umbrella API
include/coro/coro.hpp - Awaitable contract
include/coro/concepts/awaitable.hpp
Runtime Execution
- Scheduler · Creates, runs, and jointly shuts down worker contexts.
include/coro/scheduler.hpp - Dispatcher · Selects a worker context using round-robin dispatch.
include/coro/dispatcher.hpp - Worker context · Owns a worker thread and its execution engine.
include/coro/context.hpp - Execution engine · Runs ready coroutines and processes asynchronous completion work.
include/coro/engine.hpp
Async I/O
- I/O awaiter · Submits I/O, suspends the task, and resumes it on completion.
include/coro/io/io_awaiter.hpp - I/O operation metadata
include/coro/io/io_info.hpp - io_uring proxy · Runtime boundary for liburing submission and completion handling.
include/coro/uring_proxy.hpp - liburing
third_party/liburing - TCP awaitable API · Exposes non-blocking TCP flows on top of coroutine I/O.
include/coro/io/net/tcp/tcp.hpp
Coroutine Coordination
- Sync primitives · Mutexes and events suspend coroutines rather than worker threads.
include/coro/comp/mutex.hpp - Channels
include/coro/comp/channel.hpp - when_all
include/coro/comp/when_all.hpp
Consumers & Validation
- TCP echo server
examples/tcp_echo_server.cpp - Parallel calculator
examples/parallel_calc.cpp - Tests, benchmarks & CI · Ubuntu CI runs functional labs and synchronization performance checks.
.github/workflows/ci-ubuntu.yml
Other components
- CMake build · Builds the runtime, examples, tests, and benchmarks.
CMakeLists.txt
Connections 24
- CMake build → Umbrella API: builds
- CMake build → TCP echo server: builds
- CMake build → Parallel calculator: builds
- Umbrella API → task coroutine: exports
- task coroutine → Awaitable contract: conforms to
- TCP echo server → task coroutine: creates
- Parallel calculator → task coroutine: creates
- task coroutine → Scheduler: submitted to
- Scheduler → Dispatcher: uses
- Dispatcher → Worker context: selects
- Scheduler → Worker context: lifecycle
- Worker context → Execution engine: owns
- Execution engine → task coroutine: resumes
- task coroutine → I/O awaiter: awaits
- I/O awaiter → I/O operation metadata: packages
- I/O awaiter → io_uring proxy: submits through
- io_uring proxy → liburing: uses
- io_uring proxy → Execution engine: completion wakes
- TCP awaitable API → I/O awaiter: implements with
- TCP echo server → TCP awaitable API: uses
- task coroutine → Sync primitives: awaits
- task coroutine → Channels: coordinates via
- when_all → task coroutine: composes
- Tests, benchmarks & CI → CMake build: validates