PcpMutex

Struct PcpMutex 

Source
pub struct PcpMutex<'a, T> { /* private fields */ }
Expand description

A Priority Ceiling Protocol mutex

Implementations§

Source§

impl<'a, T> PcpMutex<'a, T>

Source

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}
Source

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}
Source

pub fn ceiling(&self) -> Priority

Returns priority ceiling of this mutex

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for PcpMutex<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Send for PcpMutex<'a, T>
where T: Send,

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.