# sakurs2/tinycorolab architecture

> Architecture diagram of the GitHub repository sakurs2/tinycorolab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sakurs2/tinycorolab
- Repository: https://github.com/sakurs2/tinycorolab

## Overview

#### 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: [`include/coro/task.hpp`](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/task.hpp) (task coroutine), [`CMakeLists.txt`](https://github.com/sakurs2/tinycorolab/blob/master/CMakeLists.txt) (CMake build), [`include/coro/io/io_awaiter.hpp`](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/io_awaiter.hpp) (I/O awaiter), [`include/coro/scheduler.hpp`](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/scheduler.hpp) (Scheduler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_task{{"task coroutine<br/>public coroutine<br/>[task.hpp]"}}
  node_coro_api["Umbrella API<br/>public header<br/>[coro.hpp]"]
  node_awaitable["Awaitable contract<br/>concepts<br/>[awaitable.hpp]"]
end

subgraph group_runtime["Runtime Execution"]
  node_scheduler["Scheduler<br/>[scheduler.hpp]"]
  node_dispatcher["Dispatcher<br/>dispatch policy<br/>[dispatcher.hpp]"]
  node_context["Worker context<br/>worker thread<br/>[context.hpp]"]
  node_engine["Execution engine<br/>coroutine executor<br/>[engine.hpp]"]
end

subgraph group_io["Async I/O"]
  node_io_awaiter{{"I/O awaiter<br/>coroutine awaiter<br/>[io_awaiter.hpp]"}}
  node_io_metadata["I/O operation metadata<br/>I/O descriptor<br/>[io_info.hpp]"]
  node_uring_proxy["io_uring proxy<br/>OS I/O adapter<br/>[uring_proxy.hpp]"]
  node_liburing["liburing<br/>vendored dependency"]
  node_tcp["TCP awaitable API<br/>networking layer<br/>[tcp.hpp]"]
end

subgraph group_coordination["Coroutine Coordination"]
  node_sync["Sync primitives<br/>coroutine synchronization<br/>[mutex.hpp]"]
  node_channel["Channels<br/>coroutine coordination<br/>[channel.hpp]"]
  node_when_all{{"when_all<br/>task composition<br/>[when_all.hpp]"}}
end

subgraph group_consumers["Consumers &amp; Validation"]
  node_echo_server["TCP echo server<br/>example executable"]
  node_parallel_calc["Parallel calculator<br/>example executable<br/>[parallel_calc.cpp]"]
  node_validation["Tests, benchmarks &amp; CI<br/>validation<br/>[ci-ubuntu.yml]"]
end

node_build["CMake build<br/>[CMakeLists.txt]"]

node_build -->|"builds"| node_coro_api
node_build -->|"builds"| node_echo_server
node_build -->|"builds"| node_parallel_calc
node_coro_api -->|"exports"| node_task
node_task -->|"conforms to"| node_awaitable
node_echo_server -->|"creates"| node_task
node_parallel_calc -->|"creates"| node_task
node_task -->|"submitted to"| node_scheduler
node_scheduler -->|"uses"| node_dispatcher
node_dispatcher -->|"selects"| node_context
node_scheduler -->|"lifecycle"| node_context
node_context -->|"owns"| node_engine
node_engine -->|"resumes"| node_task
node_task -->|"awaits"| node_io_awaiter
node_io_awaiter -->|"packages"| node_io_metadata
node_io_awaiter -->|"submits through"| node_uring_proxy
node_uring_proxy -->|"uses"| node_liburing
node_uring_proxy -->|"completion wakes"| node_engine
node_tcp -->|"implements with"| node_io_awaiter
node_echo_server -->|"uses"| node_tcp
node_task -->|"awaits"| node_sync
node_task -->|"coordinates via"| node_channel
node_when_all -->|"composes"| node_task
node_validation -.->|"validates"| node_build

click node_build "https://github.com/sakurs2/tinycorolab/blob/master/CMakeLists.txt"
click node_task "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/task.hpp"
click node_coro_api "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/coro.hpp"
click node_awaitable "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/concepts/awaitable.hpp"
click node_scheduler "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/scheduler.hpp"
click node_dispatcher "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/dispatcher.hpp"
click node_context "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/context.hpp"
click node_engine "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/engine.hpp"
click node_io_awaiter "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/io_awaiter.hpp"
click node_io_metadata "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/io_info.hpp"
click node_uring_proxy "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/uring_proxy.hpp"
click node_liburing "https://github.com/sakurs2/tinycorolab/tree/master/third_party/liburing"
click node_tcp "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/net/tcp/tcp.hpp"
click node_sync "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/mutex.hpp"
click node_channel "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/channel.hpp"
click node_when_all "https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/when_all.hpp"
click node_echo_server "https://github.com/sakurs2/tinycorolab/blob/master/examples/tcp_echo_server.cpp"
click node_parallel_calc "https://github.com/sakurs2/tinycorolab/blob/master/examples/parallel_calc.cpp"
click node_validation "https://github.com/sakurs2/tinycorolab/blob/master/.github/workflows/ci-ubuntu.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_task,node_coro_api,node_awaitable toneBlue
class node_scheduler,node_dispatcher,node_context,node_engine toneAmber
class node_io_awaiter,node_io_metadata,node_uring_proxy,node_liburing,node_tcp toneMint
class node_sync,node_channel,node_when_all toneRose
class node_echo_server,node_parallel_calc,node_validation toneIndigo
class node_build toneNeutral
```

## Components

### Public API

- [task coroutine](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/task.hpp): public coroutine · Owns coroutine lifetime, awaiting, and result propagation. · `include/coro/task.hpp`
- [Umbrella API](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/coro.hpp): public header · `include/coro/coro.hpp`
- [Awaitable contract](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/concepts/awaitable.hpp): concepts · `include/coro/concepts/awaitable.hpp`

### Runtime Execution

- [Scheduler](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/scheduler.hpp): singleton scheduler · Creates, runs, and jointly shuts down worker contexts. · `include/coro/scheduler.hpp`
- [Dispatcher](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/dispatcher.hpp): dispatch policy · Selects a worker context using round-robin dispatch. · `include/coro/dispatcher.hpp`
- [Worker context](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/context.hpp): worker thread · Owns a worker thread and its execution engine. · `include/coro/context.hpp`
- [Execution engine](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/engine.hpp): coroutine executor · Runs ready coroutines and processes asynchronous completion work. · `include/coro/engine.hpp`

### Async I/O

- [I/O awaiter](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/io_awaiter.hpp): coroutine awaiter · Submits I/O, suspends the task, and resumes it on completion. · `include/coro/io/io_awaiter.hpp`
- [I/O operation metadata](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/io_info.hpp): I/O descriptor · `include/coro/io/io_info.hpp`
- [io_uring proxy](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/uring_proxy.hpp): OS I/O adapter · Runtime boundary for liburing submission and completion handling. · `include/coro/uring_proxy.hpp`
- [liburing](https://github.com/sakurs2/tinycorolab/tree/master/third_party/liburing): vendored dependency · `third_party/liburing`
- [TCP awaitable API](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/io/net/tcp/tcp.hpp): networking layer · Exposes non-blocking TCP flows on top of coroutine I/O. · `include/coro/io/net/tcp/tcp.hpp`

### Coroutine Coordination

- [Sync primitives](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/mutex.hpp): coroutine synchronization · Mutexes and events suspend coroutines rather than worker threads. · `include/coro/comp/mutex.hpp`
- [Channels](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/channel.hpp): coroutine coordination · `include/coro/comp/channel.hpp`
- [when_all](https://github.com/sakurs2/tinycorolab/blob/master/include/coro/comp/when_all.hpp): task composition · `include/coro/comp/when_all.hpp`

### Consumers & Validation

- [TCP echo server](https://github.com/sakurs2/tinycorolab/blob/master/examples/tcp_echo_server.cpp): example executable · `examples/tcp_echo_server.cpp`
- [Parallel calculator](https://github.com/sakurs2/tinycorolab/blob/master/examples/parallel_calc.cpp): example executable · `examples/parallel_calc.cpp`
- [Tests, benchmarks & CI](https://github.com/sakurs2/tinycorolab/blob/master/.github/workflows/ci-ubuntu.yml): validation · Ubuntu CI runs functional labs and synchronization performance checks. · `.github/workflows/ci-ubuntu.yml`

### Other components

- [CMake build](https://github.com/sakurs2/tinycorolab/blob/master/CMakeLists.txt): build · Builds the runtime, examples, tests, and benchmarks. · `CMakeLists.txt`

## Connections

- 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
