pub struct Mutex8Guard<'a> { /* private fields */ }
Expand description
An RAII implementation of a “scoped lock(s)” of a Mutex8
.
When this structure is dropped, all the lock(s) will be released at once.
Implementations§
Source§impl Mutex8Guard<'_>
impl Mutex8Guard<'_>
Sourcepub fn release(&mut self, lock_bits: u8)
pub fn release(&mut self, lock_bits: u8)
Releases the lock(s) partially indicated by lock_bits
.
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.
If lock_bits
is same to that is being holded, self
releases all the locks; otherwise,
the others will still be being holded after the method returns.
lock_bits
must not include a bit that self
is not holding.
§Panics
Panics if lock_bits
includes a bit that self
is not holding.
§Examples
use spin_sync::Mutex8;
let mutex8 = Mutex8::new();
// Acquire 0x01 and 0x02 at the same time.
let mut guard = mutex8.lock(0x03);
{
// Fail to acquire 0x01 again.
let e = mutex8.try_lock(0x01);
assert!(e.is_err());
// Fail to acquire 0x02 again.
let e = mutex8.try_lock(0x02);
assert!(e.is_err());
}
// Release only 0x01. (0x02 is left.)
guard.release(0x01);
{
// Success to acquire 0x01 now.
let o = mutex8.try_lock(0x01);
assert!(o.is_ok());
// Still fail to acquire 0x02.
let e = mutex8.try_lock(0x02);
assert!(e.is_err());
}
Sourcepub fn lock_bits(&self) -> u8
pub fn lock_bits(&self) -> u8
Returns the bits that self
is holding.
§Example
use spin_sync::Mutex8;
let mutex8 = Mutex8::new();
// Acquire 0x01 and 0x02 at the same time.
let mut guard = mutex8.lock(0x03);
assert_eq!(0x03, guard.lock_bits());
// Release only 0x02. (0x01 is left.)
guard.release(0x02);
assert_eq!(0x01, guard.lock_bits());
// Release 0x01. (No lock is left.)
guard.release(0x01);
assert_eq!(0x00, guard.lock_bits());