# enki/libev architecture

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

- Interactive diagram: https://gitdiagram.com/enki/libev
- Repository: https://github.com/enki/libev

## Overview

##### Project Type and Purpose
- libev is an open-source, embeddable C library that provides a high-performance, cross-platform event loop/event model.
- It exposes a clean API for applications to register “watchers” (I/O, timer, signal, child, file-system, async), then runs a central event loop that dispatches callbacks when events occur.

##### File-Structure Analysis
- Top-level sources:
- ev.c / event.c       → core event-loop implementation
- ev_<backend>.c      → platform-specific backends (select, poll, epoll, kqueue, event ports, Windows)
- ev_vars.h, ev_wrap.h → internal data structures and abstraction glue
- ev.h / event.h      → public API headers defining watchers and loop functions
- ev++.h               → optional zero-overhead C++ interface
- autogen.sh, configure*, Makefile.* → GNU Autotools build system
- No monolithic binary—libev is built as a static or shared library and linked into host applications.
- Documentation (ev.3, ev.pod) provides user-facing API descriptions.

##### Key Architectural Components
a. API Layer
- Headers (ev.h, event.h) define watcher types and loop-management functions.
- C++ wrapper (ev++.h) offers method/function callback binding.
b. Core Event Loop
- Maintains lists of active watchers.
- Provides loop control (ev_run, ev_break).
- Dispatches callbacks when notified by the backend.
c. Backend Abstraction
- A thin indirection layer chooses and initializes the best system mechanism at run-time (select, poll, epoll, kqueue, etc.).
- Falls back to more portable mechanisms if needed.
d. Platform-Specific Backends
- Each ev_<backend>.c implements a uniform interface: init, polling, finalization.
- E.g., ev_epoll.c on Linux uses epoll_wait, ev_kqueue.c on BSD/Mac uses kevent.
e. Watcher Subsystems
- I/O watchers
- Timer watchers (absolute, relative, cron-like)
- Signal watchers (handle UNIX signals)
- Child watchers (reap child processes)
- Stat watchers (file-system path monitoring, optional inotify)
- Async watchers (intra-thread notifications, optional eventfd)

##### Relationships and Data Flow
- User Application → calls API Layer to:
- Initialize an ev_loop
- Create and start watchers
- Invoke ev_run to start the loop
- Core Event Loop → delegates to Backend Abstraction → invokes the selected system backend
- Backend → blocks/waits for OS events → returns a list of fired file-descriptors or signals
- Core → walks its watcher lists → matches ready watchers → invokes user callbacks
- Watchers may rearm themselves or stop after callback

##### Important Patterns and Principles
- Modular backend plugin pattern: new backends can be “plugged in” by implementing the backend interface.
- Single-threaded, non-preemptive event dispatch for minimal overhead.
- Extensible watcher architecture: users can integrate custom watcher types.
- Zero runtime overhead C++ integration via header-only wrapper.
- Autodetect and adapt to platform best mechanism (performance-driven).

##### Putting It All Together
- Next, “Core Event Loop.”
- Below the core, show “Watcher Subsystems” feeding into it.
- To the right of the core, place “Backend Abstraction,” and to its right, a group of “Backend Modules.”
- App → API → Core
- Core → Backend Abstraction → Selected Backend
- Backend → Core (ready events)
- Core → appropriate Watcher → User Callback
- Optional paths (e.g., inotify under Stat watcher).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User-facing
    UA["User Application"]:::user

    %% API Layer
    subgraph "API Layer"
        EvH["ev.h"]:::api
        EventH["event.h"]:::api
        EvPlus["ev++.h"]:::api
    end

    %% Core Event Loop
    subgraph "Core Event Loop"
        EvC["ev.c"]:::core
        EventC["event.c"]:::core
        EvVars["ev_vars.h"]:::core
        EvWrapCore["ev_wrap.h"]:::core
    end

    %% Watcher Subsystems
    subgraph "Watcher Subsystems"
        Watchers["event.c (I/O, timer, signal, child, stat, async)"]:::watcher
        StatOpt["Stat Watcher (optional inotify)"]:::watcherOpt
    end

    %% Backend Abstraction
    subgraph "Backend Abstraction"
        EvWrap["ev_wrap.h"]:::abstraction
    end

    %% Platform-Specific Backends
    subgraph "Platform-Specific Backends"
        Select["ev_select.c"]:::backend
        Poll["ev_poll.c"]:::backend
        Epoll["ev_epoll.c"]:::backend
        Kqueue["ev_kqueue.c"]:::backend
        Port["ev_port.c"]:::backend
        Win32["ev_win32.c"]:::backend
    end

    %% Connections
    UA -->|"calls API"| EvH
    UA --> EvPlus
    EvH -->|"defines API"| Core
    EventH --> Core
    EvPlus --> Core
    Core -->|"delegates"| EvWrap
    EvWrap --> Select
    EvWrap --> Poll
    EvWrap --> Epoll
    EvWrap --> Kqueue
    EvWrap --> Port
    EvWrap --> Win32
    Select -->|"ready events"| Core
    Poll --> Core
    Epoll --> Core
    Kqueue --> Core
    Port --> Core
    Win32 --> Core
    Core -->|"dispatch events"| Watchers
    Watchers -->|"invoke callbacks"| UA
    StatOpt -.-> Watchers

    %% Click Events
    click EvH "https://github.com/enki/libev/blob/master/ev.h"
    click EventH "https://github.com/enki/libev/blob/master/event.h"
    click EvPlus "https://github.com/enki/libev/blob/master/ev++.h"
    click EvC "https://github.com/enki/libev/blob/master/ev.c"
    click EventC "https://github.com/enki/libev/blob/master/event.c"
    click EvVars "https://github.com/enki/libev/blob/master/ev_vars.h"
    click EvWrapCore "https://github.com/enki/libev/blob/master/ev_wrap.h"
    click EvWrap "https://github.com/enki/libev/blob/master/ev_wrap.h"
    click Select "https://github.com/enki/libev/blob/master/ev_select.c"
    click Poll "https://github.com/enki/libev/blob/master/ev_poll.c"
    click Epoll "https://github.com/enki/libev/blob/master/ev_epoll.c"
    click Kqueue "https://github.com/enki/libev/blob/master/ev_kqueue.c"
    click Port "https://github.com/enki/libev/blob/master/ev_port.c"
    click Win32 "https://github.com/enki/libev/blob/master/ev_win32.c"

    %% Styles
    classDef user fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef api fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef core fill:#fff3b0,stroke:#333,stroke-width:1px
    classDef abstraction fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef watcher fill:#e6ccff,stroke:#333,stroke-width:1px
    classDef backend fill:#ffcccc,stroke:#333,stroke-width:1px
    classDef watcherOpt fill:#e6ccff,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
