aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Thread.zig
diff options
context:
space:
mode:
authorDavid John <dave@qword.space>2022-02-25 16:52:25 +0530
committerAndrew Kelley <andrew@ziglang.org>2022-02-27 15:34:02 -0500
commit139b731d82d0b851c8fb2e6dbb48b735e63eecd1 (patch)
tree93af126a3973c52c6b4fd039da8940f1d859a8b8 /lib/std/Thread.zig
parent104a8840dbc0a09ce5e0035470052354a98693f1 (diff)
downloadzig-139b731d82d0b851c8fb2e6dbb48b735e63eecd1.tar.gz
zig-139b731d82d0b851c8fb2e6dbb48b735e63eecd1.zip
std: rename `sched_yield` to `yield` and move it to `std.Thread`
Diffstat (limited to 'lib/std/Thread.zig')
-rw-r--r--lib/std/Thread.zig20
1 files changed, 20 insertions, 0 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,