Files
bun.sh/src/work_pool.zig
Jarred Sumner 4b24bb464c Make Bun.build more reliable (#2718)
* One possible implementation to make `Bun.build` work better

* Pass allocator in

* Make our temporary buffers a little safer

* rename

* Fix memory corruption in symbol table

* Add support for deferred idle events in ThreadPool

* Free more memory

* Use a global allocator FS cache

* more `inline`

* Make duping keys optional in StringMap

* Close file handles more often

* Update router.zig

* wip possibly delete this commit

* Fix memory issues and reduce memory usage

* > 0.8

* Switch to AsyncIO.Waker and fix memory leak in JSBundleCompletionTask

* We don't need to clone this actually

* Fix error

* Format

* Fixup

* Fixup

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
2023-04-22 19:44:23 -07:00

62 lines
1.9 KiB
Zig

const ThreadPool = @import("root").bun.ThreadPool;
const std = @import("std");
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(@truncate(u32, std.Thread.getCpuCount() catch 0), 2),
.stack_size = 2 * 1024 * 1024,
});
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 = @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);
}
};
}
pub const WorkPool = NewWorkPool(null);