# mtmucha/coros architecture

> Architecture diagram of the GitHub repository mtmucha/coros, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mtmucha/coros
- Repository: https://github.com/mtmucha/coros

## Overview

##### Main Components
- Library Interfaces (Headers):
▪ coros::Task<T>: The task abstraction that uses coroutines.
▪ coros::ThreadPool: The task execution environment managing worker threads.
▪ Awaiters: The objects enabling suspension/resume via co_await.
▪ Utility components (chain_tasks.h, enqueue_tasks.h, wait_tasks.h, start_tasks.h, etc.) for scheduling, chaining, and task management.
▪ Supporting data structures: e.g., concurrentqueue.h, deque.h (used for task scheduling algorithms).
- Execution and Scheduling Mechanisms:
- “Start Tasks” functionality:
▪ start_sync and start_async for launching tasks on a ThreadPool.
- “Wait Tasks” functions:
▪ wait_tasks and wait_tasks_async that suspend execution until a set of tasks is complete.
- “Enqueue Tasks” functions:
▪ enqueue_tasks used to schedule tasks without waiting.
- “Chain Tasks” functionality:
▪ chain_tasks, demonstrating the monadic chaining of tasks (and_then).
- Error and Result Handling:

##### Relationships and Data Flow
- User Code / Application Layer (examples/benchmarks/tests) on one side interacting with the library APIs.
- That when a user writes a coroutine (using the correct header such as start_tasks.h), it creates a coros::Task<T>.
- That after task submission, tasks run concurrently, with the thread pool managing the execution and resumption once awaited tasks are finished.
- Error propagation: if a task fails, the error is wrapped in std::expected and thus flows back through the chain – halting further chaining if an exception occurs.

##### Architectural Patterns & Design Principles
- Asynchronous Task Execution Pattern: Show how coroutines (with co_await and co_return) are central to asynchronous processing.
- Monadic Chaining: Visually represent the chain_tasks component (similar to chaining operations in functional programming) – illustrating sequential execution of tasks, where the output of one feeds as input into the next.
- Header-Only Library: Emphasize that all functionality is provided via header files (no separate binary); this is reflected by showing all core components within the “Library Core” section.

##### Technologies, Dependencies, and External Services
- Since this is a C++23 header-only library, include a note of the required compiler support (GCC 13, Clang 17, etc.) and that coroutines are core.
- Optionally, add a small note on continuous integration (GitHub Workflows) demonstrating that the library is tested across multiple configurations. This can be a boxed area that shows CI/CD as an external process ensuring quality.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U["User/Application Code"]:::user

    subgraph "Coros Library Core"
        subgraph "Task Management"
            TT["coros::Task<T>"]:::component
            AW["Awaiters (co_await support)"]:::component
            EE["Error Handling (std::expected)"]:::note
        end
        subgraph "Task Scheduling"
            ST["Start Tasks"]:::component
            WT["Wait Tasks"]:::component
            ET["Enqueue Tasks"]:::component
        end
        subgraph "Task Chaining"
            CT["Chain Tasks"]:::component
        end
        subgraph "ThreadPool Executor"
            TP["coros::ThreadPool"]:::component
        end
    end

    subgraph "Utility Components"
        CQ["Concurrent Queue"]:::utility
        DQ["Deque"]:::utility
    end

    subgraph "Testing & Examples"
        EX["Examples (code)"]:::user
        TS["Tests"]:::user
    end

    U -->|"creates tasks"| TT
    TT -->|"uses"| AW
    TT -->|"propagates"| EE
    TT -->|"schedules_via"| ST
    TT -->|"schedules_via"| WT
    TT -->|"schedules_via"| ET
    CT -->|"chains_with"| TT
    ST -->|"launches_to"| TP
    WT -->|"waits_on"| TP
    ET -->|"enqueues_to"| TP
    EX -->|"demonstrates"| TT
    TS -->|"validates"| TP
    ST -->|"utilizes"| CQ
    ET -->|"utilizes"| DQ

    classDef component fill:#f9f,stroke:#333,stroke-width:2px;
    classDef utility fill:#9f9,stroke:#333,stroke-width:2px;
    classDef user fill:#ff9,stroke:#333,stroke-width:2px;
    classDef note fill:#ffa,stroke:#333,stroke-width:2px;

    click U "https://github.com/mtmucha/coros/tree/main/examples/"
    click TT "https://github.com/mtmucha/coros/blob/main/include/task.h"
    click AW "https://github.com/mtmucha/coros/blob/main/include/task.h"
    click ST "https://github.com/mtmucha/coros/blob/main/include/start_tasks.h"
    click WT "https://github.com/mtmucha/coros/blob/main/include/wait_tasks.h"
    click ET "https://github.com/mtmucha/coros/blob/main/include/enqueue_tasks.h"
    click CT "https://github.com/mtmucha/coros/blob/main/include/chain_tasks.h"
    click TP "https://github.com/mtmucha/coros/blob/main/include/thread_pool.h"
    click CQ "https://github.com/mtmucha/coros/blob/main/include/concurrentqueue.h"
    click DQ "https://github.com/mtmucha/coros/blob/main/include/deque.h"
    click EX "https://github.com/mtmucha/coros/tree/main/examples/"
    click TS "https://github.com/mtmucha/coros/tree/main/tests/"
```
