authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-08 21:57:46-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:08-08:00
log90f7259ef17e8e07c2c3f71bea65d103cfe52c07
treec7d4025d746c265dd5907b81295b86d6f67eb80c
parentbee8005fe6817ade9191de0493888b14cdbcac31

std.Progress: use a global static Io instance

This decision should be audited and discussed. Some factors: * Passing an Io instance into start. * Avoiding reference to global static instance if it won't be used, so that it doesn't bloat the executable. * Being able to use std.debug.print, and related functionality when debugging std.Io instances and std.Progress.

3 files changed, 62 insertions(+), 40 deletions(-)

lib/std/Io/Threaded.zig+19-1
......@@ -771,6 +771,7 @@ pub fn io(t: *Threaded) Io {
771771 .windows => netWriteWindows,
772772 else => netWritePosix,
773773 },
774 .netWriteFile = netWriteFile,
774775 .netSend = switch (native_os) {
775776 .windows => netSendWindows,
776777 else => netSendPosix,
......@@ -872,6 +873,7 @@ pub fn ioBasic(t: *Threaded) Io {
872873 .netClose = netCloseUnavailable,
873874 .netRead = netReadUnavailable,
874875 .netWrite = netWriteUnavailable,
876 .netWriteFile = netWriteFileUnavailable,
875877 .netSend = netSendUnavailable,
876878 .netReceive = netReceiveUnavailable,
877879 .netInterfaceNameResolve = netInterfaceNameResolveUnavailable,
......@@ -6782,7 +6784,7 @@ fn netWriteFile(
67826784 header: []const u8,
67836785 file_reader: *File.Reader,
67846786 limit: Io.Limit,
6785) net.Stream.WriteFileError!usize {
6787) net.Stream.Writer.WriteFileError!usize {
67866788 const t: *Threaded = @ptrCast(@alignCast(userdata));
67876789 _ = t;
67886790 _ = socket_handle;
......@@ -6792,6 +6794,22 @@ fn netWriteFile(
67926794 return error.Unimplemented; // TODO
67936795}
67946796
6797fn netWriteFileUnavailable(
6798 userdata: ?*anyopaque,
6799 socket_handle: net.Socket.Handle,
6800 header: []const u8,
6801 file_reader: *File.Reader,
6802 limit: Io.Limit,
6803) net.Stream.Writer.WriteFileError!usize {
6804 const t: *Threaded = @ptrCast(@alignCast(userdata));
6805 _ = t;
6806 _ = socket_handle;
6807 _ = header;
6808 _ = file_reader;
6809 _ = limit;
6810 return error.NetworkDown;
6811}
6812
67956813fn fileWriteFilePositional(
67966814 userdata: ?*anyopaque,
67976815 file: File,
lib/std/Progress.zig+37-34
......@@ -458,13 +458,16 @@ pub fn start(options: Options) Node {
458458 if (noop_impl)
459459 return Node.none;
460460
461 const io = static_threaded_io.io();
462
461463 if (std.process.parseEnvVarInt("ZIG_PROGRESS", u31, 10)) |ipc_fd| {
462464 global_progress.update_thread = std.Thread.spawn(.{}, ipcThreadRun, .{
463 @as(posix.fd_t, switch (@typeInfo(posix.fd_t)) {
465 io,
466 @as(Io.File, .{ .handle = switch (@typeInfo(posix.fd_t)) {
464467 .int => ipc_fd,
465468 .pointer => @ptrFromInt(ipc_fd),
466469 else => @compileError("unsupported fd_t of " ++ @typeName(posix.fd_t)),
467 }),
470 } }),
468471 }) catch |err| {
469472 std.log.warn("failed to spawn IPC thread for communicating progress to parent: {s}", .{@errorName(err)});
470473 return Node.none;
......@@ -476,9 +479,9 @@ pub fn start(options: Options) Node {
476479 }
477480 const stderr: Io.File = .stderr();
478481 global_progress.terminal = stderr;
479 if (stderr.enableAnsiEscapeCodes()) |_| {
482 if (stderr.enableAnsiEscapeCodes(io)) |_| {
480483 global_progress.terminal_mode = .ansi_escape_codes;
481 } else |_| if (is_windows and stderr.isTty()) {
484 } else |_| if (is_windows and stderr.isTty(io)) {
482485 global_progress.terminal_mode = TerminalMode{ .windows_api = .{
483486 .code_page = windows.kernel32.GetConsoleOutputCP(),
484487 } };
......@@ -499,8 +502,8 @@ pub fn start(options: Options) Node {
499502
500503 if (switch (global_progress.terminal_mode) {
501504 .off => unreachable, // handled a few lines above
502 .ansi_escape_codes => std.Thread.spawn(.{}, updateThreadRun, .{}),
503 .windows_api => if (is_windows) std.Thread.spawn(.{}, windowsApiUpdateThreadRun, .{}) else unreachable,
505 .ansi_escape_codes => std.Thread.spawn(.{}, updateThreadRun, .{io}),
506 .windows_api => if (is_windows) std.Thread.spawn(.{}, windowsApiUpdateThreadRun, .{io}) else unreachable,
504507 }) |thread| {
505508 global_progress.update_thread = thread;
506509 } else |err| {
......@@ -531,7 +534,7 @@ fn wait(timeout_ns: u64) bool {
531534 return resize_flag or (global_progress.cols == 0);
532535}
533536
534fn updateThreadRun() void {
537fn updateThreadRun(io: Io) void {
535538 // Store this data in the thread so that it does not need to be part of the
536539 // linker data of the main executable.
537540 var serialized_buffer: Serialized.Buffer = undefined;
......@@ -544,7 +547,7 @@ fn updateThreadRun() void {
544547 const buffer, _ = computeRedraw(&serialized_buffer);
545548 if (stderr_mutex.tryLock()) {
546549 defer stderr_mutex.unlock();
547 write(buffer) catch return;
550 write(io, buffer) catch return;
548551 global_progress.need_clear = true;
549552 }
550553 }
......@@ -555,7 +558,7 @@ fn updateThreadRun() void {
555558 if (@atomicLoad(bool, &global_progress.done, .monotonic)) {
556559 stderr_mutex.lock();
557560 defer stderr_mutex.unlock();
558 return clearWrittenWithEscapeCodes() catch {};
561 return clearWrittenWithEscapeCodes(io) catch {};
559562 }
560563
561564 maybeUpdateSize(resize_flag);
......@@ -563,7 +566,7 @@ fn updateThreadRun() void {
563566 const buffer, _ = computeRedraw(&serialized_buffer);
564567 if (stderr_mutex.tryLock()) {
565568 defer stderr_mutex.unlock();
566 write(buffer) catch return;
569 write(io, buffer) catch return;
567570 global_progress.need_clear = true;
568571 }
569572 }
......@@ -577,7 +580,7 @@ fn windowsApiWriteMarker() void {
577580 _ = windows.kernel32.WriteConsoleW(handle, &[_]u16{windows_api_start_marker}, 1, &num_chars_written, null);
578581}
579582
580fn windowsApiUpdateThreadRun() void {
583fn windowsApiUpdateThreadRun(io: Io) void {
581584 var serialized_buffer: Serialized.Buffer = undefined;
582585
583586 {
......@@ -589,7 +592,7 @@ fn windowsApiUpdateThreadRun() void {
589592 if (stderr_mutex.tryLock()) {
590593 defer stderr_mutex.unlock();
591594 windowsApiWriteMarker();
592 write(buffer) catch return;
595 write(io, buffer) catch return;
593596 global_progress.need_clear = true;
594597 windowsApiMoveToMarker(nl_n) catch return;
595598 }
......@@ -611,7 +614,7 @@ fn windowsApiUpdateThreadRun() void {
611614 defer stderr_mutex.unlock();
612615 clearWrittenWindowsApi() catch return;
613616 windowsApiWriteMarker();
614 write(buffer) catch return;
617 write(io, buffer) catch return;
615618 global_progress.need_clear = true;
616619 windowsApiMoveToMarker(nl_n) catch return;
617620 }
......@@ -624,8 +627,9 @@ fn windowsApiUpdateThreadRun() void {
624627///
625628/// The lock is recursive; the same thread may hold the lock multiple times.
626629pub fn lockStdErr() void {
630 const io = stderr_file_writer.io;
627631 stderr_mutex.lock();
628 clearWrittenWithEscapeCodes() catch {};
632 clearWrittenWithEscapeCodes(io) catch {};
629633}
630634
631635pub fn unlockStdErr() void {
......@@ -636,10 +640,12 @@ pub fn unlockStdErr() void {
636640const stderr_writer: *Writer = &stderr_file_writer.interface;
637641/// Protected by `stderr_mutex`.
638642var stderr_file_writer: Io.File.Writer = .{
643 .io = static_threaded_io.io(),
639644 .interface = Io.File.Writer.initInterface(&.{}),
640645 .file = if (is_windows) undefined else .stderr(),
641646 .mode = .streaming,
642647};
648var static_threaded_io: Io.Threaded = .init_single_threaded;
643649
644650/// Allows the caller to freely write to the returned `Writer`,
645651/// initialized with `buffer`, until `unlockStderrWriter` is called.
......@@ -647,9 +653,10 @@ var stderr_file_writer: Io.File.Writer = .{
647653/// During the lock, any `std.Progress` information is cleared from the terminal.
648654///
649655/// The lock is recursive; the same thread may hold the lock multiple times.
650pub fn lockStderrWriter(buffer: []u8) *Writer {
656pub fn lockStderrWriter(buffer: []u8) *Io.Writer {
657 const io = stderr_file_writer.io;
651658 stderr_mutex.lock();
652 clearWrittenWithEscapeCodes() catch {};
659 clearWrittenWithEscapeCodes(io) catch {};
653660 if (is_windows) stderr_file_writer.file = .stderr();
654661 stderr_writer.flush() catch {};
655662 stderr_writer.buffer = buffer;
......@@ -663,7 +670,7 @@ pub fn unlockStderrWriter() void {
663670 stderr_mutex.unlock();
664671}
665672
666fn ipcThreadRun(fd: posix.fd_t) anyerror!void {
673fn ipcThreadRun(io: Io, file: Io.File) anyerror!void {
667674 // Store this data in the thread so that it does not need to be part of the
668675 // linker data of the main executable.
669676 var serialized_buffer: Serialized.Buffer = undefined;
......@@ -675,7 +682,7 @@ fn ipcThreadRun(fd: posix.fd_t) anyerror!void {
675682 return;
676683
677684 const serialized = serialize(&serialized_buffer);
678 writeIpc(fd, serialized) catch |err| switch (err) {
685 writeIpc(io, file, serialized) catch |err| switch (err) {
679686 error.BrokenPipe => return,
680687 };
681688 }
......@@ -687,7 +694,7 @@ fn ipcThreadRun(fd: posix.fd_t) anyerror!void {
687694 return;
688695
689696 const serialized = serialize(&serialized_buffer);
690 writeIpc(fd, serialized) catch |err| switch (err) {
697 writeIpc(io, file, serialized) catch |err| switch (err) {
691698 error.BrokenPipe => return,
692699 };
693700 }
......@@ -786,11 +793,11 @@ fn appendTreeSymbol(symbol: TreeSymbol, buf: []u8, start_i: usize) usize {
786793 }
787794}
788795
789fn clearWrittenWithEscapeCodes() anyerror!void {
796fn clearWrittenWithEscapeCodes(io: Io) anyerror!void {
790797 if (noop_impl or !global_progress.need_clear) return;
791798
792799 global_progress.need_clear = false;
793 try write(clear ++ progress_remove);
800 try write(io, clear ++ progress_remove);
794801}
795802
796803/// U+25BA or ►
......@@ -1417,13 +1424,13 @@ fn withinRowLimit(p: *Progress, nl_n: usize) bool {
14171424 return nl_n + 2 < p.rows;
14181425}
14191426
1420fn write(buf: []const u8) anyerror!void {
1421 try global_progress.terminal.writeAll(buf);
1427fn write(io: Io, buf: []const u8) anyerror!void {
1428 try global_progress.terminal.writeStreamingAll(io, buf);
14221429}
14231430
14241431var remaining_write_trash_bytes: usize = 0;
14251432
1426fn writeIpc(fd: posix.fd_t, serialized: Serialized) error{BrokenPipe}!void {
1433fn writeIpc(io: Io, file: Io.File, serialized: Serialized) error{BrokenPipe}!void {
14271434 // Byteswap if necessary to ensure little endian over the pipe. This is
14281435 // needed because the parent or child process might be running in qemu.
14291436 if (is_big_endian) for (serialized.storage) |*s| s.byteSwap();
......@@ -1434,11 +1441,7 @@ fn writeIpc(fd: posix.fd_t, serialized: Serialized) error{BrokenPipe}!void {
14341441 const storage = std.mem.sliceAsBytes(serialized.storage);
14351442 const parents = std.mem.sliceAsBytes(serialized.parents);
14361443
1437 var vecs: [3]posix.iovec_const = .{
1438 .{ .base = header.ptr, .len = header.len },
1439 .{ .base = storage.ptr, .len = storage.len },
1440 .{ .base = parents.ptr, .len = parents.len },
1441 };
1444 var vecs: [3][]const u8 = .{ header, storage, parents };
14421445
14431446 // Ensures the packet can fit in the pipe buffer.
14441447 const upper_bound_msg_len = 1 + node_storage_buffer_len * @sizeOf(Node.Storage) +
......@@ -1449,7 +1452,7 @@ fn writeIpc(fd: posix.fd_t, serialized: Serialized) error{BrokenPipe}!void {
14491452 // We do this in a separate write call to give a better chance for the
14501453 // writev below to be in a single packet.
14511454 const n = @min(parents.len, remaining_write_trash_bytes);
1452 if (posix.write(fd, parents[0..n])) |written| {
1455 if (io.vtable.fileWriteStreaming(io.userdata, file, &.{}, &.{parents[0..n]}, 1)) |written| {
14531456 remaining_write_trash_bytes -= written;
14541457 continue;
14551458 } else |err| switch (err) {
......@@ -1464,7 +1467,7 @@ fn writeIpc(fd: posix.fd_t, serialized: Serialized) error{BrokenPipe}!void {
14641467
14651468 // If this write would block we do not want to keep trying, but we need to
14661469 // know if a partial message was written.
1467 if (writevNonblock(fd, &vecs)) |written| {
1470 if (writevNonblock(io, file, &vecs)) |written| {
14681471 const total = header.len + storage.len + parents.len;
14691472 if (written < total) {
14701473 remaining_write_trash_bytes = total - written;
......@@ -1479,7 +1482,7 @@ fn writeIpc(fd: posix.fd_t, serialized: Serialized) error{BrokenPipe}!void {
14791482 }
14801483}
14811484
1482fn writevNonblock(fd: posix.fd_t, iov: []posix.iovec_const) posix.WriteError!usize {
1485fn writevNonblock(io: Io, file: Io.File, iov: [][]const u8) Io.File.Writer.Error!usize {
14831486 var iov_index: usize = 0;
14841487 var written: usize = 0;
14851488 var total_written: usize = 0;
......@@ -1488,9 +1491,9 @@ fn writevNonblock(fd: posix.fd_t, iov: []posix.iovec_const) posix.WriteError!usi
14881491 written >= iov[iov_index].len
14891492 else
14901493 return total_written) : (iov_index += 1) written -= iov[iov_index].len;
1491 iov[iov_index].base += written;
1494 iov[iov_index].ptr += written;
14921495 iov[iov_index].len -= written;
1493 written = try posix.writev(fd, iov[iov_index..]);
1496 written = try io.vtable.fileWriteStreaming(io.userdata, file, &.{}, iov, 1);
14941497 if (written == 0) return total_written;
14951498 total_written += written;
14961499 }
lib/std/debug.zig+6-5
......@@ -286,13 +286,12 @@ pub fn unlockStdErr() void {
286286pub fn lockStderrWriter(buffer: []u8) struct { *Writer, tty.Config } {
287287 const global = struct {
288288 var conf: ?tty.Config = null;
289 var single_threaded_io: Io.Threaded = .init_single_threaded;
290289 };
291 const io = global.single_threaded_io.io();
292290 const w = std.Progress.lockStderrWriter(buffer);
291 const file_writer: *File.Writer = @fieldParentPtr("interface", w);
293292 // The stderr lock also locks access to `global.conf`.
294293 if (global.conf == null) {
295 global.conf = .detect(io, .stderr());
294 global.conf = .detect(file_writer.io, .stderr());
296295 }
297296 return .{ w, global.conf.? };
298297}
......@@ -619,13 +618,15 @@ pub const StackUnwindOptions = struct {
619618///
620619/// See `writeCurrentStackTrace` to immediately print the trace instead of capturing it.
621620pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf: []usize) StackTrace {
622 var threaded: Io.Threaded = .init_single_threaded;
623 const io = threaded.ioBasic();
624621 const empty_trace: StackTrace = .{ .index = 0, .instruction_addresses = &.{} };
625622 if (!std.options.allow_stack_tracing) return empty_trace;
626623 var it: StackIterator = .init(options.context);
627624 defer it.deinit();
628625 if (!it.stratOk(options.allow_unsafe_unwind)) return empty_trace;
626
627 var threaded: Io.Threaded = .init_single_threaded;
628 const io = threaded.ioBasic();
629
629630 var total_frames: usize = 0;
630631 var index: usize = 0;
631632 var wait_for = options.first_address;