-
Notifications
You must be signed in to change notification settings - Fork 24
added a write-up of creating a simple sender #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dietmarkuehl
wants to merge
10
commits into
main
Choose a base branch
from
doc-create-a-sender
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b5458e5
added a write-up of creating a simple sender
dietmarkuehl ec687cf
address AI review feedback
dietmarkuehl 13bbb07
fixed formatting
dietmarkuehl 3137bda
fixed a trailing space in the .md file
dietmarkuehl 1ad75b6
fixed a problem with destroying the object while the operation is sto…
dietmarkuehl 730ade2
fix concurrent destruction prevention for stop_when
dietmarkuehl 12823a6
clang format
dietmarkuehl bad5162
fixed a missing header and some inconsistencies
dietmarkuehl 88c482a
Merge branch 'main' into doc-create-a-sender
dietmarkuehl ce9ba0d
tried to fix some issues with stop tokens
dietmarkuehl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // examples/tutorial/create-a-sender.cpp -*-C++-*- | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <iostream> | ||
| #include <optional> | ||
| #include <stack> | ||
| #ifdef BEMAN_HAS_MODULES | ||
| import beman.execution; | ||
| import beman.execution.detail; | ||
| #else | ||
| #include <beman/execution/execution.hpp> | ||
| #endif | ||
|
|
||
| namespace ex = beman::execution; | ||
|
|
||
| namespace { | ||
| template <typename T> | ||
| class asynchronous_stack { | ||
| struct node { | ||
| node* next{}; | ||
| virtual void complete(T) = 0; | ||
| }; | ||
| std::stack<T> stack; | ||
| node* awaiting{}; | ||
|
|
||
| template <ex::receiver Rcvr> | ||
| struct state : node { | ||
| using operation_state_concept = ex::operation_state_tag; | ||
| struct stop_fun { | ||
| state& st; | ||
| void operator()() noexcept { | ||
| std::cout << "stop callback start\n"; | ||
| state& s = this->st; | ||
| this->st.callback.reset(); | ||
| for (auto it{&this->st.self.awaiting}; it; it = &(*it)->next) { | ||
| if (*it == &this->st) { | ||
| *it = this->st.next; | ||
| break; | ||
| } | ||
| } | ||
| std::cout << "stop callback completing\n"; | ||
| ex::set_stopped(std::move(s.rcvr)); | ||
| std::cout << "stop callback done\n"; | ||
| } | ||
| }; | ||
| using stop_token_t = ex::stop_token_of_t<decltype(ex::get_env(std::declval<Rcvr&>()))>; | ||
| using callback_t = ex::stop_callback_for_t<stop_token_t, stop_fun>; | ||
| std::remove_cvref_t<Rcvr> rcvr; | ||
| asynchronous_stack& self; | ||
| std::optional<callback_t> callback; | ||
| state(Rcvr&& r, asynchronous_stack& s) : rcvr(std::forward<Rcvr>(r)), self(s) {} | ||
| void start() & noexcept { | ||
| if (not this->self.stack.empty()) { | ||
| T value(std::move(this->self.stack.top())); | ||
| this->self.stack.pop(); | ||
| ex::set_value(std::move(rcvr), std::move(value)); | ||
| } else { | ||
| this->next = std::exchange(this->self.awaiting, this); | ||
| this->callback.emplace(ex::get_stop_token(ex::get_env(this->rcvr)), stop_fun{*this}); | ||
| } | ||
| } | ||
| void complete(T value) override { | ||
| this->callback.reset(); | ||
| ex::set_value(std::move(rcvr), std::move(value)); | ||
| } | ||
| }; | ||
|
|
||
| public: | ||
| struct pop_sender { | ||
| using sender_concept = ex::sender_tag; | ||
| template <typename...> | ||
| static consteval auto get_completion_signatures() { | ||
| return ex::completion_signatures<ex::set_value_t(T), ex::set_stopped_t()>{}; | ||
| } | ||
|
|
||
| asynchronous_stack& self; | ||
| template <ex::receiver Rcvr> | ||
| auto connect(Rcvr&& rcvr) const { | ||
| static_assert(ex::operation_state<state<Rcvr>>); | ||
| return state<Rcvr>{std::forward<Rcvr>(rcvr), self}; | ||
| } | ||
| }; | ||
|
|
||
| void push(T value) { | ||
| if (this->awaiting) { | ||
| std::exchange(this->awaiting, this->awaiting->next)->complete(std::move(value)); | ||
| } else { | ||
| this->stack.push(std::move(value)); | ||
| } | ||
| } | ||
| pop_sender pop() { return pop_sender{*this}; } | ||
| }; | ||
|
|
||
| static_assert(ex::sender<asynchronous_stack<int>::pop_sender>); | ||
| static_assert(ex::sender_in<asynchronous_stack<int>::pop_sender>); | ||
|
|
||
| struct stop_test { | ||
| using sender_concept = ex::sender_tag; | ||
| template <typename...> | ||
| static consteval auto get_completion_signatures() { | ||
| return ex::completion_signatures<ex::set_value_t()>(); | ||
| } | ||
| template <typename Rcvr> | ||
| struct state { | ||
| using operation_state_concept = ex::operation_state_tag; | ||
|
|
||
| struct cb { | ||
| state* self; | ||
| auto operator()() const noexcept -> void { | ||
| std::cout << "cb\n"; | ||
| ex::set_value(std::move(self->rcvr)); | ||
| } | ||
| }; | ||
| using callback = ex::stop_callback_for_t<ex::stop_token_of_t<ex::env_of_t<Rcvr>>, cb>; | ||
|
|
||
| std::remove_cvref_t<Rcvr> rcvr; | ||
| std::optional<callback> callb; | ||
| auto start() & noexcept -> void { | ||
| std::cout << "stop_test start\n"; | ||
| this->callb.emplace(ex::get_stop_token(ex::get_env(this->rcvr)), cb{this}); | ||
| } | ||
| }; | ||
| template <typename Rcvr> | ||
| state<Rcvr> connect(Rcvr&& rcvr) const { | ||
| return {std::forward<Rcvr>(rcvr)}; | ||
| } | ||
| }; | ||
| } // namespace | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| int main() { | ||
| std::cout << std::unitbuf; | ||
| #if 1 | ||
| asynchronous_stack<int> st; | ||
| ex::counting_scope scope; | ||
| [[maybe_unused]] auto sender = st.pop() | ex::then([](int v) { std::cout << "got value=" << v << "\n"; }); | ||
|
|
||
| #if 1 | ||
| for (int value{1}; value < 4; ++value) { | ||
| st.push(value); | ||
| } | ||
| std::cout << "pushed 1,2,3\n"; | ||
|
|
||
| int count{8}; | ||
| for (int value{1}; value < count; ++value) { | ||
| ex::spawn(st.pop() | ex::then([value](int v) noexcept { | ||
| std::cout << "got value=" << v << " for request " << value << "\n"; | ||
| }) | ex::upon_stopped([value] noexcept { std::cout << "request " << value << " was stopped\n"; }), | ||
| scope.get_token()); | ||
| } | ||
|
|
||
| std::cout << "requested " << (count - 1) << " values\n"; | ||
|
|
||
| for (int value{4}; value < 7; ++value) { | ||
| st.push(value); | ||
| } | ||
| std::cout << "pushed 4,5,6\n"; | ||
| #endif | ||
|
|
||
| std::cout << "requesting stop\n"; | ||
| scope.request_stop(); | ||
| std::cout << "requested stop\n"; | ||
| ex::sync_wait(scope.join() | ex::then([] { std::cout << "joined\n"; })); | ||
| std::cout << "joined\n"; | ||
| #else | ||
|
|
||
| std::optional<ex::inplace_stop_source> source1{}; | ||
| source1.emplace(); | ||
| std::optional<ex::inplace_stop_source> source2{}; | ||
| source2.emplace(); | ||
| struct receiver { | ||
| using receiver_concept = ex::receiver_tag; | ||
| std::optional<ex::inplace_stop_source>& source1; | ||
| std::optional<ex::inplace_stop_source>& source2; | ||
| auto query(ex::get_stop_token_t) const noexcept { return this->source2->get_token(); } | ||
| auto get_env() const noexcept { | ||
| std::cout << "get_env\n"; | ||
| return *this; | ||
| } | ||
| auto set_value() noexcept { | ||
| std::cout << "receiver::set_value\n"; | ||
| source1.reset(); | ||
| std::cout << "receiver::set_value done\n"; | ||
| } | ||
| auto set_stopped() noexcept { std::cout << "receiver::set_stopped\n"; } | ||
| }; | ||
| #if 1 | ||
| auto sws(ex::connect(ex::detail::stop_when(stop_test(), source1->get_token()), receiver{source1, source2})); | ||
| #else | ||
| auto sws(ex::connect(stop_test(), receiver{source1, source2})); | ||
| #endif | ||
| std::cout << "start\n"; | ||
| ex::start(sws); | ||
| std::cout << "request stop\n"; | ||
| source1->request_stop(); | ||
| std::cout << "requested stop\n"; | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[pre-commit] reported by reviewdog 🐶