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