ublk-cpp v0.0
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "ublk/query.hpp"
9#include "ublk/ublk_cmd.h"
10#include <condy.hpp>
11
12namespace ublk {
13namespace detail {
14
15namespace ex = condy::detail::ex;
16
17template <typename Sched, typename Alloc> struct TaskEnv {
18 using start_scheduler_type = Sched;
19 using allocator_type = Alloc;
20
21 const ublksrv_ctrl_dev_info *info = nullptr;
22
23 template <typename> using env_type = TaskEnv;
24
25 template <typename Env>
26 requires requires(const Env &env) { env.query(fetch_dev_info_t{}); }
27 constexpr TaskEnv(const Env &env) noexcept
28 : info(env.query(fetch_dev_info_t{})) {}
29
30 constexpr TaskEnv() noexcept = default;
31
32 [[nodiscard]]
33 constexpr auto query(fetch_dev_info_t) const noexcept {
34 return info;
35 }
36};
37
38struct get_allocator_with_default_t {
39 template <typename Env>
40 requires requires(Env const &env) { ex::get_allocator(env); }
41 constexpr auto operator()(Env const &env) const noexcept {
42 return ex::get_allocator(env);
43 }
44
45 template <typename Env>
46 requires(!requires(Env const &env) { ex::get_allocator(env); })
47 constexpr std::allocator<std::byte> operator()(Env const &) const noexcept {
48 return {};
49 }
50};
51
52inline constexpr get_allocator_with_default_t get_allocator_with_default{};
53
54template <typename TaskFn, typename... Args>
55auto task_invoke(TaskFn &&task_fn, Args &&...args) noexcept {
56 return ex::read_env(ex::get_start_scheduler) |
57 ex::let_value([task_fn = std::forward<TaskFn>(task_fn),
58 ... args = std::forward<Args>(args)](
59 const auto &sched) noexcept {
60 using Sched = std::decay_t<decltype(sched)>;
61 return ex::read_env(get_allocator_with_default) |
62 ex::let_value([task_fn = std::move(task_fn),
63 ... args = std::move(args)](
64 const auto &alloc) mutable {
65 using Alloc = std::decay_t<decltype(alloc)>;
66 return std::move(task_fn)
67 .template invoke<Sched, Alloc>(
68 std::move(args)...);
69 });
70 });
71}
72
73} // namespace detail
74} // namespace ublk
The main namespace of the ublk-cpp library.
Definition ublk.hpp:21
Environment queries used by the library's senders.