| ... | @@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args | ... | @@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 97 | wait_group.start(); | 97 | wait_group.start(); |
| 98 | | 98 | |
| 99 | if (builtin.single_threaded) { | 99 | if (builtin.single_threaded) { |
| 100 | @call(.auto, func, args); | 100 | callFn(func, args); |
| 101 | wait_group.finish(); | 101 | wait_group.finish(); |
| 102 | return; | 102 | return; |
| 103 | } | 103 | } |
| ... | @@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args | ... | @@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 112 | fn runFn(runnable: *Runnable, _: ?usize) void { | 112 | fn runFn(runnable: *Runnable, _: ?usize) void { |
| 113 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); | 113 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); |
| 114 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); | 114 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); |
| 115 | @call(.auto, func, closure.arguments); | 115 | callFn(func, closure.arguments); |
| 116 | closure.wait_group.finish(); | 116 | closure.wait_group.finish(); |
| 117 | | 117 | |
| 118 | // The thread pool's allocator is protected by the mutex. | 118 | // The thread pool's allocator is protected by the mutex. |
| ... | @@ -129,7 +129,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args | ... | @@ -129,7 +129,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 129 | | 129 | |
| 130 | const closure = pool.allocator.create(Closure) catch { | 130 | const closure = pool.allocator.create(Closure) catch { |
| 131 | pool.mutex.unlock(); | 131 | pool.mutex.unlock(); |
| 132 | @call(.auto, func, args); | 132 | callFn(func, args); |
| 133 | wait_group.finish(); | 133 | wait_group.finish(); |
| 134 | return; | 134 | return; |
| 135 | }; | 135 | }; |
| ... | @@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar | ... | @@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar |
| 160 | wait_group.start(); | 160 | wait_group.start(); |
| 161 | | 161 | |
| 162 | if (builtin.single_threaded) { | 162 | if (builtin.single_threaded) { |
| 163 | @call(.auto, func, .{0} ++ args); | 163 | callFn(func, .{0} ++ args); |
| 164 | wait_group.finish(); | 164 | wait_group.finish(); |
| 165 | return; | 165 | return; |
| 166 | } | 166 | } |
| ... | @@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar | ... | @@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar |
| 175 | fn runFn(runnable: *Runnable, id: ?usize) void { | 175 | fn runFn(runnable: *Runnable, id: ?usize) void { |
| 176 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); | 176 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); |
| 177 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); | 177 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); |
| 178 | @call(.auto, func, .{id.?} ++ closure.arguments); | 178 | callFn(func, .{id.?} ++ closure.arguments); |
| 179 | closure.wait_group.finish(); | 179 | closure.wait_group.finish(); |
| 180 | | 180 | |
| 181 | // The thread pool's allocator is protected by the mutex. | 181 | // The thread pool's allocator is protected by the mutex. |
| ... | @@ -193,7 +193,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar | ... | @@ -193,7 +193,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar |
| 193 | const closure = pool.allocator.create(Closure) catch { | 193 | const closure = pool.allocator.create(Closure) catch { |
| 194 | const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId()); | 194 | const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId()); |
| 195 | pool.mutex.unlock(); | 195 | pool.mutex.unlock(); |
| 196 | @call(.auto, func, .{id.?} ++ args); | 196 | callFn(func, .{id.?} ++ args); |
| 197 | wait_group.finish(); | 197 | wait_group.finish(); |
| 198 | return; | 198 | return; |
| 199 | }; | 199 | }; |
| ... | @@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar | ... | @@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar |
| 213 | | 213 | |
| 214 | pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { | 214 | pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { |
| 215 | if (builtin.single_threaded) { | 215 | if (builtin.single_threaded) { |
| 216 | @call(.auto, func, args); | 216 | callFn(func, args); |
| 217 | return; | 217 | return; |
| 218 | } | 218 | } |
| 219 | | 219 | |
| ... | @@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { | ... | @@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { |
| 226 | fn runFn(runnable: *Runnable, _: ?usize) void { | 226 | fn runFn(runnable: *Runnable, _: ?usize) void { |
| 227 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); | 227 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); |
| 228 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); | 228 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); |
| 229 | @call(.auto, func, closure.arguments); | 229 | callFn(func, closure.arguments); |
| 230 | | 230 | |
| 231 | // The thread pool's allocator is protected by the mutex. | 231 | // The thread pool's allocator is protected by the mutex. |
| 232 | const mutex = &closure.pool.mutex; | 232 | const mutex = &closure.pool.mutex; |
| ... | @@ -321,3 +321,31 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void { | ... | @@ -321,3 +321,31 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void { |
| 321 | pub fn getIdCount(pool: *Pool) usize { | 321 | pub fn getIdCount(pool: *Pool) usize { |
| 322 | return @intCast(1 + pool.threads.len); | 322 | return @intCast(1 + pool.threads.len); |
| 323 | } | 323 | } |
| | 324 | |
| | 325 | inline fn callFn(comptime f: anytype, args: anytype) void { |
| | 326 | const bad_fn_ret = "expected return type of runFn to be 'void', '!void', noreturn, or !noreturn"; |
| | 327 | |
| | 328 | switch (@typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?)) { |
| | 329 | .void, .noreturn => { |
| | 330 | @call(.auto, f, args); |
| | 331 | }, |
| | 332 | .error_union => |info| { |
| | 333 | switch (info.payload) { |
| | 334 | void, noreturn => { |
| | 335 | @call(.auto, f, args) catch |err| { |
| | 336 | std.debug.print("error: {s}\n", .{@errorName(err)}); |
| | 337 | if (@errorReturnTrace()) |trace| { |
| | 338 | std.debug.dumpStackTrace(trace.*); |
| | 339 | } |
| | 340 | }; |
| | 341 | }, |
| | 342 | else => { |
| | 343 | @compileError(bad_fn_ret); |
| | 344 | }, |
| | 345 | } |
| | 346 | }, |
| | 347 | else => { |
| | 348 | @compileError(bad_fn_ret); |
| | 349 | }, |
| | 350 | } |
| | 351 | } |