From a9bee1c64bc9a6194829ea67f169f565a81ff913 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 4 Feb 2025 19:58:37 +0100 Subject: [PATCH] io/uring/Ring: add io_uring_for_each_cqe() wrapper --- src/io/uring/Ring.hxx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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