pop and push to queue

This commit is contained in:
Muaz Ahmad 2023-11-27 13:02:09 +05:00
parent 98aaa390be
commit 7512246e9b

View file

@ -21,6 +21,7 @@ pub const JobQueue = struct {
job_pool: JobPool, job_pool: JobPool,
const Self = @This(); const Self = @This();
const Node = Self.Node;
pub fn init(pool: JobPool) Self { pub fn init(pool: JobPool) Self {
return Self { return Self {
@ -29,6 +30,23 @@ pub const JobQueue = struct {
.job_pool = pool, .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); pub const JobPool = std.heap.MemoryPool(Job);