- 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>
77 lines
3.3 KiB
Markdown
77 lines
3.3 KiB
Markdown
# 03 — `timerfd`
|
|
|
|
Timers as file descriptors. Set an expiry with `timerfd_settime`, `read` the fd to retrieve the number of expirations, `epoll` notifies the loop when the timer fires. Drives everything in the runtime that needs a deadline without a separate timer thread: keepalives, debounce windows, checkpoint intervals.
|
|
|
|
## Kernel source
|
|
|
|
| Path | What |
|
|
| --- | --- |
|
|
| [`reference/linux/fs/timerfd.c`](../../../../.dev/reference/linux/fs/timerfd.c) | All three syscalls (`timerfd_create`, `timerfd_settime`, `timerfd_gettime`). |
|
|
| [`reference/linux/include/uapi/linux/timerfd.h`](../../../../.dev/reference/linux/include/uapi/linux/timerfd.h) | `TFD_*` flags. |
|
|
|
|
## Man pages
|
|
|
|
`man 2 timerfd_create`, `man 2 timerfd_settime`, `man 2 timerfd_gettime`.
|
|
|
|
## Rust FFI via `libc`
|
|
|
|
```rust
|
|
use libc::{timerfd_create, timerfd_settime, timerfd_gettime};
|
|
use libc::{itimerspec, timespec};
|
|
use libc::{TFD_CLOEXEC, TFD_NONBLOCK, TFD_TIMER_ABSTIME};
|
|
use libc::{CLOCK_MONOTONIC, CLOCK_REALTIME};
|
|
```
|
|
|
|
## Direct-syscall example
|
|
|
|
```rust
|
|
unsafe {
|
|
let fd = libc::timerfd_create(
|
|
libc::CLOCK_MONOTONIC,
|
|
libc::TFD_CLOEXEC | libc::TFD_NONBLOCK,
|
|
);
|
|
if fd < 0 { return Err(io::Error::last_os_error()); }
|
|
|
|
// one-shot: fire in 150ms, no interval
|
|
let spec = libc::itimerspec {
|
|
it_value: libc::timespec { tv_sec: 0, tv_nsec: 150_000_000 },
|
|
it_interval: libc::timespec { tv_sec: 0, tv_nsec: 0 },
|
|
};
|
|
// periodic: replace it_interval with the period, e.g. { tv_sec: 1, tv_nsec: 0 }
|
|
|
|
if libc::timerfd_settime(fd, 0, &spec, std::ptr::null_mut()) < 0 {
|
|
return Err(io::Error::last_os_error());
|
|
}
|
|
|
|
// register fd on the epoll loop; on EPOLLIN:
|
|
let mut expirations: u64 = 0;
|
|
libc::read(fd, &mut expirations as *mut _ as *mut _, 8);
|
|
// expirations > 0 means the timer fired. Usually 1 for a oneshot,
|
|
// could be >1 for a periodic timer whose consumer missed ticks.
|
|
}
|
|
```
|
|
|
|
## Key flags
|
|
|
|
| Flag | Meaning |
|
|
| --- | --- |
|
|
| `CLOCK_MONOTONIC` | Steady clock; immune to wall-clock jumps. **Default choice for runtime timers.** |
|
|
| `CLOCK_REALTIME` | Wall clock; jumps when NTP corrects or user sets time. Avoid unless you need calendar semantics. |
|
|
| `TFD_CLOEXEC` | Close on exec. Always set. |
|
|
| `TFD_NONBLOCK` | `read` doesn't block when expirations == 0. Required when on `epoll`. |
|
|
| `TFD_TIMER_ABSTIME` | Interpret `it_value` as absolute (not relative). Passed to `timerfd_settime`'s `flags`, not at create. |
|
|
|
|
## Gotchas
|
|
|
|
- **Reads are always 8 bytes.** The value is an expiration count.
|
|
- **Disarming is `timerfd_settime(fd, 0, &{0,0,0,0}, NULL)`** — a zero spec cancels pending fires.
|
|
- **Re-arming a periodic timer** replaces the spec atomically; no "pause then resume" surface.
|
|
- **Resolution is nanosecond-granular but scheduling can slip.** Don't use for sub-millisecond accuracy — use a busy loop or hardware timer for that.
|
|
|
|
## Used by
|
|
|
|
`02-event-loop-epoll.md` — housekeeping timers. `07-inotify-content-watcher.md` — 150 ms debounce window after an inotify burst. Future subscription phase — keepalive pings to long-lived connections.
|
|
|
|
## v1 port source
|
|
|
|
[`reference/crates/wo-event/src/timerfd.rs`](../../../../.dev/reference/crates/wo-event/src/timerfd.rs) (91 LOC) — `TimerFd { oneshot(dur), periodic(dur), disarm, read_expirations }`.
|