veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

reactor

lib/reactor.vk

lib/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.

signature
EV-READ = fn [] i64

Readiness event mask flags.

signature
EV-WRITE = fn [] i64
signature
EV-ERR = fn [] i64
signature
EV-HUP = fn [] i64
signature
EV-RW = fn [] i64
signature
CTL-ADD = fn [] i64

Poller control operations.

signature
CTL-MOD = fn [] i64
signature
CTL-DEL = fn [] i64
IoEvent struct
signature
IoEvent = struct { token i64, mask i64 }

One readiness notification delivered by poll.

FdEntry struct
signature
FdEntry = struct { fd i64, token i64, events i64, cb any }

Registered file descriptor entry tracked by a Reactor.

TimerEntry struct
signature
TimerEntry = struct { id i64, fire_at_ns i64, interval_ns i64, active bool, cb any }

Scheduled timer entry tracked by a Reactor.

Reactor struct
signature
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.

signature
readable? = fn [ev IoEvent] bool

Returns true if ev.mask indicates readability (EV-READ).

Executable Example
veko
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 ]
signature
writable? = fn [ev IoEvent] bool

Returns true if ev.mask indicates writability (EV-WRITE).

signature
errored? = fn [ev IoEvent] bool

Returns true if ev.mask indicates a socket error (EV-ERR).

signature
closed? = fn [ev IoEvent] bool

Returns true if ev.mask indicates peer hangup/EOF (EV-HUP).

new fn caps: .net
signature
new = fn [] Reactor

Creates a new OS-backed readiness Reactor.

signature
waker = fn [r Reactor] i64

Returns a copyable i64 waker handle that can be passed to spawn workers for wake-handle.

wake-handle fn caps: .net
signature
wake-handle = fn [poller_handle i64] enum

Thread-safe wakeup of a Reactor blocked in poll or tick using its waker handle.

wake fn
signature
wake = fn [r Reactor] enum

Wakes r if blocked in poll or tick.

signature
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 caps: .net
signature
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.

signature
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 caps: .net
signature
modify = fn [mut r Reactor, token i64, events i64] enum

Updates the readiness interest mask for a registered token.

unregister fn caps: .net
signature
unregister = fn [mut r Reactor, token i64] enum

Unregisters token from the OS poller and removes it from r.

signature
token-fd = fn [r Reactor, token i64] i64

Returns the registered OS socket fd for token, or -1 if not found.

after-cb fn caps: .clocks
signature
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.

signature
after = fn [mut r Reactor, delay dur] i64

Schedules a pull-mode one-shot timer firing after delay. Returns timer_id.

every-cb fn caps: .clocks
signature
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.

signature
every = fn [mut r Reactor, interval dur] i64

Schedules a pull-mode repeating timer firing every interval. Returns timer_id.

signature
cancel-timer = fn [mut r Reactor, timer_id i64] enum

Cancels timer timer_id (idempotent).

next-wait fn caps: .clocks
signature
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 caps: .net
signature
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.

signature
due-timers = fn [mut r Reactor] block[i64]

Pull-mode timer drain: returns block[i64] of due timer IDs without invoking callbacks.

tick fn
signature
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
signature
stop = fn [mut r Reactor] enum

Requests that run stop after the current iteration and wakes the poller.

run fn
signature
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 caps: .net
signature
close = fn [mut r Reactor] enum

Closes the underlying OS poller and releases all reactor resources (idempotent).