KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
event_ctx.h
Go to the documentation of this file.
1#ifndef KEEL_EVENT_CTX_H
2#define KEEL_EVENT_CTX_H
3
4#include <keel/allocator.h>
5#include <keel/error.h>
6#include <keel/event.h>
7#include <stdint.h>
8
9/* ── KlWatcher — generic FD callback ──────────────────────────────── */
10
17typedef void (*KlWatcherFn)(int fd, KlEventMask ready, void *user_data);
18
29
30/* ── KlEventCtx — composable event loop + watcher context ────────── */
31
34
54
62
67
68/* ── Watcher API (operates on KlEventCtx, not KlServer) ──────────── */
69
79int kl_watcher_add(KlEventCtx *ctx, int fd, KlEventMask mask,
80 KlWatcherFn on_ready, void *user_data);
81
86int kl_watcher_mod(KlEventCtx *ctx, int fd, KlEventMask mask);
87
91void kl_watcher_del(KlEventCtx *ctx, int fd);
92
100int kl_watcher_rearm(KlEventCtx *ctx, int fd);
101
102/* ── Dispatch helpers ─────────────────────────────────────────────── */
103
114static inline int kl_event_dispatch(KlEventCtx *ctx, const KlEvent *event) {
115 uintptr_t tag = (uintptr_t)event->udata;
116 if (!(tag & 1))
117 return 0; /* not a watcher — caller handles */
118 KlWatcher *w = (KlWatcher *)(tag & ~(uintptr_t)1);
119 int wfd = w->fd;
120 ctx->dispatch_dirty = 0;
121 w->on_ready(wfd, event->ready, w->user_data);
122 // cppcheck-suppress knownConditionTrueFalse
123 if (!ctx->dispatch_dirty)
124 kl_watcher_rearm(ctx, wfd);
125 return 1;
126}
127
144int kl_event_ctx_run(KlEventCtx *ctx, int max_events, int timeout_ms);
145
146#endif
KlError
Diagnostic error codes for Keel public functions.
Definition error.h:11
KlEventMask
Definition event.h:6
int kl_event_ctx_init(KlEventCtx *ctx, KlAllocator *alloc)
Initialize an event context with the given allocator.
void(* KlWatcherFn)(int fd, KlEventMask ready, void *user_data)
Callback invoked when a watched FD becomes ready.
Definition event_ctx.h:17
void kl_watcher_del(KlEventCtx *ctx, int fd)
Remove a watcher and deregister its FD from the event loop.
void kl_event_ctx_free(KlEventCtx *ctx)
Free all watchers and close the event loop.
static int kl_event_dispatch(KlEventCtx *ctx, const KlEvent *event)
Dispatch a single event if it is a watcher (tagged pointer).
Definition event_ctx.h:114
int kl_watcher_mod(KlEventCtx *ctx, int fd, KlEventMask mask)
Change the event interest mask for a registered watcher.
int kl_watcher_rearm(KlEventCtx *ctx, int fd)
Re-arm a watcher after its callback fires.
int kl_watcher_add(KlEventCtx *ctx, int fd, KlEventMask mask, KlWatcherFn on_ready, void *user_data)
Register a file descriptor with the event loop.
int kl_event_ctx_run(KlEventCtx *ctx, int max_events, int timeout_ms)
Run one tick of the event loop, dispatching all watcher events.
Bring-your-own allocator vtable.
Definition allocator.h:12
Composable event loop context.
Definition event_ctx.h:42
KlAllocator * alloc
Definition event_ctx.h:44
int64_t timer_next_id
Definition event_ctx.h:52
KlError last_error
Definition event_ctx.h:47
KlTimerEntry * timers
Definition event_ctx.h:49
int timer_count
Definition event_ctx.h:50
int dispatch_dirty
Definition event_ctx.h:46
int timer_cap
Definition event_ctx.h:51
KlWatcher * watchers
Definition event_ctx.h:45
KlEventLoop loop
Definition event_ctx.h:43
Definition event.h:16
Definition event.h:11
void * udata
Definition event.h:12
KlEventMask ready
Definition event.h:13
Timer heap entry (stored in KlEventCtx.timers array).
Definition timer.h:16
A registered FD watcher (heap-allocated, ctx-owned list).
Definition event_ctx.h:22
struct KlWatcher * next
Definition event_ctx.h:27
void * user_data
Definition event_ctx.h:26
KlWatcherFn on_ready
Definition event_ctx.h:25
KlEventMask mask
Definition event_ctx.h:24
int fd
Definition event_ctx.h:23