authorgravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2026-10-03 15:16:49+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 00:19:51+02:00
log1fcc9cbe3d040ce102739760cc14476ec6726bbe
tree6c1bfe30b55cf83026dadb8eaf0fb68fdb056006
parent6ec81be545eab6a23c164ca045608d9c41b63535

std.Io.Threaded: Implement futexWait/futexWake for serenity

Taken from the downstream patch with slight modifications: https://github.com/SerenityOS/serenity/blob/3d85104ef2ad231f31ec3de703f24306ea2aa0bf/Ports/zig/patches/0005-Extend-support-for-SerenityOS-target.patch#L19-L73

1 files changed, 45 insertions(+), 2 deletions(-)

lib/std/Io/Threaded.zig+45-2
......@@ -1102,6 +1102,33 @@ const Thread = struct {
11021102 else => unreachable,
11031103 };
11041104 },
1105 .serenity => {
1106 var tm: std.c.timespec = undefined;
1107 var tm_ptr: ?*const std.c.timespec = null;
1108 if (timeout_ns) |ns| {
1109 tm_ptr = &tm;
1110 tm = timestampToPosix(ns);
1111 }
1112 const syscall: Syscall = if (uncancelable) .{ .thread = null } else try .start();
1113 const rc = std.c.futex(
1114 @constCast(ptr),
1115 std.c.FUTEX.WAIT | std.c.FUTEX.PRIVATE_FLAG,
1116 expect,
1117 tm_ptr,
1118 null,
1119 0,
1120 );
1121 syscall.finish();
1122 if (is_debug) switch (posix.errno(rc)) {
1123 .SUCCESS => {},
1124 .AGAIN => {}, // ptr != expect
1125 .TIMEDOUT => {}, // timeout
1126 .FAULT => unreachable, // ptr was invalid
1127 .INVAL => unreachable, // ptr was misaligned
1128 .NOMEM => {}, // OOM, treat as spurious wake
1129 else => unreachable,
1130 };
1131 },
11051132 else => @compileError("unimplemented: futexWait"),
11061133 }
11071134 }
......@@ -1167,12 +1194,12 @@ const Thread = struct {
11671194 0, // there is no timeout struct
11681195 0, // there is no timeout struct pointer
11691196 );
1170 switch (posix.errno(rc)) {
1197 if (is_debug) switch (posix.errno(rc)) {
11711198 .SUCCESS => {},
11721199 .FAULT => {}, // it's ok if the ptr doesn't point to valid memory
11731200 .INVAL => unreachable, // arguments should be correct
11741201 else => unreachable, // deadlock due to operating system bug
1175 }
1202 };
11761203 },
11771204 .openbsd => {
11781205 const rc = std.c.futex(
......@@ -1191,6 +1218,22 @@ const Thread = struct {
11911218 @min(max_waiters, std.math.maxInt(c_int)),
11921219 );
11931220 },
1221 .serenity => {
1222 const rc = std.c.futex(
1223 @constCast(ptr),
1224 std.c.FUTEX.WAKE | std.c.FUTEX.PRIVATE_FLAG,
1225 max_waiters,
1226 null,
1227 null,
1228 0,
1229 );
1230 if (is_debug) switch (posix.errno(rc)) {
1231 .SUCCESS => {},
1232 .FAULT => {}, // it's ok if the ptr doesn't point to valid memory
1233 .INVAL => unreachable, // arguments should be correct
1234 else => unreachable,
1235 };
1236 },
11941237 else => @compileError("unimplemented: futexWake"),
11951238 }
11961239 }