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
/*! A mutex where waiting threads specify a priority. Exactly like `std::sync::Mutex`, except that `lock` takes a priority (integer, 0 is high). When the mutex is released, the thread which gave the highest priority will take the lock. ``` # 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 # } ``` */ extern crate fnv; mod simple; pub use simple::*; // pub mod spin_one; // contains a deadlock pub mod internal;