From 7512246e9b7739e06534bb1f6b4a7f2e28ca0a69 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 27 Nov 2023 13:02:09 +0500 Subject: [PATCH] pop and push to queue --- src/util.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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);