| ... | @@ -7,6 +7,7 @@ mutex: std.Thread.Mutex = .{}, | ... | @@ -7,6 +7,7 @@ mutex: std.Thread.Mutex = .{}, |
| 7 | cond: std.Thread.Condition = .{}, | 7 | cond: std.Thread.Condition = .{}, |
| 8 | run_queue: std.SinglyLinkedList = .{}, | 8 | run_queue: std.SinglyLinkedList = .{}, |
| 9 | is_running: bool = true, | 9 | is_running: bool = true, |
| | 10 | /// Must be a thread-safe allocator. |
| 10 | allocator: std.mem.Allocator, | 11 | allocator: std.mem.Allocator, |
| 11 | threads: if (builtin.single_threaded) [0]std.Thread else []std.Thread, | 12 | threads: if (builtin.single_threaded) [0]std.Thread else []std.Thread, |
| 12 | ids: if (builtin.single_threaded) struct { | 13 | ids: if (builtin.single_threaded) struct { |
| ... | @@ -16,12 +17,12 @@ ids: if (builtin.single_threaded) struct { | ... | @@ -16,12 +17,12 @@ ids: if (builtin.single_threaded) struct { |
| 16 | } | 17 | } |
| 17 | } else std.AutoArrayHashMapUnmanaged(std.Thread.Id, void), | 18 | } else std.AutoArrayHashMapUnmanaged(std.Thread.Id, void), |
| 18 | | 19 | |
| 19 | const Runnable = struct { | 20 | pub const Runnable = struct { |
| 20 | runFn: RunProto, | 21 | runFn: RunProto, |
| 21 | node: std.SinglyLinkedList.Node = .{}, | 22 | node: std.SinglyLinkedList.Node = .{}, |
| 22 | }; | 23 | }; |
| 23 | | 24 | |
| 24 | const RunProto = *const fn (*Runnable, id: ?usize) void; | 25 | pub const RunProto = *const fn (*Runnable, id: ?usize) void; |
| 25 | | 26 | |
| 26 | pub const Options = struct { | 27 | pub const Options = struct { |
| 27 | allocator: std.mem.Allocator, | 28 | allocator: std.mem.Allocator, |
| ... | @@ -117,12 +118,6 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args | ... | @@ -117,12 +118,6 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 117 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); | 118 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); |
| 118 | @call(.auto, func, closure.arguments); | 119 | @call(.auto, func, closure.arguments); |
| 119 | closure.wait_group.finish(); | 120 | closure.wait_group.finish(); |
| 120 | | | |
| 121 | // The thread pool's allocator is protected by the mutex. | | |
| 122 | const mutex = &closure.pool.mutex; | | |
| 123 | mutex.lock(); | | |
| 124 | defer mutex.unlock(); | | |
| 125 | | | |
| 126 | closure.pool.allocator.destroy(closure); | 121 | closure.pool.allocator.destroy(closure); |
| 127 | } | 122 | } |
| 128 | }; | 123 | }; |
| ... | @@ -179,12 +174,6 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar | ... | @@ -179,12 +174,6 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar |
| 179 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); | 174 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); |
| 180 | @call(.auto, func, .{id.?} ++ closure.arguments); | 175 | @call(.auto, func, .{id.?} ++ closure.arguments); |
| 181 | closure.wait_group.finish(); | 176 | closure.wait_group.finish(); |
| 182 | | | |
| 183 | // The thread pool's allocator is protected by the mutex. | | |
| 184 | const mutex = &closure.pool.mutex; | | |
| 185 | mutex.lock(); | | |
| 186 | defer mutex.unlock(); | | |
| 187 | | | |
| 188 | closure.pool.allocator.destroy(closure); | 177 | closure.pool.allocator.destroy(closure); |
| 189 | } | 178 | } |
| 190 | }; | 179 | }; |
| ... | @@ -228,12 +217,6 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { | ... | @@ -228,12 +217,6 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { |
| 228 | fn runFn(runnable: *Runnable, _: ?usize) void { | 217 | fn runFn(runnable: *Runnable, _: ?usize) void { |
| 229 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); | 218 | const closure: *@This() = @alignCast(@fieldParentPtr("runnable", runnable)); |
| 230 | @call(.auto, func, closure.arguments); | 219 | @call(.auto, func, closure.arguments); |
| 231 | | | |
| 232 | // The thread pool's allocator is protected by the mutex. | | |
| 233 | const mutex = &closure.pool.mutex; | | |
| 234 | mutex.lock(); | | |
| 235 | defer mutex.unlock(); | | |
| 236 | | | |
| 237 | closure.pool.allocator.destroy(closure); | 220 | closure.pool.allocator.destroy(closure); |
| 238 | } | 221 | } |
| 239 | }; | 222 | }; |