reactor
lib/reactor.vklib/reactor.vk
Pure-Veko readiness event loop, timers, and cross-thread wakeups over the OS reactor PAL:
- Linux x86_64 & arm64: epoll_create1 / epoll_ctl / epoll_pwait + eventfd2
- macOS arm64: kqueue / kevent + EVFILT_USER
- Windows x64: Win32 IOCP (CreateIoCompletionPort / GetQueuedCompletionStatus / PostQueuedCompletionStatus) + WSAPoll + loopback wake
- Wasm 3.0 (--wasm): wasi:sockets@0.3.0::net-poll
Requires .net capability.
EV-WRITE = fn [] i64
EV-ERR = fn [] i64
EV-HUP = fn [] i64
EV-RW = fn [] i64
CTL-MOD = fn [] i64
CTL-DEL = fn [] i64
FdEntry = struct { fd i64, token i64, events i64, cb any }
Registered file descriptor entry tracked by a Reactor.
TimerEntry = struct { id i64, fire_at_ns i64, interval_ns i64, active bool, cb any }
Scheduled timer entry tracked by a Reactor.
Reactor = struct { poller i64, next_token i64 = ..., next_timer i64 = ..., stopped bool = ..., fds map = ..., timers block[TimerEntry] = ..., max_tick dur = ... }
Single-threaded readiness event loop with O(1) kernel readiness + timers + thread-safe waker.
readable? = fn [ev IoEvent] bool
Returns true if ev.mask indicates readability (EV-READ).
use [reactor]
ev = reactor/IoEvent { token = 7, mask = (reactor/EV-READ) | (reactor/EV-HUP) }
if (not (reactor/readable? ev)) or (not (reactor/closed? ev)) or (reactor/writable? ev) [ err 'mask' ] else [ .ok ]
writable? = fn [ev IoEvent] bool
Returns true if ev.mask indicates writability (EV-WRITE).
waker = fn [r Reactor] i64
Returns a copyable i64 waker handle that can be passed to spawn workers for wake-handle.
wake-handle = fn [poller_handle i64] enum
Thread-safe wakeup of a Reactor blocked in poll or tick using its waker handle.
send-wake = fn [ch any, msg any, waker_handle i64] enum
Sends msg onto channel ch and immediately wakes the target reactor via waker_handle
so background spawn workers wake an OS epoll/kqueue/IOCP event loop in one call.
register-cb = fn [mut r Reactor, fd i64, events i64, cb any] i64
Registers fd with readiness events mask (EV-READ, EV-WRITE, EV-RW) and optional callback cb (.ok for pull mode).
Returns the assigned i64 token.
register = fn [mut r Reactor, fd i64, events i64] i64
Registers fd for pull-mode event delivery (no callback). Returns the assigned i64 token.
modify = fn [mut r Reactor, token i64, events i64] enum
Updates the readiness interest mask for a registered token.
unregister = fn [mut r Reactor, token i64] enum
Unregisters token from the OS poller and removes it from r.
token-fd = fn [r Reactor, token i64] i64
Returns the registered OS socket fd for token, or -1 if not found.
after-cb = fn [mut r Reactor, delay dur, cb any] i64
Schedules a one-shot timer firing after delay with optional callback cb (.ok for pull mode).
Returns the assigned timer_id.
after = fn [mut r Reactor, delay dur] i64
Schedules a pull-mode one-shot timer firing after delay. Returns timer_id.
every-cb = fn [mut r Reactor, interval dur, cb any] i64
Schedules a repeating timer firing every interval with optional callback cb. Returns timer_id.
every = fn [mut r Reactor, interval dur] i64
Schedules a pull-mode repeating timer firing every interval. Returns timer_id.
cancel-timer = fn [mut r Reactor, timer_id i64] enum
Cancels timer timer_id (idempotent).
next-wait = fn [r Reactor, max_wait dur] dur
Computes the effective wait duration bounded by max_wait and the earliest active timer deadline.
poll = fn [mut r Reactor, timeout dur] block[IoEvent]
Polls the OS reactor for ready I/O events up to timeout (automatically capped by pending timers).
Returns a block[IoEvent] of ready notifications.
due-timers = fn [mut r Reactor] block[i64]
Pull-mode timer drain: returns block[i64] of due timer IDs without invoking callbacks.
tick = fn [mut r Reactor, timeout dur] i64
Runs one callback-mode reactor iteration: polls I/O up to timeout, invokes FD callbacks and due timer callbacks.
Returns total number of events + timers dispatched.
stop = fn [mut r Reactor] enum
Requests that run stop after the current iteration and wakes the poller.
run = fn [mut r Reactor, max_iters i64] i64
Runs the callback-mode event loop until stop is called (or max_iters > 0 iterations elapse).
close = fn [mut r Reactor] enum
Closes the underlying OS poller and releases all reactor resources (idempotent).