writeonce/docs/plan/exploration/linux/02-eventfd.md
shoney.arickathil c0b0dbb846 docs: audit all markdown against the code, fix findings, flatten status folders
- README: shipped concurrency/HTTP/WebSockets sat in the roadmap as "not yet
  available"; "no package manager" contradicted [deps]; the deps example
  would not have compiled (the key IS the module name)
- runtime/README: leads with wovm, wo-rt.c demoted to a historical section;
  dropped 2 nonexistent recipes, crates/rt, @gc refcounting, 13 suites -> 18
- employee + log-watcher READMEs claimed "does not compile"; both are gates
- error catalog: +10 emitted codes incl WO-E250, the only diagnostic the
  shipped query surface raises; recorded why the sweep rotted
- language-surface: group-by parses, then the typechecker refuses it
- 00-code-review + 00-link-audit re-run; history kept, not rewritten
- 48 dead Rust-era exploration links de-linked rather than re-pointed (their
  prose names the retired plan by number); successor map -> discarded.md
- 08-project-structure: compiler/plan/ never existed; corpus has 9 dirs, 5 empty
- releasing.md: dropped a --draft step the workflow never had
- new docs/00-doc-audit.md: findings + disposition, incl one row where the
  audit was wrong and the doc it accused was right
- status folders removed: 34 stories flat, status only in frontmatter; 252
  links recomputed from resolved paths; board/board-views/structure retaught
- story 24 -> in-progress, since frontmatter is now the only truth
- new iteration 38: fs mutation verbs + net.connect, the two capability
  families no iteration owned
- new iteration 39: gofiber/fiber v3.5.0 parity study. The ledger called
  CSRF/sessions unblocked by iteration 34's HMAC, but the runtime has no
  source of randomness at all
- linkcheck skips .dev/.superpowers: 0 broken paths, 0 bad anchors

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 19:20:22 +02:00

2.6 KiB

02 — eventfd

Counter as a file descriptor. write(fd, &n, 8) adds n to the counter; read(fd, &buf, 8) drains it to zero (or decrements by one in semaphore mode). Paired with epoll, it's the cheapest way to wake the event loop from another flow — cross-thread signalling, scheduled work, shutdown requests.

Kernel source

Path What
reference/linux/fs/eventfd.c SYSCALL_DEFINE2(eventfd, ...), struct eventfd_ctx, read/write handlers.
reference/linux/include/uapi/linux/eventfd.h EFD_* flags.

Man pages

man 2 eventfd.

Rust FFI via libc

use libc::{eventfd, EFD_CLOEXEC, EFD_NONBLOCK, EFD_SEMAPHORE};
// read / write go through plain libc::read / libc::write

extern "C" {
    // eventfd is already wrapped in libc
}

Direct-syscall example

unsafe {
    let fd = libc::eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK);
    if fd < 0 { return Err(io::Error::last_os_error()); }

    // wake the loop from anywhere
    let one: u64 = 1;
    libc::write(fd, &one as *const _ as *const _, 8);

    // inside the loop, on EPOLLIN:
    let mut buf: u64 = 0;
    let n = libc::read(fd, &mut buf as *mut _ as *mut _, 8);
    // buf now holds the accumulated count (or 1 if EFD_SEMAPHORE)
}

Key flags

Flag Meaning
EFD_CLOEXEC Close on exec. Always set.
EFD_NONBLOCK read returns EAGAIN when counter is zero instead of blocking. Required when the fd is on an epoll loop.
EFD_SEMAPHORE Each read decrements by one (otherwise reads drain the whole counter). Useful as a bounded work queue.

Gotchas

  • Always 8-byte read / write. Short reads/writes return EINVAL — the counter is u64, full word or nothing.
  • Writing u64::MAX returns EINVAL; the counter can't hold more than u64::MAX - 1.
  • Multiple writers are OK; the kernel serialises. But reads race — use EFD_SEMAPHORE if you want one consumer per write.
  • Not async-signal-safe. Don't write(fd, ...) from a signal handler; use signalfd instead (see 04-signalfd.md).

Used by

02-event-loop-epoll.md — wake the loop for shutdown or internal work. Future sub crate ([09-native-subscriptions], not yet planned) uses it to signal that a subscriber queue has drained.

v1 port source

reference/crates/wo-event/src/eventfd.rs (66 LOC) — EventFd { new, write, read, as_raw_fd }.