# facebookexperimental/libunifex architecture

> Architecture diagram of the GitHub repository facebookexperimental/libunifex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/facebookexperimental/libunifex
- Repository: https://github.com/facebookexperimental/libunifex

## Overview

##### Main Components

a) Core Components:
- Sender/Receiver Framework
- Schedulers System
- Async I/O System
- Coroutine Integration Layer
- Stream Processing System
- Cancellation System

b) Platform-Specific Components:
- Linux I/O Implementation (io_uring)
- Windows Implementation (IOCP, Thread Pool)

**┌─────────────────── Application Layer ───────────────────┐**
- (User Code)                         │
- High-Level APIs ──────────────────┐ │
- Algorithms                                         │ │
- Async Streams                                      │ │
- Coroutine Integration                             │ │
- Core Layer ─────────────────────┐ │ │
- Sender/Receiver Framework                        │ │ │
- Schedulers                                       │ │ │
- Cancellation System                             │ │ │
- Platform Abstraction Layer ────────┐ │ │ │
- Linux Support        │      Windows Support      │ │ │ │
- io_uring          │      - IOCP               │ │ │ │
- epoll             │      - Thread Pool        │ │ │ │
- ┘

##### Key Elements to Highlight

a) Core Abstractions:
- Cancellation paths
- Scheduler interaction points

b) Cross-cutting Concerns:
- Type erasure mechanisms
- Memory management
- Error handling
- Debugging facilities

##### Implementation Details

- Scheduler implementations (static_thread_pool, inline_scheduler, etc.)
- Stream processing components
- Async primitives (mutex, events)
- Platform-specific I/O implementations

##### Notes for Implementation

- Keep the abstraction level high enough to show the architecture but detailed enough to understand the system's working
- Clear boundaries between platform-specific and platform-independent code
- Extensibility points where users can plug in their implementations
- Version compatibility notes where relevant

- Clear text labels
- Export to high-resolution formats

Remember to maintain consistency with the project's modular structure as seen in the include/unifex directory, and reflect the test organization shown in the test directory.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Application["Application Layer"]
        UserCode["User Code"]:::application
    end

    subgraph HighLevel["High-Level APIs"]
        Algorithms["Algorithms"]:::highlevel
        AsyncStreams["Async Streams"]:::highlevel
        StreamProcessing["Stream Processing"]:::highlevel
        click StreamProcessing "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/stream_concepts.hpp"
        CoroutineInteg["Coroutine Integration"]:::highlevel
        click CoroutineInteg "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/coroutine.hpp"
    end

    subgraph Core["Core Layer"]
        SenderReceiver["Sender/Receiver Framework"]:::core
        click SenderReceiver "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/sender_concepts.hpp"
        Schedulers["Schedulers"]:::core
        click Schedulers "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/scheduler_concepts.hpp"
        Cancellation["Cancellation System"]:::core
        click Cancellation "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/inplace_stop_token.hpp"
    end

    subgraph Platform["Platform Abstraction Layer"]
        subgraph Linux["Linux Support"]
            IOUring["io_uring"]:::linux
            click IOUring "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/linux/io_uring_context.hpp"
            Epoll["epoll"]:::linux
            click Epoll "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/linux/io_epoll_context.hpp"
        end

        subgraph Windows["Windows Support"]
            IOCP["IOCP"]:::windows
            click IOCP "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/win32/low_latency_iocp_context.hpp"
            ThreadPool["Thread Pool"]:::windows
            click ThreadPool "https://github.com/facebookexperimental/libunifex/blob/main/include/unifex/win32/windows_thread_pool.hpp"
        end
    end

    %% Connections
    UserCode --> Algorithms
    UserCode --> AsyncStreams
    UserCode --> CoroutineInteg

    Algorithms --> SenderReceiver
    AsyncStreams --> StreamProcessing
    StreamProcessing --> SenderReceiver
    CoroutineInteg --> SenderReceiver

    SenderReceiver -.-> Cancellation
    SenderReceiver --> Schedulers

    Schedulers --> IOUring
    Schedulers --> Epoll
    Schedulers --> IOCP
    Schedulers --> ThreadPool

    %% Styles
    classDef application fill:#f9f,stroke:#333,stroke-width:2px
    classDef highlevel fill:#aaf,stroke:#333,stroke-width:2px
    classDef core fill:#9d9,stroke:#333,stroke-width:2px
    classDef linux fill:#f96,stroke:#333,stroke-width:2px
    classDef windows fill:#69f,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1["Application Layer"]:::application
        L2["High-Level APIs"]:::highlevel
        L3["Core Layer"]:::core
        L4["Linux Components"]:::linux
        L5["Windows Components"]:::windows
        L6["--> Control Flow"]
        L7["-.-> Cancellation Flow"]
    end
```
