Skip to content

Commit e35b93c

Browse files
committed
switch LIFO queue to FIFO
1 parent e582d09 commit e35b93c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/bls/ThreadPool.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,36 @@ threads: [MAX_WORKERS]std.Thread = undefined,
4242
shutdown: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
4343
queue: JobQueue,
4444

45+
/// Thread-safe FIFO work queue. Workers wait on `cond` for new items
46+
/// and pop them in submission order.
4547
const JobQueue = struct {
4648
mutex: std.Thread.Mutex = .{},
4749
cond: std.Thread.Condition = .{},
4850
head: ?*WorkItem = null,
51+
tail: ?*WorkItem = null,
4952

5053
fn pushBatch(self: *JobQueue, items: []*WorkItem) void {
5154
self.mutex.lock();
5255
defer self.mutex.unlock();
56+
5357
for (items) |item| {
54-
item.next = self.head;
55-
self.head = item;
58+
item.next = null;
59+
if (self.tail) |tail| {
60+
tail.next = item;
61+
} else {
62+
self.head = item;
63+
}
64+
self.tail = item;
5665
}
5766
self.cond.broadcast();
5867
}
5968

6069
fn pop(self: *JobQueue) ?*WorkItem {
61-
// Called with mutex held
6270
const item = self.head orelse return null;
6371
self.head = item.next;
72+
if (self.head == null) {
73+
self.tail = null;
74+
}
6475
item.next = null;
6576
return item;
6677
}

0 commit comments

Comments
 (0)