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
49
50
51
52
53
54
55
56
57
58
59
60
61
/*!
A mutex where waiting threads specify a priority.
Exactly like `std::sync::Mutex`, except that `lock` takes an integer priority (0 is high). When
the mutex is released, the thread which gave the highest priority will take the lock.
**Status**: As far as I can tell it's correct, although not particularly fast (releasing the lock
takes 3-4μs on my machine). Poisoning is untested.
```
# extern crate rand;
# extern crate priomutex;
# fn main() {
use priomutex::Mutex;
use rand::{Rng, thread_rng};
use std::mem;
use std::sync::Arc;
use std::thread;
let mutex = Arc::new(Mutex::new(()));
let guard = mutex.lock(0).unwrap(); // take the lock
for _ in 0..10 {
let mutex = mutex.clone();
thread::spawn(move|| {
let p = thread_rng().gen::<usize>(); // generate a random priority
let guard = mutex.lock(p).unwrap(); // block until we take the lock
println!("Took the lock: {}", p);
// lock is released here
});
}
thread::sleep_ms(100); // give the threads a chance to spawn
mem::drop(guard); // go go go!
// At this point you should see the lock being taken in priority-order
# }
```
*/
pub use *;