mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Grigory <grigory.orlov.set@gmail.com> Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Co-authored-by: Meghan Denny <hello@nektro.net> Co-authored-by: Kenta Iwasaki <63115601+lithdew@users.noreply.github.com> Co-authored-by: John-David Dalton <john.david.dalton@gmail.com> Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com> Co-authored-by: paperdave <paperdave@users.noreply.github.com> Co-authored-by: Georgijs Vilums <georgijs.vilums@gmail.com> Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
63 lines
1.9 KiB
Zig
63 lines
1.9 KiB
Zig
const ThreadPool = bun.ThreadPool;
|
|
const std = @import("std");
|
|
const bun = @import("root").bun;
|
|
|
|
pub const Batch = ThreadPool.Batch;
|
|
pub const Task = ThreadPool.Task;
|
|
|
|
pub fn NewWorkPool(comptime max_threads: ?usize) type {
|
|
return struct {
|
|
var pool: ThreadPool = undefined;
|
|
var loaded: bool = false;
|
|
|
|
fn create() *ThreadPool {
|
|
@setCold(true);
|
|
|
|
pool = ThreadPool.init(.{
|
|
.max_threads = max_threads orelse @max(@as(u32, @truncate(std.Thread.getCpuCount() catch 0)), 2),
|
|
.stack_size = ThreadPool.default_thread_stack_size,
|
|
});
|
|
return &pool;
|
|
}
|
|
pub inline fn get() *ThreadPool {
|
|
// lil racy
|
|
if (loaded) return &pool;
|
|
loaded = true;
|
|
|
|
return create();
|
|
}
|
|
|
|
pub fn scheduleBatch(batch: ThreadPool.Batch) void {
|
|
get().schedule(batch);
|
|
}
|
|
|
|
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: *@This() = @fieldParentPtr("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);
|
|
}
|
|
};
|
|
}
|
|
|
|
pub const WorkPool = NewWorkPool(null);
|