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