Skip to content
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

sync, coop: apply cooperative scheduling to sync::broadcast::Receiver #6870

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tokio/src/sync/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
use crate::loom::cell::UnsafeCell;
use crate::loom::sync::atomic::{AtomicBool, AtomicUsize};
use crate::loom::sync::{Arc, Mutex, MutexGuard, RwLock, RwLockReadGuard};
use crate::runtime::coop::cooperative;
use crate::util::linked_list::{self, GuardedLinkedList, LinkedList};
use crate::util::WakeList;

Expand Down Expand Up @@ -1262,8 +1263,7 @@ impl<T: Clone> Receiver<T> {
/// }
/// ```
pub async fn recv(&mut self) -> Result<T, RecvError> {
let fut = Recv::new(self);
fut.await
cooperative(Recv::new(self)).await
}

/// Attempts to return a pending value on this receiver without awaiting.
Expand Down
16 changes: 16 additions & 0 deletions tokio/tests/sync_broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,19 @@ fn send_in_waker_drop() {
// Shouldn't deadlock.
let _ = tx.send(());
}

#[tokio::test]
async fn receiver_recv_is_cooperative() {
let (tx, mut rx) = broadcast::channel(8);

tokio::select! {
biased;
_ = async {
loop {
assert!(tx.send(()).is_ok());
assert!(rx.recv().await.is_ok());
}
} => {},
_ = tokio::task::yield_now() => {},
}
}
Loading