feature. See also
. The project being documented here (as the example) is the Zig library itself.
Uring.sleep
fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.Cancelable!void
File
Code
fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.Cancelable!void {
const ev: *Evented = @ptrCast(@alignCast(userdata));
const timespec: linux.kernel_timespec, const clock: Io.Clock, const timeout_flags: u32 = timespec: switch (timeout) {
.none => .{
.{
.sec = std.math.maxInt(i64),
.nsec = std.time.ns_per_s - 1,
},
.awake,
linux.IORING_TIMEOUT_ABS,
},
.duration => |duration| {
const ns = duration.raw.toNanoseconds();
break :timespec .{
.{
.sec = @intCast(@divFloor(ns, std.time.ns_per_s)),
.nsec = @intCast(@mod(ns, std.time.ns_per_s)),
},
duration.clock,
0,
};
},
.deadline => |deadline| {
const ns = deadline.raw.toNanoseconds();
break :timespec .{
.{
.sec = @intCast(@divFloor(ns, std.time.ns_per_s)),
.nsec = @intCast(@mod(ns, std.time.ns_per_s)),
},
deadline.clock,
linux.IORING_TIMEOUT_ABS,
};
},
};
var cancel_region: CancelRegion = .init();
defer cancel_region.deinit();
const thread = try cancel_region.awaitIoUring();
thread.enqueue().* = .{
.opcode = .TIMEOUT,
.flags = 0,
.ioprio = 0,
.fd = 0,
.off = 0,
.addr = @intFromPtr(×pec),
.len = 1,
.rw_flags = timeout_flags | @as(u32, switch (clock) {
.real => linux.IORING_TIMEOUT_REALTIME,
else => 0,
.boot => linux.IORING_TIMEOUT_BOOTTIME,
}),
.user_data = @intFromPtr(cancel_region.fiber),
.buf_index = 0,
.personality = 0,
.splice_fd_in = 0,
.addr3 = 0,
.resv = 0,
};
ev.yield(null, .nothing);
// errors. The user had a chance to check clock resolution before
// getting here, which would have reported 0, making this a legal
// amount of time to sleep.
}