An io_uring completion loop for Zig: one ring, one tick, and a user_data type you define.
Zig's standard library already encodes every io_uring operation. What it does not have is the loop around them: when to submit, when to enter, and what runs when. uring is that loop, and nothing else.
It is parameterized by your tag, the value user_data carries home, so a completion means whatever you say it means. The handler is known at comptime, which makes every completion a direct call. Nothing allocates after init, and the loop never closes a descriptor, never frees a byte, and never rearms an operation.
const std = @import("std");
const uring = @import("uring");
const Tag = packed struct(u64) { op: u8, slot: u16, gen: u16, spare: u24 };
var cqes: [64]std.os.linux.io_uring_cqe = undefined;
var loop: uring.Loop(Tag) = try .init(.{
.cqes = &cqes,
.setup = .{ .single_issuer = true, .defer_taskrun = true },
});
defer loop.deinit();
_ = try loop.recv(.{ .op = 1, .slot = 7, .gen = 1 }, fd, .{ .buffer = buf }, 0);
// One pass per call. The deadline is the caller's, computed from the loop's
// clock every tick, because a moving deadline is application state.
while (!loop.stopped) {
const next = server.nextDeadline(loop.now());
try loop.tick(&server, Server.onCompletion, .{ .until = next });
}A program with no deadlines of its own calls run instead, which ticks until
stop.
Deadlines ride the enter, not an operation: io_uring_enter takes a timeout
through IORING_ENTER_EXT_ARG, so moving a deadline costs no submission and no
cancel.
- The loop:
Loop(Tag).tick,run,post,stop, andnow. One pass publishes, reaps, enters, dispatches, and submits, in that order. - The tag: any 64-bit type.
ping, a packed struct, a pointer.Cqedecodes it once and hands it to a comptime handler. - Waiting:
Wait.poll,no_wait,once, anduntil, and what each costs. - Deadlines: a deadline through the enter with no submitted operation, and
timeoutwhen you want the kernel to own the clock. - Operations: every opcode the standard library encodes, each taking your tag and returning the SQE.
- Registered resources: files, buffers, eventfds, NAPI, and the probe.
- Provided buffers:
BufferGroupwith a multishot receive that fills ring buffers the kernel picks.stdrequests incremental consumption (a completion is a chunk);initBufferGroup(..., .{ .incremental = false })registers whole-buffer mode, where a completion is one message on a message socket. - The ring:
Ring, its setup struct, and the submit discipline that keeps the queue from filling. - Performance: the loop against the equivalent hand-written loop, measured: about 3 ns per completion and two clock reads per idle pass.
Not implemented:
msg_ring, futex operations,tee,madvise,openat2,timeout_update, and a genericURING_CMDfor non-socket commands: the standard library does not encode them. Build the SQE throughLoop.sqeif you need one.- Registration for personalities, restrictions, io-wq worker limits,
sync_cancel,clamp,pbuf_status, andfiles_update2: same reason.
zig fetch --save https://baskingspot.dev/uring/v0.4.0.tar.gzAdd the module to your build:
const uring = b.dependency("uring", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("uring", uring.module("uring"));uring builds with Zig 0.17, on Linux only, because io_uring is Linux.
- The guide says which piece to use for a task, with examples that compile.
- The API reference documents every symbol.
just cinix develop provides the toolchain. just lists the recipes, and just ci runs what CI runs. just bench compares the loop against the equivalent hand-written loop.
MIT. See LICENSE. The logo files in docs/static/ are baskingspot marks and are not covered by it. See docs/static/LICENSE.
Built with LLM assistance. How, in detail.