authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-14 21:38:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-14 21:38:16-07:00
logf33395ce6a66df517c09037c4a4f1756ef7f6f2e
tree984095938c945f70ecaf2877de1d7549153b18e7
parent716b128a24ffc44a8694f3d61e3d74b5297fd564

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.

1 files changed, 18 insertions(+), 0 deletions(-)

lib/std/Progress.zig+18
...@@ -269,6 +269,19 @@ pub const Node = struct {...@@ -269,6 +269,19 @@ pub const Node = struct {
269 storageByIndex(index).setIpcFd(fd);269 storageByIndex(index).setIpcFd(fd);
270 }270 }
271271
272 /// Posix-only. Thread-safe. Assumes the node is storing an IPC file
273 /// descriptor.
274 pub fn getIpcFd(node: Node) ?posix.fd_t {
275 const index = node.index.unwrap() orelse return null;
276 const storage = storageByIndex(index);
277 const int = @atomicLoad(u32, &storage.completed_count, .monotonic);
278 return switch (@typeInfo(posix.fd_t)) {
279 .Int => @bitCast(int),
280 .Pointer => @ptrFromInt(int),
281 else => @compileError("unsupported fd_t of " ++ @typeName(posix.fd_t)),
282 };
283 }
284
272 fn storageByIndex(index: Node.Index) *Node.Storage {285 fn storageByIndex(index: Node.Index) *Node.Storage {
273 return &global_progress.node_storage[@intFromEnum(index)];286 return &global_progress.node_storage[@intFromEnum(index)];
274 }287 }
...@@ -329,6 +342,11 @@ var default_draw_buffer: [4096]u8 = undefined;...@@ -329,6 +342,11 @@ var default_draw_buffer: [4096]u8 = undefined;
329342
330var debug_start_trace = std.debug.Trace.init;343var debug_start_trace = std.debug.Trace.init;
331344
345pub const have_ipc = switch (builtin.os.tag) {
346 .wasi, .freestanding, .windows => false,
347 else => true,
348};
349
332const noop_impl = builtin.single_threaded or switch (builtin.os.tag) {350const noop_impl = builtin.single_threaded or switch (builtin.os.tag) {
333 .wasi, .freestanding => true,351 .wasi, .freestanding => true,
334 else => false,352 else => false,