diff mbox series

[2/3] io_uring: Add io_uring_peek_cqe to mini_liburing

Message ID 44916a6a3f94bfcff78da2c602c916c856d7cfcf.1712268605.git.ozlinuxc@gmail.com (mailing list archive)
State Not Applicable
Headers show
Series Add REQ_F_CQE_SKIP support to io_uring zerocopy | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Oliver Crumrine April 4, 2024, 10:19 p.m. UTC
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@gmail.com>
---
 tools/include/io_uring/mini_liburing.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff mbox series

Patch

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;