How enki/libev works
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).