summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync/tests/loom_list.rs
blob: 4067f865ce487736fc673b9d2abd54ee5a60bcac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::sync::mpsc::list;

use loom::thread;
use std::sync::Arc;

#[test]
fn smoke() {
    use crate::sync::mpsc::block::Read::*;

    const NUM_TX: usize = 2;
    const NUM_MSG: usize = 2;

    loom::model(|| {
        let (tx, mut rx) = list::channel();
        let tx = Arc::new(tx);

        for th in 0..NUM_TX {
            let tx = tx.clone();

            thread::spawn(move || {
                for i in 0..NUM_MSG {
                    tx.push((th, i));
                }
            });
        }

        let mut next = vec![0; NUM_TX];

        loop {
            match rx.pop(&tx) {
                Some(Value((th, v))) => {
                    assert_eq!(v, next[th]);
                    next[th] += 1;

                    if next.iter().all(|&i| i == NUM_MSG) {
                        break;
                    }
                }
                Some(Closed) => {
                    panic!();
                }
                None => {
                    thread::yield_now();
                }
            }
        }
    });
}