join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
io_ring_buffer.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_IO_RING_BUFFER_HPP
26#define JOIN_CORE_IO_RING_BUFFER_HPP
27
28// libjoin.
29#include <join/allocator.hpp>
30#include <join/error.hpp>
31#include <join/function.hpp>
32#include <join/memory.hpp>
33#include <join/utils.hpp>
34
35// C++.
36#include <atomic>
37
38// Libraries.
39#ifdef JOIN_HAS_IO_URING
40#include <liburing.h>
41#endif
42
43namespace join
44{
49 {
50 public:
51#ifdef JOIN_HAS_IO_URING
56 explicit IoRingBuffer (io_uring* ring) noexcept
57 : _uring (ring)
58 {
59 }
60#else
64 IoRingBuffer () noexcept = default;
65#endif
66
71 IoRingBuffer (const IoRingBuffer& other) = delete;
72
78 IoRingBuffer& operator= (const IoRingBuffer& other) = delete;
79
84 {
86 }
87
95 template <size_t Count, size_t Size>
97
102 int unregisterBuffer () noexcept;
103
104#ifndef JOIN_HAS_IO_URING
109 int select () noexcept;
110#endif
111
116 void recycle (uint16_t bid) noexcept;
117
121 void bind () noexcept
122 {
123 _armed.fetch_add (1, std::memory_order_relaxed);
124 }
125
129 void unbind () noexcept
130 {
131 _armed.fetch_sub (1, std::memory_order_release);
132 }
133
138 bool armed () const noexcept
139 {
140 return _armed.load (std::memory_order_acquire) != 0;
141 }
142
148 void* get (uint16_t bid) const noexcept
149 {
150 return _base + (bid * _size);
151 }
152
157 uint16_t group () const noexcept
158 {
159 return _group;
160 }
161
166 uint32_t size () const noexcept
167 {
168 return _size;
169 }
170
175 uint16_t count () const noexcept
176 {
177 return _count;
178 }
179
180 private:
182 static constexpr uint16_t _none = UINT16_MAX;
183
184#ifdef JOIN_HAS_IO_URING
186 io_uring* _uring = nullptr;
187
189 LocalMem _mapping;
190
192 io_uring_buf_ring* _ring = nullptr;
193#else
195 uint16_t _free = _none;
196#endif
197
199 char* _base = nullptr;
200
202 Function<void ()> _release;
203
205 uint32_t _size = 0;
206
208 uint16_t _count = 0;
209
211 uint16_t _group = 0;
212
214 std::atomic<uint32_t> _armed{0};
215 };
216}
217
218#ifdef JOIN_HAS_IO_URING
220#else
222#endif
223
224#endif
memory arena owning backend and managing one or more pools.
Definition memory.hpp:53
fixed-capacity move-only allocation-free alternative to std::function.
Definition function.hpp:44
pool of buffers provided to the I/O backend.
Definition io_ring_buffer.hpp:49
uint32_t size() const noexcept
get the size of a buffer.
Definition io_ring_buffer.hpp:166
IoRingBuffer & operator=(const IoRingBuffer &other)=delete
copy assignment operator.
void bind() noexcept
bind an operation to this buffer ring.
Definition io_ring_buffer.hpp:121
void recycle(uint16_t bid) noexcept
give a buffer back.
Definition io_ring_buffer_epoll.hpp:108
uint16_t group() const noexcept
get the buffer group id.
Definition io_ring_buffer.hpp:157
~IoRingBuffer()
destroy instance.
Definition io_ring_buffer.hpp:83
IoRingBuffer(const IoRingBuffer &other)=delete
copy constructor.
int unregisterBuffer() noexcept
withdraw the buffers and give them back to the arena.
Definition io_ring_buffer_epoll.hpp:69
IoRingBuffer() noexcept=default
create instance.
bool armed() const noexcept
check whether operations are still bound to this buffer ring.
Definition io_ring_buffer.hpp:138
void unbind() noexcept
unbind an operation from this buffer ring.
Definition io_ring_buffer.hpp:129
int select() noexcept
take a free buffer.
Definition io_ring_buffer_epoll.hpp:91
void * get(uint16_t bid) const noexcept
get the address of a buffer.
Definition io_ring_buffer.hpp:148
int registerBuffer(uint16_t group, LocalMem::Allocator< Count, Size > &arena)
provide the arena buffers to the backend.
Definition io_ring_buffer_epoll.hpp:30
uint16_t count() const noexcept
get the number of buffers.
Definition io_ring_buffer.hpp:175
local anonymous memory provider.
Definition memory.hpp:122
Definition acceptor.hpp:32