authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-10 18:14:43-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:09-08:00
log95b0399d1bf7e12aefeba79652141d202f805fe4
tree5867007c138fc954a9102ed8c3f42e6d087528bc
parent3a6e15449b887481a0ed24fc948157c222f0072a

std: finish implementing futexWait with timer


3 files changed, 39 insertions(+), 35 deletions(-)

lib/std/Io/Threaded.zig-1
......@@ -1147,7 +1147,6 @@ const GroupClosure = struct {
11471147 const group = gc.group;
11481148 const group_state: *std.atomic.Value(usize) = @ptrCast(&group.state);
11491149 const event: *Io.Event = @ptrCast(&group.context);
1150
11511150 current_thread.current_closure = closure;
11521151 current_thread.cancel_protection = .unblocked;
11531152
lib/std/Progress.zig+33-28
......@@ -24,7 +24,7 @@ terminal_mode: TerminalMode,
2424update_worker: ?Io.Future(void),
2525
2626/// Atomically set by SIGWINCH as well as the root done() function.
27redraw_event: Io.ResetEvent,
27redraw_event: Io.Event,
2828/// Indicates a request to shut down and reset global state.
2929/// Accessed atomically.
3030done: bool,
......@@ -333,8 +333,9 @@ pub const Node = struct {
333333 }
334334 } else {
335335 @atomicStore(bool, &global_progress.done, true, .monotonic);
336 global_progress.redraw_event.set();
337 if (global_progress.update_worker) |worker| worker.await(global_progress.io);
336 const io = global_progress.io;
337 global_progress.redraw_event.set(io);
338 if (global_progress.update_worker) |*worker| worker.await(io);
338339 }
339340 }
340341
......@@ -421,7 +422,7 @@ pub const StartFailure = union(enum) {
421422 unstarted,
422423 spawn_ipc_worker: error{ConcurrencyUnavailable},
423424 spawn_update_worker: error{ConcurrencyUnavailable},
424 parse_env_var: error{},
425 parse_env_var: error{ InvalidCharacter, Overflow },
425426};
426427
427428const node_storage_buffer_len = 83;
......@@ -452,7 +453,7 @@ const noop_impl = builtin.single_threaded or switch (builtin.os.tag) {
452453/// Call `Node.end` when done.
453454///
454455/// If an error occurs, `start_failure` will be populated.
455pub fn start(options: Options, io: Io) Node {
456pub fn start(io: Io, options: Options) Node {
456457 // Ensure there is only 1 global Progress object.
457458 if (global_progress.node_end_index != 0) {
458459 debug_start_trace.dump();
......@@ -467,8 +468,8 @@ pub fn start(options: Options, io: Io) Node {
467468
468469 assert(options.draw_buffer.len >= 200);
469470 global_progress.draw_buffer = options.draw_buffer;
470 global_progress.refresh_rate_ns = options.refresh_rate_ns;
471 global_progress.initial_delay_ns = options.initial_delay_ns;
471 global_progress.refresh_rate_ns = @intCast(options.refresh_rate_ns.toNanoseconds());
472 global_progress.initial_delay_ns = @intCast(options.initial_delay_ns.toNanoseconds());
472473
473474 if (noop_impl)
474475 return Node.none;
......@@ -541,9 +542,13 @@ pub fn setStatus(new_status: Status) void {
541542}
542543
543544/// Returns whether a resize is needed to learn the terminal size.
544fn wait(timeout_ns: u64) bool {
545 const resize_flag = if (global_progress.redraw_event.timedWait(timeout_ns)) |_| true else |err| switch (err) {
546 error.Timeout => false,
545fn wait(io: Io, timeout_ns: u64) bool {
546 const timeout: Io.Timeout = .{ .duration = .{
547 .clock = .awake,
548 .raw = .fromNanoseconds(timeout_ns),
549 } };
550 const resize_flag = if (global_progress.redraw_event.waitTimeout(io, timeout)) |_| true else |err| switch (err) {
551 error.Timeout, error.Canceled => false,
547552 };
548553 global_progress.redraw_event.reset();
549554 return resize_flag or (global_progress.cols == 0);
......@@ -555,34 +560,34 @@ fn updateThreadRun(io: Io) void {
555560 var serialized_buffer: Serialized.Buffer = undefined;
556561
557562 {
558 const resize_flag = wait(global_progress.initial_delay_ns);
563 const resize_flag = wait(io, global_progress.initial_delay_ns);
559564 if (@atomicLoad(bool, &global_progress.done, .monotonic)) return;
560565 maybeUpdateSize(resize_flag);
561566
562567 const buffer, _ = computeRedraw(&serialized_buffer);
563 if (io.tryLockStderrWriter(&.{})) |w| {
568 if (io.tryLockStderrWriter(&.{})) |fw| {
564569 defer io.unlockStderrWriter();
565570 global_progress.need_clear = true;
566 w.writeAll(buffer) catch return;
571 fw.writeAllUnescaped(buffer) catch return;
567572 }
568573 }
569574
570575 while (true) {
571 const resize_flag = wait(global_progress.refresh_rate_ns);
576 const resize_flag = wait(io, global_progress.refresh_rate_ns);
572577
573578 if (@atomicLoad(bool, &global_progress.done, .monotonic)) {
574 const w = io.lockStderrWriter(&.{}) catch return;
579 const fw = io.lockStderrWriter(&.{}) catch return;
575580 defer io.unlockStderrWriter();
576 return clearWrittenWithEscapeCodes(w) catch {};
581 return clearWrittenWithEscapeCodes(fw) catch {};
577582 }
578583
579584 maybeUpdateSize(resize_flag);
580585
581586 const buffer, _ = computeRedraw(&serialized_buffer);
582 if (io.tryLockStderrWriter(&.{})) |w| {
587 if (io.tryLockStderrWriter(&.{})) |fw| {
583588 defer io.unlockStderrWriter();
584589 global_progress.need_clear = true;
585 w.writeAll(buffer) catch return;
590 fw.writeAllUnescaped(buffer) catch return;
586591 }
587592 }
588593}
......@@ -599,22 +604,22 @@ fn windowsApiUpdateThreadRun(io: Io) void {
599604 var serialized_buffer: Serialized.Buffer = undefined;
600605
601606 {
602 const resize_flag = wait(global_progress.initial_delay_ns);
607 const resize_flag = wait(io, global_progress.initial_delay_ns);
603608 if (@atomicLoad(bool, &global_progress.done, .monotonic)) return;
604609 maybeUpdateSize(resize_flag);
605610
606611 const buffer, const nl_n = computeRedraw(&serialized_buffer);
607 if (io.tryLockStderrWriter()) |w| {
612 if (io.tryLockStderrWriter()) |fw| {
608613 defer io.unlockStderrWriter();
609614 windowsApiWriteMarker();
610615 global_progress.need_clear = true;
611 w.writeAll(buffer) catch return;
616 fw.writeAllUnescaped(buffer) catch return;
612617 windowsApiMoveToMarker(nl_n) catch return;
613618 }
614619 }
615620
616621 while (true) {
617 const resize_flag = wait(global_progress.refresh_rate_ns);
622 const resize_flag = wait(io, global_progress.refresh_rate_ns);
618623
619624 if (@atomicLoad(bool, &global_progress.done, .monotonic)) {
620625 _ = io.lockStderrWriter() catch return;
......@@ -625,24 +630,24 @@ fn windowsApiUpdateThreadRun(io: Io) void {
625630 maybeUpdateSize(resize_flag);
626631
627632 const buffer, const nl_n = computeRedraw(&serialized_buffer);
628 if (io.tryLockStderrWriter()) |w| {
633 if (io.tryLockStderrWriter()) |fw| {
629634 defer io.unlockStderrWriter();
630635 clearWrittenWindowsApi() catch return;
631636 windowsApiWriteMarker();
632637 global_progress.need_clear = true;
633 w.writeAll(buffer) catch return;
638 fw.writeAllUnescaped(buffer) catch return;
634639 windowsApiMoveToMarker(nl_n) catch return;
635640 }
636641 }
637642}
638643
639fn ipcThreadRun(io: Io, file: Io.File) anyerror!void {
644fn ipcThreadRun(io: Io, file: Io.File) void {
640645 // Store this data in the thread so that it does not need to be part of the
641646 // linker data of the main executable.
642647 var serialized_buffer: Serialized.Buffer = undefined;
643648
644649 {
645 _ = wait(global_progress.initial_delay_ns);
650 _ = wait(io, global_progress.initial_delay_ns);
646651
647652 if (@atomicLoad(bool, &global_progress.done, .monotonic))
648653 return;
......@@ -654,7 +659,7 @@ fn ipcThreadRun(io: Io, file: Io.File) anyerror!void {
654659 }
655660
656661 while (true) {
657 _ = wait(global_progress.refresh_rate_ns);
662 _ = wait(io, global_progress.refresh_rate_ns);
658663
659664 if (@atomicLoad(bool, &global_progress.done, .monotonic))
660665 return;
......@@ -1504,7 +1509,7 @@ fn handleSigWinch(sig: posix.SIG, info: *const posix.siginfo_t, ctx_ptr: ?*anyop
15041509 _ = info;
15051510 _ = ctx_ptr;
15061511 assert(sig == .WINCH);
1507 global_progress.redraw_event.set();
1512 global_progress.redraw_event.set(global_progress.io);
15081513}
15091514
15101515const have_sigwinch = switch (builtin.os.tag) {
lib/std/debug.zig+6-6
......@@ -816,9 +816,9 @@ pub fn writeStackTrace(st: *const StackTrace, writer: *Writer, fwm: File.Writer.
816816}
817817/// A thin wrapper around `writeStackTrace` which writes to stderr and ignores write errors.
818818pub fn dumpStackTrace(st: *const StackTrace) void {
819 const stderr, const tty_config = lockStderrWriter(&.{});
819 const stderr = lockStderrWriter(&.{});
820820 defer unlockStderrWriter();
821 writeStackTrace(st, stderr, tty_config) catch |err| switch (err) {
821 writeStackTrace(st, &stderr.interface, stderr.mode) catch |err| switch (err) {
822822 error.WriteFailed => {},
823823 };
824824}
......@@ -1682,21 +1682,21 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
16821682 pub fn dump(t: @This()) void {
16831683 if (!enabled) return;
16841684
1685 const stderr, const tty_config = lockStderrWriter(&.{});
1685 const stderr = lockStderrWriter(&.{});
16861686 defer unlockStderrWriter();
16871687 const end = @min(t.index, size);
16881688 for (t.addrs[0..end], 0..) |frames_array, i| {
1689 stderr.print("{s}:\n", .{t.notes[i]}) catch return;
1689 stderr.interface.print("{s}:\n", .{t.notes[i]}) catch return;
16901690 var frames_array_mutable = frames_array;
16911691 const frames = mem.sliceTo(frames_array_mutable[0..], 0);
16921692 const stack_trace: StackTrace = .{
16931693 .index = frames.len,
16941694 .instruction_addresses = frames,
16951695 };
1696 writeStackTrace(&stack_trace, stderr, tty_config) catch return;
1696 writeStackTrace(&stack_trace, &stderr.interface, stderr.mode) catch return;
16971697 }
16981698 if (t.index > end) {
1699 stderr.print("{d} more traces not shown; consider increasing trace size\n", .{
1699 stderr.interface.print("{d} more traces not shown; consider increasing trace size\n", .{
17001700 t.index - end,
17011701 }) catch return;
17021702 }