File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed
Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -42,25 +42,36 @@ threads: [MAX_WORKERS]std.Thread = undefined,
4242shutdown : std .atomic .Value (bool ) = std .atomic .Value (bool ).init (false ),
4343queue : JobQueue ,
4444
45+ /// Thread-safe FIFO work queue. Workers wait on `cond` for new items
46+ /// and pop them in submission order.
4547const 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 }
You can’t perform that action at this time.
0 commit comments