Optional Recursive Mutex in Asio Integration#1846
Open
RobertLeahy wants to merge 2 commits intoNVIDIA:mainfrom
Open
Optional Recursive Mutex in Asio Integration#1846RobertLeahy wants to merge 2 commits intoNVIDIA:mainfrom
RobertLeahy wants to merge 2 commits intoNVIDIA:mainfrom
Conversation
Reduces the size of a "frame" by removing the std::unique_lock member variable and making the "frame" itself the lock guard. This is the lock management method shown when presenting asioexec::completion_token in the CppCon 2025 talk "std::execution in Asio Codebases: Adopting Senders Without a Rewrite."
For general purpose (i.e. potentially multithreaded) use the asynchronous operations which result when passing the asioexec:: completion_token and ::use_sender completion tokens must use a recursive mutex internally. However if the user knows that no multithreaded use will occur this recursive mutex is pure overhead. Provided the asioexec::thread_unsafe_completion_token and _use_sender completion tokens which do not make use of a recursive mutex for the aforementioned use case.
ericniebler
approved these changes
Feb 14, 2026
| Receiver r_; | ||
| asio_impl::cancellation_signal signal_; | ||
| std::recursive_mutex m_; | ||
| [[no_unique_address]] |
Collaborator
There was a problem hiding this comment.
in operation states, this should be STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS
Comment on lines
+209
to
+215
| const auto prev = self.frames_->prev_; | ||
| self.frames_->prev_ = nullptr; | ||
| self.frames_ = prev; | ||
| self.m_.unlock(); | ||
| if (!prev) { | ||
| break; | ||
| } |
Collaborator
There was a problem hiding this comment.
why not use std::exchange?
Suggested change
| const auto prev = self.frames_->prev_; | |
| self.frames_->prev_ = nullptr; | |
| self.frames_ = prev; | |
| self.m_.unlock(); | |
| if (!prev) { | |
| break; | |
| } | |
| self.frames_ = std::exchange(self.frames_->prev_, nullptr); | |
| self.m_.unlock(); | |
| if (!self.frames_) { | |
| break; | |
| } |
| @@ -326,7 +343,7 @@ namespace asioexec { | |||
| [&](auto&&... args) { | |||
| std::invoke( | |||
Collaborator
There was a problem hiding this comment.
STDEXEC::__invoke compiles about ~2x faster than libstdc++'s std::invoke.
Comment on lines
+164
to
+167
| : self_([&]() noexcept { | ||
| self.m_.lock(); | ||
| return &self; | ||
| }()) |
Collaborator
There was a problem hiding this comment.
lambdas are expensive at compile time. this could simply be:
Suggested change
| : self_([&]() noexcept { | |
| self.m_.lock(); | |
| return &self; | |
| }()) | |
| : self_((self.m_.lock(), &self)) |
or if you find that distasteful, you could use a delegating constructor:
explicit frame_(operation_state_base& self) noexcept
: frame_(std::unique_lock(self.m_), self) {
}
private:
explicit frame_(std::unique_lock<Mutex> lk, operation_state_base& self) noexcept
: self_(&self) {
lk.release();
}| }; | ||
|
|
||
| template <typename Mutex> | ||
| struct t { |
Collaborator
There was a problem hiding this comment.
please give this a more descriptive name
|
|
||
| template <typename Signatures, typename Receiver, typename Allocator> | ||
| template <typename Mutex, typename Signatures, typename Receiver, typename Allocator> | ||
| requires requires(const Receiver& r) { ::STDEXEC::get_allocator(::STDEXEC::get_env(r)); } |
Collaborator
There was a problem hiding this comment.
Suggested change
| requires requires(const Receiver& r) { ::STDEXEC::get_allocator(::STDEXEC::get_env(r)); } | |
| requires STDEXEC::__callable<STDEXEC::get_allocator_t, STDEXEC::env_of_t<Receiver>> |
| explicit sender(Sender) -> sender<Sender>; | ||
|
|
||
| template <typename Mutex> | ||
| struct t { |
Collaborator
|
/ok to test 72c179e |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Please do not squash.
Resolves #1781.