Mutex8

Struct Mutex8 

Source
pub struct Mutex8(/* private fields */);
Expand description

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

The differences between Mutex8 and Mutex are as follows.

  • Mutex8 is not template structure. User must make sure to acquire lock before accessing to the protected object. (Compiler cannot check it.)
  • Mutex8 gives up poisoning strategy. (This feature makes the performance better. It is a good idea to use Mutex8 instead of Mutex for the performance.)
  • User can acquire 2 or more than 2 locks of one Mutex8 instance at once.

Implementations§

Source§

impl Mutex8

Source

pub const LEN: usize = 8usize

The number of mutexes that one Mutex8 has.

Source

pub const fn new() -> Self

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();
Source§

impl Mutex8

Source

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

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

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

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());
}
Source

pub fn locked_bits(&self) -> u8

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());
Source

pub const fn len(&self) -> usize

The number of mutexes that one Mutex8 has.

Trait Implementations§

Source§

impl Debug for Mutex8

Source§

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

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

impl Display for Mutex8

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Mutex8

§

impl RefUnwindSafe for Mutex8

§

impl Send for Mutex8

§

impl Sync for Mutex8

§

impl Unpin for Mutex8

§

impl UnwindSafe for Mutex8

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.