[][src]Struct spin_sync::Mutex8

pub struct Mutex8(_);

Mutex8 is a set of mutexes. Each instance includes 8 mutexes.

The differences between Mutex8 and Mutex are as follows.

Implementations

impl Mutex8[src]

pub const LEN: usize[src]

The number of mutexes that one Mutex8 has.

pub const fn new() -> Self[src]

Creates a new instance in an unlocked state ready for use.

Unlike to std::sync::Mutex , this is a const function. It can be use to initialize static variable.

Examples

Declaring a static variable.

use spin_sync::Mutex8;

static mutex8: Mutex8 = Mutex8::new();

Declaring a local variable.

use spin_sync::Mutex8;

let mutex8 = Mutex8::new();

impl Mutex8[src]

pub fn lock(&self, lock_bits: u8) -> Mutex8Guard<'_>[src]

Blocks the current thread until acquiring the lock(s) indicated by lock_bits and returns an RAII guard object.

Each bit of lock_bits indicates the lock of Mutex8 . For example, '0x01' corresponds to the first lock and '0x02' does to the second lock. If 2 or more than 2 bits are set, the lock_bits means all of them. In case of '0x03', for example, it means both the first and the second locks.

Examples

use spin_sync::Mutex8;

let mutex8 = Mutex8::new();

// Acquire '0x01' and '0x02' in order.
{
    let guard1 = mutex8.lock(0x01);
    let guard2 = mutex8.lock(0x02);
}

// Acquire '0x01' and '0x02' at the same time.
{
    let guard3 = mutex8.lock(0x03);
}

pub fn try_lock(&self, lock_bits: u8) -> TryLockResult<Mutex8Guard<'_>>[src]

Attempts to acquire lock(s) indicated by lock_bits and returns an RAII guard object if succeeded.

Each bit of lock_bits indicates the lock of Mutex8 . For example, '0x01' corresponds to the first lock and '0x02' does to the second lock. If 2 or more than 2 bits are set, the lock_bits means all of them. In case of '0x03', for example, it means both the first and the second locks.

Behaves like lock except for this method returns an error immediately if another user is holding the lock.

This method does not block.

Errors

If another user is holding this mutex, TryLockError::WouldBlock is returned.

Examples

use spin_sync::Mutex8;

let mutex8 = Mutex8::new();

// Try to acquire 0x01 twice. The second try will be fail.
{
    let result1 = mutex8.try_lock(0x01);
    assert_eq!(true, result1.is_ok());

    let result2 = mutex8.try_lock(0x01);
    assert_eq!(true, result2.is_err());
}

// Try to acquire 0x01 and 0x02 at the same time.
// After that, neither 0x01 nor 0x02 can be locked.
{
    // Acquire locks 0x01 and 0x02 at once.
    let result1 = mutex8.try_lock(0x03);
    assert_eq!(true, result1.is_ok());

    let result2 = mutex8.try_lock(0x01);
    assert_eq!(true, result2.is_err());

    let result3 = mutex8.try_lock(0x02);
    assert_eq!(true, result3.is_err());
}

pub fn locked_bits(&self) -> u8[src]

Returns the bits that some Mutex8Guard instance(s) is holding.

Example

use spin_sync::Mutex8;

let mutex8 = Mutex8::new();

// Acquire 0x01.
let guard1 = mutex8.lock(0x01);
assert_eq!(0x01, mutex8.locked_bits());

// Acquire 0x02.
let guard2 = mutex8.lock(0x02);
assert_eq!(0x03, mutex8.locked_bits());

// Acquire 0x04 and 0x08 at the same time.
let mut guard3 = mutex8.lock(0x0c);
assert_eq!(0x0f, mutex8.locked_bits());

// Release 0x08.
guard3.release(0x08);
assert_eq!(0x07, mutex8.locked_bits());

pub const fn len(&self) -> usize[src]

The number of mutexes that one Mutex8 has.

Trait Implementations

impl Debug for Mutex8[src]

impl Display for Mutex8[src]

Auto Trait Implementations

impl RefUnwindSafe for Mutex8

impl Send for Mutex8

impl Sync for Mutex8

impl Unpin for Mutex8

impl UnwindSafe for Mutex8

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.