|
| 1 | +// examples/environment.cpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef _MSC_VER |
| 5 | +#include <beman/task/task.hpp> |
| 6 | +#include <beman/execution/execution.hpp> |
| 7 | +#include <beman/net/net.hpp> |
| 8 | +#include <chrono> |
| 9 | +#include <string> |
| 10 | +#include <thread> |
| 11 | +#include <utility> |
| 12 | +#include <type_traits> |
| 13 | +#include "demo-scope.hpp" |
| 14 | +#include "demo-thread_pool.hpp" |
| 15 | + |
| 16 | +namespace ex = beman::execution; |
| 17 | +namespace net = beman::net; |
| 18 | +using namespace std::chrono_literals; |
| 19 | + |
| 20 | +// ---------------------------------------------------------------------------- |
| 21 | + |
| 22 | +template <ex::scheduler Sched, ex::sender Sender> |
| 23 | +void spawn(Sched&& sched, demo::scope& scope, Sender&& sender) { |
| 24 | + scope.spawn(ex::detail::write_env(std::forward<Sender>(sender), |
| 25 | + ex::detail::make_env(ex::get_scheduler, std::forward<Sched>(sched)))); |
| 26 | +} |
| 27 | + |
| 28 | +// ---------------------------------------------------------------------------- |
| 29 | + |
| 30 | +class environment { |
| 31 | + static thread_local std::string name; |
| 32 | + |
| 33 | + public: |
| 34 | + static auto get() -> std::string { return name; } |
| 35 | + static auto set(const std::string& n) -> void { name = n; } |
| 36 | +}; |
| 37 | +thread_local std::string environment::name{"<none>"}; |
| 38 | + |
| 39 | +struct env_scheduler { |
| 40 | + using scheduler_concept = ex::scheduler_t; |
| 41 | + |
| 42 | + std::string name; |
| 43 | + ex::task_scheduler scheduler; |
| 44 | + |
| 45 | + template <typename Sched> |
| 46 | + env_scheduler(std::string n, Sched&& sched) : name(std::move(n)), scheduler(std::forward<Sched>(sched)) {} |
| 47 | + |
| 48 | + template <ex::receiver Rcvr> |
| 49 | + struct receiver { |
| 50 | + using receiver_concept = ex::receiver_t; |
| 51 | + |
| 52 | + std::remove_cvref_t<Rcvr> rcvr; |
| 53 | + std::string name; |
| 54 | + |
| 55 | + receiver(Rcvr&& r, std::string n) : rcvr(std::forward<Rcvr>(r)), name(std::move(n)) {} |
| 56 | + auto get_env() const noexcept { return ex::get_env(this->rcvr); } |
| 57 | + auto set_value() && noexcept { |
| 58 | + environment::set(std::move(this->name)); |
| 59 | + ex::set_value(std::move(this->rcvr)); |
| 60 | + } |
| 61 | + template <typename E> |
| 62 | + auto set_error(E&& e) && noexcept { |
| 63 | + environment::set(std::move(this->name)); |
| 64 | + ex::set_error(std::move(this->rcvr), std::forward<E>(e)); |
| 65 | + } |
| 66 | + auto set_stopped() && noexcept { |
| 67 | + environment::set(std::move(this->name)); |
| 68 | + ex::set_stopped(std::move(this->rcvr)); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + struct env { |
| 73 | + std::string name; |
| 74 | + ex::task_scheduler scheduler; |
| 75 | + template <typename Tag> |
| 76 | + auto query(const ex::get_completion_scheduler_t<Tag>&) const noexcept { |
| 77 | + return env_scheduler(this->name, this->scheduler); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + struct sender { |
| 82 | + using sender_concept = ex::sender_t; |
| 83 | + using task_sender = decltype(ex::schedule(std::declval<ex::task_scheduler>())); |
| 84 | + template <typename E> |
| 85 | + auto get_completion_signatures(const E& e) const noexcept { |
| 86 | + return ex::get_completion_signatures(this->sender, e); |
| 87 | + } |
| 88 | + |
| 89 | + std::string name; |
| 90 | + task_sender sender; |
| 91 | + |
| 92 | + auto get_env() const noexcept -> env { |
| 93 | + return env{this->name, ex::get_completion_scheduler<ex::set_value_t>(ex::get_env(this->sender))}; |
| 94 | + } |
| 95 | + |
| 96 | + template <ex::receiver Rcvr> |
| 97 | + auto connect(Rcvr&& rcvr) && { |
| 98 | + return ex::connect(std::move(this->sender), |
| 99 | + receiver<Rcvr>(std::forward<Rcvr>(rcvr), std::move(this->name))); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + auto schedule() -> sender { return sender{this->name, ex::schedule(this->scheduler)}; } |
| 104 | + bool operator==(const env_scheduler&) const = default; |
| 105 | +}; |
| 106 | + |
| 107 | +struct with_env { |
| 108 | + using scheduler_type = env_scheduler; |
| 109 | +}; |
| 110 | + |
| 111 | +// ---------------------------------------------------------------------------- |
| 112 | + |
| 113 | +std::ostream& print_env(std::ostream& out) { |
| 114 | + return out << "tid=" << std::this_thread::get_id() << " " |
| 115 | + << "env=" << environment::get(); |
| 116 | +} |
| 117 | + |
| 118 | +ex::task<void, with_env> run(auto scheduler, auto duration) { |
| 119 | + std::cout << print_env << " duration=" << duration << " start\n" << std::flush; |
| 120 | + for (int i = 0; i != 4; ++i) { |
| 121 | + co_await net::resume_after(scheduler, duration); |
| 122 | + std::cout << print_env << " duration=" << duration << "\n" << std::flush; |
| 123 | + } |
| 124 | + std::cout << print_env << " duration=" << duration << " done\n" << std::flush; |
| 125 | +} |
| 126 | + |
| 127 | +const std::string text("####"); |
| 128 | +[[maybe_unused]] const std::string black("\x1b[30m" + text + "\x1b[0m:"); |
| 129 | +[[maybe_unused]] const std::string red("\x1b[31m" + text + "\x1b[0m:"); |
| 130 | +[[maybe_unused]] const std::string green("\x1b[32m" + text + "\x1b[0m:"); |
| 131 | +[[maybe_unused]] const std::string yellow("\x1b[33m" + text + "\x1b[0m:"); |
| 132 | +[[maybe_unused]] const std::string blue("\x1b[34m" + text + "\x1b[0m:"); |
| 133 | +[[maybe_unused]] const std::string magenta("\x1b[35m" + text + "\x1b[0m:"); |
| 134 | +[[maybe_unused]] const std::string cyan("\x1b[36m" + text + "\x1b[0m:"); |
| 135 | +[[maybe_unused]] const std::string white("\x1b[37m" + text + "\x1b[0m:"); |
| 136 | + |
| 137 | +int main() { |
| 138 | + demo::thread_pool pool1; |
| 139 | + demo::thread_pool pool2; |
| 140 | + net::io_context context; |
| 141 | + demo::scope scope; |
| 142 | + |
| 143 | + environment::set("main"); |
| 144 | + |
| 145 | + ex::sync_wait(ex::schedule(pool1.get_scheduler()) | ex::then([] { environment::set("thread1"); }) | |
| 146 | + ex::then([] { std::cout << print_env << "\n"; })); |
| 147 | + std::cout << print_env << "\n"; |
| 148 | + |
| 149 | + spawn(env_scheduler(magenta, pool1.get_scheduler()), scope, run(context.get_scheduler(), 100ms)); |
| 150 | + spawn(env_scheduler(green, pool1.get_scheduler()), scope, run(context.get_scheduler(), 150ms)); |
| 151 | + spawn(env_scheduler(blue, pool1.get_scheduler()), scope, run(context.get_scheduler(), 250ms)); |
| 152 | + |
| 153 | + while (!scope.empty()) { |
| 154 | + context.run(); |
| 155 | + } |
| 156 | +} |
| 157 | +#else |
| 158 | +int main() {} |
| 159 | +#endif |
0 commit comments