diff --git a/src/threads.zig b/src/threads.zig index 752c5e4..46e7a07 100644 --- a/src/threads.zig +++ b/src/threads.zig @@ -1,23 +1,52 @@ const std = @import("std"); +const util = @import("util.zig"); + +const QueueWrap = struct { + alloc: std.mem.Allocator, + queue: *util.JobQueue, + job_pool: util.JobPool, + + const Self = @This(); + + fn init(alloc: std.mem.Allocator, job_pool: util.JobPool) !Self { + var q = try alloc.create(util.JobQueue); + q.* = util.JobQueue.init(job_pool); + return Self { + .alloc = alloc, + .queue = q, + .job_pool = job_pool, + }; + } + fn deinit(self: *Self) void { + self.job_pool.deinit(); + self.alloc.destroy(self.queue); + } +}; + pub const ThreadManager = struct { threads: std.ArrayList(std.Thread), arena: std.heap.ArenaAllocator, + queue: QueueWrap, const Self = @This(); pub fn init(alloc: std.mem.Allocator, n_quant_jobs: usize) !Self { _ = n_quant_jobs; var arena = std.heap.ArenaAllocator.init(alloc); + var arena_alloc = arena.allocator(); + var job_pool = util.JobPool.init(alloc); var thread_mgr = Self { - .threads = std.ArrayList(std.Thread).init(arena.allocator()), + .threads = std.ArrayList(std.Thread).init(arena_alloc), .arena = arena, + .queue = try QueueWrap.init(arena_alloc, job_pool), }; return thread_mgr; } pub fn deinit(self: *Self) void { self.threads.deinit(); + self.queue.deinit(); self.arena.deinit(); } }; diff --git a/src/util.zig b/src/util.zig index dcc845b..da77145 100644 --- a/src/util.zig +++ b/src/util.zig @@ -10,6 +10,28 @@ pub const Options = struct { pub const Block = [64]u8; pub const BlockQuantized = [64]i16; +pub const ThreadList = std.ArrayList(std.Thread); +pub const Job = struct { + source: *Block, + target: *BlockQuantized, +}; +pub const JobQueue = struct { + mutex: std.Thread.Mutex, + queue: std.TailQueue(Job), + job_pool: JobPool, + + const Self = @This(); + + pub fn init(pool: JobPool) Self { + return Self { + .mutex = std.Thread.Mutex{}, + .queue = std.TailQueue(Job){}, + .job_pool = pool, + }; + } +}; +pub const JobPool = std.heap.MemoryPool(Job); + pub const Buffers = struct { arena: std.heap.ArenaAllocator,