Skip to content

Commit 9dbab0c

Browse files
committed
use mutex for get and deinit
1 parent 50f294d commit 9dbab0c

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/ThreadPool.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ partial_p2: [MAX_WORKERS]c.blst_p2 = undefined,
4242
has_work: [MAX_WORKERS]bool = [_]bool{false} ** MAX_WORKERS,
4343

4444
var instance: ?*ThreadPool = null;
45+
var mutex: std.Thread.Mutex = .{};
4546

4647
/// Pairing size is = ~3.1KB * `MAX_WORKERS` = ~50KB
4748
/// We allocate 4 pages (4 * 16KB) for this at startup.
4849
const allocator = std.heap.page_allocator;
4950

5051
/// Returns the global thread pool singleton, creating it if necessary.
5152
pub fn get() *ThreadPool {
53+
mutex.lock();
54+
defer mutex.unlock();
55+
5256
if (instance) |pool| return pool;
5357
const pool = allocator.create(ThreadPool) catch
5458
@panic("ThreadPool: failed to allocate");
@@ -82,7 +86,9 @@ pub fn deinit(pool: *ThreadPool) void {
8286
for (pool.threads[0 .. n_workers - 1]) |t| {
8387
t.join();
8488
}
89+
mutex.lock();
8590
if (instance == pool) instance = null;
91+
mutex.unlock();
8692
allocator.destroy(pool);
8793
}
8894

0 commit comments

Comments
 (0)