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-09 15:52:58-07:00
logffafcac41573a73f613d9cd2f241d7217a9cca16
tree212778d04b01477244a7dafac983d36832812334
parent5ad627a53173fd14e36f189767fabdda48e9f294

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 {...@@ -1102,6 +1102,33 @@ const Thread = struct {
1102 else => unreachable,1102 else => unreachable,
1103 };1103 };
1104 },1104 },
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 },
1105 else => @compileError("unimplemented: futexWait"),1132 else => @compileError("unimplemented: futexWait"),
1106 }1133 }
1107 }1134 }
...@@ -1167,12 +1194,12 @@ const Thread = struct {...@@ -1167,12 +1194,12 @@ const Thread = struct {
1167 0, // there is no timeout struct1194 0, // there is no timeout struct
1168 0, // there is no timeout struct pointer1195 0, // there is no timeout struct pointer
1169 );1196 );
1170 switch (posix.errno(rc)) {1197 if (is_debug) switch (posix.errno(rc)) {
1171 .SUCCESS => {},1198 .SUCCESS => {},
1172 .FAULT => {}, // it's ok if the ptr doesn't point to valid memory1199 .FAULT => {}, // it's ok if the ptr doesn't point to valid memory
1173 .INVAL => unreachable, // arguments should be correct1200 .INVAL => unreachable, // arguments should be correct
1174 else => unreachable, // deadlock due to operating system bug1201 else => unreachable, // deadlock due to operating system bug
1175 }1202 };
1176 },1203 },
1177 .openbsd => {1204 .openbsd => {
1178 const rc = std.c.futex(1205 const rc = std.c.futex(
...@@ -1191,6 +1218,22 @@ const Thread = struct {...@@ -1191,6 +1218,22 @@ const Thread = struct {
1191 @min(max_waiters, std.math.maxInt(c_int)),1218 @min(max_waiters, std.math.maxInt(c_int)),
1192 );1219 );
1193 },1220 },
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 },
1194 else => @compileError("unimplemented: futexWake"),1237 else => @compileError("unimplemented: futexWake"),
1195 }1238 }
1196 }1239 }