zig build: add a -j<N> option for limiting concurrency

This commit is contained in:
Andrew Kelley
2023-02-19 16:21:36 -07:00
parent 96d798db8b
commit cb09470063
3 changed files with 29 additions and 6 deletions

View File

@@ -17,7 +17,14 @@ const Runnable = struct {
const RunProto = *const fn (*Runnable) void;
pub fn init(pool: *Pool, allocator: std.mem.Allocator) !void {
pub const Options = struct {
allocator: std.mem.Allocator,
n_jobs: ?u32 = null,
};
pub fn init(pool: *Pool, options: Options) !void {
const allocator = options.allocator;
pool.* = .{
.allocator = allocator,
.threads = &[_]std.Thread{},
@@ -27,7 +34,7 @@ pub fn init(pool: *Pool, allocator: std.mem.Allocator) !void {
return;
}
const thread_count = std.math.max(1, std.Thread.getCpuCount() catch 1);
const thread_count = options.n_jobs orelse @max(1, std.Thread.getCpuCount() catch 1);
pool.threads = try allocator.alloc(std.Thread, thread_count);
errdefer allocator.free(pool.threads);