83 io_uring_params params;
84 std::memset(¶ms, 0,
sizeof(params));
86 params.flags |= IORING_SETUP_CLAMP;
87 params.flags |= IORING_SETUP_SINGLE_ISSUER;
88 params.flags |= IORING_SETUP_SUBMIT_ALL;
89 params.flags |= IORING_SETUP_R_DISABLED;
91 size_t ring_entries = options.sq_size_;
92 if (options.cq_size_ != 0) {
93 params.flags |= IORING_SETUP_CQSIZE;
94 params.cq_entries = options.cq_size_;
97 if (options.enable_iopoll_) {
98 params.flags |= IORING_SETUP_IOPOLL;
99#if !IO_URING_CHECK_VERSION(2, 9)
100 if (options.enable_hybrid_iopoll_) {
101 params.flags |= IORING_SETUP_HYBRID_IOPOLL;
106 if (options.enable_sqpoll_) {
107 params.flags |= IORING_SETUP_SQPOLL;
108 params.sq_thread_idle = options.sqpoll_idle_time_ms_;
109 if (options.sqpoll_thread_cpu_.has_value()) {
110 params.flags |= IORING_SETUP_SQ_AFF;
111 params.sq_thread_cpu = *options.sqpoll_thread_cpu_;
115 if (options.attach_wq_target_ !=
nullptr) {
116 params.flags |= IORING_SETUP_ATTACH_WQ;
117 params.wq_fd = options.attach_wq_target_->ring_.ring()->ring_fd;
120 if (options.enable_defer_taskrun_) {
121 params.flags |= IORING_SETUP_DEFER_TASKRUN;
122 params.flags |= IORING_SETUP_TASKRUN_FLAG;
125 if (options.enable_coop_taskrun_) {
126 params.flags |= IORING_SETUP_COOP_TASKRUN;
127 params.flags |= IORING_SETUP_TASKRUN_FLAG;
130 if (options.enable_sqe128_) {
131 params.flags |= IORING_SETUP_SQE128;
134 if (options.enable_cqe32_) {
135 params.flags |= IORING_SETUP_CQE32;
138#if !IO_URING_CHECK_VERSION(2, 13)
139 if (options.enable_sqe_mixed_) {
140 params.flags |= IORING_SETUP_SQE_MIXED;
144#if !IO_URING_CHECK_VERSION(2, 13)
145 if (options.enable_cqe_mixed_) {
146 params.flags |= IORING_SETUP_CQE_MIXED;
152#if !IO_URING_CHECK_VERSION(2, 5)
153 if (options.enable_no_mmap_) {
154 params.flags |= IORING_SETUP_NO_MMAP;
155 buf = options.no_mmap_buf_;
156 buf_size = options.no_mmap_buf_size_;
160 int r = ring_.init(ring_entries, ¶ms, buf, buf_size);
162 throw make_system_error(
"io_uring_queue_init_params", -r);
165 event_interval_ = options.event_interval_;
166 disable_register_ring_fd_ = options.disable_register_ring_fd_;
189 void notify() { async_waiter_.notify(ring_); }
191 void schedule(WorkInvoker *work) {
192 auto *runtime = detail::Context::current().runtime();
193 if (runtime ==
this) {
194 local_queue_.push_back(work);
198 auto state = state_.load();
199 if (runtime !=
nullptr && state == State::Enabled) {
201 io_uring_sqe *sqe = runtime->ring_.get_sqe();
202 prep_msg_ring_(sqe, work);
203 runtime->pend_work();
207#if !IO_URING_CHECK_VERSION(2, 12)
208 if (runtime ==
nullptr && state == State::Enabled) {
210 io_uring_sqe sqe = {};
211 prep_msg_ring_(&sqe, work);
212 [[maybe_unused]]
int r = io_uring_register_sync_msg(&sqe);
219 std::lock_guard<std::mutex> lock(mutex_);
220 bool need_notify = global_queue_.empty();
221 global_queue_.push_back(work);
228 void pend_work() { pending_works_++; }
230 void resume_work() { pending_works_--; }
241 State expected = State::Idle;
242 if (!state_.compare_exchange_strong(expected, State::Running)) {
243 throw std::logic_error(
"Runtime is already running or stopped");
245 auto d1 =
defer([
this]() { state_.store(State::Stopped); });
247 [[maybe_unused]]
int r;
248 r = io_uring_enable_rings(ring_.ring());
251 state_.store(State::Enabled);
253 if (!disable_register_ring_fd_) {
254 r = io_uring_register_ring_fd(ring_.ring());
256 throw make_system_error(
"io_uring_register_ring_fd", -r);
260 detail::Context::current().init(&ring_,
this);
261 auto d2 =
defer([]() { detail::Context::current().reset(); });
264 std::lock_guard<std::mutex> lock(mutex_);
265 flush_global_queue_();
271 if (tick_count_ % event_interval_ == 0) {
275 if (
auto *work = local_queue_.pop_front()) {
280 if (pending_works_ == 0) {
306 void flush_global_queue_() {
307 local_queue_.push_back(std::move(global_queue_));
308 async_waiter_.async_wait(ring_);
311 void prep_msg_ring_(io_uring_sqe *sqe, WorkInvoker *work) {
312 auto data = encode_work(work, WorkType::Schedule);
313 io_uring_prep_msg_ring(sqe, this->ring_.ring()->ring_fd, 0,
314 reinterpret_cast<uint64_t
>(data), 0);
315 io_uring_sqe_set_data(sqe, encode_work(
nullptr, WorkType::Schedule));
318 size_t flush_ring_() {
319 return ring_.reap_completions(
320 [
this](io_uring_cqe *cqe) { process_cqe_(cqe); });
323 size_t flush_ring_wait_() {
324 return ring_.reap_completions_wait(
325 [
this](io_uring_cqe *cqe) { process_cqe_(cqe); });
328 void process_cqe_(io_uring_cqe *cqe) {
329 auto *data_raw = io_uring_cqe_get_data(cqe);
330 auto [data, type] = decode_work(data_raw);
332 if (type == WorkType::Ignore) {
334 assert(cqe->res != -EINVAL);
335 }
else if (type == WorkType::Notify) {
336 if (cqe->res == -EOPNOTSUPP) {
341 std::lock_guard<std::mutex> lock(mutex_);
342 flush_global_queue_();
343 }
else if (type == WorkType::SendFd) {
345 if (
fd_table.fd_accepter_ ==
nullptr) [[unlikely]] {
346 throw std::logic_error(
"No way to accept sent fd");
348 uint64_t payload =
reinterpret_cast<uint64_t
>(data) >> 3;
352 int target_fd =
static_cast<int>(payload - 1);
355 }
else if (type == WorkType::Schedule) {
356 if (data ==
nullptr) {
357 assert(cqe->res == 0);
360 auto *work =
static_cast<WorkInvoker *
>(data);
362 local_queue_.push_back(work);
364 }
else if (type == WorkType::Common) {
365 auto *handle =
static_cast<OpFinishHandleBase *
>(data);
366 auto action = handle->handle_cqe(cqe);
367 if (action.op_finish) {
370 if (action.queue_work) {
371 local_queue_.push_back(handle);
374 assert(
false &&
"Invalid work type");
379 enum class State : uint8_t {
385 static_assert(std::atomic<State>::is_always_lock_free);
387 using WorkListQueue =
388 IntrusiveSingleList<WorkInvoker, &WorkInvoker::work_queue_entry_>;
392 detail::AsyncWaiter async_waiter_;
393 WorkListQueue global_queue_;
394 std::atomic_size_t pending_works_ = 1;
395 std::atomic<State> state_ = State::Idle;
398 WorkListQueue local_queue_;
400 size_t tick_count_ = 0;
403 size_t event_interval_ = 61;
404 bool disable_register_ring_fd_ =
false;