Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
IoUringProactor.hpp
Go to the documentation of this file.
1#pragma once
4#include <liburing.h>
5#include <atomic>
6#include <mutex>
7#include <functional>
8#include <thread>
9#include <vector>
10
11namespace orbit::network {
12
13class IoUringProactor : public Proactor {
14public:
15 IoUringProactor(unsigned entries = 1024);
16 ~IoUringProactor() override;
17
18 void run_once(int timeout_ms) override;
19
20 void async_read(socket_t fd, void* buffer, size_t size, std::function<void(ssize_t)> callback) override;
21 void async_write(socket_t fd, const void* buffer, size_t size, std::function<void(ssize_t)> callback) override;
22 void async_wait_read(socket_t fd, std::function<void()> callback) override;
23 void async_wait_write(socket_t fd, std::function<void()> callback) override;
24 void async_sendfile(socket_t out_fd, int in_fd, off_t offset, size_t count, std::function<void(ssize_t)> callback) override;
25 void async_accept(socket_t fd, std::function<void(socket_t, sockaddr_in)> callback) override;
26 void async_connect(socket_t fd, const sockaddr_in& addr, std::function<void(int)> callback) override;
27
28 void remove(socket_t fd) override;
29
30private:
31 // Cancels and reaps everything in flight, then exits the ring (the destructor runs it on its own thread).
32 void teardown();
33 struct io_uring ring_;
34 std::mutex sq_mutex_;
35 // Contexts submitted to the kernel and not yet completed.
36 std::atomic<long> inflight_{0};
37 // IORING_FEAT_FAST_POLL (Linux 5.7+): a recv/send/accept that cannot
38 // complete yet arms an internal poll instead of blocking an io-wq worker
39 // thread. Without it, operations are punted to workers (IOSQE_ASYNC) so
40 // they never stall the submitting thread (#169).
41 bool fast_poll_{false};
42 unsigned io_flags() const;
43
44 // Only the loop thread (the one calling run_once) touches the ring.
45 // Other threads -- handlers on the worker pool, user threads -- queue
46 // their operation and wake the loop through an eventfd; the loop submits
47 // the queue in one batch. Submitting from those threads made io_uring
48 // run the operation's follow-up work on them (a sleeping pool worker),
49 // and a thread that exits can have its I/O cancelled (#169, #220).
50 // Set by the first run_once(), which also arms the wake-up read: a
51 // request is driven by the thread that submitted it, so even that read
52 // must come from the loop thread.
53 std::atomic<std::thread::id> owner_{};
54 int wake_fd_{-1};
55 uint64_t wake_value_{0};
56 std::mutex pending_mutex_;
57
58 enum class OpType {
59 WAKE,
60 READ,
61 WRITE,
65 ACCEPT,
67 };
68
69 struct IoContext {
71 int fd{-1};
72
73 std::function<void(ssize_t)> io_cb;
74 std::function<void()> wait_cb;
75 std::function<void(socket_t, sockaddr_in)> accept_cb;
76 std::function<void(int)> connect_cb;
77
78 sockaddr_in client_addr{};
79 socklen_t client_len{sizeof(sockaddr_in)};
80
81 // For sendfile fallback
82 int in_fd{-1};
83 off_t offset{0};
84 size_t count{0};
85 };
86
87 struct io_uring_sqe* get_sqe_safe();
88
89 using Prep = std::function<void(struct io_uring_sqe*)>;
90 struct Pending {
91 IoContext* ctx; // nullptr for requests without a completion (cancel)
93 unsigned flags;
94 };
95 std::vector<Pending> pending_;
96 IoContext wake_ctx_; // type set to WAKE in the constructor
97
98 // Submits now on the loop thread, otherwise queues and wakes the loop.
99 void submit(IoContext* ctx, Prep prep, unsigned flags = 0);
100 // With sq_mutex_ held: prepares one request (no io_uring_submit).
101 void prepare_locked(IoContext* ctx, const Prep& prep, unsigned flags);
104};
105
106} // namespace network
Definition IoUringProactor.hpp:13
void async_wait_read(socket_t fd, std::function< void()> callback) override
void async_wait_write(socket_t fd, std::function< void()> callback) override
std::mutex sq_mutex_
Definition IoUringProactor.hpp:34
struct io_uring ring_
Definition IoUringProactor.hpp:33
std::atomic< std::thread::id > owner_
Definition IoUringProactor.hpp:53
void prepare_locked(IoContext *ctx, const Prep &prep, unsigned flags)
void async_sendfile(socket_t out_fd, int in_fd, off_t offset, size_t count, std::function< void(ssize_t)> callback) override
bool fast_poll_
Definition IoUringProactor.hpp:41
int wake_fd_
Definition IoUringProactor.hpp:54
std::vector< Pending > pending_
Definition IoUringProactor.hpp:95
void async_write(socket_t fd, const void *buffer, size_t size, std::function< void(ssize_t)> callback) override
void remove(socket_t fd) override
void run_once(int timeout_ms) override
IoContext wake_ctx_
Definition IoUringProactor.hpp:96
IoUringProactor(unsigned entries=1024)
std::atomic< long > inflight_
Definition IoUringProactor.hpp:36
struct io_uring_sqe * get_sqe_safe()
std::function< void(struct io_uring_sqe *)> Prep
Definition IoUringProactor.hpp:89
std::mutex pending_mutex_
Definition IoUringProactor.hpp:56
void async_connect(socket_t fd, const sockaddr_in &addr, std::function< void(int)> callback) override
void submit(IoContext *ctx, Prep prep, unsigned flags=0)
uint64_t wake_value_
Definition IoUringProactor.hpp:55
OpType
Definition IoUringProactor.hpp:58
void async_accept(socket_t fd, std::function< void(socket_t, sockaddr_in)> callback) override
void async_read(socket_t fd, void *buffer, size_t size, std::function< void(ssize_t)> callback) override
Definition Proactor.hpp:9
Definition Http2Session.hpp:21
::ssize_t ssize_t
Definition PlatformSocket.hpp:44
int socket_t
Definition PlatformSocket.hpp:39
::socklen_t socklen_t
Definition PlatformSocket.hpp:43
Definition IoUringProactor.hpp:69
int in_fd
Definition IoUringProactor.hpp:82
std::function< void(ssize_t)> io_cb
Definition IoUringProactor.hpp:73
std::function< void(socket_t, sockaddr_in)> accept_cb
Definition IoUringProactor.hpp:75
int fd
Definition IoUringProactor.hpp:71
std::function< void()> wait_cb
Definition IoUringProactor.hpp:74
size_t count
Definition IoUringProactor.hpp:84
sockaddr_in client_addr
Definition IoUringProactor.hpp:78
OpType type
Definition IoUringProactor.hpp:70
std::function< void(int)> connect_cb
Definition IoUringProactor.hpp:76
socklen_t client_len
Definition IoUringProactor.hpp:79
off_t offset
Definition IoUringProactor.hpp:83
Definition IoUringProactor.hpp:90
unsigned flags
Definition IoUringProactor.hpp:93
IoContext * ctx
Definition IoUringProactor.hpp:91
Prep prep
Definition IoUringProactor.hpp:92