possum/concurrency/
mod.rs

1pub(crate) mod sync;
2
3#[cfg(not(feature = "shuttle"))]
4pub use std::thread;
5
6// This isn't available in loom or shuttle yet. Unfortunately for shuttle it means threads are
7// spawned outside its control, and it doesn't work.
8#[cfg(feature = "shuttle")]
9pub use shuttle::thread;
10
11#[cfg(not(feature = "shuttle"))]
12pub(crate) fn run_blocking<F, R>(f: F) -> R
13where
14    F: FnOnce() -> R + Send,
15    R: Send,
16{
17    if false {
18        let (sender, receiver) = std::sync::mpsc::channel();
19        let tx_thread = std::thread::scope(|scope| {
20            scope.spawn(|| {
21                let res = f();
22                sender.send(res).unwrap();
23            });
24            receiver.recv().unwrap()
25        });
26        tx_thread
27    } else {
28        f()
29    }
30}
31
32#[cfg(feature = "shuttle")]
33pub(crate) fn run_blocking<F, R>(f: F) -> R
34where
35    F: FnOnce() -> R + Send,
36    R: Send,
37{
38    use std::sync::mpsc;
39    let (sender, receiver) = mpsc::channel();
40    let tx_thread = std::thread::scope(|scope| {
41        scope.spawn(|| {
42            let res = f();
43            sender.send(res).unwrap();
44        });
45        loop {
46            shuttle::thread::yield_now();
47            match receiver.try_recv() {
48                Err(mpsc::TryRecvError::Empty) => continue,
49                default => return default.unwrap(),
50            }
51        }
52    });
53    tx_thread
54}