-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add documentation for async_rw_mutex
#1356
Open
msimberg
wants to merge
2
commits into
pika-org:main
Choose a base branch
from
msimberg:async-rw-mutex-documentation
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.
+189
−35
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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 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 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 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,85 @@ | ||
// Copyright (c) 2024 ETH Zurich | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <pika/async_rw_mutex.hpp> | ||
#include <pika/execution.hpp> | ||
#include <pika/init.hpp> | ||
|
||
#include <fmt/printf.h> | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
namespace ex = pika::execution::experimental; | ||
namespace tt = pika::this_thread::experimental; | ||
|
||
pika::start(argc, argv); | ||
ex::thread_pool_scheduler sched{}; | ||
|
||
{ | ||
// Below we will access the value proteced by the mutex with the | ||
// following implied dependency graph: | ||
// | ||
// /--> ro_access1 --\ | ||
// rw_access1 +---> ro_access2 ---+---> rw_access2 | ||
// \--> ro_access3 --/ | ||
// | ||
// Note that the senders themselves don't depend on each other | ||
// explicitly as above, but the senders provided by the mutex enforce | ||
// the given order. | ||
ex::async_rw_mutex<int> m{0}; | ||
|
||
// This read-write access is guaranteed to not run concurrently with any | ||
// other accesses. It will also run first since we requested the sender | ||
// first from the mutex. | ||
auto rw_access1 = | ||
m.readwrite() | ex::continues_on(sched) | ex::then([](auto w) { | ||
w.get() = 13; | ||
fmt::print("updated value to {}\n", w.get()); | ||
}); | ||
|
||
// These read-only accesses can only read the value, but they can run | ||
// concurrently. They'll see the write from the access above. | ||
auto ro_access1 = | ||
m.read() | ex::continues_on(sched) | ex::then([](auto w) { | ||
Check notice on line 49 in examples/documentation/async_rw_mutex_documentation.cpp Codacy Production / Codacy Static Code Analysisexamples/documentation/async_rw_mutex_documentation.cpp#L49
|
||
static_assert(std::is_const_v< | ||
std::remove_reference_t<decltype(w.get())>>); | ||
fmt::print("value is now {}\n", w.get()); | ||
}); | ||
auto ro_access2 = | ||
m.read() | ex::continues_on(sched) | ex::then([](auto w) { | ||
static_assert(std::is_const_v< | ||
std::remove_reference_t<decltype(w.get())>>); | ||
fmt::print("value is {} here as well\n", w.get()); | ||
}); | ||
auto ro_access3 = | ||
m.read() | ex::continues_on(sched) | ex::then([](auto w) { | ||
static_assert(std::is_const_v< | ||
std::remove_reference_t<decltype(w.get())>>); | ||
fmt::print("and {} here too\n", w.get()); | ||
}); | ||
|
||
// This read-write access will run once all the above read-only accesses | ||
// are done. | ||
auto rw_access2 = | ||
m.readwrite() | ex::continues_on(sched) | ex::then([](auto w) { | ||
w.get() = 42; | ||
fmt::print("value is {} at the end\n", w.get()); | ||
}); | ||
|
||
// Start and wait for all the work to finish. | ||
tt::sync_wait(ex::when_all(std::move(rw_access1), std::move(ro_access1), | ||
std::move(ro_access2), std::move(ro_access3), | ||
std::move(rw_access2))); | ||
} | ||
|
||
pika::finalize(); | ||
pika::stop(); | ||
|
||
return 0; | ||
} |
This file contains 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.
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.
Question: in principle, given that DAG, can we
sync_wait
just the last readwrite access?My doubt is this one. They do not depend on each other as sender, but the mutex implicitly creates a dependency. This "difference" (if it is a difference, or is it just conceptual?) requires that we start each sender independently, or we can just
sync_wait
the last one and all the chain is started as if they were actually dependent?