io_uring: Add wait functions
This makes it possible to write trivial applications that aren't
necessarily using a full blown event loop.
Change-Id: If19cc9f0c21c029a5fda6a0d4f3eae8677841999
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/stdplus/io_uring.cpp b/src/stdplus/io_uring.cpp
index eaa2848..7acf2c7 100644
--- a/src/stdplus/io_uring.cpp
+++ b/src/stdplus/io_uring.cpp
@@ -12,6 +12,14 @@
namespace stdplus
{
+__kernel_timespec chronoToKTS(std::chrono::nanoseconds t) noexcept
+{
+ __kernel_timespec ts;
+ ts.tv_sec = std::chrono::floor<std::chrono::seconds>(t).count();
+ ts.tv_nsec = (t % std::chrono::seconds(1)).count();
+ return ts;
+}
+
IoUring::IoUring(size_t queue_size)
{
CHECK_RET(io_uring_queue_init(queue_size, &ring, 0), "io_uring_queue_init");
@@ -109,6 +117,20 @@
}
}
+void IoUring::wait()
+{
+ io_uring_cqe* cqe;
+ CHECK_RET(io_uring_wait_cqe(&ring, &cqe), "io_uring_wait_cqe");
+}
+
+void IoUring::wait(std::chrono::nanoseconds timeout)
+{
+ io_uring_cqe* cqe;
+ auto kts = chronoToKTS(timeout);
+ CHECK_RET(io_uring_wait_cqe_timeout(&ring, &cqe, &kts),
+ "io_uring_wait_cqe_timeout");
+}
+
stdplus::ManagedFd& IoUring::getEventFd()
{
if (event_fd)