add thread control via wake hints

This commit is contained in:
Muaz Ahmad 2023-11-28 15:03:41 +05:00
parent 4abd5b9019
commit 221519c705

View file

@ -9,6 +9,7 @@ const AtomicU32 = std.atomic.Atomic(u32);
const Signals = struct { const Signals = struct {
quit: AtomicBool, quit: AtomicBool,
processed: AtomicU32, processed: AtomicU32,
eof_block: AtomicU32,
const Self = @This(); const Self = @This();
@ -16,6 +17,7 @@ const Signals = struct {
return Self{ return Self{
.quit = AtomicBool.init(false), .quit = AtomicBool.init(false),
.processed = AtomicU32.init(0), .processed = AtomicU32.init(0),
.eof_block = AtomicU32.init(0),
}; };
} }
}; };
@ -69,6 +71,21 @@ pub const ThreadManager = struct {
self.arena.deinit(); self.arena.deinit();
self.job_pool.deinit(); self.job_pool.deinit();
} }
pub fn quit(self: *Self) void {
self.signals.quit.store(true, .Release);
self.unblock();
for (self.threads.items) |thread| {
thread.join();
}
}
pub fn eof(self: *Self) void {
self.signals.eof_block.store(1, .Release);
}
pub fn unblock(self: *Self) void {
self.signals.eof_block.store(0, .Release);
}
}; };
fn quantize_loop(queue: *util.JobQueue, signals: *Signals, Q_Lum: *util.QTable, Q_Chrom: *util.QTable) void { fn quantize_loop(queue: *util.JobQueue, signals: *Signals, Q_Lum: *util.QTable, Q_Chrom: *util.QTable) void {
@ -76,5 +93,8 @@ fn quantize_loop(queue: *util.JobQueue, signals: *Signals, Q_Lum: *util.QTable,
const job = queue.pop() orelse continue; const job = queue.pop() orelse continue;
transform.quantize(job.source, job.target, if (job.is_lum) Q_Lum else Q_Chrom); transform.quantize(job.source, job.target, if (job.is_lum) Q_Lum else Q_Chrom);
_ = @atomicRmw(u32, &signals.processed.value, .Add, 1, .SeqCst); _ = @atomicRmw(u32, &signals.processed.value, .Add, 1, .SeqCst);
if (signals.eof_block.load(.Acquire) == 1) {
std.Thread.Futex.wait(&signals.eof_block, 1);
}
} }
} }