Condy v1.1.0
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "condy/concepts.hpp"
12#include "condy/condy_uring.hpp"
14#include <type_traits>
15
16#if !IO_URING_CHECK_VERSION(2, 4) // >= 2.4
20#define CONDY_FILE_INDEX_ALLOC IORING_FILE_INDEX_ALLOC
21#else
25#define CONDY_FILE_INDEX_ALLOC (IORING_FILE_INDEX_ALLOC - 1)
26#endif
27
28namespace condy {
29
30namespace detail {
31
32template <typename CoroFunc> struct SpawnHelper {
33 void operator()(auto &&res) {
34 co_spawn(func(std::forward<decltype(res)>(res))).detach();
35 }
36 std::decay_t<CoroFunc> func;
37};
38
39template <typename Channel> struct PushHelper {
40 void operator()(auto &&res) {
41 channel.force_push(std::forward<decltype(res)>(res));
42 }
43 std::decay_t<Channel> &channel;
44};
45
46} // namespace detail
47
57template <typename CoroFunc> auto will_spawn(CoroFunc &&coro) {
58 return detail::SpawnHelper<std::decay_t<CoroFunc>>{
59 std::forward<CoroFunc>(coro)};
60}
61
72template <typename Channel> auto will_push(Channel &channel) {
73 return detail::PushHelper<std::decay_t<Channel>>{channel};
74}
75
76namespace detail {
77
78struct FixedFd {
79 int value;
80 operator int() const { return value; }
81};
82
83template <typename T> struct FixedBuffer {
84 T value;
85 int buf_index;
86};
87
88} // namespace detail
89
98inline auto fixed(int fd) { return detail::FixedFd{fd}; }
99
109template <BufferLike Buffer> auto fixed(int buf_index, Buffer &&buf) {
110 return detail::FixedBuffer<Buffer>{std::forward<Buffer>(buf), buf_index};
111}
112
121inline auto fixed(int buf_index, const struct iovec *iov) {
122 return detail::FixedBuffer<const iovec *>{iov, buf_index};
123}
124
133inline auto fixed(int buf_index, const struct msghdr *msg) {
134 return detail::FixedBuffer<const msghdr *>{msg, buf_index};
135}
136
147 return static_cast<BundledProvidedBufferPool &>(buffer);
148}
149
157 return static_cast<BundledProvidedBufferQueue &>(buffer);
158}
159
160} // namespace condy
Thread-safe bounded channel for communication and synchronization.
Definition channel.hpp:33
Provided buffer pool.
Definition provided_buffers.hpp:408
Provided buffer queue.
Definition provided_buffers.hpp:177
The main namespace for the Condy library.
Definition condy.hpp:28
auto will_spawn(CoroFunc &&coro)
Helper to build an invocable that spawns a coroutine on invocation.
Definition helpers.hpp:57
auto & bundled(ProvidedBufferPool &buffer)
Get the bundled variant of a provided buffer pool. This will enable buffer bundling feature of io_uri...
Definition helpers.hpp:146
auto fixed(int fd)
Mark a file descriptor as fixed for io_uring operations.
Definition helpers.hpp:98
MutableBuffer buffer(void *data, size_t size)
Create a buffer object from various data sources.
Definition buffers.hpp:84
auto will_push(Channel &channel)
Helper to build an invocable that pushes the result to a channel on invocation.
Definition helpers.hpp:72
Task< T, Allocator > co_spawn(Runtime &runtime, Coro< T, Allocator > coro)
Spawn a coroutine as a task in the given runtime.
Definition task.hpp:240
Support for io_uring provided buffers.