pub struct PcpMutex<'a, T> { /* private fields */ }
Expand description
A Priority Ceiling Protocol mutex
Implementations§
Source§impl<'a, T> PcpMutex<'a, T>
impl<'a, T> PcpMutex<'a, T>
Sourcepub fn new(res: T, ceiling: Priority) -> Self
pub fn new(res: T, ceiling: Priority) -> Self
Creates a new PcpMutex with a default global group
Examples found in repository?
examples/pcp.rs (line 7)
6fn main() {
7 let a = Arc::new(PcpMutex::new(0, 3));
8 let b = Arc::new(PcpMutex::new(0, 3));
9
10 let mut handles = vec![];
11
12 {
13 let a = a.clone();
14 let b = b.clone();
15 let handle = thread::spawn(move || {
16 println!("Thread 1 tries a lock");
17 a.lock(|a| {
18 println!("Thread 1 holds a lock");
19 *a += 1;
20 thread::sleep(std::time::Duration::from_millis(100));
21 println!("Thread 1 tries b lock");
22 b.lock(|b| {
23 println!("Thread 1 holds b lock");
24 *b += 1;
25 });
26 println!("Thread 1 released b lock");
27 });
28 println!("Thread 1 released a lock");
29 });
30 handles.push(handle);
31 }
32
33 {
34 let a = a.clone();
35 let b = b.clone();
36 let handle = thread::spawn(move || {
37 println!("Thread 2 tries b lock");
38 b.lock(|b| {
39 println!("Thread 2 holds b lock");
40 *b += 1;
41 thread::sleep(std::time::Duration::from_millis(100));
42 println!("Thread 2 tries a lock");
43 a.lock(|a| {
44 println!("Thread 2 holds a lock");
45 *a += 1;
46 });
47 println!("Thread 2 released a lock");
48 });
49 println!("Thread 2 released b lock");
50 });
51 handles.push(handle);
52 }
53
54 for handle in handles {
55 handle.join().unwrap();
56 }
57
58 println!("Done a: {}, b: {}", a.lock(|a| *a), b.lock(|b| *b));
59}
Sourcepub fn lock<R>(&'a self, f: impl FnOnce(&mut T) -> R) -> R
pub fn lock<R>(&'a self, f: impl FnOnce(&mut T) -> R) -> R
Locks the mutex and executes critical section in a closure.
Examples found in repository?
examples/pcp.rs (lines 17-27)
6fn main() {
7 let a = Arc::new(PcpMutex::new(0, 3));
8 let b = Arc::new(PcpMutex::new(0, 3));
9
10 let mut handles = vec![];
11
12 {
13 let a = a.clone();
14 let b = b.clone();
15 let handle = thread::spawn(move || {
16 println!("Thread 1 tries a lock");
17 a.lock(|a| {
18 println!("Thread 1 holds a lock");
19 *a += 1;
20 thread::sleep(std::time::Duration::from_millis(100));
21 println!("Thread 1 tries b lock");
22 b.lock(|b| {
23 println!("Thread 1 holds b lock");
24 *b += 1;
25 });
26 println!("Thread 1 released b lock");
27 });
28 println!("Thread 1 released a lock");
29 });
30 handles.push(handle);
31 }
32
33 {
34 let a = a.clone();
35 let b = b.clone();
36 let handle = thread::spawn(move || {
37 println!("Thread 2 tries b lock");
38 b.lock(|b| {
39 println!("Thread 2 holds b lock");
40 *b += 1;
41 thread::sleep(std::time::Duration::from_millis(100));
42 println!("Thread 2 tries a lock");
43 a.lock(|a| {
44 println!("Thread 2 holds a lock");
45 *a += 1;
46 });
47 println!("Thread 2 released a lock");
48 });
49 println!("Thread 2 released b lock");
50 });
51 handles.push(handle);
52 }
53
54 for handle in handles {
55 handle.join().unwrap();
56 }
57
58 println!("Done a: {}, b: {}", a.lock(|a| *a), b.lock(|b| *b));
59}
Trait Implementations§
impl<'a, T> Send for PcpMutex<'a, T>where
T: Send,
impl<'a, T> Sync for PcpMutex<'a, T>where
T: Send,
Auto Trait Implementations§
impl<'a, T> !Freeze for PcpMutex<'a, T>
impl<'a, T> !RefUnwindSafe for PcpMutex<'a, T>
impl<'a, T> Unpin for PcpMutex<'a, T>where
T: Unpin,
impl<'a, T> UnwindSafe for PcpMutex<'a, T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more