Skip to content

Commit

Permalink
remove race from threadpool
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 authored and steeve committed Jan 17, 2025
1 parent f6dea8b commit 890e271
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/ThreadPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 890e271

Please sign in to comment.