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 useMutex8
instead ofMutex
for the performance.)- User can acquire 2 or more than 2 locks of one
Mutex8
instance at once.
Implementations§
Source§impl Mutex8
impl Mutex8
Sourcepub const fn new() -> Self
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
impl Mutex8
Sourcepub fn lock(&self, lock_bits: u8) -> Mutex8Guard<'_>
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);
}
Sourcepub fn try_lock(&self, lock_bits: u8) -> TryLockResult<Mutex8Guard<'_>>
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());
}
Sourcepub fn locked_bits(&self) -> u8
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());