Skip to main content

Locksmith

Struct Locksmith 

Source
pub struct Locksmith { /* private fields */ }
Expand description

Factory for distributing lock scope capabilities across threads or cores.

Program-wide singleton – only one Locksmith can exist at a time. Created via new (with a limit) or unlimited (no limit). Issues KeyVouchers that can be sent to target threads/cores and redeemed for KeyHandles.

!Send, !Sync – stays on the creating thread/core. Only the vouchers travel.

§Caveats

The singleton invariant relies on a global AtomicBool. On Unix, fork() duplicates the parent’s memory – including the flag – into the child process. The child inherits LOCKSMITH_EXISTS = true but has no Locksmith value, so it can never create one and the flag is never cleared. Avoid fork() after creating a Locksmith, or use exec immediately (the standard fork/exec pattern resets the address space).

Implementations§

Source§

impl Locksmith

Source

pub fn new(limit: usize) -> Option<Self>

Try to create a new Locksmith with the given voucher limit.

Returns None if a Locksmith already exists. Only one can exist at a time (enforced via a global AtomicBool). When the Locksmith is dropped, the slot is released.

Source

pub fn unlimited() -> Option<Self>

Try to create a Locksmith with no voucher limit.

Returns None if a Locksmith already exists. issue() will always succeed (no limit check).

Source

pub fn create(limit: usize) -> Self

Create a Locksmith with the given limit, panicking if one already exists.

§Panics

Panics if a Locksmith already exists.

Source

pub fn create_unlimited() -> Self

Create an unlimited Locksmith, panicking if one already exists.

§Panics

Panics if a Locksmith already exists.

Source

pub fn issue(&self) -> Option<KeyVoucher>

Issue a KeyVoucher.

Returns None if the limit has been reached. If the Locksmith is unlimited, always returns Some.

The voucher is Send and can be transferred to another thread or core.

The counter only increments – slots are not reclaimed when vouchers or key handles are dropped. For embedded systems with static core counts, issue exactly N vouchers at init.

Source

pub fn issued(&self) -> usize

Number of vouchers issued so far.

Source

pub const fn limit(&self) -> Option<usize>

The configured limit, or None if unlimited.

Source

pub fn remaining(&self) -> Option<usize>

Remaining vouchers that can be issued, or None if unlimited.

Trait Implementations§

Source§

impl Debug for Locksmith

Source§

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

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

impl Drop for Locksmith

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.