Condy v1.6.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"
13#include "condy/context.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) noexcept {
34 // This helper will only be called inside the coroutine context, so it's
35 // safe to assume that the runtime is available.
36 assert(detail::Context::current().runtime() != nullptr);
37 co_spawn(func(std::forward<decltype(res)>(res))).detach();
38 }
39 CoroFunc func;
40};
41
42template <typename Channel> struct PushHelper {
43 void operator()(auto &&res) noexcept {
44 channel.force_push(std::forward<decltype(res)>(res));
45 }
46 Channel &channel;
47};
48
49} // namespace detail
50
60template <typename CoroFunc> auto will_spawn(CoroFunc &&coro) {
61 return detail::SpawnHelper<std::decay_t<CoroFunc>>{
62 std::forward<CoroFunc>(coro)};
63}
64
75template <typename Channel> auto will_push(Channel &channel) {
76 return detail::PushHelper<Channel>{channel};
77}
78
79namespace detail {
80
81struct FixedFd {
82 int value;
83 operator int() const { return value; }
84};
85
86template <typename T> struct FixedBuffer {
87 T value;
88 int buf_index;
89};
90
91} // namespace detail
92
101inline auto fixed(int fd) { return detail::FixedFd{fd}; }
102
112template <BufferLike Buffer> auto fixed(int buf_index, Buffer &&buf) {
113 return detail::FixedBuffer<Buffer>{std::forward<Buffer>(buf), buf_index};
114}
115
124inline auto fixed(int buf_index, const struct iovec *iov) {
125 return detail::FixedBuffer<const iovec *>{iov, buf_index};
126}
127
136inline auto fixed(int buf_index, const struct msghdr *msg) {
137 return detail::FixedBuffer<const msghdr *>{msg, buf_index};
138}
139
140} // namespace condy
Thread-safe bounded channel for communication and synchronization.
Definition channel.hpp:40
The main namespace for the Condy library.
Definition condy.hpp:30
auto will_spawn(CoroFunc &&coro)
Helper to build an invocable that spawns a coroutine on invocation.
Definition helpers.hpp:60
Task< T, Allocator > co_spawn(Runtime &runtime, Coro< T, Allocator > coro) noexcept
Spawn a coroutine as a task in the given runtime.
Definition task.hpp:266
auto fixed(int fd)
Mark a file descriptor as fixed for io_uring operations.
Definition helpers.hpp:101
auto will_push(Channel &channel)
Helper to build an invocable that pushes the result to a channel on invocation.
Definition helpers.hpp:75