pub struct LockSet<L> { /* private fields */ }Expand description
A prepared set of locks, pre-sorted by LockId.
For tuples of arity 3 and above and for slices, all locks must be
at the same level. 2-tuples may contain locks at different levels
(the key advances to the maximum of the two). Construct once,
lock many times via MutexKey::lock.
§Examples
use surelock::{mutex::Mutex, set::LockSet};
let a: Mutex<u32> = Mutex::new(1);
let b: Mutex<u32> = Mutex::new(2);
// Sort happens once at construction time.
let set = LockSet::new((&a, &b));Implementations§
Source§impl<L> LockSet<L>
impl<L> LockSet<L>
Sourcepub fn new<'a>(group: L) -> Selfwhere
L: Acquirable<'a>,
pub fn new<'a>(group: L) -> Selfwhere
L: Acquirable<'a>,
Create a new LockSet, pre-sorting by LockId.
Construction allocates a small index vector proportional to
the number of locks. For hot loops acquiring the same set
repeatedly, construct the LockSet once outside the loop
and reuse it.
An empty group (e.g., an empty slice) is valid. Locking an
empty set is a no-op that still advances the key’s level to
the group’s MaxLvl.
See try_new for a non-panicking
alternative that returns None on duplicates.
§Panics
Panics if any two locks in the group share the same
LockId (i.e., the same &Mutex appears
more than once).
Sourcepub fn try_new<'a>(group: L) -> Option<Self>where
L: Acquirable<'a>,
pub fn try_new<'a>(group: L) -> Option<Self>where
L: Acquirable<'a>,
Try to create a new LockSet, returning None if any locks
are duplicated.
Same as new but returns None instead of
panicking when two locks share the same
LockId. An empty group always returns
Some.
§Examples
use surelock::{mutex::Mutex, set::LockSet};
let a: Mutex<u32> = Mutex::new(1);
let b: Mutex<u32> = Mutex::new(2);
// Distinct locks -- succeeds.
assert!(LockSet::try_new((&a, &b)).is_some());
// Same lock twice -- returns None.
assert!(LockSet::try_new((&a, &a)).is_none());