| ... | @@ -12,22 +12,16 @@ const Io = std.Io; | ... | @@ -12,22 +12,16 @@ const Io = std.Io; |
| 12 | const net = std.Io.net; | 12 | const net = std.Io.net; |
| 13 | const HostName = std.Io.net.HostName; | 13 | const HostName = std.Io.net.HostName; |
| 14 | const IpAddress = std.Io.net.IpAddress; | 14 | const IpAddress = std.Io.net.IpAddress; |
| 15 | const Allocator = std.mem.Allocator; | | |
| 16 | const Alignment = std.mem.Alignment; | 15 | const Alignment = std.mem.Alignment; |
| 17 | const assert = std.debug.assert; | 16 | const assert = std.debug.assert; |
| 18 | const posix = std.posix; | 17 | const posix = std.posix; |
| 19 | | 18 | |
| 20 | /// Thread-safe. | 19 | main_thread: Thread, |
| 21 | allocator: Allocator, | 20 | stack_size: usize = default_stack_size, |
| 22 | mutex: std.Thread.Mutex = .{}, | 21 | capacity: std.atomic.Value(Capacity), |
| 23 | cond: std.Thread.Condition = .{}, | 22 | capacity_error: ?std.Thread.CpuCountError, |
| 24 | run_queue: std.SinglyLinkedList = .{}, | 23 | concurrent_limit: Io.Limit = .unlimited, |
| 25 | join_requested: bool = false, | 24 | pid: Pid = .unknown, |
| 26 | threads: std.ArrayList(std.Thread), | | |
| 27 | stack_size: usize, | | |
| 28 | thread_capacity: std.atomic.Value(ThreadCapacity), | | |
| 29 | thread_capacity_error: ?std.Thread.CpuCountError, | | |
| 30 | concurrent_count: usize, | | |
| 31 | | 25 | |
| 32 | wsa: if (is_windows) Wsa else struct {} = .{}, | 26 | wsa: if (is_windows) Wsa else struct {} = .{}, |
| 33 | | 27 | |
| ... | @@ -35,22 +29,626 @@ have_signal_handler: bool, | ... | @@ -35,22 +29,626 @@ have_signal_handler: bool, |
| 35 | old_sig_io: if (have_sig_io) posix.Sigaction else void, | 29 | old_sig_io: if (have_sig_io) posix.Sigaction else void, |
| 36 | old_sig_pipe: if (have_sig_pipe) posix.Sigaction else void, | 30 | old_sig_pipe: if (have_sig_pipe) posix.Sigaction else void, |
| 37 | | 31 | |
| 38 | pub const ThreadCapacity = enum(usize) { | 32 | pub const Pid = enum(if (posix.pid_t == void) u0 else posix.pid_t) { |
| 39 | unknown = 0, | 33 | unknown = 0, |
| 40 | _, | 34 | _, |
| | 35 | }; |
| | 36 | |
| | 37 | pub const Thread = struct { |
| | 38 | /// The value that needs to be passed to pthread_kill or tgkill in order to |
| | 39 | /// send a signal. |
| | 40 | signal_id: SignalId, |
| | 41 | /// Points to the next thread in the list. Singly-linked so that |
| | 42 | /// it can be updated lock-free. |
| | 43 | list_node: std.SinglyLinkedList.Node = .{}, |
| | 44 | run_queue: std.SinglyLinkedList.Node = .{}, |
| | 45 | current_closure: ?*Closure = null, |
| | 46 | completion: Completion, |
| | 47 | mutex: std.Thread.Mutex, |
| | 48 | cond: std.Thread.Condition, |
| | 49 | join_requested: bool, |
| | 50 | |
| | 51 | threadlocal var current: *Thread = undefined; |
| | 52 | |
| | 53 | const SignalId = if (use_pthreads) std.c.pthread_t else std.Thread.Id; |
| | 54 | |
| | 55 | const Completion = switch (native_os) { |
| | 56 | .windows => @compileError("TODO"), |
| | 57 | .linux => struct { |
| | 58 | state: State = State.init(.running), |
| | 59 | child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1), |
| | 60 | parent_tid: i32 = undefined, |
| | 61 | mapped: []align(std.heap.page_size_min) u8, |
| | 62 | |
| | 63 | /// State to synchronize detachment of spawner thread to spawned thread |
| | 64 | const State = std.atomic.Value(enum(switch (builtin.zig_backend) { |
| | 65 | .stage2_riscv64 => u32, |
| | 66 | else => u8, |
| | 67 | }) { |
| | 68 | running, |
| | 69 | detached, |
| | 70 | completed, |
| | 71 | }); |
| | 72 | |
| | 73 | |
| | 74 | /// Calls `munmap(mapped.ptr, mapped.len)` then `exit(1)` without touching the stack (which lives in `mapped.ptr`). |
| | 75 | /// Ported over from musl libc's pthread detached implementation: |
| | 76 | /// https://github.com/ifduyue/musl/search?q=__unmapself |
| | 77 | fn freeAndExit(self: *Completion) noreturn { |
| | 78 | switch (builtin.target.cpu.arch) { |
| | 79 | .x86 => asm volatile ( |
| | 80 | \\ movl $91, %%eax # SYS_munmap |
| | 81 | \\ movl %[ptr], %%ebx |
| | 82 | \\ movl %[len], %%ecx |
| | 83 | \\ int $128 |
| | 84 | \\ movl $1, %%eax # SYS_exit |
| | 85 | \\ movl $0, %%ebx |
| | 86 | \\ int $128 |
| | 87 | : |
| | 88 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 89 | [len] "r" (self.mapped.len), |
| | 90 | : .{ .memory = true }), |
| | 91 | .x86_64 => asm volatile (switch (builtin.target.abi) { |
| | 92 | .gnux32, .muslx32 => |
| | 93 | \\ movl $0x4000000b, %%eax # SYS_munmap |
| | 94 | \\ syscall |
| | 95 | \\ movl $0x4000003c, %%eax # SYS_exit |
| | 96 | \\ xor %%rdi, %%rdi |
| | 97 | \\ syscall |
| | 98 | , |
| | 99 | else => |
| | 100 | \\ movl $11, %%eax # SYS_munmap |
| | 101 | \\ syscall |
| | 102 | \\ movl $60, %%eax # SYS_exit |
| | 103 | \\ xor %%rdi, %%rdi |
| | 104 | \\ syscall |
| | 105 | , |
| | 106 | } |
| | 107 | : |
| | 108 | : [ptr] "{rdi}" (@intFromPtr(self.mapped.ptr)), |
| | 109 | [len] "{rsi}" (self.mapped.len), |
| | 110 | ), |
| | 111 | .arm, .armeb, .thumb, .thumbeb => asm volatile ( |
| | 112 | \\ mov r7, #91 // SYS_munmap |
| | 113 | \\ mov r0, %[ptr] |
| | 114 | \\ mov r1, %[len] |
| | 115 | \\ svc 0 |
| | 116 | \\ mov r7, #1 // SYS_exit |
| | 117 | \\ mov r0, #0 |
| | 118 | \\ svc 0 |
| | 119 | : |
| | 120 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 121 | [len] "r" (self.mapped.len), |
| | 122 | : .{ .memory = true }), |
| | 123 | .aarch64, .aarch64_be => asm volatile ( |
| | 124 | \\ mov x8, #215 // SYS_munmap |
| | 125 | \\ mov x0, %[ptr] |
| | 126 | \\ mov x1, %[len] |
| | 127 | \\ svc 0 |
| | 128 | \\ mov x8, #93 // SYS_exit |
| | 129 | \\ mov x0, #0 |
| | 130 | \\ svc 0 |
| | 131 | : |
| | 132 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 133 | [len] "r" (self.mapped.len), |
| | 134 | : .{ .memory = true }), |
| | 135 | .alpha => asm volatile ( |
| | 136 | \\ ldi $0, 73 # SYS_munmap |
| | 137 | \\ mov %[ptr], $16 |
| | 138 | \\ mov %[len], $17 |
| | 139 | \\ callsys |
| | 140 | \\ ldi $0, 1 # SYS_exit |
| | 141 | \\ ldi $16, 0 |
| | 142 | \\ callsys |
| | 143 | : |
| | 144 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 145 | [len] "r" (self.mapped.len), |
| | 146 | : .{ .memory = true }), |
| | 147 | .hexagon => asm volatile ( |
| | 148 | \\ r6 = #215 // SYS_munmap |
| | 149 | \\ r0 = %[ptr] |
| | 150 | \\ r1 = %[len] |
| | 151 | \\ trap0(#1) |
| | 152 | \\ r6 = #93 // SYS_exit |
| | 153 | \\ r0 = #0 |
| | 154 | \\ trap0(#1) |
| | 155 | : |
| | 156 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 157 | [len] "r" (self.mapped.len), |
| | 158 | : .{ .memory = true }), |
| | 159 | .hppa => asm volatile ( |
| | 160 | \\ ldi 91, %%r20 /* SYS_munmap */ |
| | 161 | \\ copy %[ptr], %%r26 |
| | 162 | \\ copy %[len], %%r25 |
| | 163 | \\ ble 0x100(%%sr2, %%r0) |
| | 164 | \\ ldi 1, %%r20 /* SYS_exit */ |
| | 165 | \\ ldi 0, %%r26 |
| | 166 | \\ ble 0x100(%%sr2, %%r0) |
| | 167 | : |
| | 168 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 169 | [len] "r" (self.mapped.len), |
| | 170 | : .{ .memory = true }), |
| | 171 | .m68k => asm volatile ( |
| | 172 | \\ move.l #91, %%d0 // SYS_munmap |
| | 173 | \\ move.l %[ptr], %%d1 |
| | 174 | \\ move.l %[len], %%d2 |
| | 175 | \\ trap #0 |
| | 176 | \\ move.l #1, %%d0 // SYS_exit |
| | 177 | \\ move.l #0, %%d1 |
| | 178 | \\ trap #0 |
| | 179 | : |
| | 180 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 181 | [len] "r" (self.mapped.len), |
| | 182 | : .{ .memory = true }), |
| | 183 | .microblaze, .microblazeel => asm volatile ( |
| | 184 | \\ ori r12, r0, 91 # SYS_munmap |
| | 185 | \\ ori r5, %[ptr], 0 |
| | 186 | \\ ori r6, %[len], 0 |
| | 187 | \\ brki r14, 0x8 |
| | 188 | \\ ori r12, r0, 1 # SYS_exit |
| | 189 | \\ or r5, r0, r0 |
| | 190 | \\ brki r14, 0x8 |
| | 191 | : |
| | 192 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 193 | [len] "r" (self.mapped.len), |
| | 194 | : .{ .memory = true }), |
| | 195 | // We set `sp` to the address of the current function as a workaround for a Linux |
| | 196 | // kernel bug that caused syscalls to return EFAULT if the stack pointer is invalid. |
| | 197 | // The bug was introduced in 46e12c07b3b9603c60fc1d421ff18618241cb081 and fixed in |
| | 198 | // 7928eb0370d1133d0d8cd2f5ddfca19c309079d5. |
| | 199 | .mips, .mipsel => asm volatile ( |
| | 200 | \\ move $sp, $t9 |
| | 201 | \\ li $v0, 4091 # SYS_munmap |
| | 202 | \\ move $a0, %[ptr] |
| | 203 | \\ move $a1, %[len] |
| | 204 | \\ syscall |
| | 205 | \\ li $v0, 4001 # SYS_exit |
| | 206 | \\ li $a0, 0 |
| | 207 | \\ syscall |
| | 208 | : |
| | 209 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 210 | [len] "r" (self.mapped.len), |
| | 211 | : .{ .memory = true }), |
| | 212 | .mips64, .mips64el => asm volatile (switch (builtin.target.abi) { |
| | 213 | .gnuabin32, .muslabin32 => |
| | 214 | \\ li $v0, 6011 # SYS_munmap |
| | 215 | \\ move $a0, %[ptr] |
| | 216 | \\ move $a1, %[len] |
| | 217 | \\ syscall |
| | 218 | \\ li $v0, 6058 # SYS_exit |
| | 219 | \\ li $a0, 0 |
| | 220 | \\ syscall |
| | 221 | , |
| | 222 | else => |
| | 223 | \\ li $v0, 5011 # SYS_munmap |
| | 224 | \\ move $a0, %[ptr] |
| | 225 | \\ move $a1, %[len] |
| | 226 | \\ syscall |
| | 227 | \\ li $v0, 5058 # SYS_exit |
| | 228 | \\ li $a0, 0 |
| | 229 | \\ syscall |
| | 230 | , |
| | 231 | } |
| | 232 | : |
| | 233 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 234 | [len] "r" (self.mapped.len), |
| | 235 | : .{ .memory = true }), |
| | 236 | .or1k => asm volatile ( |
| | 237 | \\ l.ori r11, r0, 215 # SYS_munmap |
| | 238 | \\ l.ori r3, %[ptr] |
| | 239 | \\ l.ori r4, %[len] |
| | 240 | \\ l.sys 1 |
| | 241 | \\ l.ori r11, r0, 93 # SYS_exit |
| | 242 | \\ l.ori r3, r0, r0 |
| | 243 | \\ l.sys 1 |
| | 244 | : |
| | 245 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 246 | [len] "r" (self.mapped.len), |
| | 247 | : .{ .memory = true }), |
| | 248 | .powerpc, .powerpcle, .powerpc64, .powerpc64le => asm volatile ( |
| | 249 | \\ li 0, 91 # SYS_munmap |
| | 250 | \\ mr 3, %[ptr] |
| | 251 | \\ mr 4, %[len] |
| | 252 | \\ sc |
| | 253 | \\ li 0, 1 # SYS_exit |
| | 254 | \\ li 3, 0 |
| | 255 | \\ sc |
| | 256 | \\ blr |
| | 257 | : |
| | 258 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 259 | [len] "r" (self.mapped.len), |
| | 260 | : .{ .memory = true }), |
| | 261 | .riscv32, .riscv64 => asm volatile ( |
| | 262 | \\ li a7, 215 # SYS_munmap |
| | 263 | \\ mv a0, %[ptr] |
| | 264 | \\ mv a1, %[len] |
| | 265 | \\ ecall |
| | 266 | \\ li a7, 93 # SYS_exit |
| | 267 | \\ mv a0, zero |
| | 268 | \\ ecall |
| | 269 | : |
| | 270 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 271 | [len] "r" (self.mapped.len), |
| | 272 | : .{ .memory = true }), |
| | 273 | .s390x => asm volatile ( |
| | 274 | \\ lgr %%r2, %[ptr] |
| | 275 | \\ lgr %%r3, %[len] |
| | 276 | \\ svc 91 # SYS_munmap |
| | 277 | \\ lghi %%r2, 0 |
| | 278 | \\ svc 1 # SYS_exit |
| | 279 | : |
| | 280 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 281 | [len] "r" (self.mapped.len), |
| | 282 | : .{ .memory = true }), |
| | 283 | .sh, .sheb => asm volatile ( |
| | 284 | \\ mov #91, r3 ! SYS_munmap |
| | 285 | \\ mov %[ptr], r4 |
| | 286 | \\ mov %[len], r5 |
| | 287 | \\ trapa #31 |
| | 288 | \\ or r0, r0 |
| | 289 | \\ or r0, r0 |
| | 290 | \\ or r0, r0 |
| | 291 | \\ or r0, r0 |
| | 292 | \\ or r0, r0 |
| | 293 | \\ mov #1, r3 ! SYS_exit |
| | 294 | \\ mov #0, r4 |
| | 295 | \\ trapa #31 |
| | 296 | \\ or r0, r0 |
| | 297 | \\ or r0, r0 |
| | 298 | \\ or r0, r0 |
| | 299 | \\ or r0, r0 |
| | 300 | \\ or r0, r0 |
| | 301 | : |
| | 302 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 303 | [len] "r" (self.mapped.len), |
| | 304 | : .{ .memory = true }), |
| | 305 | .sparc => asm volatile ( |
| | 306 | \\ # See sparc64 comments below. |
| | 307 | \\ 1: |
| | 308 | \\ cmp %%fp, 0 |
| | 309 | \\ beq 2f |
| | 310 | \\ nop |
| | 311 | \\ ba 1b |
| | 312 | \\ restore |
| | 313 | \\ 2: |
| | 314 | \\ mov 73, %%g1 // SYS_munmap |
| | 315 | \\ mov %[ptr], %%o0 |
| | 316 | \\ mov %[len], %%o1 |
| | 317 | \\ t 0x3 # ST_FLUSH_WINDOWS |
| | 318 | \\ t 0x10 |
| | 319 | \\ mov 1, %%g1 // SYS_exit |
| | 320 | \\ mov 0, %%o0 |
| | 321 | \\ t 0x10 |
| | 322 | : |
| | 323 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 324 | [len] "r" (self.mapped.len), |
| | 325 | : .{ .memory = true }), |
| | 326 | .sparc64 => asm volatile ( |
| | 327 | \\ # SPARCs really don't like it when active stack frames |
| | 328 | \\ # is unmapped (it will result in a segfault), so we |
| | 329 | \\ # force-deactivate it by running `restore` until |
| | 330 | \\ # all frames are cleared. |
| | 331 | \\ 1: |
| | 332 | \\ cmp %%fp, 0 |
| | 333 | \\ beq 2f |
| | 334 | \\ nop |
| | 335 | \\ ba 1b |
| | 336 | \\ restore |
| | 337 | \\ 2: |
| | 338 | \\ mov 73, %%g1 // SYS_munmap |
| | 339 | \\ mov %[ptr], %%o0 |
| | 340 | \\ mov %[len], %%o1 |
| | 341 | \\ # Flush register window contents to prevent background |
| | 342 | \\ # memory access before unmapping the stack. |
| | 343 | \\ flushw |
| | 344 | \\ t 0x6d |
| | 345 | \\ mov 1, %%g1 // SYS_exit |
| | 346 | \\ mov 0, %%o0 |
| | 347 | \\ t 0x6d |
| | 348 | : |
| | 349 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 350 | [len] "r" (self.mapped.len), |
| | 351 | : .{ .memory = true }), |
| | 352 | .loongarch32, .loongarch64 => asm volatile ( |
| | 353 | \\ or $a0, $zero, %[ptr] |
| | 354 | \\ or $a1, $zero, %[len] |
| | 355 | \\ ori $a7, $zero, 215 # SYS_munmap |
| | 356 | \\ syscall 0 # call munmap |
| | 357 | \\ ori $a0, $zero, 0 |
| | 358 | \\ ori $a7, $zero, 93 # SYS_exit |
| | 359 | \\ syscall 0 # call exit |
| | 360 | : |
| | 361 | : [ptr] "r" (@intFromPtr(self.mapped.ptr)), |
| | 362 | [len] "r" (self.mapped.len), |
| | 363 | : .{ .memory = true }), |
| | 364 | else => |cpu_arch| @compileError("Unsupported linux arch: " ++ @tagName(cpu_arch)), |
| | 365 | } |
| | 366 | unreachable; |
| | 367 | } |
| | 368 | }, |
| | 369 | else => void, |
| | 370 | }; |
| | 371 | |
| | 372 | const AllocateError = error{OutOfMemory}; |
| | 373 | |
| | 374 | fn allocate(stack_size: usize) AllocateError!*Thread { |
| | 375 | if (use_pthreads) { |
| | 376 | @compileError("TODO"); |
| | 377 | } else if (is_windows) { |
| | 378 | @compileError("TODO"); |
| | 379 | } else if (native_os == .linux) { |
| | 380 | const linux = std.os.linux; |
| | 381 | const page_size = std.heap.pageSize(); |
| | 382 | |
| | 383 | var guard_offset: usize = undefined; |
| | 384 | var stack_offset: usize = undefined; |
| | 385 | var tls_offset: usize = undefined; |
| | 386 | var instance_offset: usize = undefined; |
| | 387 | |
| | 388 | const map_bytes = blk: { |
| | 389 | var bytes: usize = page_size; |
| | 390 | guard_offset = bytes; |
| | 391 | |
| | 392 | bytes += @max(page_size, stack_size); |
| | 393 | bytes = std.mem.alignForward(usize, bytes, page_size); |
| | 394 | stack_offset = bytes; |
| | 395 | |
| | 396 | bytes = std.mem.alignForward(usize, bytes, linux.tls.area_desc.alignment); |
| | 397 | tls_offset = bytes; |
| | 398 | bytes += linux.tls.area_desc.size; |
| | 399 | |
| | 400 | bytes = std.mem.alignForward(usize, bytes, @alignOf(Thread)); |
| | 401 | instance_offset = bytes; |
| | 402 | bytes += @sizeOf(Thread); |
| | 403 | |
| | 404 | bytes = std.mem.alignForward(usize, bytes, page_size); |
| | 405 | break :blk bytes; |
| | 406 | }; |
| | 407 | |
| | 408 | // Map all memory needed without read/write permissions to avoid |
| | 409 | // committing the whole region right away. Anonymous mapping ensures |
| | 410 | // file descriptor limits are not exceeded. |
| | 411 | const mapped = posix.mmap( |
| | 412 | null, |
| | 413 | map_bytes, |
| | 414 | posix.PROT.NONE, |
| | 415 | .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, |
| | 416 | -1, |
| | 417 | 0, |
| | 418 | ) catch |err| switch (err) { |
| | 419 | error.MemoryMappingNotSupported => unreachable, |
| | 420 | error.AccessDenied => unreachable, |
| | 421 | error.PermissionDenied => unreachable, |
| | 422 | error.ProcessFdQuotaExceeded => unreachable, |
| | 423 | error.SystemFdQuotaExceeded => unreachable, |
| | 424 | error.MappingAlreadyExists => unreachable, |
| | 425 | else => |e| return e, |
| | 426 | }; |
| | 427 | assert(mapped.len >= map_bytes); |
| | 428 | errdefer posix.munmap(mapped); |
| | 429 | |
| | 430 | // map everything but the guard page as read/write |
| | 431 | posix.mprotect( |
| | 432 | @alignCast(mapped[guard_offset..]), |
| | 433 | posix.PROT.READ | posix.PROT.WRITE, |
| | 434 | ) catch |err| switch (err) { |
| | 435 | error.AccessDenied => unreachable, |
| | 436 | else => |e| return e, |
| | 437 | }; |
| | 438 | |
| | 439 | // Prepare the TLS segment and prepare a user_desc struct when needed on x86 |
| | 440 | var tls_ptr = linux.tls.prepareArea(mapped[tls_offset..]); |
| | 441 | var user_desc: if (builtin.target.cpu.arch == .x86) linux.user_desc else void = undefined; |
| | 442 | if (builtin.target.cpu.arch == .x86) { |
| | 443 | defer tls_ptr = @intFromPtr(&user_desc); |
| | 444 | user_desc = .{ |
| | 445 | .entry_number = linux.tls.area_desc.gdt_entry_number, |
| | 446 | .base_addr = tls_ptr, |
| | 447 | .limit = 0xfffff, |
| | 448 | .flags = .{ |
| | 449 | .seg_32bit = 1, |
| | 450 | .contents = 0, // Data |
| | 451 | .read_exec_only = 0, |
| | 452 | .limit_in_pages = 1, |
| | 453 | .seg_not_present = 0, |
| | 454 | .useable = 1, |
| | 455 | }, |
| | 456 | }; |
| | 457 | } |
| | 458 | |
| | 459 | const instance: *Thread = @ptrCast(@alignCast(&mapped[instance_offset])); |
| | 460 | instance.* = .{ |
| | 461 | .signal_id = undefined, // Initialized on spawn. |
| | 462 | .completion = .{ |
| | 463 | .mapped = mapped, |
| | 464 | .stack_offset = stack_offset, |
| | 465 | }, |
| | 466 | }; |
| | 467 | return instance; |
| | 468 | } else { |
| | 469 | @compileError("unimplemented"); |
| | 470 | } |
| | 471 | } |
| | 472 | |
| | 473 | const SpawnError = error{ |
| | 474 | ThreadQuotaExceeded, |
| | 475 | SystemResources, |
| | 476 | Unexpected, |
| | 477 | }; |
| | 478 | |
| | 479 | fn spawn(thread: *Thread) SpawnError!void { |
| | 480 | if (use_pthreads) { |
| | 481 | const c = std.c; |
| | 482 | const stack_size = {}; // TODO |
| | 483 | |
| | 484 | var attr: c.pthread_attr_t = undefined; |
| | 485 | if (c.pthread_attr_init(&attr) != .SUCCESS) return error.SystemResources; |
| | 486 | defer assert(c.pthread_attr_destroy(&attr) == .SUCCESS); |
| | 487 | |
| | 488 | assert(c.pthread_attr_setstacksize(&attr, stack_size) == .SUCCESS); |
| | 489 | assert(c.pthread_attr_setguardsize(&attr, std.heap.pageSize()) == .SUCCESS); |
| | 490 | |
| | 491 | var handle: c.pthread_t = undefined; |
| | 492 | switch (c.pthread_create( |
| | 493 | &handle, |
| | 494 | &attr, |
| | 495 | posixStart, |
| | 496 | @ptrCast(thread), |
| | 497 | )) { |
| | 498 | .SUCCESS => { |
| | 499 | thread.signal_id = handle; |
| | 500 | return; |
| | 501 | }, |
| | 502 | .AGAIN => return error.SystemResources, |
| | 503 | .PERM => unreachable, |
| | 504 | .INVAL => unreachable, |
| | 505 | else => |err| return posix.unexpectedErrno(err), |
| | 506 | } |
| | 507 | @compileError("TODO"); |
| | 508 | } else if (is_windows) { |
| | 509 | @compileError("TODO"); |
| | 510 | } else if (native_os == .linux) { |
| | 511 | const linux = std.os.linux; |
| | 512 | |
| | 513 | const flags: u32 = linux.CLONE.THREAD | linux.CLONE.DETACHED | |
| | 514 | linux.CLONE.VM | linux.CLONE.FS | linux.CLONE.FILES | |
| | 515 | linux.CLONE.PARENT_SETTID | linux.CLONE.CHILD_CLEARTID | |
| | 516 | linux.CLONE.SIGHAND | linux.CLONE.SYSVSEM | linux.CLONE.SETTLS; |
| | 517 | |
| | 518 | switch (linux.errno(linux.clone( |
| | 519 | linuxStart, |
| | 520 | @intFromPtr(&thread.completion.mapped[thread.completion.stack_offset]), |
| | 521 | flags, |
| | 522 | @intFromPtr(thread), |
| | 523 | &thread.parent_tid, |
| | 524 | thread.completion.tls_ptr, |
| | 525 | &thread.child_tid.raw, |
| | 526 | ))) { |
| | 527 | .SUCCESS => return, |
| | 528 | .AGAIN => return error.ThreadQuotaExceeded, |
| | 529 | .INVAL => unreachable, |
| | 530 | .NOMEM => return error.SystemResources, |
| | 531 | .NOSPC => unreachable, |
| | 532 | .PERM => unreachable, |
| | 533 | .USERS => unreachable, |
| | 534 | else => |err| return posix.unexpectedErrno(err), |
| | 535 | } |
| | 536 | } else { |
| | 537 | @compileError("unimplemented"); |
| | 538 | } |
| | 539 | } |
| | 540 | |
| | 541 | fn linuxStart(raw_arg: usize) callconv(.c) u8 { |
| | 542 | const t: *Thread = @ptrFromInt(raw_arg); |
| | 543 | worker(t); |
| | 544 | switch (t.completion.swap(.completed, .seq_cst)) { |
| | 545 | .running => return 0, |
| | 546 | .completed => unreachable, |
| | 547 | .detached => t.completion.freeAndExit(), |
| | 548 | } |
| | 549 | unreachable; |
| | 550 | } |
| | 551 | |
| | 552 | fn posixStart(raw_arg: ?*anyopaque) callconv(.c) ?*anyopaque { |
| | 553 | const t: *Thread = @ptrCast(@alignCast(raw_arg)); |
| | 554 | worker(t); |
| | 555 | return null; |
| | 556 | } |
| | 557 | |
| | 558 | fn worker(t: *Thread) void { |
| | 559 | current = t; |
| | 560 | |
| | 561 | t.mutex.lock(); |
| | 562 | |
| | 563 | while (true) { |
| | 564 | while (t.run_queue.popFirst()) |closure_node| { |
| | 565 | t.mutex.unlock(); |
| | 566 | const closure: *Closure = @fieldParentPtr("node", closure_node); |
| | 567 | closure.start(closure); |
| | 568 | t.mutex.lock(); |
| | 569 | } |
| | 570 | if (t.join_requested) break; |
| | 571 | t.cond.wait(&t.mutex); |
| | 572 | } |
| | 573 | } |
| | 574 | |
| | 575 | fn checkCancel(thread: *Thread) error{Canceled}!void { |
| | 576 | const closure = thread.current_closure orelse return; |
| | 577 | switch (@cmpxchgStrong( |
| | 578 | CancelStatus, |
| | 579 | &closure.cancel_status, |
| | 580 | .requested, |
| | 581 | .acknowledged, |
| | 582 | .acq_rel, |
| | 583 | .acquire, |
| | 584 | ) orelse return error.Canceled) { |
| | 585 | .none => return, |
| | 586 | .requested => unreachable, |
| | 587 | .acknowledged => unreachable, |
| | 588 | _ => return, |
| | 589 | } |
| | 590 | } |
| | 591 | |
| | 592 | fn beginSyscall(thread: *Thread) error{Canceled}!void { |
| | 593 | const closure = thread.current_closure orelse return; |
| | 594 | |
| | 595 | switch (@cmpxchgStrong( |
| | 596 | CancelStatus, |
| | 597 | &closure.cancel_status, |
| | 598 | .none, |
| | 599 | thread.signal_id, |
| | 600 | .acq_rel, |
| | 601 | .acquire, |
| | 602 | ) orelse return) { |
| | 603 | .none => unreachable, |
| | 604 | .requested => { |
| | 605 | @atomicStore(CancelStatus, &closure.cancel_status, .acknowledged, .acquire); |
| | 606 | return error.Canceled; |
| | 607 | }, |
| | 608 | .acknowledged => unreachable, |
| | 609 | _ => unreachable, |
| | 610 | } |
| | 611 | } |
| | 612 | |
| | 613 | fn endSyscall(thread: *Thread) error{Canceled}!void { |
| | 614 | const closure = thread.current_closure orelse return; |
| 41 | | 615 | |
| 42 | pub fn init(n: usize) ThreadCapacity { | 616 | switch (@cmpxchgStrong( |
| 43 | assert(n != 0); | 617 | CancelStatus, |
| | 618 | &closure.cancel_status, |
| | 619 | thread.signal_id, |
| | 620 | .none, |
| | 621 | .acq_rel, |
| | 622 | .release, |
| | 623 | ) orelse return) { |
| | 624 | .none => unreachable, |
| | 625 | .requested => { |
| | 626 | @atomicStore(CancelStatus, &closure.cancel_status, .acknowledged, .release); |
| | 627 | return error.Canceled; |
| | 628 | }, |
| | 629 | .acknowledged => return, |
| | 630 | _ => unreachable, |
| | 631 | } |
| | 632 | } |
| | 633 | }; |
| | 634 | |
| | 635 | pub const Capacity = enum(isize) { |
| | 636 | unknown = -30000, |
| | 637 | _, |
| | 638 | |
| | 639 | pub fn init(n: isize) Capacity { |
| | 640 | assert(n > 0); |
| 44 | return @enumFromInt(n); | 641 | return @enumFromInt(n); |
| 45 | } | 642 | } |
| 46 | | 643 | |
| 47 | pub fn get(tc: ThreadCapacity) ?usize { | 644 | pub fn get(tc: Capacity) ?usize { |
| 48 | if (tc == .unknown) return null; | 645 | if (tc == .unknown) return null; |
| 49 | return @intFromEnum(tc); | 646 | return @intFromEnum(tc); |
| 50 | } | 647 | } |
| 51 | }; | 648 | }; |
| 52 | | 649 | |
| 53 | threadlocal var current_closure: ?*Closure = null; | 650 | pub const default_stack_size = 16 * 1024 * 1024; |
| | 651 | pub const use_pthreads = !is_windows and native_os != .wasi and builtin.link_libc; |
| 54 | | 652 | |
| 55 | const max_iovecs_len = 8; | 653 | const max_iovecs_len = 8; |
| 56 | const splat_buffer_size = 64; | 654 | const splat_buffer_size = 64; |
| ... | @@ -59,85 +657,108 @@ comptime { | ... | @@ -59,85 +657,108 @@ comptime { |
| 59 | if (@TypeOf(posix.IOV_MAX) != void) assert(max_iovecs_len <= posix.IOV_MAX); | 657 | if (@TypeOf(posix.IOV_MAX) != void) assert(max_iovecs_len <= posix.IOV_MAX); |
| 60 | } | 658 | } |
| 61 | | 659 | |
| 62 | const CancelId = enum(usize) { | 660 | const CancelStatus = enum(usize) { |
| | 661 | /// Cancellation has neither been requested, nor checked. The async |
| | 662 | /// operation will check status before entering a blocking syscall. |
| | 663 | /// This is also the status used for uninteruptible tasks. |
| 63 | none = 0, | 664 | none = 0, |
| 64 | canceling = std.math.maxInt(usize), | 665 | /// Cancellation has been requested and the status will be checked before |
| | 666 | /// entering a blocking syscall. |
| | 667 | requested = std.math.maxInt(usize) - 1, |
| | 668 | /// Cancellation has been acknowledged and is in progress. Signals should |
| | 669 | /// not be sent. |
| | 670 | acknowledged = std.math.maxInt(usize), |
| | 671 | /// Stores a `Thread.SignalId` and indicates that sending a signal to this thread |
| | 672 | /// is needed in order to cancel. This state is set before going into |
| | 673 | /// a blocking operation that needs to get unblocked via signal. |
| 65 | _, | 674 | _, |
| 66 | | 675 | |
| 67 | const ThreadId = if (std.Thread.use_pthreads) std.c.pthread_t else std.Thread.Id; | 676 | const Unpacked = union(enum) { |
| 68 | | 677 | none, |
| 69 | fn currentThread() CancelId { | 678 | requested, |
| 70 | if (std.Thread.use_pthreads) { | 679 | acknowledeged, |
| 71 | return @enumFromInt(@intFromPtr(std.c.pthread_self())); | 680 | signal_id: Thread.SignalId, |
| 72 | } else { | 681 | }; |
| 73 | return @enumFromInt(std.Thread.getCurrentId()); | | |
| 74 | } | | |
| 75 | } | | |
| 76 | | 682 | |
| 77 | fn toThreadId(cancel_id: CancelId) ThreadId { | 683 | fn unpack(cs: CancelStatus) Unpacked { |
| 78 | if (std.Thread.use_pthreads) { | 684 | return switch (cs) { |
| 79 | return @ptrFromInt(@intFromEnum(cancel_id)); | 685 | .none => .none, |
| 80 | } else { | 686 | .requested => .requested, |
| 81 | return @intCast(@intFromEnum(cancel_id)); | 687 | .acknowledged => .acknowledged, |
| 82 | } | 688 | _ => |signal_id| .{ .signal_id = signal_id }, |
| | 689 | }; |
| 83 | } | 690 | } |
| 84 | }; | 691 | }; |
| 85 | | 692 | |
| 86 | const Closure = struct { | 693 | const Closure = struct { |
| 87 | start: Start, | 694 | start: Start, |
| 88 | node: std.SinglyLinkedList.Node = .{}, | 695 | node: std.SinglyLinkedList.Node = .{}, |
| 89 | cancel_tid: CancelId, | 696 | cancel_status: CancelStatus, |
| 90 | /// Whether this task bumps minimum number of threads in the pool. | 697 | /// Whether this task bumps minimum number of threads in the pool. |
| 91 | is_concurrent: bool, | 698 | is_concurrent: bool, |
| 92 | | 699 | |
| 93 | const Start = *const fn (*Closure) void; | 700 | const Start = *const fn (*Closure) void; |
| 94 | | 701 | |
| 95 | fn requestCancel(closure: *Closure) void { | 702 | fn requestCancel(closure: *Closure, t: *Threaded) void { |
| 96 | switch (@atomicRmw(CancelId, &closure.cancel_tid, .Xchg, .canceling, .acq_rel)) { | 703 | var signal_id = switch (@atomicRmw(CancelStatus, &closure.cancel_status, .Xchg, .requested, .monotonic).unpack()) { |
| 97 | .none, .canceling => {}, | 704 | .none, .acknowledged, .requested => return, |
| 98 | else => |tid| { | 705 | else => |signal_id| signal_id, |
| 99 | if (std.Thread.use_pthreads) { | 706 | }; |
| 100 | const rc = std.c.pthread_kill(tid.toThreadId(), .IO); | 707 | // The task will enter a blocking syscall before checking for cancellation again. |
| 101 | if (is_debug) assert(rc == 0); | 708 | // We can send a signal to interrupt the syscall, but if it arrives before |
| 102 | } else if (native_os == .linux) { | 709 | // the syscall instruction, it will be missed. Therefore, this code tries |
| 103 | _ = std.os.linux.tgkill(std.os.linux.getpid(), @bitCast(tid.toThreadId()), .IO); | 710 | // again until the cancellation request is acknowledged. |
| 104 | } | 711 | const max_attempts = 3; |
| 105 | }, | 712 | for (0..max_attempts) |_| { |
| | 713 | if (use_pthreads) { |
| | 714 | const rc = std.c.pthread_kill(signal_id.toThreadId(), .IO); |
| | 715 | if (is_debug) assert(rc == 0); |
| | 716 | } else if (native_os == .linux) { |
| | 717 | const pid: posix.pid_t = p: { |
| | 718 | const cached_pid = @atomicLoad(Pid, &t.pid, .monotonic); |
| | 719 | if (cached_pid != .unknown) break :p @intFromEnum(cached_pid); |
| | 720 | const pid = std.os.linux.getpid(); |
| | 721 | @atomicStore(Pid, &t.pid, @enumFromInt(pid), .monotonic); |
| | 722 | break :p pid; |
| | 723 | }; |
| | 724 | _ = std.os.linux.tgkill(pid, @bitCast(signal_id.toThreadId()), .IO); |
| | 725 | } else { |
| | 726 | return; |
| | 727 | } |
| | 728 | |
| | 729 | // TODO make this a nanosleep with 1 << attempt duration |
| | 730 | std.Thread.yield() catch {}; |
| | 731 | |
| | 732 | switch (@atomicRmw(CancelStatus, &closure.cancel_status, .Xchg, .requested, .monotonic).unpack()) { |
| | 733 | .requested => continue, |
| | 734 | .none, .acknowledged => return, |
| | 735 | else => |new_signal_id| signal_id = new_signal_id, |
| | 736 | } |
| 106 | } | 737 | } |
| 107 | } | 738 | } |
| 108 | }; | 739 | }; |
| 109 | | 740 | |
| 110 | pub const InitError = std.Thread.CpuCountError || Allocator.Error; | 741 | pub const CpuCountError = error{ |
| | 742 | PermissionDenied, |
| | 743 | SystemResources, |
| | 744 | Unsupported, |
| | 745 | } || Io.UnexpectedError; |
| 111 | | 746 | |
| 112 | /// Related: | 747 | /// Related: |
| 113 | /// * `init_single_threaded` | 748 | /// * `init_single_threaded` |
| 114 | pub fn init( | 749 | pub fn init() Threaded { |
| 115 | /// Must be threadsafe. Only used for the following functions: | | |
| 116 | /// * `Io.VTable.async` | | |
| 117 | /// * `Io.VTable.concurrent` | | |
| 118 | /// * `Io.VTable.groupAsync` | | |
| 119 | /// If these functions are avoided, then `Allocator.failing` may be passed | | |
| 120 | /// here. | | |
| 121 | gpa: Allocator, | | |
| 122 | ) Threaded { | | |
| 123 | const cpu_count = std.Thread.getCpuCount(); | 750 | const cpu_count = std.Thread.getCpuCount(); |
| 124 | | 751 | |
| 125 | var t: Threaded = .{ | 752 | var t: Threaded = .{ |
| 126 | .allocator = gpa, | | |
| 127 | .threads = .empty, | 753 | .threads = .empty, |
| 128 | .stack_size = std.Thread.SpawnConfig.default_stack_size, | 754 | .capacity = .init(if (cpu_count) |n| .init(n) else |_| .unknown), |
| 129 | .thread_capacity = .init(if (cpu_count) |n| .init(n) else |_| .unknown), | 755 | .capacity_error = if (cpu_count) |_| null else |e| e, |
| 130 | .thread_capacity_error = if (cpu_count) |_| null else |e| e, | | |
| 131 | .concurrent_count = 0, | 756 | .concurrent_count = 0, |
| 132 | .old_sig_io = undefined, | 757 | .old_sig_io = undefined, |
| 133 | .old_sig_pipe = undefined, | 758 | .old_sig_pipe = undefined, |
| 134 | .have_signal_handler = false, | 759 | .have_signal_handler = false, |
| 135 | }; | 760 | }; |
| 136 | | 761 | |
| 137 | if (cpu_count) |n| { | | |
| 138 | t.threads.ensureTotalCapacityPrecise(gpa, n - 1) catch {}; | | |
| 139 | } else |_| {} | | |
| 140 | | | |
| 141 | if (posix.Sigaction != void) { | 762 | if (posix.Sigaction != void) { |
| 142 | // This causes sending `posix.SIG.IO` to thread to interrupt blocking | 763 | // This causes sending `posix.SIG.IO` to thread to interrupt blocking |
| 143 | // syscalls, returning `posix.E.INTR`. | 764 | // syscalls, returning `posix.E.INTR`. |
| ... | @@ -161,11 +782,9 @@ pub fn init( | ... | @@ -161,11 +782,9 @@ pub fn init( |
| 161 | /// * cancel requests have no effect. | 782 | /// * cancel requests have no effect. |
| 162 | /// * `deinit` is safe, but unnecessary to call. | 783 | /// * `deinit` is safe, but unnecessary to call. |
| 163 | pub const init_single_threaded: Threaded = .{ | 784 | pub const init_single_threaded: Threaded = .{ |
| 164 | .allocator = .failing, | | |
| 165 | .threads = .empty, | 785 | .threads = .empty, |
| 166 | .stack_size = std.Thread.SpawnConfig.default_stack_size, | 786 | .capacity = .init(.init(1)), |
| 167 | .thread_capacity = .init(.init(1)), | 787 | .capacity_error = null, |
| 168 | .thread_capacity_error = null, | | |
| 169 | .concurrent_count = 0, | 788 | .concurrent_count = 0, |
| 170 | .old_sig_io = undefined, | 789 | .old_sig_io = undefined, |
| 171 | .old_sig_pipe = undefined, | 790 | .old_sig_pipe = undefined, |
| ... | @@ -173,9 +792,7 @@ pub const init_single_threaded: Threaded = .{ | ... | @@ -173,9 +792,7 @@ pub const init_single_threaded: Threaded = .{ |
| 173 | }; | 792 | }; |
| 174 | | 793 | |
| 175 | pub fn deinit(t: *Threaded) void { | 794 | pub fn deinit(t: *Threaded) void { |
| 176 | const gpa = t.allocator; | 795 | join(t); |
| 177 | t.join(); | | |
| 178 | t.threads.deinit(gpa); | | |
| 179 | if (is_windows and t.wsa.status == .initialized) { | 796 | if (is_windows and t.wsa.status == .initialized) { |
| 180 | if (ws2_32.WSACleanup() != 0) recoverableOsBugDetected(); | 797 | if (ws2_32.WSACleanup() != 0) recoverableOsBugDetected(); |
| 181 | } | 798 | } |
| ... | @@ -186,46 +803,29 @@ pub fn deinit(t: *Threaded) void { | ... | @@ -186,46 +803,29 @@ pub fn deinit(t: *Threaded) void { |
| 186 | t.* = undefined; | 803 | t.* = undefined; |
| 187 | } | 804 | } |
| 188 | | 805 | |
| 189 | pub fn setThreadCapacity(t: *Threaded, n: usize) void { | 806 | pub fn setCapacity(t: *Threaded, n: usize) void { |
| 190 | t.thread_capacity.store(.init(n), .monotonic); | 807 | t.capacity.store(.init(n), .monotonic); |
| 191 | } | 808 | } |
| 192 | | 809 | |
| 193 | pub fn getThreadCapacity(t: *Threaded) ?usize { | 810 | pub fn getCapacity(t: *Threaded) ?usize { |
| 194 | return t.thread_capacity.load(.monotonic).get(); | 811 | return t.capacity.load(.monotonic).get(); |
| 195 | } | | |
| 196 | | | |
| 197 | pub fn getCurrentThreadId() usize { | | |
| 198 | @panic("TODO"); | | |
| 199 | } | 812 | } |
| 200 | | 813 | |
| 201 | fn join(t: *Threaded) void { | 814 | fn join(t: *Threaded) void { |
| 202 | if (builtin.single_threaded) return; | 815 | if (builtin.single_threaded) return; |
| 203 | { | | |
| 204 | t.mutex.lock(); | | |
| 205 | defer t.mutex.unlock(); | | |
| 206 | t.join_requested = true; | | |
| 207 | } | | |
| 208 | t.cond.broadcast(); | | |
| 209 | for (t.threads.items) |thread| thread.join(); | | |
| 210 | } | | |
| 211 | | 816 | |
| 212 | fn worker(t: *Threaded) void { | 817 | { |
| 213 | t.mutex.lock(); | 818 | var it: ?*const std.SinglyLinkedList.Node = &t.main_thread.list_node; |
| 214 | defer t.mutex.unlock(); | 819 | while (it) |n| : (it = n.next) { |
| 215 | | 820 | const thread: *Thread = @fieldParentPtr("list_node", n); |
| 216 | while (true) { | 821 | { |
| 217 | while (t.run_queue.popFirst()) |closure_node| { | 822 | thread.mutex.lock(); |
| 218 | t.mutex.unlock(); | 823 | defer thread.mutex.unlock(); |
| 219 | const closure: *Closure = @fieldParentPtr("node", closure_node); | 824 | thread.join_requested = true; |
| 220 | const is_concurrent = closure.is_concurrent; | 825 | thread.cond.signal(); |
| 221 | closure.start(closure); | | |
| 222 | t.mutex.lock(); | | |
| 223 | if (is_concurrent) { | | |
| 224 | t.concurrent_count -= 1; | | |
| 225 | } | 826 | } |
| | 827 | thread.join(); |
| 226 | } | 828 | } |
| 227 | if (t.join_requested) break; | | |
| 228 | t.cond.wait(&t.mutex); | | |
| 229 | } | 829 | } |
| 230 | } | 830 | } |
| 231 | | 831 | |
| ... | @@ -237,7 +837,6 @@ pub fn io(t: *Threaded) Io { | ... | @@ -237,7 +837,6 @@ pub fn io(t: *Threaded) Io { |
| 237 | .concurrent = concurrent, | 837 | .concurrent = concurrent, |
| 238 | .await = await, | 838 | .await = await, |
| 239 | .cancel = cancel, | 839 | .cancel = cancel, |
| 240 | .cancelRequested = cancelRequested, | | |
| 241 | .select = select, | 840 | .select = select, |
| 242 | | 841 | |
| 243 | .groupAsync = groupAsync, | 842 | .groupAsync = groupAsync, |
| ... | @@ -333,7 +932,6 @@ pub fn ioBasic(t: *Threaded) Io { | ... | @@ -333,7 +932,6 @@ pub fn ioBasic(t: *Threaded) Io { |
| 333 | .concurrent = concurrent, | 932 | .concurrent = concurrent, |
| 334 | .await = await, | 933 | .await = await, |
| 335 | .cancel = cancel, | 934 | .cancel = cancel, |
| 336 | .cancelRequested = cancelRequested, | | |
| 337 | .select = select, | 935 | .select = select, |
| 338 | | 936 | |
| 339 | .groupAsync = groupAsync, | 937 | .groupAsync = groupAsync, |
| ... | @@ -428,22 +1026,10 @@ const AsyncClosure = struct { | ... | @@ -428,22 +1026,10 @@ const AsyncClosure = struct { |
| 428 | | 1026 | |
| 429 | fn start(closure: *Closure) void { | 1027 | fn start(closure: *Closure) void { |
| 430 | const ac: *AsyncClosure = @alignCast(@fieldParentPtr("closure", closure)); | 1028 | const ac: *AsyncClosure = @alignCast(@fieldParentPtr("closure", closure)); |
| 431 | const tid: CancelId = .currentThread(); | 1029 | const current_thread = Thread.current; |
| 432 | if (@cmpxchgStrong(CancelId, &closure.cancel_tid, .none, tid, .acq_rel, .acquire)) |cancel_tid| { | 1030 | current_thread.current_closure = closure; |
| 433 | assert(cancel_tid == .canceling); | | |
| 434 | // Even though we already know the task is canceled, we must still | | |
| 435 | // run the closure in order to make the return value valid and in | | |
| 436 | // case there are side effects. | | |
| 437 | } | | |
| 438 | current_closure = closure; | | |
| 439 | ac.func(ac.contextPointer(), ac.resultPointer()); | 1031 | ac.func(ac.contextPointer(), ac.resultPointer()); |
| 440 | current_closure = null; | 1032 | current_thread.current_closure = null; |
| 441 | | | |
| 442 | // In case a cancel happens after successful task completion, prevents | | |
| 443 | // signal from being delivered to the thread in `requestCancel`. | | |
| 444 | if (@cmpxchgStrong(CancelId, &closure.cancel_tid, tid, .none, .acq_rel, .acquire)) |cancel_tid| { | | |
| 445 | assert(cancel_tid == .canceling); | | |
| 446 | } | | |
| 447 | | 1033 | |
| 448 | if (@atomicRmw(?*ResetEvent, &ac.select_condition, .Xchg, done_reset_event, .release)) |select_reset| { | 1034 | if (@atomicRmw(?*ResetEvent, &ac.select_condition, .Xchg, done_reset_event, .release)) |select_reset| { |
| 449 | assert(select_reset != done_reset_event); | 1035 | assert(select_reset != done_reset_event); |
| ... | @@ -464,14 +1050,14 @@ const AsyncClosure = struct { | ... | @@ -464,14 +1050,14 @@ const AsyncClosure = struct { |
| 464 | } | 1050 | } |
| 465 | | 1051 | |
| 466 | fn init( | 1052 | fn init( |
| 467 | gpa: Allocator, | 1053 | ac: *AsyncClosure, |
| 468 | mode: enum { async, concurrent }, | 1054 | mode: enum { async, concurrent }, |
| 469 | result_len: usize, | 1055 | result_len: usize, |
| 470 | result_alignment: Alignment, | 1056 | result_alignment: Alignment, |
| 471 | context: []const u8, | 1057 | context: []const u8, |
| 472 | context_alignment: Alignment, | 1058 | context_alignment: Alignment, |
| 473 | func: *const fn (context: *const anyopaque, result: *anyopaque) void, | 1059 | func: *const fn (context: *const anyopaque, result: *anyopaque) void, |
| 474 | ) Allocator.Error!*AsyncClosure { | 1060 | ) void { |
| 475 | const max_context_misalignment = context_alignment.toByteUnits() -| @alignOf(AsyncClosure); | 1061 | const max_context_misalignment = context_alignment.toByteUnits() -| @alignOf(AsyncClosure); |
| 476 | const worst_case_context_offset = context_alignment.forward(@sizeOf(AsyncClosure) + max_context_misalignment); | 1062 | const worst_case_context_offset = context_alignment.forward(@sizeOf(AsyncClosure) + max_context_misalignment); |
| 477 | const worst_case_result_offset = result_alignment.forward(worst_case_context_offset + context.len); | 1063 | const worst_case_result_offset = result_alignment.forward(worst_case_context_offset + context.len); |
| ... | @@ -529,7 +1115,7 @@ fn async( | ... | @@ -529,7 +1115,7 @@ fn async( |
| 529 | } | 1115 | } |
| 530 | | 1116 | |
| 531 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 1117 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 532 | const cpu_count = t.getThreadCapacity() orelse { | 1118 | const may_spawn = takeCapacity(t) catch { |
| 533 | return concurrent(userdata, result.len, result_alignment, context, context_alignment, start) catch { | 1119 | return concurrent(userdata, result.len, result_alignment, context, context_alignment, start) catch { |
| 534 | start(context.ptr, result.ptr); | 1120 | start(context.ptr, result.ptr); |
| 535 | return null; | 1121 | return null; |
| ... | @@ -538,42 +1124,25 @@ fn async( | ... | @@ -538,42 +1124,25 @@ fn async( |
| 538 | | 1124 | |
| 539 | const gpa = t.allocator; | 1125 | const gpa = t.allocator; |
| 540 | const ac = AsyncClosure.init(gpa, .async, result.len, result_alignment, context, context_alignment, start) catch { | 1126 | const ac = AsyncClosure.init(gpa, .async, result.len, result_alignment, context, context_alignment, start) catch { |
| | 1127 | returnCapacity(t); |
| 541 | start(context.ptr, result.ptr); | 1128 | start(context.ptr, result.ptr); |
| 542 | return null; | 1129 | return null; |
| 543 | }; | 1130 | }; |
| 544 | | 1131 | |
| 545 | t.mutex.lock(); | 1132 | @memcpy(ac.contextPointer()[0..context.len], context); |
| 546 | | 1133 | |
| 547 | const thread_capacity = cpu_count - 1 + t.concurrent_count; | 1134 | if (may_spawn) { |
| | 1135 | // TODO Allocate Thread |
| 548 | | 1136 | |
| 549 | t.threads.ensureTotalCapacityPrecise(gpa, thread_capacity) catch { | 1137 | thread.run_queue.prepend(&ac.closure.node); |
| 550 | t.mutex.unlock(); | | |
| 551 | ac.deinit(gpa); | | |
| 552 | start(context.ptr, result.ptr); | | |
| 553 | return null; | | |
| 554 | }; | | |
| 555 | | 1138 | |
| 556 | t.run_queue.prepend(&ac.closure.node); | 1139 | // TODO start thread |
| 557 | | 1140 | |
| 558 | if (t.threads.items.len < thread_capacity) { | 1141 | return @ptrCast(ac); |
| 559 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { | | |
| 560 | if (t.threads.items.len == 0) { | | |
| 561 | assert(t.run_queue.popFirst() == &ac.closure.node); | | |
| 562 | t.mutex.unlock(); | | |
| 563 | ac.deinit(gpa); | | |
| 564 | start(context.ptr, result.ptr); | | |
| 565 | return null; | | |
| 566 | } | | |
| 567 | // Rely on other workers to do it. | | |
| 568 | t.mutex.unlock(); | | |
| 569 | t.cond.signal(); | | |
| 570 | return @ptrCast(ac); | | |
| 571 | }; | | |
| 572 | t.threads.appendAssumeCapacity(thread); | | |
| 573 | } | 1142 | } |
| 574 | | 1143 | |
| 575 | t.mutex.unlock(); | 1144 | const thread = Thread.current; |
| 576 | t.cond.signal(); | 1145 | thread.run_queue.prepend(&ac.closure.node); |
| 577 | return @ptrCast(ac); | 1146 | return @ptrCast(ac); |
| 578 | } | 1147 | } |
| 579 | | 1148 | |
| ... | @@ -588,7 +1157,7 @@ fn concurrent( | ... | @@ -588,7 +1157,7 @@ fn concurrent( |
| 588 | if (builtin.single_threaded) return error.ConcurrencyUnavailable; | 1157 | if (builtin.single_threaded) return error.ConcurrencyUnavailable; |
| 589 | | 1158 | |
| 590 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 1159 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 591 | const cpu_count = t.getThreadCapacity() orelse 1; | 1160 | const cpu_count = t.getCapacity() orelse 1; |
| 592 | | 1161 | |
| 593 | const gpa = t.allocator; | 1162 | const gpa = t.allocator; |
| 594 | const ac = AsyncClosure.init(gpa, .concurrent, result_len, result_alignment, context, context_alignment, start) catch { | 1163 | const ac = AsyncClosure.init(gpa, .concurrent, result_len, result_alignment, context, context_alignment, start) catch { |
| ... | @@ -598,9 +1167,9 @@ fn concurrent( | ... | @@ -598,9 +1167,9 @@ fn concurrent( |
| 598 | t.mutex.lock(); | 1167 | t.mutex.lock(); |
| 599 | | 1168 | |
| 600 | t.concurrent_count += 1; | 1169 | t.concurrent_count += 1; |
| 601 | const thread_capacity = cpu_count - 1 + t.concurrent_count; | 1170 | const capacity = cpu_count - 1 + t.concurrent_count; |
| 602 | | 1171 | |
| 603 | t.threads.ensureTotalCapacity(gpa, thread_capacity) catch { | 1172 | t.threads.ensureTotalCapacity(gpa, capacity) catch { |
| 604 | t.mutex.unlock(); | 1173 | t.mutex.unlock(); |
| 605 | ac.deinit(gpa); | 1174 | ac.deinit(gpa); |
| 606 | return error.ConcurrencyUnavailable; | 1175 | return error.ConcurrencyUnavailable; |
| ... | @@ -608,7 +1177,7 @@ fn concurrent( | ... | @@ -608,7 +1177,7 @@ fn concurrent( |
| 608 | | 1177 | |
| 609 | t.run_queue.prepend(&ac.closure.node); | 1178 | t.run_queue.prepend(&ac.closure.node); |
| 610 | | 1179 | |
| 611 | if (t.threads.items.len < thread_capacity) { | 1180 | if (t.threads.items.len < capacity) { |
| 612 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { | 1181 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { |
| 613 | assert(t.run_queue.popFirst() == &ac.closure.node); | 1182 | assert(t.run_queue.popFirst() == &ac.closure.node); |
| 614 | t.mutex.unlock(); | 1183 | t.mutex.unlock(); |
| ... | @@ -635,24 +1204,13 @@ const GroupClosure = struct { | ... | @@ -635,24 +1204,13 @@ const GroupClosure = struct { |
| 635 | | 1204 | |
| 636 | fn start(closure: *Closure) void { | 1205 | fn start(closure: *Closure) void { |
| 637 | const gc: *GroupClosure = @alignCast(@fieldParentPtr("closure", closure)); | 1206 | const gc: *GroupClosure = @alignCast(@fieldParentPtr("closure", closure)); |
| 638 | const tid: CancelId = .currentThread(); | 1207 | const current_thread = Thread.current; |
| 639 | const group = gc.group; | 1208 | const group = gc.group; |
| 640 | const group_state: *std.atomic.Value(usize) = @ptrCast(&group.state); | 1209 | const group_state: *std.atomic.Value(usize) = @ptrCast(&group.state); |
| 641 | const reset_event: *ResetEvent = @ptrCast(&group.context); | 1210 | const reset_event: *ResetEvent = @ptrCast(&group.context); |
| 642 | if (@cmpxchgStrong(CancelId, &closure.cancel_tid, .none, tid, .acq_rel, .acquire)) |cancel_tid| { | 1211 | current_thread.current_closure = closure; |
| 643 | assert(cancel_tid == .canceling); | | |
| 644 | // Even though we already know the task is canceled, we must still | | |
| 645 | // run the closure in case there are side effects. | | |
| 646 | } | | |
| 647 | current_closure = closure; | | |
| 648 | gc.func(group, gc.contextPointer()); | 1212 | gc.func(group, gc.contextPointer()); |
| 649 | current_closure = null; | 1213 | current_thread.current_closure = null; |
| 650 | | | |
| 651 | // In case a cancel happens after successful task completion, prevents | | |
| 652 | // signal from being delivered to the thread in `requestCancel`. | | |
| 653 | if (@cmpxchgStrong(CancelId, &closure.cancel_tid, tid, .none, .acq_rel, .acquire)) |cancel_tid| { | | |
| 654 | assert(cancel_tid == .canceling); | | |
| 655 | } | | |
| 656 | | 1214 | |
| 657 | const prev_state = group_state.fetchSub(sync_one_pending, .acq_rel); | 1215 | const prev_state = group_state.fetchSub(sync_one_pending, .acq_rel); |
| 658 | assert((prev_state / sync_one_pending) > 0); | 1216 | assert((prev_state / sync_one_pending) > 0); |
| ... | @@ -717,7 +1275,7 @@ fn groupAsync( | ... | @@ -717,7 +1275,7 @@ fn groupAsync( |
| 717 | if (builtin.single_threaded) return start(group, context.ptr); | 1275 | if (builtin.single_threaded) return start(group, context.ptr); |
| 718 | | 1276 | |
| 719 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 1277 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 720 | const cpu_count = t.getThreadCapacity() orelse 1; | 1278 | const cpu_count = t.getCapacity() orelse 1; |
| 721 | | 1279 | |
| 722 | const gpa = t.allocator; | 1280 | const gpa = t.allocator; |
| 723 | const gc = GroupClosure.init(gpa, t, group, context, context_alignment, start) catch { | 1281 | const gc = GroupClosure.init(gpa, t, group, context, context_alignment, start) catch { |
| ... | @@ -730,9 +1288,9 @@ fn groupAsync( | ... | @@ -730,9 +1288,9 @@ fn groupAsync( |
| 730 | gc.node = .{ .next = @ptrCast(@alignCast(group.token)) }; | 1288 | gc.node = .{ .next = @ptrCast(@alignCast(group.token)) }; |
| 731 | group.token = &gc.node; | 1289 | group.token = &gc.node; |
| 732 | | 1290 | |
| 733 | const thread_capacity = cpu_count - 1 + t.concurrent_count; | 1291 | const capacity = cpu_count - 1 + t.concurrent_count; |
| 734 | | 1292 | |
| 735 | t.threads.ensureTotalCapacityPrecise(gpa, thread_capacity) catch { | 1293 | t.threads.ensureTotalCapacityPrecise(gpa, capacity) catch { |
| 736 | t.mutex.unlock(); | 1294 | t.mutex.unlock(); |
| 737 | gc.deinit(gpa); | 1295 | gc.deinit(gpa); |
| 738 | return start(group, context.ptr); | 1296 | return start(group, context.ptr); |
| ... | @@ -740,7 +1298,7 @@ fn groupAsync( | ... | @@ -740,7 +1298,7 @@ fn groupAsync( |
| 740 | | 1298 | |
| 741 | t.run_queue.prepend(&gc.closure.node); | 1299 | t.run_queue.prepend(&gc.closure.node); |
| 742 | | 1300 | |
| 743 | if (t.threads.items.len < thread_capacity) { | 1301 | if (t.threads.items.len < capacity) { |
| 744 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { | 1302 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { |
| 745 | assert(t.run_queue.popFirst() == &gc.closure.node); | 1303 | assert(t.run_queue.popFirst() == &gc.closure.node); |
| 746 | t.mutex.unlock(); | 1304 | t.mutex.unlock(); |
| ... | @@ -775,7 +1333,7 @@ fn groupWait(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void { | ... | @@ -775,7 +1333,7 @@ fn groupWait(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void { |
| 775 | var node: *std.SinglyLinkedList.Node = @ptrCast(@alignCast(token)); | 1333 | var node: *std.SinglyLinkedList.Node = @ptrCast(@alignCast(token)); |
| 776 | while (true) { | 1334 | while (true) { |
| 777 | const gc: *GroupClosure = @fieldParentPtr("node", node); | 1335 | const gc: *GroupClosure = @fieldParentPtr("node", node); |
| 778 | gc.closure.requestCancel(); | 1336 | gc.closure.requestCancel(t); |
| 779 | node = node.next orelse break; | 1337 | node = node.next orelse break; |
| 780 | } | 1338 | } |
| 781 | reset_event.waitUncancelable(); | 1339 | reset_event.waitUncancelable(); |
| ... | @@ -801,7 +1359,7 @@ fn groupCancel(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void | ... | @@ -801,7 +1359,7 @@ fn groupCancel(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void |
| 801 | var node: *std.SinglyLinkedList.Node = @ptrCast(@alignCast(token)); | 1359 | var node: *std.SinglyLinkedList.Node = @ptrCast(@alignCast(token)); |
| 802 | while (true) { | 1360 | while (true) { |
| 803 | const gc: *GroupClosure = @fieldParentPtr("node", node); | 1361 | const gc: *GroupClosure = @fieldParentPtr("node", node); |
| 804 | gc.closure.requestCancel(); | 1362 | gc.closure.requestCancel(t); |
| 805 | node = node.next orelse break; | 1363 | node = node.next orelse break; |
| 806 | } | 1364 | } |
| 807 | } | 1365 | } |
| ... | @@ -844,21 +1402,10 @@ fn cancel( | ... | @@ -844,21 +1402,10 @@ fn cancel( |
| 844 | _ = result_alignment; | 1402 | _ = result_alignment; |
| 845 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 1403 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 846 | const ac: *AsyncClosure = @ptrCast(@alignCast(any_future)); | 1404 | const ac: *AsyncClosure = @ptrCast(@alignCast(any_future)); |
| 847 | ac.closure.requestCancel(); | 1405 | ac.closure.requestCancel(t); |
| 848 | ac.waitAndDeinit(t.allocator, result); | 1406 | ac.waitAndDeinit(t.allocator, result); |
| 849 | } | 1407 | } |
| 850 | | 1408 | |
| 851 | fn cancelRequested(userdata: ?*anyopaque) bool { | | |
| 852 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | | |
| 853 | _ = t; | | |
| 854 | const closure = current_closure orelse return false; | | |
| 855 | return @atomicLoad(CancelId, &closure.cancel_tid, .acquire) == .canceling; | | |
| 856 | } | | |
| 857 | | | |
| 858 | fn checkCancel(t: *Threaded) error{Canceled}!void { | | |
| 859 | if (cancelRequested(t)) return error.Canceled; | | |
| 860 | } | | |
| 861 | | | |
| 862 | fn mutexLock(userdata: ?*anyopaque, prev_state: Io.Mutex.State, mutex: *Io.Mutex) Io.Cancelable!void { | 1409 | fn mutexLock(userdata: ?*anyopaque, prev_state: Io.Mutex.State, mutex: *Io.Mutex) Io.Cancelable!void { |
| 863 | if (builtin.single_threaded) unreachable; // Interface should have prevented this. | 1410 | if (builtin.single_threaded) unreachable; // Interface should have prevented this. |
| 864 | if (native_os == .netbsd) @panic("TODO"); | 1411 | if (native_os == .netbsd) @panic("TODO"); |
| ... | @@ -1043,35 +1590,47 @@ const dirMake = switch (native_os) { | ... | @@ -1043,35 +1590,47 @@ const dirMake = switch (native_os) { |
| 1043 | | 1590 | |
| 1044 | fn dirMakePosix(userdata: ?*anyopaque, dir: Io.Dir, sub_path: []const u8, mode: Io.Dir.Mode) Io.Dir.MakeError!void { | 1591 | fn dirMakePosix(userdata: ?*anyopaque, dir: Io.Dir, sub_path: []const u8, mode: Io.Dir.Mode) Io.Dir.MakeError!void { |
| 1045 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 1592 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| | 1593 | _ = t; |
| | 1594 | const current_thread = Thread.current; |
| 1046 | | 1595 | |
| 1047 | var path_buffer: [posix.PATH_MAX]u8 = undefined; | 1596 | var path_buffer: [posix.PATH_MAX]u8 = undefined; |
| 1048 | const sub_path_posix = try pathToPosix(sub_path, &path_buffer); | 1597 | const sub_path_posix = try pathToPosix(sub_path, &path_buffer); |
| 1049 | | 1598 | |
| | 1599 | try current_thread.beginSyscall(); |
| 1050 | while (true) { | 1600 | while (true) { |
| 1051 | try t.checkCancel(); | | |
| 1052 | switch (posix.errno(posix.system.mkdirat(dir.handle, sub_path_posix, mode))) { | 1601 | switch (posix.errno(posix.system.mkdirat(dir.handle, sub_path_posix, mode))) { |
| 1053 | .SUCCESS => return, | 1602 | .SUCCESS => { |
| 1054 | .INTR => continue, | 1603 | try current_thread.endSyscall(); |
| 1055 | .CANCELED => return error.Canceled, | 1604 | break; |
| 1056 | | 1605 | }, |
| 1057 | .ACCES => return error.AccessDenied, | 1606 | .INTR => { |
| 1058 | .BADF => |err| return errnoBug(err), // File descriptor used after closed. | 1607 | try current_thread.checkCancel(); |
| 1059 | .PERM => return error.PermissionDenied, | 1608 | continue; |
| 1060 | .DQUOT => return error.DiskQuota, | 1609 | }, |
| 1061 | .EXIST => return error.PathAlreadyExists, | 1610 | else => |e| { |
| 1062 | .FAULT => |err| return errnoBug(err), | 1611 | try current_thread.endSyscall(); |
| 1063 | .LOOP => return error.SymLinkLoop, | 1612 | switch (e) { |
| 1064 | .MLINK => return error.LinkQuotaExceeded, | 1613 | .CANCELED => return error.Canceled, |
| 1065 | .NAMETOOLONG => return error.NameTooLong, | 1614 | .ACCES => return error.AccessDenied, |
| 1066 | .NOENT => return error.FileNotFound, | 1615 | .BADF => |err| return errnoBug(err), // File descriptor used after closed. |
| 1067 | .NOMEM => return error.SystemResources, | 1616 | .PERM => return error.PermissionDenied, |
| 1068 | .NOSPC => return error.NoSpaceLeft, | 1617 | .DQUOT => return error.DiskQuota, |
| 1069 | .NOTDIR => return error.NotDir, | 1618 | .EXIST => return error.PathAlreadyExists, |
| 1070 | .ROFS => return error.ReadOnlyFileSystem, | 1619 | .FAULT => |err| return errnoBug(err), |
| 1071 | // dragonfly: when dir_fd is unlinked from filesystem | 1620 | .LOOP => return error.SymLinkLoop, |
| 1072 | .NOTCONN => return error.FileNotFound, | 1621 | .MLINK => return error.LinkQuotaExceeded, |
| 1073 | .ILSEQ => return error.BadPathName, | 1622 | .NAMETOOLONG => return error.NameTooLong, |
| 1074 | else => |err| return posix.unexpectedErrno(err), | 1623 | .NOENT => return error.FileNotFound, |
| | 1624 | .NOMEM => return error.SystemResources, |
| | 1625 | .NOSPC => return error.NoSpaceLeft, |
| | 1626 | .NOTDIR => return error.NotDir, |
| | 1627 | .ROFS => return error.ReadOnlyFileSystem, |
| | 1628 | // dragonfly: when dir_fd is unlinked from filesystem |
| | 1629 | .NOTCONN => return error.FileNotFound, |
| | 1630 | .ILSEQ => return error.BadPathName, |
| | 1631 | else => |err| return posix.unexpectedErrno(err), |
| | 1632 | } |
| | 1633 | }, |
| 1075 | } | 1634 | } |
| 1076 | } | 1635 | } |
| 1077 | } | 1636 | } |
| ... | @@ -1981,6 +2540,7 @@ fn dirOpenFilePosix( | ... | @@ -1981,6 +2540,7 @@ fn dirOpenFilePosix( |
| 1981 | flags: Io.File.OpenFlags, | 2540 | flags: Io.File.OpenFlags, |
| 1982 | ) Io.File.OpenError!Io.File { | 2541 | ) Io.File.OpenError!Io.File { |
| 1983 | const t: *Threaded = @ptrCast(@alignCast(userdata)); | 2542 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| | 2543 | const current_thread = Thread.current; |
| 1984 | | 2544 | |
| 1985 | var path_buffer: [posix.PATH_MAX]u8 = undefined; | 2545 | var path_buffer: [posix.PATH_MAX]u8 = undefined; |
| 1986 | const sub_path_posix = try pathToPosix(sub_path, &path_buffer); | 2546 | const sub_path_posix = try pathToPosix(sub_path, &path_buffer); |
| ... | @@ -2017,40 +2577,52 @@ fn dirOpenFilePosix( | ... | @@ -2017,40 +2577,52 @@ fn dirOpenFilePosix( |
| 2017 | }, | 2577 | }, |
| 2018 | }; | 2578 | }; |
| 2019 | | 2579 | |
| 2020 | const fd: posix.fd_t = while (true) { | 2580 | try current_thread.beginSyscall(); |
| 2021 | try t.checkCancel(); | 2581 | const fd = while (true) { |
| 2022 | const rc = openat_sym(dir.handle, sub_path_posix, os_flags, @as(posix.mode_t, 0)); | 2582 | const rc = openat_sym(dir.handle, sub_path_posix, os_flags, @as(posix.mode_t, 0)); |
| 2023 | switch (posix.errno(rc)) { | 2583 | switch (posix.errno(rc)) { |
| 2024 | .SUCCESS => break @intCast(rc), | 2584 | .SUCCESS => { |
| 2025 | .INTR => continue, | 2585 | const fd: posix.fd_t = @intCast(rc); |
| 2026 | .CANCELED => return error.Canceled, | 2586 | errdefer posix.close(fd); |
| 2027 | | 2587 | try current_thread.endSyscall(); |
| 2028 | .FAULT => |err| return errnoBug(err), | 2588 | break fd; |
| 2029 | .INVAL => return error.BadPathName, | 2589 | }, |
| 2030 | .BADF => |err| return errnoBug(err), // File descriptor used after closed. | 2590 | .INTR => { |
| 2031 | .ACCES => return error.AccessDenied, | 2591 | try current_thread.checkCancel(); |
| 2032 | .FBIG => return error.FileTooBig, | 2592 | continue; |
| 2033 | .OVERFLOW => return error.FileTooBig, | 2593 | }, |
| 2034 | .ISDIR => return error.IsDir, | 2594 | else => |e| { |
| 2035 | .LOOP => return error.SymLinkLoop, | 2595 | try current_thread.endSyscall(); |
| 2036 | .MFILE => return error.ProcessFdQuotaExceeded, | 2596 | switch (e) { |
| 2037 | .NAMETOOLONG => return error.NameTooLong, | 2597 | .CANCELED => return error.Canceled, |
| 2038 | .NFILE => return error.SystemFdQuotaExceeded, | 2598 | .FAULT => |err| return errnoBug(err), |
| 2039 | .NODEV => return error.NoDevice, | 2599 | .INVAL => return error.BadPathName, |
| 2040 | .NOENT => return error.FileNotFound, | 2600 | .BADF => |err| return errnoBug(err), // File descriptor used after closed. |
| 2041 | .SRCH => return error.ProcessNotFound, | 2601 | .ACCES => return error.AccessDenied, |
| 2042 | .NOMEM => return error.SystemResources, | 2602 | .FBIG => return error.FileTooBig, |
| 2043 | .NOSPC => return error.NoSpaceLeft, | 2603 | .OVERFLOW => return error.FileTooBig, |
| 2044 | .NOTDIR => return error.NotDir, | 2604 | .ISDIR => return error.IsDir, |
| 2045 | .PERM => return error.PermissionDenied, | 2605 | .LOOP => return error.SymLinkLoop, |
| 2046 | .EXIST => return error.PathAlreadyExists, | 2606 | .MFILE => return error.ProcessFdQuotaExceeded, |
| 2047 | .BUSY => return error.DeviceBusy, | 2607 | .NAMETOOLONG => return error.NameTooLong, |
| 2048 | .OPNOTSUPP => return error.FileLocksNotSupported, | 2608 | .NFILE => return error.SystemFdQuotaExceeded, |
| 2049 | .AGAIN => return error.WouldBlock, | 2609 | .NODEV => return error.NoDevice, |
| 2050 | .TXTBSY => return error.FileBusy, | 2610 | .NOENT => return error.FileNotFound, |
| 2051 | .NXIO => return error.NoDevice, | 2611 | .SRCH => return error.ProcessNotFound, |
| 2052 | .ILSEQ => return error.BadPathName, | 2612 | .NOMEM => return error.SystemResources, |
| 2053 | else => |err| return posix.unexpectedErrno(err), | 2613 | .NOSPC => return error.NoSpaceLeft, |
| | 2614 | .NOTDIR => return error.NotDir, |
| | 2615 | .PERM => return error.PermissionDenied, |
| | 2616 | .EXIST => return error.PathAlreadyExists, |
| | 2617 | .BUSY => return error.DeviceBusy, |
| | 2618 | .OPNOTSUPP => return error.FileLocksNotSupported, |
| | 2619 | .AGAIN => return error.WouldBlock, |
| | 2620 | .TXTBSY => return error.FileBusy, |
| | 2621 | .NXIO => return error.NoDevice, |
| | 2622 | .ILSEQ => return error.BadPathName, |
| | 2623 | else => |err| return posix.unexpectedErrno(err), |
| | 2624 | } |
| | 2625 | }, |
| 2054 | } | 2626 | } |
| 2055 | }; | 2627 | }; |
| 2056 | errdefer posix.close(fd); | 2628 | errdefer posix.close(fd); |
| ... | @@ -6208,6 +6780,7 @@ fn initializeWsa(t: *Threaded) error{NetworkDown}!void { | ... | @@ -6208,6 +6780,7 @@ fn initializeWsa(t: *Threaded) error{NetworkDown}!void { |
| 6208 | | 6780 | |
| 6209 | fn doNothingSignalHandler(_: posix.SIG) callconv(.c) void {} | 6781 | fn doNothingSignalHandler(_: posix.SIG) callconv(.c) void {} |
| 6210 | | 6782 | |
| | 6783 | |
| 6211 | test { | 6784 | test { |
| 6212 | _ = @import("Threaded/test.zig"); | 6785 | _ = @import("Threaded/test.zig"); |
| 6213 | } | 6786 | } |