]> git.sesse.net Git - plocate/blob - io_uring_engine.h
Run include-what-you-use.
[plocate] / io_uring_engine.h
1 #ifndef IO_URING_ENGINE_H
2 #define IO_URING_ENGINE_H 1
3
4 #include <stddef.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <functional>
8 #include <queue>
9 #include <string_view>
10
11 struct io_uring_sqe;
12 #ifndef WITHOUT_URING
13 #include <liburing.h>
14 #endif
15
16 class IOUringEngine {
17 public:
18         IOUringEngine(size_t slop_bytes);
19         void submit_read(int fd, size_t len, off_t offset, std::function<void(std::string_view)> cb);
20         void finish();
21         size_t get_waiting_reads() const { return pending_reads + queued_reads.size(); }
22
23 private:
24 #ifndef WITHOUT_URING
25         void submit_read_internal(io_uring_sqe *sqe, int fd, size_t len, off_t offset, std::function<void(std::string_view)> cb);
26
27         io_uring ring;
28 #endif
29         size_t pending_reads = 0;  // Number of requests we have going in the ring.
30         bool using_uring;
31         const size_t slop_bytes;
32
33         struct QueuedRead {
34                 int fd;
35                 size_t len;
36                 off_t offset;
37                 std::function<void(std::string_view)> cb;
38         };
39         std::queue<QueuedRead> queued_reads;
40
41         struct PendingRead {
42                 void *buf;
43                 size_t len;
44                 std::function<void(std::string_view)> cb;
45
46                 // For re-submission.
47                 int fd;
48                 off_t offset;
49                 iovec iov;
50         };
51
52         // 256 simultaneous requests should be ample, for slow and fast media alike.
53         static constexpr size_t queue_depth = 256;
54 };
55
56 // A wrapper around pread() that returns an incomplete read.
57 // Always synchronous (no io_uring).
58 void complete_pread(int fd, void *ptr, size_t len, off_t offset);
59
60 #endif  // !defined(IO_URING_ENGINE_H)