Skip to content

Commit ff4beff

Browse files
Add test for futex bucket collision handling
Test verifies that wake() correctly discriminates between different addresses that hash to the same bucket. The test finds two addresses in the same bucket and verifies only the targeted address is woken. Co-authored-by: Lukáš Lalinský <[email protected]>
1 parent a7d1c86 commit ff4beff

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

‎src/sync/Futex.zig‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,79 @@ test "Futex: multiple waiters different addresses" {
368368
// and was canceled by the timeout
369369
try std.testing.expectEqual(1, woken);
370370
}
371+
372+
test "Futex: same bucket different addresses" {
373+
const Main = struct {
374+
fn waiterFunc(io: *Runtime, v: *u32, w: *u32) !void {
375+
while (@atomicLoad(u32, v, .acquire) == 0) {
376+
try Futex.wait(io, v, 0);
377+
}
378+
_ = @atomicRmw(u32, w, .Add, 1, .monotonic);
379+
}
380+
381+
fn wakerFunc(io: *Runtime, val: *u32) !void {
382+
@atomicStore(u32, val, 1, .release);
383+
Futex.wake(io, val, 1);
384+
}
385+
386+
fn run(io: *Runtime, w: *u32) !void {
387+
// Allocate array of values to find addresses in the same bucket
388+
var values = try std.testing.allocator.alloc(u32, 2000);
389+
defer std.testing.allocator.free(values);
390+
@memset(values, 0);
391+
392+
// Find two addresses that hash to the same bucket
393+
const addr1: *u32 = &values[0];
394+
var addr2: *u32 = &values[1];
395+
const bucket1 = getBucket(&io.futex_table, @intFromPtr(addr1));
396+
397+
var found = false;
398+
for (values[1..]) |*val| {
399+
const bucket2 = getBucket(&io.futex_table, @intFromPtr(val));
400+
if (bucket1 == bucket2) {
401+
addr2 = val;
402+
found = true;
403+
break;
404+
}
405+
}
406+
407+
// Verify we found two addresses in the same bucket
408+
try std.testing.expect(found);
409+
try std.testing.expect(addr1 != addr2);
410+
411+
// Spawn two waiters, each on a different address (but same bucket)
412+
var waiter1 = try io.spawn(waiterFunc, .{ io, addr1, w }, .{});
413+
defer waiter1.cancel(io);
414+
415+
var waiter2 = try io.spawn(waiterFunc, .{ io, addr2, w }, .{});
416+
defer waiter2.cancel(io);
417+
418+
// Wake only addr1
419+
var waker = try io.spawn(wakerFunc, .{ io, addr1 }, .{});
420+
try waker.join(io);
421+
422+
var timeout: Timeout = .init;
423+
defer timeout.clear(io);
424+
timeout.set(io, 10);
425+
426+
// waiter1 should complete successfully
427+
try waiter1.join(io);
428+
429+
// waiter2 should timeout since it's on addr2 which was never woken
430+
waiter2.join(io) catch |err| {
431+
return if (err == error.Canceled) {} else err;
432+
};
433+
}
434+
};
435+
436+
const rt = try Runtime.init(std.testing.allocator, .{});
437+
defer rt.deinit();
438+
439+
var woken: u32 = 0;
440+
441+
var task = try rt.spawn(Main.run, .{ rt, &woken }, .{});
442+
task.join(rt) catch {};
443+
444+
// Only waiter1 should have woken - waiter2 was on a different address in same bucket
445+
try std.testing.expectEqual(1, woken);
446+
}

0 commit comments

Comments
 (0)