File: deadlock.rs

package info (click to toggle)
rust-loom 0.5.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 568 kB
  • sloc: makefile: 2
file content (35 lines) | stat: -rw-r--r-- 839 bytes parent folder | download | duplicates (4)
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
#![deny(warnings, rust_2018_idioms)]

use loom::sync::Mutex;
use loom::thread;

use std::rc::Rc;

#[test]
#[should_panic]
fn two_mutexes_deadlock() {
    loom::model(|| {
        let a = Rc::new(Mutex::new(1));
        let b = Rc::new(Mutex::new(2));

        let th1 = {
            let a = a.clone();
            let b = b.clone();

            thread::spawn(move || {
                let a_lock = a.lock().unwrap();
                let b_lock = b.lock().unwrap();
                assert_eq!(*a_lock + *b_lock, 3);
            })
        };
        let th2 = {
            thread::spawn(move || {
                let b_lock = b.lock().unwrap();
                let a_lock = a.lock().unwrap();
                assert_eq!(*a_lock + *b_lock, 3);
            })
        };
        th1.join().unwrap();
        th2.join().unwrap();
    });
}