Skip to main content

LockSet

Struct LockSet 

Source
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>

Source

pub fn new<'a>(group: L) -> Self
where 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).

Source

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

Trait Implementations§

Source§

impl<L: Debug> Debug for LockSet<L>

Source§

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

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

impl<'a, L: Acquirable<'a>> Lockable<'a> for LockSet<L>

Source§

type Guard = <L as Acquirable<'a>>::Guard

The guard type returned when the lock(s) are held.
Source§

type MinLvl = <L as Acquirable<'a>>::MinLvl

The minimum level in this target.
Source§

type MaxLvl = <L as Acquirable<'a>>::MaxLvl

The maximum level in this target. The key advances to this level after acquisition.
Source§

fn lock_impl(&'a self) -> Self::Guard

Acquire the lock(s) and return the guard(s).

Auto Trait Implementations§

§

impl<L> Freeze for LockSet<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for LockSet<L>
where L: RefUnwindSafe,

§

impl<L> Send for LockSet<L>
where L: Send,

§

impl<L> Sync for LockSet<L>
where L: Sync,

§

impl<L> Unpin for LockSet<L>
where L: Unpin,

§

impl<L> UnsafeUnpin for LockSet<L>
where L: UnsafeUnpin,

§

impl<L> UnwindSafe for LockSet<L>
where L: UnwindSafe,

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, 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.