diff options
author | 2022-06-22 23:21:48 -0700 | |
---|---|---|
committer | 2022-06-22 23:21:48 -0700 | |
commit | 729d445b6885f69dd2c6355f38707bd42851c791 (patch) | |
tree | f87a7c408929ea3f57bbb7ace380cf869da83c0e /src/work_pool.zig | |
parent | 25f820c6bf1d8ec6d444ef579cc036b8c0607b75 (diff) | |
download | bun-jarred/rename.tar.gz bun-jarred/rename.tar.zst bun-jarred/rename.zip |
change the directory structurejarred/rename
Diffstat (limited to '')
-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); + } }; } |