signal and thread spawns
This commit is contained in:
parent
c63d0520c7
commit
18a998e737
1 changed files with 31 additions and 5 deletions
|
@ -2,8 +2,21 @@ const std = @import("std");
|
||||||
|
|
||||||
const util = @import("util.zig");
|
const util = @import("util.zig");
|
||||||
|
|
||||||
|
const AtomicBool = std.atomic.Atomic(bool);
|
||||||
|
|
||||||
|
const Signals = struct {
|
||||||
|
quit: AtomicBool,
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
fn init() Self {
|
||||||
|
return Self {
|
||||||
|
.quit = AtomicBool.init(false),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const QueueWrap = struct {
|
const QueueWrap = struct {
|
||||||
alloc: std.mem.Allocator,
|
|
||||||
queue: *util.JobQueue,
|
queue: *util.JobQueue,
|
||||||
job_pool: util.JobPool,
|
job_pool: util.JobPool,
|
||||||
|
|
||||||
|
@ -13,7 +26,6 @@ const QueueWrap = struct {
|
||||||
var q = try alloc.create(util.JobQueue);
|
var q = try alloc.create(util.JobQueue);
|
||||||
q.* = util.JobQueue.init(job_pool);
|
q.* = util.JobQueue.init(job_pool);
|
||||||
return Self {
|
return Self {
|
||||||
.alloc = alloc,
|
|
||||||
.queue = q,
|
.queue = q,
|
||||||
.job_pool = job_pool,
|
.job_pool = job_pool,
|
||||||
};
|
};
|
||||||
|
@ -23,22 +35,29 @@ const QueueWrap = struct {
|
||||||
pub const ThreadManager = struct {
|
pub const ThreadManager = struct {
|
||||||
threads: std.ArrayList(std.Thread),
|
threads: std.ArrayList(std.Thread),
|
||||||
arena: std.heap.ArenaAllocator,
|
arena: std.heap.ArenaAllocator,
|
||||||
queue: QueueWrap,
|
queue_wrp: QueueWrap,
|
||||||
job_pool: util.JobPool,
|
job_pool: util.JobPool,
|
||||||
|
signals: *Signals,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
pub fn init(alloc: std.mem.Allocator, n_quant_jobs: usize) !Self {
|
pub fn init(alloc: std.mem.Allocator, n_quant_jobs: usize) !Self {
|
||||||
_ = n_quant_jobs;
|
|
||||||
var arena = std.heap.ArenaAllocator.init(alloc);
|
var arena = std.heap.ArenaAllocator.init(alloc);
|
||||||
var arena_alloc = arena.allocator();
|
var arena_alloc = arena.allocator();
|
||||||
var job_pool = util.JobPool.init(alloc);
|
var job_pool = util.JobPool.init(alloc);
|
||||||
var thread_mgr = Self {
|
var thread_mgr = Self {
|
||||||
.threads = std.ArrayList(std.Thread).init(arena_alloc),
|
.threads = std.ArrayList(std.Thread).init(arena_alloc),
|
||||||
.arena = arena,
|
.arena = arena,
|
||||||
.queue = try QueueWrap.init(arena_alloc, job_pool),
|
.queue_wrp = try QueueWrap.init(arena_alloc, job_pool),
|
||||||
.job_pool = job_pool,
|
.job_pool = job_pool,
|
||||||
|
.signals = try arena_alloc.create(Signals),
|
||||||
};
|
};
|
||||||
|
thread_mgr.signals.* = Signals.init();
|
||||||
|
|
||||||
|
for (0..n_quant_jobs) |_| {
|
||||||
|
try thread_mgr.threads.append(try std.Thread.spawn(.{}, quantize_loop, .{thread_mgr.queue_wrp.queue, thread_mgr.signals}));
|
||||||
|
}
|
||||||
|
|
||||||
return thread_mgr;
|
return thread_mgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,3 +66,10 @@ pub const ThreadManager = struct {
|
||||||
self.job_pool.deinit();
|
self.job_pool.deinit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn quantize_loop(queue: *util.JobQueue, signals: *Signals) void {
|
||||||
|
while (queue.HasJobs() or !signals.quit.load(std.builtin.AtomicOrder.Acquire)) : (std.time.sleep(1)) {
|
||||||
|
const job = queue.pop() orelse continue;
|
||||||
|
std.debug.print("{any}\n", .{job});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue