Zig 0.16: sleep

Laziest approach: create a global var io in ui, since it is used by the
global allocator wrapper.
This commit is contained in:
LN Liberda 2026-05-22 21:01:31 +02:00
parent 6862d1d5cb
commit 496891089e

View file

@ -12,6 +12,8 @@ pub var inited: bool = false;
pub var main_thread: std.Thread.Id = undefined;
pub var oom_threads = std.atomic.Value(usize).init(0);
pub var io = &main.io;
pub var rows: u32 = undefined;
pub var cols: u32 = undefined;
@ -26,8 +28,6 @@ pub fn quit() noreturn {
std.process.exit(0);
}
const sleep = if (@hasDecl(std.time, "sleep")) std.time.sleep else std.Thread.sleep;
// Should be called when malloc fails. Will show a message to the user, wait
// for a second and return to give it another try.
// Glitch: this function may be called while we're in the process of drawing
@ -43,12 +43,12 @@ pub fn oom() void {
const haveui = inited;
deinit();
std.debug.print("\x1b7\x1b[JOut of memory, trying again in 1 second. Hit Ctrl-C to abort.\x1b8", .{});
sleep(std.time.ns_per_s);
io.sleep(.fromSeconds(1), .awake) catch unreachable;
if (haveui)
init();
} else {
_ = oom_threads.fetchAdd(1, .monotonic);
sleep(std.time.ns_per_s);
io.sleep(.fromSeconds(1), .awake) catch unreachable;
_ = oom_threads.fetchSub(1, .monotonic);
}
}
@ -630,7 +630,7 @@ pub fn getch(block: bool) i32 {
}
if (ch == c.ERR) {
if (!block) return 0;
sleep(10*std.time.ns_per_ms);
io.sleep(.fromSeconds(10), .awake) catch unreachable;
continue;
}
return ch;