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