File tree Expand file tree Collapse file tree 1 file changed +6
-0
lines changed
Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Original file line number Diff line number Diff line change @@ -42,13 +42,17 @@ partial_p2: [MAX_WORKERS]c.blst_p2 = undefined,
4242has_work : [MAX_WORKERS ]bool = [_ ]bool {false } ** MAX_WORKERS ,
4343
4444var 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.
4849const allocator = std .heap .page_allocator ;
4950
5051/// Returns the global thread pool singleton, creating it if necessary.
5152pub 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
You can’t perform that action at this time.
0 commit comments