From f33395ce6a66df517c09037c4a4f1756ef7f6f2e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 14 Jul 2024 21:38:05 -0700 Subject: std.Progress: add getIpcFd and have_ipc API This makes advanced use cases possible such as a long-lived child process whose progress node gets re-attached to a different parent. --- lib/std/Progress.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/std/Progress.zig') diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index bb5adcefd1..190f667897 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -269,6 +269,19 @@ pub const Node = struct { storageByIndex(index).setIpcFd(fd); } + /// Posix-only. Thread-safe. Assumes the node is storing an IPC file + /// descriptor. + pub fn getIpcFd(node: Node) ?posix.fd_t { + const index = node.index.unwrap() orelse return null; + const storage = storageByIndex(index); + const int = @atomicLoad(u32, &storage.completed_count, .monotonic); + return switch (@typeInfo(posix.fd_t)) { + .Int => @bitCast(int), + .Pointer => @ptrFromInt(int), + else => @compileError("unsupported fd_t of " ++ @typeName(posix.fd_t)), + }; + } + fn storageByIndex(index: Node.Index) *Node.Storage { return &global_progress.node_storage[@intFromEnum(index)]; } @@ -329,6 +342,11 @@ var default_draw_buffer: [4096]u8 = undefined; var debug_start_trace = std.debug.Trace.init; +pub const have_ipc = switch (builtin.os.tag) { + .wasi, .freestanding, .windows => false, + else => true, +}; + const noop_impl = builtin.single_threaded or switch (builtin.os.tag) { .wasi, .freestanding => true, else => false, -- cgit v1.2.3