COROIO: coroio/uring.hpp Source File
COROIO
 
Loading...
Searching...
No Matches
uring.hpp
1#pragma once
2
3#include "base.hpp"
4#include "socket.hpp"
5#include "poller.hpp"
6
7#include <liburing.h>
8#include <assert.h>
9#include <sys/eventfd.h>
10#include <sys/epoll.h>
11#include <sys/utsname.h>
12
13#include <system_error>
14#include <iostream>
15#include <tuple>
16#include <vector>
17#include <coroutine>
18#include <queue>
19
20namespace NNet {
21
64class TUring: public TPollerBase {
65public:
75 TUring(int queueSize = 256);
86 void Read(int fd, void* buf, int size, std::coroutine_handle<> handle);
95 void Write(int fd, const void* buf, int size, std::coroutine_handle<> handle);
104 void Recv(int fd, void* buf, int size, std::coroutine_handle<> handle);
113 void Send(int fd, const void* buf, int size, std::coroutine_handle<> handle);
122 void Accept(int fd, struct sockaddr* addr, socklen_t* len, std::coroutine_handle<> handle);
131 void Connect(int fd, const sockaddr* addr, socklen_t len, std::coroutine_handle<> handle);
137 void Cancel(int fd);
143 void Cancel(std::coroutine_handle<> h);
149 void Register(int fd);
156 int Wait(timespec ts = {10,0});
162 void Poll() {
163 Wait(GetTimeout());
164 }
165
170 int Result();
172 void Submit();
173
179 std::tuple<int, int, int> Kernel() const;
185 const std::string& KernelStr() const;
186
187private:
195 io_uring_sqe* GetSqe() {
196 io_uring_sqe* r = io_uring_get_sqe(&Ring_);
197 if (!r) {
198 Submit();
199 r = io_uring_get_sqe(&Ring_);
200 }
201 return r;
202 }
203
204 int RingFd_;
205 int EpollFd_;
206 struct io_uring Ring_;
207 std::queue<int> Results_;
208 std::vector<char> Buffer_;
209 std::tuple<int, int, int> Kernel_;
210 std::string KernelStr_;
211};
212
213} // namespace NNet
timespec GetTimeout() const
Computes the poll timeout based on scheduled timers.
Definition poller.hpp:278
TPollerBase()=default
Default constructor.
Asynchronous file handle driven by the poller's implementation.
Definition socket.hpp:742
Socket type driven by the poller's implementation.
Definition socket.hpp:519
NNet::TPollerDrivenFileHandle< TUring > TFileHandle
Alias for the poller-driven file handle type.
Definition uring.hpp:69
int Result()
Retrieves the result of the last completed I/O completion.
int Wait(timespec ts={10, 0})
Waits for I/O completions.
const std::string & KernelStr() const
For testing: retrieves a string describing kernel.
void Cancel(int fd)
Cancels pending operations on the specified file descriptor.
void Read(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous read operation.
void Recv(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous receive operation.
~TUring()
Destructor cleans up the io_uring and related resources.
void Poll()
Polls for I/O completions.
Definition uring.hpp:162
void Send(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous send operation.
void Connect(int fd, const sockaddr *addr, socklen_t len, std::coroutine_handle<> handle)
Posts an asynchronous connect operation.
NNet::TPollerDrivenSocket< TUring > TSocket
Alias for the poller-driven socket type.
Definition uring.hpp:67
std::tuple< int, int, int > Kernel() const
For testing: retrieves kernel version.
void Write(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous write operation.
void Submit()
Submits queued I/O requests to the kernel.
TUring(int queueSize=256)
Constructs a TUring instance.
void Cancel(std::coroutine_handle<> h)
Cancels pending operations associated with a specific coroutine handle.
void Accept(int fd, struct sockaddr *addr, socklen_t *len, std::coroutine_handle<> handle)
Posts an asynchronous accept operation.
void Register(int fd)
Registers a file descriptor with the IO_uring poller.