diff options
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Thread.zig | 20 | ||||
| -rw-r--r-- | lib/std/Thread/StaticResetEvent.zig | 4 | ||||
| -rw-r--r-- | lib/std/os.zig | 19 |
3 files changed, 22 insertions, 21 deletions
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index 60bbe2c09c..e28471d6b3 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -350,6 +350,26 @@ pub fn join(self: Thread) void { return self.impl.join(); } +pub const YieldError = error{ + /// The system is not configured to allow yielding + SystemCannotYield, +}; + +/// Yields the current thread potentially allowing other threads to run. +pub fn yield() YieldError!void { + if (builtin.os.tag == .windows) { + // The return value has to do with how many other threads there are; it is not + // an error condition on Windows. + _ = os.windows.kernel32.SwitchToThread(); + return; + } + switch (os.errno(os.system.sched_yield())) { + .SUCCESS => return, + .NOSYS => return error.SystemCannotYield, + else => return error.SystemCannotYield, + } +} + /// State to synchronize detachment of spawner thread to spawned thread const Completion = Atomic(enum(u8) { running, diff --git a/lib/std/Thread/StaticResetEvent.zig b/lib/std/Thread/StaticResetEvent.zig index 80789cefec..fcd13f6ac7 100644 --- a/lib/std/Thread/StaticResetEvent.zig +++ b/lib/std/Thread/StaticResetEvent.zig @@ -181,7 +181,7 @@ pub const AtomicEvent = struct { timer = time.Timer.start() catch return error.TimedOut; while (@atomicLoad(u32, waiters, .Acquire) != WAKE) { - std.os.sched_yield() catch std.atomic.spinLoopHint(); + std.Thread.yield() catch std.atomic.spinLoopHint(); if (timeout) |timeout_ns| { if (timer.read() >= timeout_ns) return error.TimedOut; @@ -293,7 +293,7 @@ pub const AtomicEvent = struct { return @intToPtr(?windows.HANDLE, handle); }, LOADING => { - std.os.sched_yield() catch std.atomic.spinLoopHint(); + std.Thread.yield() catch std.atomic.spinLoopHint(); handle = @atomicLoad(usize, &event_handle, .Monotonic); }, else => { diff --git a/lib/std/os.zig b/lib/std/os.zig index 3567420a51..3e230b773d 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -6067,25 +6067,6 @@ pub fn dn_expand( return error.InvalidDnsPacket; } -pub const SchedYieldError = error{ - /// The system is not configured to allow yielding - SystemCannotYield, -}; - -pub fn sched_yield() SchedYieldError!void { - if (builtin.os.tag == .windows) { - // The return value has to do with how many other threads there are; it is not - // an error condition on Windows. - _ = windows.kernel32.SwitchToThread(); - return; - } - switch (errno(system.sched_yield())) { - .SUCCESS => return, - .NOSYS => return error.SystemCannotYield, - else => return error.SystemCannotYield, - } -} - pub const SetSockOptError = error{ /// The socket is already connected, and a specified option cannot be set while the socket is connected. AlreadyConnected, |
