| ... | ... | @@ -1,3 +1,4 @@ |
| 1 | const builtin = @import("builtin"); |
| 1 | 2 | const std = @import("std"); |
| 2 | 3 | const assert = std.debug.assert; |
| 3 | 4 | const WaitGroup = @This(); |
| ... | ... | @@ -43,3 +44,24 @@ pub fn isDone(wg: *WaitGroup) bool { |
| 43 | 44 | |
| 44 | 45 | return (state / one_pending) == 0; |
| 45 | 46 | } |
| 47 | |
| 48 | // Spawns a new thread for the task. This is appropriate when the callee |
| 49 | // delegates all work. |
| 50 | pub fn spawnManager( |
| 51 | wg: *WaitGroup, |
| 52 | comptime func: anytype, |
| 53 | args: anytype, |
| 54 | ) void { |
| 55 | if (builtin.single_threaded) { |
| 56 | @call(.auto, func, args); |
| 57 | return; |
| 58 | } |
| 59 | const Manager = struct { |
| 60 | fn run(wg_inner: *WaitGroup, args_inner: @TypeOf(args)) void { |
| 61 | defer wg_inner.finish(); |
| 62 | @call(.auto, func, args_inner); |
| 63 | } |
| 64 | }; |
| 65 | wg.start(); |
| 66 | _ = std.Thread.spawn(.{}, Manager.run, .{ wg, args }) catch Manager.run(wg, args); |
| 67 | } |