aboutsummaryrefslogtreecommitdiff
path: root/src/work_pool.zig
blob: 090f7421659fa1d8ac10b45364d4319dcd335da9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const ThreadPool = @import("thread_pool");
const std = @import("std");
var pool: ThreadPool = undefined;
var loaded: bool = false;

pub const Batch = ThreadPool.Batch;
pub const Task = ThreadPool.Task;

fn create() *ThreadPool {
    @setCold(true);

    pool = ThreadPool.init(.{
        .max_threads = @floatToInt(u32, @floor(@intToFloat(f32, @maximum(std.Thread.getCpuCount() catch 0, 2)) * 0.8)),
        .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));
}