use loom::hint; use loom::thread; #[cfg(feature = "async")] use std::future::Future; #[cfg(feature = "async")] use std::pin::Pin; #[cfg(feature = "async")] use std::task::{self, Poll}; #[cfg(feature = "std")] use std::time::Duration;
let t = thread::spawn(move || loop { match receiver.try_recv() {
Ok(msg) => break msg,
Err(TryRecvError::Empty) => hint::spin_loop(),
Err(TryRecvError::Disconnected) => panic!("Should not be disconnected"),
}
});
// Make sure the receiver can be dropped while a send is happening in parallel #[cfg(feature = "async")] #[test] fn poll_then_drop_receiver_during_send() {
loom::model(|| { let (sender, mut receiver) = oneshot::channel::<u128>();
let (waker, _waker_handle) = helpers::waker::waker(); letmut context = task::Context::from_waker(&waker);
// Put the channel into the receiving state
assert_eq!(Pin::new(&mut receiver).poll(&mut context), Poll::Pending);
// Spawn a separate thread that sends in parallel let t = thread::spawn(move || { let _ = sender.send(1234);
});
// Drop the receiver. Loom will make sure all thread interleavings with the send are tested
drop(receiver);
// The send operation should also not have panicked
t.join().unwrap();
})
}
// Sending should cause the waker from the latest poll to be woken up
sender.send(1234).unwrap();
assert_eq!(waker_handle1.clone_count(), 1);
assert_eq!(waker_handle1.drop_count(), 1);
assert_eq!(waker_handle1.wake_count(), 0);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.