From 729d445b6885f69dd2c6355f38707bd42851c791 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 22 Jun 2022 23:21:48 -0700 Subject: change the directory structure --- src/work_pool.zig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/work_pool.zig') 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); + } }; } -- cgit v1.2.3