authorgravatar for dave@qword.spaceDavid John <dave@qword.space> 2022-02-25 16:52:25+05:30
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-27 15:34:02-05:00
log139b731d82d0b851c8fb2e6dbb48b735e63eecd1
tree93af126a3973c52c6b4fd039da8940f1d859a8b8
parent104a8840dbc0a09ce5e0035470052354a98693f1

std: rename `sched_yield` to `yield` and move it to `std.Thread`


3 files changed, 22 insertions(+), 21 deletions(-)

lib/std/Thread.zig+20
......@@ -350,6 +350,26 @@ pub fn join(self: Thread) void {
350350 return self.impl.join();
351351}
352352
353pub const YieldError = error{
354 /// The system is not configured to allow yielding
355 SystemCannotYield,
356};
357
358/// Yields the current thread potentially allowing other threads to run.
359pub fn yield() YieldError!void {
360 if (builtin.os.tag == .windows) {
361 // The return value has to do with how many other threads there are; it is not
362 // an error condition on Windows.
363 _ = os.windows.kernel32.SwitchToThread();
364 return;
365 }
366 switch (os.errno(os.system.sched_yield())) {
367 .SUCCESS => return,
368 .NOSYS => return error.SystemCannotYield,
369 else => return error.SystemCannotYield,
370 }
371}
372
353373/// State to synchronize detachment of spawner thread to spawned thread
354374const Completion = Atomic(enum(u8) {
355375 running,
lib/std/Thread/StaticResetEvent.zig+2-2
......@@ -181,7 +181,7 @@ pub const AtomicEvent = struct {
181181 timer = time.Timer.start() catch return error.TimedOut;
182182
183183 while (@atomicLoad(u32, waiters, .Acquire) != WAKE) {
184 std.os.sched_yield() catch std.atomic.spinLoopHint();
184 std.Thread.yield() catch std.atomic.spinLoopHint();
185185 if (timeout) |timeout_ns| {
186186 if (timer.read() >= timeout_ns)
187187 return error.TimedOut;
......@@ -293,7 +293,7 @@ pub const AtomicEvent = struct {
293293 return @intToPtr(?windows.HANDLE, handle);
294294 },
295295 LOADING => {
296 std.os.sched_yield() catch std.atomic.spinLoopHint();
296 std.Thread.yield() catch std.atomic.spinLoopHint();
297297 handle = @atomicLoad(usize, &event_handle, .Monotonic);
298298 },
299299 else => {
lib/std/os.zig-19
......@@ -6067,25 +6067,6 @@ pub fn dn_expand(
60676067 return error.InvalidDnsPacket;
60686068}
60696069
6070pub const SchedYieldError = error{
6071 /// The system is not configured to allow yielding
6072 SystemCannotYield,
6073};
6074
6075pub fn sched_yield() SchedYieldError!void {
6076 if (builtin.os.tag == .windows) {
6077 // The return value has to do with how many other threads there are; it is not
6078 // an error condition on Windows.
6079 _ = windows.kernel32.SwitchToThread();
6080 return;
6081 }
6082 switch (errno(system.sched_yield())) {
6083 .SUCCESS => return,
6084 .NOSYS => return error.SystemCannotYield,
6085 else => return error.SystemCannotYield,
6086 }
6087}
6088
60896070pub const SetSockOptError = error{
60906071 /// The socket is already connected, and a specified option cannot be set while the socket is connected.
60916072 AlreadyConnected,