authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-04-01 02:23:41-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-02 16:30:59-07:00
logc8950b5dd549ffe2ead1979528e33c7a94540b8c
tree5fe94c7410ea933ae24fdaad0ba67bcbc8a1dd35
parent5952fc2c73993e1356e09ec4425f0ff6f99309f8

EventLoop: fix `std.Io.Condition` implementation

1. a fiber can't put itself on a queue that allows it to be rescheduled 2. allow the idle fiber to unlock a mutex held by another fiber by ignoring reschedule requests originating from the idle fiber

1 files changed, 23 insertions(+), 16 deletions(-)

lib/std/Io/EventLoop.zig+23-16
......@@ -380,8 +380,7 @@ fn schedule(el: *EventLoop, thread: *Thread, ready_queue: Fiber.Queue) void {
380380
381381fn mainIdle(el: *EventLoop, message: *const SwitchMessage) callconv(.withStackAlign(.c, @max(@alignOf(Thread), @alignOf(Context)))) noreturn {
382382 message.handle(el);
383 const thread: *Thread = &el.threads.allocated[0];
384 el.idle(thread);
383 el.idle(&el.threads.allocated[0]);
385384 el.yield(@ptrCast(&el.main_fiber_buffer), .nothing);
386385 unreachable; // switched to dead fiber
387386}
......@@ -480,10 +479,14 @@ const SwitchMessage = struct {
480479 reschedule,
481480 recycle: *Fiber,
482481 register_awaiter: *?*Fiber,
483 lock_mutex: struct {
482 mutex_lock: struct {
484483 prev_state: Io.Mutex.State,
485484 mutex: *Io.Mutex,
486485 },
486 condition_wait: struct {
487 cond: *Io.Condition,
488 mutex: *Io.Mutex,
489 },
487490 exit,
488491 };
489492
......@@ -492,7 +495,7 @@ const SwitchMessage = struct {
492495 thread.current_context = message.contexts.ready;
493496 switch (message.pending_task) {
494497 .nothing => {},
495 .reschedule => {
498 .reschedule => if (message.contexts.prev != &thread.idle_context) {
496499 const prev_fiber: *Fiber = @alignCast(@fieldParentPtr("context", message.contexts.prev));
497500 assert(prev_fiber.queue_next == null);
498501 el.schedule(thread, .{ .head = prev_fiber, .tail = prev_fiber });
......@@ -511,16 +514,16 @@ const SwitchMessage = struct {
511514 .acq_rel,
512515 ) == Fiber.finished) el.schedule(thread, .{ .head = prev_fiber, .tail = prev_fiber });
513516 },
514 .lock_mutex => |lock_mutex| {
517 .mutex_lock => |mutex_lock| {
515518 const prev_fiber: *Fiber = @alignCast(@fieldParentPtr("context", message.contexts.prev));
516519 assert(prev_fiber.queue_next == null);
517 var prev_state = lock_mutex.prev_state;
520 var prev_state = mutex_lock.prev_state;
518521 while (switch (prev_state) {
519522 else => next_state: {
520523 prev_fiber.queue_next = @ptrFromInt(@intFromEnum(prev_state));
521524 break :next_state @cmpxchgWeak(
522525 Io.Mutex.State,
523 &lock_mutex.mutex.state,
526 &mutex_lock.mutex.state,
524527 prev_state,
525528 @enumFromInt(@intFromPtr(prev_fiber)),
526529 .release,
......@@ -529,7 +532,7 @@ const SwitchMessage = struct {
529532 },
530533 .unlocked => @cmpxchgWeak(
531534 Io.Mutex.State,
532 &lock_mutex.mutex.state,
535 &mutex_lock.mutex.state,
533536 .unlocked,
534537 .locked_once,
535538 .acquire,
......@@ -541,6 +544,13 @@ const SwitchMessage = struct {
541544 },
542545 }) |next_state| prev_state = next_state;
543546 },
547 .condition_wait => |condition_wait| {
548 const prev_fiber: *Fiber = @alignCast(@fieldParentPtr("context", message.contexts.prev));
549 assert(prev_fiber.queue_next == null);
550 const cond_state: *?*Fiber = @ptrCast(&condition_wait.cond.state);
551 assert(@atomicRmw(?*Fiber, cond_state, .Xchg, prev_fiber, .release) == null); // More than one wait on same Condition is illegal.
552 condition_wait.mutex.unlock(el.io());
553 },
544554 .exit => for (el.threads.allocated[0..@atomicLoad(u32, &el.threads.active, .acquire)]) |*each_thread| {
545555 getSqe(&thread.io_uring).* = .{
546556 .opcode = .MSG_RING,
......@@ -1242,7 +1252,7 @@ fn sleep(userdata: ?*anyopaque, clockid: std.posix.clockid_t, deadline: Io.Deadl
12421252
12431253fn mutexLock(userdata: ?*anyopaque, prev_state: Io.Mutex.State, mutex: *Io.Mutex) error{Canceled}!void {
12441254 const el: *EventLoop = @alignCast(@ptrCast(userdata));
1245 el.yield(null, .{ .lock_mutex = .{
1255 el.yield(null, .{ .mutex_lock = .{
12461256 .prev_state = prev_state,
12471257 .mutex = mutex,
12481258 } });
......@@ -1271,13 +1281,10 @@ fn mutexUnlock(userdata: ?*anyopaque, prev_state: Io.Mutex.State, mutex: *Io.Mut
12711281
12721282fn conditionWait(userdata: ?*anyopaque, cond: *Io.Condition, mutex: *Io.Mutex) Io.Cancelable!void {
12731283 const el: *EventLoop = @alignCast(@ptrCast(userdata));
1274 const cond_state: *?*Fiber = @ptrCast(&cond.state);
1275 const thread: *Thread = .current();
1276 const fiber = thread.currentFiber();
1277 const prev = @atomicRmw(?*Fiber, cond_state, .Xchg, fiber, .acquire);
1278 assert(prev == null); // More than one wait on same Condition is illegal.
1279 mutex.unlock(el.io());
1280 el.yield(null, .nothing);
1284 el.yield(null, .{ .condition_wait = .{
1285 .cond = cond,
1286 .mutex = mutex,
1287 } });
12811288 try mutex.lock(el.io());
12821289}
12831290