diff --git a/src/util.zig b/src/util.zig index da77145..0364b34 100644 --- a/src/util.zig +++ b/src/util.zig @@ -21,6 +21,7 @@ pub const JobQueue = struct { job_pool: JobPool, const Self = @This(); + const Node = Self.Node; pub fn init(pool: JobPool) Self { return Self { @@ -29,6 +30,23 @@ pub const JobQueue = struct { .job_pool = pool, }; } + + pub fn prepend(self: *Self, job: Job) void { + self.mutex.lock(); + defer self.mutex.unlock(); + var n = try self.job_pool.create(Node); + n.data = job; + return self.queue.prepend(n); + } + + pub fn pop(self: *Self) ?Job { + self.mutex.lock(); + defer self.mutex.unlock(); + const n = self.queue.pop() orelse return null; + const job = n.data; + self.job_pool.destroy(n); + return job; + } }; pub const JobPool = std.heap.MemoryPool(Job);