# lwhyz/workspace architecture

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

- Interactive diagram: https://gitdiagram.com/lwhyz/workspace
- Repository: https://github.com/lwhyz/workspace

## Overview

1. **Repository shape**
   This is a header-only C++11 asynchronous execution framework called **workspace**. The public API lives under `include/workspace/`, with small standalone programs in `example/`, benchmarks in `bench/` and `benchmark/`, and unit tests in `test/`. Build and packaging are CMake-based.

2. **Top-level architecture**
   The framework has three core runtime layers:
   - **workbranch**: an executable task pool / worker-branch abstraction
   - **supervisor**: a control plane that monitors and adjusts workbranch capacity
   - **workspace**: a container/dispatcher that owns and routes between multiple workbranches and supervisors  
   Around these sit supporting utilities like a future collector and internal thread/task helpers.

3. **workbranch: the execution engine**
   `workbranch` is the primary data plane. It owns a thread-safe task queue and a set of worker threads that continuously pull tasks and execute them. It supports:
   - async submission with or without return values
   - task priorities (`normal` vs `urgent`)
   - batched “sequence” tasks that are executed serially by one worker
   - waiting for all queued/in-flight tasks to finish
   - exception handling, either logged locally or propagated via `std::future`

4. **Task flow through workbranch**
   A task enters through `submit(...)`, is wrapped into the framework’s task abstraction, placed into the internal queue, then consumed by a worker thread. If the task returns a value, the submission path produces a `std::future`; otherwise it is fire-and-forget. For exception-bearing tasks, the framework either logs the exception inside the worker or stores it in the future for later retrieval on the caller side.

5. **Scheduling and wait strategies**
   Worker idle behavior is configurable via three waiting modes:
   - `lowlatancy`: busy-wait / yield for minimal latency
   - `balance`: spin first, then sleep briefly
   - `blocking`: sleep on a condition variable until work arrives or shutdown/wait conditions change  
   This is a meaningful architectural boundary because it trades off latency, CPU usage, and contention under different workloads.

6. **supervisor: dynamic control plane**
   `supervisor` is a monitoring and auto-scaling layer for one or more workbranches. It observes load and adjusts the number of worker threads within configured bounds. It can also run a periodic “tick callback” after each adjustment, which is intended for logging or custom control logic. In the architecture diagram, this is the feedback loop between observed queue pressure and worker pool size.

7. **workspace: ownership and dispatch hub**
   `workspace` is a manager/dispatcher that owns multiple `workbranch` and `supervisor` instances via internal unique ownership. It assigns component IDs, provides indexed access, and coordinates task distribution across branches. Its main architectural role is to prevent lifetime hazards, reduce cross-component contention, and provide simple load balancing across managed branches.

8. **Cross-branch task distribution**
   When a task is submitted to `workspace`, it is dispatched to one of the managed `workbranch` instances according to the framework’s balancing logic. This is the main boundary between orchestration and execution: `workspace` decides *where* work goes, while `workbranch` decides *when and how* worker threads run it. The benchmarks suggest this dispatch layer reduces contention when branches are configured with fewer worker threads.

9. **Auxiliary future collector**
   `wsp::futures<T>` is a small batching utility for collecting many futures of the same result type. It supports storing futures, waiting on all of them, and extracting their values as a vector. This is a convenience layer above the core execution engine and is not a scheduler itself.

10. **Lifecycle and safety boundaries**
   The repo emphasizes ownership and thread-safety boundaries:
   - `workbranch` and `supervisor` are thread-safe
   - `workspace` is **not** thread-safe
   - `futures` is **not** thread-safe  
   A key design constraint is that `workspace` should own and outlive its managed components to avoid dangling references, especially between workbranches and supervisors.

11. **Examples, tests, and benchmarks**
   The `example/` programs demonstrate the main execution patterns: basic async work, urgent tasks, sequence batching, exception propagation, supervisor-driven scaling, and workspace-managed multi-branch dispatch. The `test/` directory covers core behavior such as function execution, exception handling, supervisor logic, workbranch behavior, and workspace behavior. The two benchmark areas measure queue/synchronization overhead and idle-wait latency under different scheduling strategies.

12. **External/runtime dependencies**
   The library targets standard C++11 and is cross-platform in principle, but its runtime model depends on native threading (`std::thread` / pthread-style behavior) and `std::future`/`std::condition_variable`. There are no external services; the architecture is entirely in-process concurrency and scheduling.

Main entry points: [`include/workspace/workbranch.hpp`](https://github.com/lwhyz/workspace/blob/main/include/workspace/workbranch.hpp) (workbranch), [`example`](https://github.com/lwhyz/workspace/tree/main/example) (examples), [`test`](https://github.com/lwhyz/workspace/tree/main/test) (tests), [`CMakeLists.txt`](https://github.com/lwhyz/workspace/blob/main/CMakeLists.txt) (cmake)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_api["Public API"]
end

subgraph group_grp_core["Core runtime"]
  node_n_workspace["workspace<br/>dispatcher/owner<br/>[workspace.hpp]"]
  node_n_supervisor["supervisor<br/>control plane<br/>[supervisor.hpp]"]
  node_n_workbranch["workbranch<br/>executor pool<br/>[workbranch.hpp]"]
  node_n_taskqueue["task queue<br/>queue core<br/>[taskqueue.hpp]"]
  node_n_async_api["async submit<br/>submission API"]
  node_n_wait_modes["wait modes<br/>idle strategy"]
  node_n_sequence["sequence tasks<br/>batching"]
  node_n_exceptions["exceptions<br/>error flow"]
end

subgraph group_grp_support["Support"]
  node_n_utility["utility<br/>helpers<br/>[utility.hpp]"]
  node_n_futures["futures<br/>future collector"]
  node_n_threading["threading<br/>runtime deps"]
end

subgraph group_grp_examples["Examples"]
  node_n_examples["examples<br/>usage demos"]
end

subgraph group_grp_validation["Validation"]
  node_n_bench["benchmarks<br/>perf suite"]
  node_n_benchmark["benchmark<br/>perf suite"]
  node_n_tests["tests<br/>unit suite"]
end

subgraph group_grp_build["Build & docs"]
  node_n_cmake["cmake<br/>build system<br/>[CMakeLists.txt]"]
  node_n_docs["docs<br/>[README.md]"]
end

node_n_workspace -->|"owns/dispatches"| node_n_workbranch
node_n_workspace -->|"owns/coordinates"| node_n_supervisor
node_n_supervisor -->|"scales"| node_n_workbranch
node_n_workbranch -->|"uses"| node_n_taskqueue
node_n_workbranch -->|"implements"| node_n_async_api
node_n_async_api -->|"routes"| node_n_exceptions
node_n_workbranch -->|"supports"| node_n_sequence
node_n_workbranch -->|"configures"| node_n_wait_modes
node_n_workbranch -->|"depends on"| node_n_threading
node_n_workbranch -->|"uses"| node_n_utility
node_n_futures -->|"depends on"| node_n_threading
node_n_futures -->|"collects outputs"| node_n_async_api
node_n_examples -.->|"demonstrates"| node_n_workspace
node_n_examples -.->|"demonstrates"| node_n_workbranch
node_n_examples -.->|"demonstrates"| node_n_supervisor
node_n_tests -.->|"verifies"| node_n_workspace
node_n_tests -.->|"verifies"| node_n_workbranch
node_n_tests -.->|"verifies"| node_n_supervisor
node_n_bench -.->|"measures"| node_n_workbranch
node_n_benchmark -.->|"measures"| node_n_wait_modes
node_n_cmake -->|"builds"| node_n_examples
node_n_cmake -->|"builds"| node_n_tests
node_n_cmake -->|"builds"| node_n_bench
node_n_docs -.->|"documents"| node_n_cmake

click node_n_workspace "https://github.com/lwhyz/workspace/blob/main/include/workspace/workspace.hpp"
click node_n_supervisor "https://github.com/lwhyz/workspace/blob/main/include/workspace/supervisor.hpp"
click node_n_workbranch "https://github.com/lwhyz/workspace/blob/main/include/workspace/workbranch.hpp"
click node_n_taskqueue "https://github.com/lwhyz/workspace/blob/main/include/workspace/taskqueue.hpp"
click node_n_utility "https://github.com/lwhyz/workspace/blob/main/include/workspace/utility.hpp"
click node_n_examples "https://github.com/lwhyz/workspace/tree/main/example"
click node_n_bench "https://github.com/lwhyz/workspace/tree/main/bench"
click node_n_benchmark "https://github.com/lwhyz/workspace/tree/main/benchmark"
click node_n_tests "https://github.com/lwhyz/workspace/tree/main/test"
click node_n_cmake "https://github.com/lwhyz/workspace/blob/main/CMakeLists.txt"
click node_n_docs "https://github.com/lwhyz/workspace/blob/main/README.md"

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_n_workspace,node_n_supervisor,node_n_workbranch,node_n_taskqueue,node_n_async_api,node_n_wait_modes,node_n_sequence,node_n_exceptions toneAmber
class node_n_utility,node_n_futures,node_n_threading toneMint
class node_n_examples toneRose
class node_n_bench,node_n_benchmark,node_n_tests toneIndigo
class node_n_cmake,node_n_docs toneTeal
```

## Components

### Core runtime

Execution, control, and dispatch layers.

- [workspace](https://github.com/lwhyz/workspace/blob/main/include/workspace/workspace.hpp): dispatcher/owner · Top-level manager that owns multiple branches and supervisors, dispatches work across them, and provides indexed access. · `include/workspace/workspace.hpp`
- [supervisor](https://github.com/lwhyz/workspace/blob/main/include/workspace/supervisor.hpp): control plane · Monitors load and adjusts worker capacity within configured bounds, with optional tick callbacks. · `include/workspace/supervisor.hpp`
- [workbranch](https://github.com/lwhyz/workspace/blob/main/include/workspace/workbranch.hpp): executor pool · Primary execution engine: owns worker threads and a task queue, submits async work, and waits for completion. · `include/workspace/workbranch.hpp`
- [task queue](https://github.com/lwhyz/workspace/blob/main/include/workspace/taskqueue.hpp): queue core · Thread-safe queue backing workbranch task intake and worker consumption. · `include/workspace/taskqueue.hpp`
- **async submit**: submission API · Fire-and-forget and value-returning submission paths, including exception propagation through futures.
- **wait modes**: idle strategy · Low-latency, balanced, and blocking worker idle policies.
- **sequence tasks**: batching · Serialized task batches executed in order by a single worker.
- **exceptions**: error flow · Local logging or future-based propagation of task exceptions.

### Support

Shared utilities used by the runtime.

- [utility](https://github.com/lwhyz/workspace/blob/main/include/workspace/utility.hpp): helpers · Shared internal helpers for task wrapping, threading, and execution glue. · `include/workspace/utility.hpp`
- **futures**: future collector · Batches homogeneous futures, waits on them, and extracts values as a vector.
- **threading**: runtime deps · Uses std::thread, std::future, and std::condition_variable for in-process concurrency.

### Examples

Standalone usage programs.

- [examples](https://github.com/lwhyz/workspace/tree/main/example): usage demos · Standalone programs demonstrating async work, priorities, batching, scaling, and multi-branch dispatch. · `example`

### Validation

Tests and benchmarks.

- [benchmarks](https://github.com/lwhyz/workspace/tree/main/bench): perf suite · Measures queue/synchronization overhead and idle-wait latency across scheduling strategies. · `bench`
- [benchmark](https://github.com/lwhyz/workspace/tree/main/benchmark): perf suite · Additional benchmark harnesses for runtime behavior and waiting strategies. · `benchmark`
- [tests](https://github.com/lwhyz/workspace/tree/main/test): unit suite · Core behavior tests for function execution, exceptions, supervisor logic, workbranch, and workspace. · `test`

### Build & docs

Repository build, packaging, and documentation entry points.

- [cmake](https://github.com/lwhyz/workspace/blob/main/CMakeLists.txt): build system · CMake-based build and packaging entry points for the header-only library and its tools. · `CMakeLists.txt`
- [docs](https://github.com/lwhyz/workspace/blob/main/README.md): project docs · README, bench notes, and Doxygen configuration for usage and documentation. · `README.md`

## Connections

- workspace → workbranch: owns/dispatches · Workspace manages multiple branches and routes work to one of them.
- workspace → supervisor: owns/coordinates · Workspace keeps supervisors with the branches they regulate.
- supervisor → workbranch: scales · Supervisor observes load and adjusts worker count on a branch.
- workbranch → task queue: uses · Workbranch submits to and drains from its internal task queue.
- workbranch → async submit: implements · The public submission surface is provided by workbranch.
- async submit → exceptions: routes · Submission paths either log exceptions or propagate them through futures.
- workbranch → sequence tasks: supports · Workbranch executes batched sequence tasks serially on one worker.
- workbranch → wait modes: configures · Idle worker behavior is controlled by the chosen wait strategy.
- workbranch → threading: depends on · Execution relies on native threading and synchronization primitives.
- workbranch → utility: uses · Helper utilities support task wrapping and internal execution glue.
- futures → threading: depends on · Future collection uses standard future synchronization semantics.
- futures → async submit: collects outputs · Collects many result-bearing submissions and extracts their values.
- examples → workspace: demonstrates · Examples show workspace-managed dispatch across branches.
- examples → workbranch: demonstrates · Examples cover async work, urgency, batching, and waiting behavior.
- examples → supervisor: demonstrates · Examples include supervisor-driven scaling behavior.
- tests → workspace: verifies · Tests cover workspace behavior and access patterns.
- tests → workbranch: verifies · Tests cover execution, waiting, and exception handling.
- tests → supervisor: verifies · Tests cover supervision and scaling logic.
- benchmarks → workbranch: measures · Benchmarks target queueing and synchronization overhead in the executor.
- benchmark → wait modes: measures · Benchmark harnesses compare idle-wait latency strategies.
- cmake → examples: builds · CMake config builds the example programs.
- cmake → tests: builds · CMake config builds the test suite.
- cmake → benchmarks: builds · CMake config builds benchmark targets.
- docs → cmake: documents · Repository docs describe build and usage entry points.
