From 7b323f84ca876c86bbe06f132d5a5d3775def3a2 Mon Sep 17 00:00:00 2001 From: kprotty Date: Sat, 26 Jun 2021 13:00:54 -0500 Subject: [PATCH] std.Thread: more fixes --- lib/std/Thread.zig | 40 ++++++++++++++++++++++++++++++++++++++-- lib/std/Thread/Futex.zig | 25 ++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index db049b5d21..cbdcf00e9f 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -45,7 +45,7 @@ else if (use_pthreads) else if (target.os.tag == .linux) LinuxThreadImpl else - @compileLog("Unsupported operating system", target.os.tag); + UnsupportedImpl; impl: Impl, @@ -200,6 +200,40 @@ fn callFn(comptime f: anytype, args: anytype) switch (Impl) { } } +const UnsupportedImpl = struct { + pub const ThreadHandle = void; + + fn getCurrentId() u64 { + return unsupported({}); + } + + fn getCpuCount() !usize { + return unsupported({}); + } + + fn spawn(config: SpawnConfig, comptime f: anytype, args: anytype) !Impl { + return unsupported(.{config, f, args}); + } + + fn getHandle(self: Impl) ThreadHandle { + return unsupported(self); + } + + fn detach(self: Impl) void { + return unsupported(self); + } + + fn join(self: Impl) void { + return unsupported(self); + } + + fn unsupported(unusued: anytype) noreturn { + @compileLog("Unsupported operating system", target.os.tag); + _ = unusued; + unreachable; + } +}; + const WindowsThreadImpl = struct { const windows = os.windows; @@ -725,7 +759,9 @@ const LinuxThreadImpl = struct { \\ li a7, 93 \\ ecall ), - else => @compileError("Platform not supported"), + else => |cpu_arch| { + @compileLog("linux arch", cpu_arch, "is not supported"); + }, }); } } diff --git a/lib/std/Thread/Futex.zig b/lib/std/Thread/Futex.zig index b1b2128caa..de7dd5e73b 100644 --- a/lib/std/Thread/Futex.zig +++ b/lib/std/Thread/Futex.zig @@ -64,10 +64,9 @@ pub fn wait(ptr: *const Atomic(u32), expect: u32, timeout: ?u64) error{TimedOut} /// Unblocks at most `num_waiters` callers blocked in a `wait()` call on `ptr`. /// `num_waiters` of 1 unblocks at most one `wait(ptr, ...)` and `maxInt(u32)` unblocks effectively all `wait(ptr, ...)`. pub fn wake(ptr: *const Atomic(u32), num_waiters: u32) void { - if (num_waiters == 0 or single_threaded) { - return; - } - + if (single_threaded) return; + if (num_waiters == 0) return; + return OsFutex.wake(ptr, num_waiters); } @@ -80,7 +79,23 @@ else if (target.isDarwin()) else if (std.builtin.link_libc) PosixFutex else - @compileError("Operating System unsupported"); + UnsupportedFutex; + +const UnsupportedFutex = struct { + fn wait(ptr: *const Atomic(u32), expect: u32, timeout: ?u64) error{TimedOut}!void { + return unsupported(.{ptr, expect, timeout}); + } + + fn wake(ptr: *const Atomic(u32), num_waiters: u32) void { + return unsupported(.{ptr, num_waiters}); + } + + fn unsupported(unused: anytype) noreturn { + @compileLog("Unsupported operating system", target.os.tag); + _ = unused; + unreachable; + } +}; const WindowsFutex = struct { const windows = std.os.windows;