| ... | ... | @@ -85,6 +85,7 @@ pub const Node = struct { |
| 85 | 85 | estimated_total_count: u32, |
| 86 | 86 | name: [max_name_len]u8, |
| 87 | 87 | |
| 88 | /// Not thread-safe. |
| 88 | 89 | fn getIpcFd(s: Storage) ?posix.fd_t { |
| 89 | 90 | return if (s.estimated_total_count == std.math.maxInt(u32)) switch (@typeInfo(posix.fd_t)) { |
| 90 | 91 | .Int => @bitCast(s.completed_count), |
| ... | ... | @@ -93,15 +94,21 @@ pub const Node = struct { |
| 93 | 94 | } else null; |
| 94 | 95 | } |
| 95 | 96 | |
| 97 | /// Thread-safe. |
| 96 | 98 | fn setIpcFd(s: *Storage, fd: posix.fd_t) void { |
| 97 | | s.estimated_total_count = std.math.maxInt(u32); |
| 98 | | s.completed_count = switch (@typeInfo(posix.fd_t)) { |
| 99 | const integer: u32 = switch (@typeInfo(posix.fd_t)) { |
| 99 | 100 | .Int => @bitCast(fd), |
| 100 | 101 | .Pointer => @intFromPtr(fd), |
| 101 | 102 | else => @compileError("unsupported fd_t of " ++ @typeName(posix.fd_t)), |
| 102 | 103 | }; |
| 104 | // `estimated_total_count` max int indicates the special state that |
| 105 | // causes `completed_count` to be treated as a file descriptor, so |
| 106 | // the order here matters. |
| 107 | @atomicStore(u32, &s.completed_count, integer, .seq_cst); |
| 108 | @atomicStore(u32, &s.estimated_total_count, std.math.maxInt(u32), .seq_cst); |
| 103 | 109 | } |
| 104 | 110 | |
| 111 | /// Not thread-safe. |
| 105 | 112 | fn byteSwap(s: *Storage) void { |
| 106 | 113 | s.completed_count = @byteSwap(s.completed_count); |
| 107 | 114 | s.estimated_total_count = @byteSwap(s.estimated_total_count); |
| ... | ... | @@ -208,7 +215,9 @@ pub const Node = struct { |
| 208 | 215 | pub fn setEstimatedTotalItems(n: Node, count: usize) void { |
| 209 | 216 | const index = n.index.unwrap() orelse return; |
| 210 | 217 | const storage = storageByIndex(index); |
| 211 | | @atomicStore(u32, &storage.estimated_total_count, std.math.lossyCast(u32, count), .monotonic); |
| 218 | // Avoid u32 max int which is used to indicate a special state. |
| 219 | const saturated = @min(std.math.maxInt(u32) - 1, count); |
| 220 | @atomicStore(u32, &storage.estimated_total_count, saturated, .monotonic); |
| 212 | 221 | } |
| 213 | 222 | |
| 214 | 223 | /// Thread-safe. |
| ... | ... | @@ -243,10 +252,13 @@ pub const Node = struct { |
| 243 | 252 | } |
| 244 | 253 | } |
| 245 | 254 | |
| 246 | | /// Posix-only. Used by `std.process.Child`. |
| 255 | /// Posix-only. Used by `std.process.Child`. Thread-safe. |
| 247 | 256 | pub fn setIpcFd(node: Node, fd: posix.fd_t) void { |
| 248 | 257 | const index = node.index.unwrap() orelse return; |
| 249 | | assert(fd != -1); |
| 258 | assert(fd >= 0); |
| 259 | assert(fd != posix.STDOUT_FILENO); |
| 260 | assert(fd != posix.STDIN_FILENO); |
| 261 | assert(fd != posix.STDERR_FILENO); |
| 250 | 262 | storageByIndex(index).setIpcFd(fd); |
| 251 | 263 | } |
| 252 | 264 | |
| ... | ... | @@ -582,8 +594,8 @@ fn serialize(serialized_buffer: *Serialized.Buffer) Serialized { |
| 582 | 594 | while (begin_parent != .unused) { |
| 583 | 595 | const dest_storage = &serialized_buffer.storage[serialized_len]; |
| 584 | 596 | @memcpy(&dest_storage.name, &storage_ptr.name); |
| 585 | | dest_storage.completed_count = @atomicLoad(u32, &storage_ptr.completed_count, .monotonic); |
| 586 | | dest_storage.estimated_total_count = @atomicLoad(u32, &storage_ptr.estimated_total_count, .monotonic); |
| 597 | dest_storage.completed_count = @atomicLoad(u32, &storage_ptr.completed_count, .seq_cst); |
| 598 | dest_storage.estimated_total_count = @atomicLoad(u32, &storage_ptr.estimated_total_count, .seq_cst); |
| 587 | 599 | const end_parent = @atomicLoad(Node.Parent, parent_ptr, .seq_cst); |
| 588 | 600 | if (begin_parent == end_parent) { |
| 589 | 601 | any_ipc = any_ipc or (dest_storage.getIpcFd() != null); |