Condy v1.8
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
runtime.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
10#include "condy/detail/ring.hpp"
12#include <atomic>
13#include <cerrno>
14#include <cstddef>
15#include <cstdint>
16#include <cstring>
17#include <limits>
18
19namespace condy {
20namespace detail {
21
22class ThreadLocalRing : public ThreadLocalSingleton<ThreadLocalRing> {
23public:
24 Ring *ring() { return &ring_; }
25
26 ThreadLocalRing() : ring_(create_ring_()) {}
27
28private:
29 // NOLINTNEXTLINE(bugprone-exception-escape)
30 static Ring create_ring_() noexcept {
31 io_uring_params params = {};
32 params.flags |= IORING_SETUP_CLAMP;
33 params.flags |= IORING_SETUP_SINGLE_ISSUER;
34 params.flags |= IORING_SETUP_SUBMIT_ALL;
35 // If we can construct Runtime, we should be able to construct this
36 // thread-local ring. So we ignore errors here.
37 return Ring(8, &params, nullptr, 0, std::numeric_limits<size_t>::max());
38 }
39
40private:
41 Ring ring_;
42};
43
44inline int sync_msg_ring(io_uring_sqe *sqe_data) noexcept {
45#if !IO_URING_CHECK_VERSION(2, 12) // >= 2.12
46 return io_uring_register_sync_msg(sqe_data);
47#else
48 auto *ring = ThreadLocalRing::current().ring();
49 auto *sqe = ring->get_sqe();
50 *sqe = *sqe_data;
51 int r = 0;
52 auto n =
53 ring->reap_completions_wait([&](io_uring_cqe *cqe) { r = cqe->res; });
54 if (n < 0) {
55 return static_cast<int>(n);
56 }
57 assert(n == 1);
58 return r;
59#endif
60}
61
62class CancelRequest {
63public:
64 CancelRequest(uintptr_t data) : data_(data) {}
65
66 void wait() noexcept {
67 while (!finished_.load(std::memory_order_acquire)) {
68 finished_.wait(false, std::memory_order_relaxed);
69 }
70 }
71
72 void notify() noexcept {
73 finished_.store(true, std::memory_order_release);
74 finished_.notify_one();
75 }
76
77 uintptr_t data() const noexcept { return data_; }
78
79private:
80 uintptr_t data_;
81 std::atomic_bool finished_ = false;
82};
83
84class OpFinishHandleBase {
85public:
86 using HandleFunc = bool (*)(void *, io_uring_cqe *) noexcept;
87
88 bool handle(io_uring_cqe *cqe) noexcept {
89 assert(handle_func_ != nullptr);
90 return handle_func_(this, cqe);
91 }
92
93protected:
94 OpFinishHandleBase() = default;
95
96protected:
97 HandleFunc handle_func_ = nullptr;
98};
99
100} // namespace detail
101} // namespace condy
The main namespace for the Condy library.
Definition condy.hpp:37
Wrapper classes for liburing interfaces.