22 Ring(
unsigned int entries, io_uring_params *params,
23 [[maybe_unused]]
void *buf, [[maybe_unused]]
size_t buf_size,
25 : submit_batch_(submit_batch) {
27#if !IO_URING_CHECK_VERSION(2, 5)
28 if (params->flags & IORING_SETUP_NO_MMAP) {
29 r = io_uring_queue_init_mem(entries, &ring_, params, buf, buf_size);
31 r = io_uring_queue_init_params(entries, &ring_, params);
34 r = io_uring_queue_init_params(entries, &ring_, params);
37 throw make_system_error(
"io_uring_queue_init_params", -r);
41 ~Ring() { io_uring_queue_exit(&ring_); }
43 CONDY_DELETE_COPY_MOVE(Ring);
46 void submit() noexcept {
47 maybe_submit_count_ = 0;
48 io_uring_submit(&ring_);
51 void maybe_submit() noexcept {
52 maybe_submit_count_++;
53 if (maybe_submit_count_ >= submit_batch_) {
58 template <
typename Func>
59 ssize_t reap_completions_wait(Func &&process_func)
noexcept {
64 int r = io_uring_submit_and_wait(&ring_, 1);
65 if (r >= 0) [[likely]] {
66 maybe_submit_count_ = 0;
68 }
else if (r == -EINTR) {
75 io_uring_for_each_cqe(&ring_, head, cqe) {
77#if !IO_URING_CHECK_VERSION(2, 13)
78 reaped += io_uring_cqe_nr(cqe);
83 io_uring_cq_advance(&ring_, reaped);
87 template <
typename Func>
88 ssize_t reap_completions(Func &&process_func)
noexcept {
90 int r = io_uring_peek_cqe(&ring_, &cqe);
99 io_uring_for_each_cqe(&ring_, head, cqe) {
101#if !IO_URING_CHECK_VERSION(2, 13)
102 reaped += io_uring_cqe_nr(cqe);
107 io_uring_cq_advance(&ring_, reaped);
111 void reserve_space(
size_t n)
noexcept {
114 space_left = io_uring_sq_space_left(&ring_);
115 if (space_left >= n) {
122 io_uring *ring() noexcept {
return &ring_; }
124 io_uring_sqe *get_sqe() noexcept {
return get_sqe_<io_uring_get_sqe>(); }
126#if !IO_URING_CHECK_VERSION(2, 13)
127 io_uring_sqe *get_sqe128() noexcept {
128 if (ring_.flags & (IORING_SETUP_SQE128 | IORING_SETUP_SQE_MIXED))
130 return get_sqe_<io_uring_get_sqe128>();
136 bool check_cqe32([[maybe_unused]] io_uring_cqe *cqe)
const noexcept {
137 auto ring_flags = ring_.flags;
138 if (ring_flags & IORING_SETUP_CQE32) {
141#if !IO_URING_CHECK_VERSION(2, 13)
142 if (ring_flags & IORING_SETUP_CQE_MIXED) {
143 return cqe->flags & IORING_CQE_F_32;
150 template <io_uring_sqe *(*get_sqe)(struct io_uring *)>
151 io_uring_sqe *get_sqe_() noexcept {
152 [[maybe_unused]]
int r;
155 sqe = get_sqe(&ring_);
160 if (ring_.flags & IORING_SETUP_SQPOLL) {
161 r = io_uring_sqring_wait(&ring_);
170 size_t submit_batch_;
171 size_t maybe_submit_count_ = 0;
The main namespace for the Condy library.
Internal utility classes and functions used by Condy.