pop and push to queue
This commit is contained in:
parent
98aaa390be
commit
7512246e9b
1 changed files with 18 additions and 0 deletions
18
src/util.zig
18
src/util.zig
|
@ -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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue