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 {...@@ -350,6 +350,26 @@ pub fn join(self: Thread) void {
350 return self.impl.join();350 return self.impl.join();
351}351}
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
353/// State to synchronize detachment of spawner thread to spawned thread373/// State to synchronize detachment of spawner thread to spawned thread
354const Completion = Atomic(enum(u8) {374const Completion = Atomic(enum(u8) {
355 running,375 running,
lib/std/Thread/StaticResetEvent.zig+2-2
...@@ -181,7 +181,7 @@ pub const AtomicEvent = struct {...@@ -181,7 +181,7 @@ pub const AtomicEvent = struct {
181 timer = time.Timer.start() catch return error.TimedOut;181 timer = time.Timer.start() catch return error.TimedOut;
182182
183 while (@atomicLoad(u32, waiters, .Acquire) != WAKE) {183 while (@atomicLoad(u32, waiters, .Acquire) != WAKE) {
184 std.os.sched_yield() catch std.atomic.spinLoopHint();184 std.Thread.yield() catch std.atomic.spinLoopHint();
185 if (timeout) |timeout_ns| {185 if (timeout) |timeout_ns| {
186 if (timer.read() >= timeout_ns)186 if (timer.read() >= timeout_ns)
187 return error.TimedOut;187 return error.TimedOut;
...@@ -293,7 +293,7 @@ pub const AtomicEvent = struct {...@@ -293,7 +293,7 @@ pub const AtomicEvent = struct {
293 return @intToPtr(?windows.HANDLE, handle);293 return @intToPtr(?windows.HANDLE, handle);
294 },294 },
295 LOADING => {295 LOADING => {
296 std.os.sched_yield() catch std.atomic.spinLoopHint();296 std.Thread.yield() catch std.atomic.spinLoopHint();
297 handle = @atomicLoad(usize, &event_handle, .Monotonic);297 handle = @atomicLoad(usize, &event_handle, .Monotonic);
298 },298 },
299 else => {299 else => {
lib/std/os.zig-19
...@@ -6067,25 +6067,6 @@ pub fn dn_expand(...@@ -6067,25 +6067,6 @@ pub fn dn_expand(
6067 return error.InvalidDnsPacket;6067 return error.InvalidDnsPacket;
6068}6068}
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
6089pub const SetSockOptError = error{6070pub const SetSockOptError = error{
6090 /// The socket is already connected, and a specified option cannot be set while the socket is connected.6071 /// The socket is already connected, and a specified option cannot be set while the socket is connected.
6091 AlreadyConnected,6072 AlreadyConnected,