[PATCH 2/3] io_uring: Add io_uring_peek_cqe to mini_liburing

From: Oliver Crumrine
Date: Thu Apr 04 2024 - 22:19:12 EST


mini_liburing.h is used by the io_uring tests as a stand-in for
liburing. The next patch in this series needs the io_uring_peek_cqe
function, and I speculate that future tests will too, so add that here.

Please scrutinize this code, I am somewhat new to io_uring, and whilst I
have tested this and know it to work, I very well could've made a
mistake somewhere without noticing.

Signed-off-by: Oliver Crumrine <ozlinuxc@xxxxxxxxx>
---
tools/include/io_uring/mini_liburing.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/tools/include/io_uring/mini_liburing.h b/tools/include/io_uring/mini_liburing.h
index 9ccb16074eb5..94dcc7f9aa33 100644
--- a/tools/include/io_uring/mini_liburing.h
+++ b/tools/include/io_uring/mini_liburing.h
@@ -182,6 +182,24 @@ static inline int io_uring_wait_cqe(struct io_uring *ring,
return 0;
}

+static inline int io_uring_peek_cqe(struct io_uring *ring,
+ struct io_uring_cqe **cqe_ptr)
+{
+ struct io_uring_cq *cq = &ring->cq;
+ const unsigned int mask = *cq->kring_mask;
+ unsigned int head = *cq->khead;
+ unsigned int tail = *cq->ktail;
+
+ *cqe_ptr = NULL;
+
+ read_barrier();
+ if (head-tail) {
+ *cqe_ptr = &cq->cqes[head & mask];
+ return 0;
+ }
+ return -EAGAIN;
+}
+
static inline int io_uring_submit(struct io_uring *ring)
{
struct io_uring_sq *sq = &ring->sq;
--
2.44.0