diff --git a/src/io/uring/Ring.hxx b/src/io/uring/Ring.hxx index 987c9fffd..f5033f6aa 100644 --- a/src/io/uring/Ring.hxx +++ b/src/io/uring/Ring.hxx @@ -126,6 +126,35 @@ public: void SeenCompletion(struct io_uring_cqe &cqe) noexcept { io_uring_cqe_seen(&ring, &cqe); } + + /** + * Invoke a function with a reference to each completion (but + * do not mark it "seen" or advance the completion queue + * head). + */ + unsigned ForEachCompletion(struct io_uring_cqe *cqe, + std::invocable auto f) noexcept { + unsigned dummy, n = 0; + + io_uring_for_each_cqe(&ring, dummy, cqe) { + ++n; + f(*cqe); + } + + return n; + } + + /** + * Like ForEachCompletion(), but advance the completion queue + * head. + */ + unsigned VisitCompletions(struct io_uring_cqe *cqe, + std::invocable auto f) noexcept { + unsigned n = ForEachCompletion(cqe, f); + if (n > 0) + io_uring_cq_advance(&ring, n); + return n; + } }; } // namespace Uring