diff options
Diffstat (limited to 'src/work_pool.zig')
-rw-r--r-- | src/work_pool.zig | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/work_pool.zig b/src/work_pool.zig index a663e7186..9ddf106e9 100644 --- a/src/work_pool.zig +++ b/src/work_pool.zig @@ -33,6 +33,28 @@ pub fn NewWorkPool(comptime max_threads: ?usize) type { pub fn schedule(task: *ThreadPool.Task) void { get().schedule(ThreadPool.Batch.from(task)); } + + pub fn go(allocator: std.mem.Allocator, comptime Context: type, context: Context, comptime function: fn (Context) void) !void { + const TaskType = struct { + task: Task, + context: Context, + allocator: std.mem.Allocator, + + pub fn callback(task: *Task) void { + var this_task = @fieldParentPtr(@This(), "task", task); + function(this_task.context); + this_task.allocator.destroy(this_task); + } + }; + + var task_ = try allocator.create(TaskType); + task_.* = .{ + .task = .{ .callback = TaskType.callback }, + .context = context, + .allocator = allocator, + }; + schedule(&task_.task); + } }; } |