diff --git a/runtime/src/retire_tickets.h b/runtime/src/retire_tickets.h index a483028593..675460cac2 100644 --- a/runtime/src/retire_tickets.h +++ b/runtime/src/retire_tickets.h @@ -24,7 +24,7 @@ class RetireTickets { while (!pending_.empty()) { const Entry& front = pending_.front(); if (!front.idle && !query(front.semaphore, front.value)) break; - completed_ = front.ticket; + if (front.ticket > completed_) completed_ = front.ticket; pending_.pop_front(); } return completed_; @@ -34,8 +34,18 @@ class RetireTickets { // that must have them done (and may not hold the lock that guards this object while it waits) waits for. std::vector> pending_through(std::uint64_t ticket) const { std::vector> out; - for (const Entry& entry : pending_) - if (entry.ticket <= ticket && !entry.idle) out.emplace_back(entry.semaphore, entry.value); + for (const Entry& entry : pending_) { + if (entry.ticket > ticket || entry.idle) continue; + bool merged = false; + for (auto& [sem, val] : out) { + if (sem == entry.semaphore) { + if (entry.value > val) val = entry.value; + merged = true; + break; + } + } + if (!merged) out.emplace_back(entry.semaphore, entry.value); + } return out; } diff --git a/runtime/test/retire_tickets_test.cpp b/runtime/test/retire_tickets_test.cpp index 8eb5e02d06..f1dc836ded 100644 --- a/runtime/test/retire_tickets_test.cpp +++ b/runtime/test/retire_tickets_test.cpp @@ -30,6 +30,24 @@ int main() { queue_one_destroyed = true; if (tickets.completed(query) != 4 || queried_destroyed) return 4; + // pending_through() must coalesce repeated submissions on the same queue to the highest + // timeline value, omit idle queues, and exclude tickets past the requested cutoff. + tickets.record(5, 1, 10); + tickets.record(6, 2, 20); + tickets.record(7, 1, 12); + tickets.record(8, 1, 15); + tickets.record(9, 2, 25); + tickets.queue_idle(2); + const auto wait_list = tickets.pending_through(7); + if (wait_list.size() != 1 || wait_list[0].first != 1 || wait_list[0].second != 12) return 5; + + // A non-monotonic ticket must not move the completed prefix backward. + done[1] = 15; + if (tickets.completed(query) != 9) return 6; + tickets.record(3, 1, 16); + done[1] = 16; + if (tickets.completed(query) != 9) return 7; + std::puts("out-of-order queue completion kept the retirement prefix safe"); return 0; }