diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-05-24 08:22:47 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-05-27 20:56:48 -0700 |
| commit | f97c2f28fdc3061bc7e30ccfcafaccbee77993b6 (patch) | |
| tree | a2c4165829d84b35df23346b1808a43e0cccec41 /lib | |
| parent | f6873c6b00544923d5699737651f2bc4fe29fd06 (diff) | |
| download | zig-f97c2f28fdc3061bc7e30ccfcafaccbee77993b6.tar.gz zig-f97c2f28fdc3061bc7e30ccfcafaccbee77993b6.zip | |
update the codebase for the new std.Progress API
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/compiler/aro/aro/Diagnostics.zig | 4 | ||||
| -rw-r--r-- | lib/compiler/build_runner.zig | 20 | ||||
| -rw-r--r-- | lib/std/Build.zig | 10 | ||||
| -rw-r--r-- | lib/std/Build/Step.zig | 22 | ||||
| -rw-r--r-- | lib/std/Build/Step/CheckFile.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/CheckObject.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/Compile.zig | 6 | ||||
| -rw-r--r-- | lib/std/Build/Step/ConfigHeader.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/Fmt.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/InstallArtifact.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/InstallDir.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/InstallFile.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/ObjCopy.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/Options.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/RemoveDir.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/Run.zig | 18 | ||||
| -rw-r--r-- | lib/std/Build/Step/TranslateC.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/WriteFile.zig | 2 | ||||
| -rw-r--r-- | lib/std/Progress.zig | 43 | ||||
| -rw-r--r-- | lib/std/debug.zig | 21 | ||||
| -rw-r--r-- | lib/std/json/dynamic.zig | 4 | ||||
| -rw-r--r-- | lib/std/log.zig | 8 | ||||
| -rw-r--r-- | lib/std/zig/ErrorBundle.zig | 4 | ||||
| -rw-r--r-- | lib/std/zig/Server.zig | 2 |
24 files changed, 112 insertions, 74 deletions
diff --git a/lib/compiler/aro/aro/Diagnostics.zig b/lib/compiler/aro/aro/Diagnostics.zig index f894403648..8f80e4393d 100644 --- a/lib/compiler/aro/aro/Diagnostics.zig +++ b/lib/compiler/aro/aro/Diagnostics.zig @@ -528,7 +528,7 @@ const MsgWriter = struct { config: std.io.tty.Config, fn init(config: std.io.tty.Config) MsgWriter { - std.debug.getStderrMutex().lock(); + std.debug.lockStdErr(); return .{ .w = std.io.bufferedWriter(std.io.getStdErr().writer()), .config = config, @@ -537,7 +537,7 @@ const MsgWriter = struct { pub fn deinit(m: *MsgWriter) void { m.w.flush() catch {}; - std.debug.getStderrMutex().unlock(); + std.debug.unlockStdErr(); } pub fn print(m: *MsgWriter, comptime fmt: []const u8, args: anytype) void { diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index 35dcbb882b..d4c96fe08b 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -289,8 +289,7 @@ pub fn main() !void { .windows_api => {}, } - var progress: std.Progress = .{ .dont_print_on_dumb = true }; - const main_progress_node = progress.start("", 0); + const main_progress_node = std.Progress.start(.{}); builder.debug_log_scopes = debug_log_scopes.items; builder.resolveInstallPrefix(install_prefix, dir_list); @@ -385,7 +384,7 @@ fn runStepNames( arena: std.mem.Allocator, b: *std.Build, step_names: []const []const u8, - parent_prog_node: *std.Progress.Node, + parent_prog_node: std.Progress.Node, thread_pool_options: std.Thread.Pool.Options, run: *Run, seed: u32, @@ -452,7 +451,7 @@ fn runStepNames( { defer parent_prog_node.end(); - var step_prog = parent_prog_node.start("steps", step_stack.count()); + const step_prog = parent_prog_node.start("steps", step_stack.count()); defer step_prog.end(); var wait_group: std.Thread.WaitGroup = .{}; @@ -467,7 +466,7 @@ fn runStepNames( if (step.state == .skipped_oom) continue; thread_pool.spawnWg(&wait_group, workerMakeOneStep, .{ - &wait_group, &thread_pool, b, step, &step_prog, run, + &wait_group, &thread_pool, b, step, step_prog, run, }); } } @@ -891,7 +890,7 @@ fn workerMakeOneStep( thread_pool: *std.Thread.Pool, b: *std.Build, s: *Step, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, run: *Run, ) void { // First, check the conditions for running this step. If they are not met, @@ -941,11 +940,10 @@ fn workerMakeOneStep( } } - var sub_prog_node = prog_node.start(s.name, 0); - sub_prog_node.activate(); + const sub_prog_node = prog_node.start(s.name, 0); defer sub_prog_node.end(); - const make_result = s.make(&sub_prog_node); + const make_result = s.make(sub_prog_node); // No matter the result, we want to display error/warning messages. const show_compile_errors = !run.prominent_compile_errors and @@ -954,8 +952,8 @@ fn workerMakeOneStep( const show_stderr = s.result_stderr.len > 0; if (show_error_msgs or show_compile_errors or show_stderr) { - sub_prog_node.context.lock_stderr(); - defer sub_prog_node.context.unlock_stderr(); + std.debug.lockStdErr(); + defer std.debug.unlockStdErr(); printErrorMessages(b, s, run) catch {}; } diff --git a/lib/std/Build.zig b/lib/std/Build.zig index a2e8a7c564..4443fa404c 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1059,7 +1059,7 @@ pub fn getUninstallStep(b: *Build) *Step { return &b.uninstall_tls.step; } -fn makeUninstall(uninstall_step: *Step, prog_node: *std.Progress.Node) anyerror!void { +fn makeUninstall(uninstall_step: *Step, prog_node: std.Progress.Node) anyerror!void { _ = prog_node; const uninstall_tls: *TopLevelStep = @fieldParentPtr("step", uninstall_step); const b: *Build = @fieldParentPtr("uninstall_tls", uninstall_tls); @@ -2281,10 +2281,10 @@ pub const LazyPath = union(enum) { .cwd_relative => |p| return src_builder.pathFromCwd(p), .generated => |gen| { var file_path: []const u8 = gen.file.step.owner.pathFromRoot(gen.file.path orelse { - std.debug.getStderrMutex().lock(); + std.debug.lockStdErr(); const stderr = std.io.getStdErr(); dumpBadGetPathHelp(gen.file.step, stderr, src_builder, asking_step) catch {}; - std.debug.getStderrMutex().unlock(); + std.debug.unlockStdErr(); @panic("misconfigured build script"); }); @@ -2351,8 +2351,8 @@ fn dumpBadDirnameHelp( comptime msg: []const u8, args: anytype, ) anyerror!void { - debug.getStderrMutex().lock(); - defer debug.getStderrMutex().unlock(); + debug.lockStdErr(); + defer debug.unlockStdErr(); const stderr = io.getStdErr(); const w = stderr.writer(); diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 01bea6c0ce..a965735843 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -58,7 +58,7 @@ pub const TestResults = struct { } }; -pub const MakeFn = *const fn (step: *Step, prog_node: *std.Progress.Node) anyerror!void; +pub const MakeFn = *const fn (step: *Step, prog_node: std.Progress.Node) anyerror!void; pub const State = enum { precheck_unstarted, @@ -176,7 +176,7 @@ pub fn init(options: StepOptions) Step { /// If the Step's `make` function reports `error.MakeFailed`, it indicates they /// have already reported the error. Otherwise, we add a simple error report /// here. -pub fn make(s: *Step, prog_node: *std.Progress.Node) error{ MakeFailed, MakeSkipped }!void { +pub fn make(s: *Step, prog_node: std.Progress.Node) error{ MakeFailed, MakeSkipped }!void { const arena = s.owner.allocator; s.makeFn(s, prog_node) catch |err| switch (err) { @@ -217,7 +217,7 @@ pub fn getStackTrace(s: *Step) ?std.builtin.StackTrace { }; } -fn makeNoOp(step: *Step, prog_node: *std.Progress.Node) anyerror!void { +fn makeNoOp(step: *Step, prog_node: std.Progress.Node) anyerror!void { _ = prog_node; var all_cached = true; @@ -303,7 +303,7 @@ pub fn addError(step: *Step, comptime fmt: []const u8, args: anytype) error{OutO pub fn evalZigProcess( s: *Step, argv: []const []const u8, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, ) !?[]const u8 { assert(argv.len != 0); const b = s.owner; @@ -313,12 +313,16 @@ pub fn evalZigProcess( try handleChildProcUnsupported(s, null, argv); try handleVerbose(s.owner, null, argv); + const sub_prog_node = prog_node.start("", 0); + defer sub_prog_node.end(); + var child = std.process.Child.init(argv, arena); child.env_map = &b.graph.env_map; child.stdin_behavior = .Pipe; child.stdout_behavior = .Pipe; child.stderr_behavior = .Pipe; child.request_resource_usage_statistics = true; + child.progress_node = sub_prog_node; child.spawn() catch |err| return s.fail("unable to spawn {s}: {s}", .{ argv[0], @errorName(err), @@ -337,11 +341,6 @@ pub fn evalZigProcess( const Header = std.zig.Server.Message.Header; var result: ?[]const u8 = null; - var node_name: std.ArrayListUnmanaged(u8) = .{}; - defer node_name.deinit(gpa); - var sub_prog_node = prog_node.start("", 0); - defer sub_prog_node.end(); - const stdout = poller.fifo(.stdout); poll: while (true) { @@ -379,11 +378,6 @@ pub fn evalZigProcess( .extra = extra_array, }; }, - .progress => { - node_name.clearRetainingCapacity(); - try node_name.appendSlice(gpa, body); - sub_prog_node.setName(node_name.items); - }, .emit_bin_path => { const EbpHdr = std.zig.Server.Message.EmitBinPath; const ebp_hdr = @as(*align(1) const EbpHdr, @ptrCast(body)); diff --git a/lib/std/Build/Step/CheckFile.zig b/lib/std/Build/Step/CheckFile.zig index b3323f9e98..b7ce2ded61 100644 --- a/lib/std/Build/Step/CheckFile.zig +++ b/lib/std/Build/Step/CheckFile.zig @@ -46,7 +46,7 @@ pub fn setName(check_file: *CheckFile, name: []const u8) void { check_file.step.name = name; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const check_file: *CheckFile = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index fa0ccc339d..84c9c62abb 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -550,7 +550,7 @@ pub fn checkComputeCompare( check_object.checks.append(check) catch @panic("OOM"); } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const gpa = b.allocator; diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index f660ef64a6..e27dd65619 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -967,7 +967,7 @@ fn getGeneratedFilePath(compile: *Compile, comptime tag_name: []const u8, asking const maybe_path: ?*GeneratedFile = @field(compile, tag_name); const generated_file = maybe_path orelse { - std.debug.getStderrMutex().lock(); + std.debug.lockStdErr(); const stderr = std.io.getStdErr(); std.Build.dumpBadGetPathHelp(&compile.step, stderr, compile.step.owner, asking_step) catch {}; @@ -976,7 +976,7 @@ fn getGeneratedFilePath(compile: *Compile, comptime tag_name: []const u8, asking }; const path = generated_file.path orelse { - std.debug.getStderrMutex().lock(); + std.debug.lockStdErr(); const stderr = std.io.getStdErr(); std.Build.dumpBadGetPathHelp(&compile.step, stderr, compile.step.owner, asking_step) catch {}; @@ -987,7 +987,7 @@ fn getGeneratedFilePath(compile: *Compile, comptime tag_name: []const u8, asking return path; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { const b = step.owner; const arena = b.allocator; const compile: *Compile = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index 4a0e64e8d0..212ea605ed 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -164,7 +164,7 @@ fn putValue(config_header: *ConfigHeader, field_name: []const u8, comptime T: ty } } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const config_header: *ConfigHeader = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/Fmt.zig b/lib/std/Build/Step/Fmt.zig index 3010d701b1..f346c6cc39 100644 --- a/lib/std/Build/Step/Fmt.zig +++ b/lib/std/Build/Step/Fmt.zig @@ -36,7 +36,7 @@ pub fn create(owner: *std.Build, options: Options) *Fmt { return fmt; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { // zig fmt is fast enough that no progress is needed. _ = prog_node; diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index c56bafcfb5..bd1d5db4a9 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -115,7 +115,7 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins return install_artifact; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const install_artifact: *InstallArtifact = @fieldParentPtr("step", step); const b = step.owner; diff --git a/lib/std/Build/Step/InstallDir.zig b/lib/std/Build/Step/InstallDir.zig index 1722b975f7..0a6edafb33 100644 --- a/lib/std/Build/Step/InstallDir.zig +++ b/lib/std/Build/Step/InstallDir.zig @@ -56,7 +56,7 @@ pub fn create(owner: *std.Build, options: Options) *InstallDir { return install_dir; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const install_dir: *InstallDir = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/InstallFile.zig b/lib/std/Build/Step/InstallFile.zig index 6fa6d6bc99..8202a9d796 100644 --- a/lib/std/Build/Step/InstallFile.zig +++ b/lib/std/Build/Step/InstallFile.zig @@ -36,7 +36,7 @@ pub fn create( return install_file; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const install_file: *InstallFile = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/ObjCopy.zig b/lib/std/Build/Step/ObjCopy.zig index 515736dbc1..966764adcc 100644 --- a/lib/std/Build/Step/ObjCopy.zig +++ b/lib/std/Build/Step/ObjCopy.zig @@ -90,7 +90,7 @@ pub fn getOutputSeparatedDebug(objcopy: *const ObjCopy) ?std.Build.LazyPath { return if (objcopy.output_file_debug) |*file| .{ .generated = .{ .file = file } } else null; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { const b = step.owner; const objcopy: *ObjCopy = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index c4daed73ff..2937cf70e1 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -410,7 +410,7 @@ pub fn getOutput(options: *Options) LazyPath { return .{ .generated = .{ .file = &options.generated_file } }; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { // This step completes so quickly that no progress is necessary. _ = prog_node; diff --git a/lib/std/Build/Step/RemoveDir.zig b/lib/std/Build/Step/RemoveDir.zig index 64a3c72668..6483a684aa 100644 --- a/lib/std/Build/Step/RemoveDir.zig +++ b/lib/std/Build/Step/RemoveDir.zig @@ -22,7 +22,7 @@ pub fn create(owner: *std.Build, dir_path: []const u8) *RemoveDir { return remove_dir; } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { // TODO update progress node while walking file system. // Should the standard library support this use case?? _ = prog_node; diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index fec5b5ab67..d49d0b3ce2 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -574,7 +574,7 @@ const IndexedOutput = struct { tag: @typeInfo(Arg).Union.tag_type.?, output: *Output, }; -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { const b = step.owner; const arena = b.allocator; const run: *Run = @fieldParentPtr("step", step); @@ -878,7 +878,7 @@ fn runCommand( argv: []const []const u8, has_side_effects: bool, output_dir_path: []const u8, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, ) !void { const step = &run.step; const b = step.owner; @@ -1195,7 +1195,7 @@ fn spawnChildAndCollect( run: *Run, argv: []const []const u8, has_side_effects: bool, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, ) !ChildProcResult { const b = run.step.owner; const arena = b.allocator; @@ -1235,6 +1235,10 @@ fn spawnChildAndCollect( child.stdin_behavior = .Pipe; } + if (run.stdio != .zig_test) { + child.progress_node = prog_node.start("", 0); + } + try child.spawn(); var timer = try std.time.Timer.start(); @@ -1264,7 +1268,7 @@ const StdIoResult = struct { fn evalZigTest( run: *Run, child: *std.process.Child, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, ) !StdIoResult { const gpa = run.step.owner.allocator; const arena = run.step.owner.allocator; @@ -1291,7 +1295,7 @@ fn evalZigTest( var metadata: ?TestMetadata = null; var sub_prog_node: ?std.Progress.Node = null; - defer if (sub_prog_node) |*n| n.end(); + defer if (sub_prog_node) |n| n.end(); poll: while (true) { while (stdout.readableLength() < @sizeOf(Header)) { @@ -1406,7 +1410,7 @@ const TestMetadata = struct { expected_panic_msgs: []const u32, string_bytes: []const u8, next_index: u32, - prog_node: *std.Progress.Node, + prog_node: std.Progress.Node, fn testName(tm: TestMetadata, index: u32) []const u8 { return std.mem.sliceTo(tm.string_bytes[tm.names[index]..], 0); @@ -1421,7 +1425,7 @@ fn requestNextTest(in: fs.File, metadata: *TestMetadata, sub_prog_node: *?std.Pr if (metadata.expected_panic_msgs[i] != 0) continue; const name = metadata.testName(i); - if (sub_prog_node.*) |*n| n.end(); + if (sub_prog_node.*) |n| n.end(); sub_prog_node.* = metadata.prog_node.start(name, 0); try sendRunTestMessage(in, i); diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index cb1b48e3c0..e07744c2da 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -116,7 +116,7 @@ pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) voi translate_c.c_macros.append(translate_c.step.owner.dupe(name_and_value)) catch @panic("OOM"); } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { const b = step.owner; const translate_c: *TranslateC = @fieldParentPtr("step", step); diff --git a/lib/std/Build/Step/WriteFile.zig b/lib/std/Build/Step/WriteFile.zig index 401c5b78ec..0639573b8f 100644 --- a/lib/std/Build/Step/WriteFile.zig +++ b/lib/std/Build/Step/WriteFile.zig @@ -198,7 +198,7 @@ fn maybeUpdateName(write_file: *WriteFile) void { } } -fn make(step: *Step, prog_node: *std.Progress.Node) !void { +fn make(step: *Step, prog_node: std.Progress.Node) !void { _ = prog_node; const b = step.owner; const write_file: *WriteFile = @fieldParentPtr("step", step); diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index b7ac1fa328..c5c1d17b93 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -58,7 +58,7 @@ pub const Options = struct { /// cannot fit into this buffer which will look bad but not cause any malfunctions. /// /// Must be at least 200 bytes. - draw_buffer: []u8, + draw_buffer: []u8 = &default_draw_buffer, /// How many nanoseconds between writing updates to the terminal. refresh_rate_ns: u64 = 60 * std.time.ns_per_ms, /// How many nanoseconds to keep the output hidden @@ -67,6 +67,7 @@ pub const Options = struct { /// 0 means unknown. estimated_total_items: usize = 0, root_name: []const u8 = "", + disable_printing: bool = false, }; /// Represents one unit of progress. Each node can have children nodes, or @@ -203,6 +204,13 @@ pub const Node = struct { @atomicStore(u32, &storage.estimated_total_count, std.math.lossyCast(u32, count), .monotonic); } + /// Thread-safe. + pub fn increaseEstimatedTotalItems(n: Node, count: usize) void { + const index = n.index.unwrap() orelse return; + const storage = storageByIndex(index); + _ = @atomicRmw(u32, &storage.estimated_total_count, .Add, std.math.lossyCast(u32, count), .monotonic); + } + /// Finish a started `Node`. Thread-safe. pub fn end(n: Node) void { const index = n.index.unwrap() orelse return; @@ -290,6 +298,8 @@ var node_parents_buffer: [default_node_storage_buffer_len]Node.Parent = undefine var node_storage_buffer: [default_node_storage_buffer_len]Node.Storage = undefined; var node_freelist_buffer: [default_node_storage_buffer_len]Node.OptionalIndex = undefined; +var default_draw_buffer: [2000]u8 = undefined; + /// Initializes a global Progress instance. /// /// Asserts there is only one global Progress instance. @@ -318,6 +328,9 @@ pub fn start(options: Options) Node { } } else |env_err| switch (env_err) { error.EnvironmentVariableNotFound => { + if (options.disable_printing) { + return .{ .index = .none }; + } const stderr = std.io.getStdErr(); if (stderr.supportsAnsiEscapeCodes()) { global_progress.terminal = stderr; @@ -330,7 +343,7 @@ pub fn start(options: Options) Node { global_progress.terminal = stderr; } - if (global_progress.terminal == null) { + if (global_progress.terminal == null or !global_progress.supports_ansi_escape_codes) { return .{ .index = .none }; } @@ -379,7 +392,10 @@ fn updateThreadRun() void { return clearTerminal(); const buffer = computeRedraw(); - write(buffer); + if (stderr_mutex.tryLock()) { + defer stderr_mutex.unlock(); + write(buffer); + } } while (true) { @@ -390,10 +406,25 @@ fn updateThreadRun() void { return clearTerminal(); const buffer = computeRedraw(); - write(buffer); + if (stderr_mutex.tryLock()) { + defer stderr_mutex.unlock(); + write(buffer); + } } } +/// Allows the caller to freely write to stderr until `unlockStdErr` is called. +/// +/// During the lock, any `std.Progress` information is cleared from the terminal. +pub fn lockStdErr() void { + stderr_mutex.lock(); + clearTerminal(); +} + +pub fn unlockStdErr() void { + stderr_mutex.unlock(); +} + fn ipcThreadRun(fd: posix.fd_t) anyerror!void { { _ = wait(global_progress.initial_delay_ns); @@ -432,6 +463,8 @@ const tree_line = "\x1B\x28\x30\x78\x1B\x28\x42 "; // │ const tree_langle = "\x1B\x28\x30\x6d\x71\x1B\x28\x42 "; // └─ fn clearTerminal() void { + if (global_progress.newline_count == 0) return; + var i: usize = 0; const buf = global_progress.draw_buffer; @@ -876,3 +909,5 @@ fn handleSigWinch(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) assert(sig == posix.SIG.WINCH); global_progress.redraw_event.set(); } + +var stderr_mutex: std.Thread.Mutex = .{}; diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 980b027f0a..41439df5e6 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -77,19 +77,28 @@ const PdbOrDwarf = union(enum) { } }; -var stderr_mutex = std.Thread.Mutex{}; +/// Allows the caller to freely write to stderr until `unlockStdErr` is called. +/// +/// During the lock, any `std.Progress` information is cleared from the terminal. +pub fn lockStdErr() void { + std.Progress.lockStdErr(); +} + +pub fn unlockStdErr() void { + std.Progress.unlockStdErr(); +} /// Print to stderr, unbuffered, and silently returning on failure. Intended /// for use in "printf debugging." Use `std.log` functions for proper logging. pub fn print(comptime fmt: []const u8, args: anytype) void { - stderr_mutex.lock(); - defer stderr_mutex.unlock(); + lockStdErr(); + defer unlockStdErr(); const stderr = io.getStdErr().writer(); nosuspend stderr.print(fmt, args) catch return; } pub fn getStderrMutex() *std.Thread.Mutex { - return &stderr_mutex; + @compileError("deprecated. call std.debug.lockStdErr() and std.debug.unlockStdErr() instead which will integrate properly with std.Progress"); } /// TODO multithreaded awareness @@ -107,8 +116,8 @@ pub fn getSelfDebugInfo() !*DebugInfo { /// Tries to print a hexadecimal view of the bytes, unbuffered, and ignores any error returned. /// Obtains the stderr mutex while dumping. pub fn dump_hex(bytes: []const u8) void { - stderr_mutex.lock(); - defer stderr_mutex.unlock(); + lockStdErr(); + defer unlockStdErr(); dump_hex_fallible(bytes) catch {}; } diff --git a/lib/std/json/dynamic.zig b/lib/std/json/dynamic.zig index a56d37bf0b..a1849b0fed 100644 --- a/lib/std/json/dynamic.zig +++ b/lib/std/json/dynamic.zig @@ -52,8 +52,8 @@ pub const Value = union(enum) { } pub fn dump(self: Value) void { - std.debug.getStderrMutex().lock(); - defer std.debug.getStderrMutex().unlock(); + std.debug.lockStdErr(); + defer std.debug.unlockStdErr(); const stderr = std.io.getStdErr().writer(); stringify(self, .{}, stderr) catch return; diff --git a/lib/std/log.zig b/lib/std/log.zig index 0562d09c51..b2c05112b0 100644 --- a/lib/std/log.zig +++ b/lib/std/log.zig @@ -45,8 +45,8 @@ //! const prefix = "[" ++ comptime level.asText() ++ "] " ++ scope_prefix; //! //! // Print the message to stderr, silently ignoring any errors -//! std.debug.getStderrMutex().lock(); -//! defer std.debug.getStderrMutex().unlock(); +//! std.debug.lockStdErr(); +//! defer std.debug.unlockStdErr(); //! const stderr = std.io.getStdErr().writer(); //! nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return; //! } @@ -152,8 +152,8 @@ pub fn defaultLog( var bw = std.io.bufferedWriter(stderr); const writer = bw.writer(); - std.debug.getStderrMutex().lock(); - defer std.debug.getStderrMutex().unlock(); + std.debug.lockStdErr(); + defer std.debug.unlockStdErr(); nosuspend { writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; bw.flush() catch return; diff --git a/lib/std/zig/ErrorBundle.zig b/lib/std/zig/ErrorBundle.zig index 5a6651b248..2d69ff901d 100644 --- a/lib/std/zig/ErrorBundle.zig +++ b/lib/std/zig/ErrorBundle.zig @@ -155,8 +155,8 @@ pub const RenderOptions = struct { }; pub fn renderToStdErr(eb: ErrorBundle, options: RenderOptions) void { - std.debug.getStderrMutex().lock(); - defer std.debug.getStderrMutex().unlock(); + std.debug.lockStdErr(); + defer std.debug.unlockStdErr(); const stderr = std.io.getStdErr(); return renderToWriter(eb, options, stderr.writer()) catch return; } diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index 10e14a55fc..7f8de00b4a 100644 --- a/lib/std/zig/Server.zig +++ b/lib/std/zig/Server.zig @@ -14,8 +14,6 @@ pub const Message = struct { zig_version, /// Body is an ErrorBundle. error_bundle, - /// Body is a UTF-8 string. - progress, /// Body is a EmitBinPath. emit_bin_path, /// Body is a TestMetadata |
