From 1c80766d3f92ac88e304edba23c1f4be8ff1afae Mon Sep 17 00:00:00 2001 From: David Rubin Date: Fri, 6 Dec 2024 20:43:52 -0800 Subject: [PATCH] remove race from threadpool --- src/ThreadPool.zig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ThreadPool.zig b/src/ThreadPool.zig index 1d0123b..aec09d7 100644 --- a/src/ThreadPool.zig +++ b/src/ThreadPool.zig @@ -291,6 +291,7 @@ pub noinline fn shutdown(self: *ThreadPool) void { // Wake up any threads sleeping on the idle_event. // TODO: I/O polling notification here. if (sync.idle > 0) self.idle_event.shutdown(); + if (sync.spawned == 0) self.join_event.notify(); return; }); } @@ -335,11 +336,8 @@ fn unregister(noalias self: *ThreadPool, noalias maybe_thread: ?*Thread) void { fn join(self: *ThreadPool) void { // Wait for the thread pool to be shutdown() then for all threads to enter a joinable state - var sync: Sync = @bitCast(self.sync.load(.monotonic)); - if (!(sync.state == .shutdown and sync.spawned == 0)) { - self.join_event.wait(); - sync = @bitCast(self.sync.load(.monotonic)); - } + self.join_event.wait(); + const sync: Sync = @bitCast(self.sync.load(.monotonic)); assert(sync.state == .shutdown); assert(sync.spawned == 0);