From a525e35a12eba21835c6979865dd26f02138dfa1 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Fri, 14 Mar 2025 07:55:33 -0600 Subject: [PATCH] :art: Refactor `msg::send` to use `incite_on` Problem: - `msg::send` rolls its own receiver and op state. The functionality is composable using `just | incite_on`. Solution: - Use the tools available in `async`. --- CMakeLists.txt | 2 +- include/msg/send.hpp | 78 +++++--------------------------------------- test/msg/send.cpp | 6 ++-- 3 files changed, 12 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c67ea635..e96d8e4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ add_versioned_package("gh:boostorg/mp11#boost-1.83.0") fmt_recipe(11.1.3) add_versioned_package("gh:intel/cpp-baremetal-concurrency#7c5b26c") add_versioned_package("gh:intel/cpp-std-extensions#7725142") -add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#6acab81") +add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#0525974") set(GEN_STR_CATALOG ${CMAKE_CURRENT_LIST_DIR}/tools/gen_str_catalog.py diff --git a/include/msg/send.hpp b/include/msg/send.hpp index e8ce03b7..0fa2c7cb 100644 --- a/include/msg/send.hpp +++ b/include/msg/send.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include #include @@ -17,65 +19,12 @@ namespace msg { namespace _send_recv { -template -using scheduler_sender = decltype(std::declval().schedule()); - template concept valid_send_action = requires { typename std::remove_cvref_t::is_send_action; }; -template -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -struct op_state { - template S, typename Sch, typename R> - constexpr op_state(S &&s, Sch &&sch, R &&r) - : send(s), ops{async::connect(std::forward(sch).schedule(), - std::forward(r))} {} - constexpr op_state(op_state &&) = delete; - - using ops_t = async::connect_result_t, Rcvr>; - - constexpr auto start() & -> void { - async::start(ops); - send(); - } - - SA send; - ops_t ops; -}; - -template struct sender { - using is_sender = void; - - [[no_unique_address]] SA send; - [[no_unique_address]] Sched sched; - - public: - template - [[nodiscard]] constexpr auto - connect(R &&r) && -> op_state> { - async::check_connect(); - return {std::move(send), std::move(sched), std::forward(r)}; - } - - template - [[nodiscard]] constexpr auto - connect(R &&r) const & -> op_state> { - async::check_connect(); - return {send, sched, std::forward(r)}; - } - - template - [[nodiscard]] constexpr static auto get_completion_signatures(Env const &) - -> async::completion_signatures_of_t, Env> { - return {}; - } -}; - -template -sender(Sndr, Sched) -> sender; - -template struct send_action : F { +template struct send_action { + [[no_unique_address]] F f; using is_send_action = void; }; template send_action(F) -> send_action; @@ -88,9 +37,8 @@ template struct pipeable { template Self> friend constexpr auto operator|(S &&s, Self &&self) -> async::sender auto { - return _send_recv::sender{ - std::forward(s), - async::trigger_scheduler{}} | + return async::just(std::forward(s).f) | + async::incite_on(async::trigger_scheduler{}) | std::forward(self).a; } }; @@ -102,8 +50,8 @@ template struct pipeable { template constexpr auto send(F &&f, Args &&...args) { return _send_recv::send_action{ - [f = std::forward(f), ... args = std::forward(args)]() { - return f(args...); + [f = std::forward(f), ... as = std::forward(args)] { + return std::move(f)(std::move(as)...); }}; } @@ -122,14 +70,4 @@ template (s) | then_receive(std::forward(f), std::forward(args)...); } - -struct send_t; } // namespace msg - -template -struct async::debug::context_for> { - using tag = msg::send_t; - constexpr static auto name = stdx::ct_string{"msg_send"}; - using type = msg::_send_recv::op_state; - using children = stdx::type_list<>; -}; diff --git a/test/msg/send.cpp b/test/msg/send.cpp index 674bac0f..650eeca1 100644 --- a/test/msg/send.cpp +++ b/test/msg/send.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include @@ -29,7 +29,7 @@ TEMPLATE_TEST_CASE("request-response", "[send]", decltype([] {})) { msg::then_receive( [&](auto recvd, auto x) { var = recvd + x; }, 17); CHECK(var == 0); - CHECK(async::start_detached_unstoppable(s)); + CHECK(async::sync_wait(s)); CHECK(var == 59); } @@ -77,6 +77,6 @@ TEST_CASE("request-response through handler", "[send]") { 0x80) | msg::then_receive<"cb", msg_view_t>( [&](auto v) { var = v.get("id"_f); }); - CHECK(async::start_detached_unstoppable(s)); + CHECK(async::sync_wait(s)); CHECK(var == 0x80); }