sync: apply cooperative scheduling to sync::broadcast::Receiver (#6870)

This commit is contained in:
Timo
2024-09-26 16:52:46 +02:00
committed by GitHub
parent c8af499990
commit 21df16d759
2 changed files with 18 additions and 2 deletions
+2 -2
View File
@@ -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;
@@ -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.
+16
View File
@@ -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() => {},
}
}