Module: sys.poll

Source: ./sys/poll.reef


Overview

sys/poll - poll(2) event loop

Monitor multiple file descriptors for readability/writability events. Supports up to 64 file descriptors per poll set.

Example - Wait for readable pipe: poll_clear() let idx = poll_add(read_fd, POLLIN()) let ready = poll_wait(5000) // 5 second timeout if ready > 0 and poll_readable(idx) let data = fd_read(read_fd, 256) end if


Functions

fn poll_add(fd: int, events: int): int

Add fd to poll set with event mask Returns index for checking results

fn poll_wait(timeout_ms: int): int

Wait for events (timeout_ms: -1=block, 0=non-blocking, >0=ms) Returns number of ready fds, 0 on timeout

fn poll_count(): int

Number of fds currently in this thread's poll set

The natural pair of poll_add()'s returned index: until reef-lang#54 there was no way to ask this from Reef at all.

fn poll_revents(index: int): option.Option[int]

Get raw revents for fd at index

0.9.2 C16 (reef-lang#54): None when the index is not in the poll set.

This returned 0 both for "index out of range" and for "this fd has no pending events" -- and the second is the overwhelmingly common legitimate answer, so the sentinel collided with the normal case rather than a rare one. The bound could not be tested from Reef until reef_poll_count() was added, because the count is a file-static __thread int in reef_poll.c.

The C clamp is deliberately left in place: it is what prevents an out-of-bounds read of reef_poll_fds. C still answers 0 for both cases; this is the layer that distinguishes them -- the same shape as sys.hoststat.cpu_pct_at over reef_hoststat_ncpu().

DELIBERATELY NO poll_revents_or. None here means the index is NOT IN THE POLL SET -- a stale index kept across a poll_clear(), or one that was never added. poll_revents_or(idx, 0) would answer "no pending events" for exactly that case, which is the pre-reef-lang#54 behaviour and the reason this function was converted: a caller reads "idle" forever instead of learning the index is gone, and that is how you miss a hangup. The default belongs nowhere near this one; use poll_count() to validate an index.

fn poll_readable(index: int): bool

Check if fd at index is readable

fn poll_writable(index: int): bool

Check if fd at index is writable

fn poll_error(index: int): bool

Check if fd at index has error

fn poll_hangup(index: int): bool

Check if fd at index has hangup

fn POLLIN(): int

Event constants

fn POLLOUT(): int

fn POLLERR(): int

fn POLLHUP(): int


Procedures

proc poll_clear()

Reset the poll fd set


Generated by reefc doc