-
Notifications
You must be signed in to change notification settings - Fork 3
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
Implement separate signal and message flows #66
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb8250d
Implement separate signal and message flows
s0me0ne-unkn0wn 7bf13f5
Fight data races
s0me0ne-unkn0wn 7171cf9
`cargo fmt`
s0me0ne-unkn0wn 315cd35
Leave only signal receiving separate endpoint
s0me0ne-unkn0wn a560af3
Add an example
s0me0ne-unkn0wn 54f8803
Add error handling
s0me0ne-unkn0wn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ pub use futures::{ | |
future::{BoxFuture, Fuse, Future}, | ||
poll, select, | ||
stream::{self, select, select_with_strategy, FuturesUnordered, PollNext}, | ||
task::{Context, Poll}, | ||
task::{AtomicWaker, Context, Poll}, | ||
FutureExt, StreamExt, | ||
}; | ||
#[doc(hidden)] | ||
|
@@ -217,22 +217,54 @@ pub type SubsystemIncomingMessages<M> = self::stream::SelectWithStrategy< | |
(), | ||
>; | ||
|
||
#[derive(Debug, Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing documentation of its purpose. |
||
struct SignalsReceivedInner { | ||
waker: AtomicWaker, | ||
value: AtomicUsize, | ||
} | ||
|
||
/// Future to wait on for the watermark predicate | ||
pub struct SignalsReceivedWaiter<'a, F: Fn(usize) -> bool> { | ||
owner: &'a SignalsReceivedInner, | ||
predicate: F, | ||
} | ||
|
||
impl<F: Fn(usize) -> bool> Future for SignalsReceivedWaiter<'_, F> { | ||
type Output = usize; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.owner.waker.register(cx.waker()); | ||
let value = self.owner.value.load(atomic::Ordering::Acquire); | ||
if (self.predicate)(value) { | ||
Poll::Ready(value) | ||
} else { | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
|
||
/// Watermark to track the received signals. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct SignalsReceived(Arc<AtomicUsize>); | ||
pub struct SignalsReceived(Arc<SignalsReceivedInner>); | ||
|
||
impl SignalsReceived { | ||
/// Load the current value of received signals. | ||
pub fn load(&self) -> usize { | ||
// It's imperative that we prevent reading a stale value from memory because of reordering. | ||
// Memory barrier to ensure that no reads or writes in the current thread before this load are reordered. | ||
// All writes in other threads using release semantics become visible to the current thread. | ||
self.0.load(atomic::Ordering::Acquire) | ||
self.0.value.load(atomic::Ordering::Acquire) | ||
} | ||
|
||
/// Increase the number of signals by one. | ||
pub fn inc(&self) { | ||
let _previous = self.0.fetch_add(1, atomic::Ordering::AcqRel); | ||
let _previous = self.0.value.fetch_add(1, atomic::Ordering::AcqRel); | ||
self.0.waker.wake(); | ||
} | ||
|
||
/// Wait until a predicate for the watermark is true. | ||
pub fn wait_until<F: Fn(usize) -> bool>(&self, predicate: F) -> SignalsReceivedWaiter<F> { | ||
SignalsReceivedWaiter { owner: &self.0, predicate } | ||
} | ||
} | ||
|
||
|
@@ -416,9 +448,15 @@ pub trait SubsystemContext: Send + 'static { | |
/// using `pending!()` macro you will end up with a busy loop! | ||
async fn try_recv(&mut self) -> Result<Option<FromOrchestra<Self::Message, Self::Signal>>, ()>; | ||
|
||
/// Receive a message. | ||
/// Receive a signal or a message. | ||
async fn recv(&mut self) -> Result<FromOrchestra<Self::Message, Self::Signal>, Self::Error>; | ||
|
||
/// Receive a signal. | ||
async fn recv_signal(&mut self) -> Result<Self::Signal, Self::Error>; | ||
sandreim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Receive a message. | ||
async fn recv_msg(&mut self) -> Result<Self::Message, Self::Error>; | ||
|
||
/// Spawn a child task on the executor. | ||
fn spawn( | ||
&mut self, | ||
|
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.
This won't work, since it requires two borrows of self if both streams are to be polled.
Provide a method that produces two adapters.
where both
SplitOfMessageStream
andSplitOfSignalStream
doesimpl Stream
or exposesasync fn recv()
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.
That is exactly what was discussed earlier in this thread :)
This comment by @sandreim explains how it is supposed to work (and before he explained that I was doubting if it could work myself).