Skip to main content

Lock

Struct Lock 

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

A cross-process named lock.

Lock is a lightweight handle backed by an Arc; cloning it is O(1) and all clones share the same underlying state — including the process-level mutual-exclusion guarantee.

§Name rules

  • Must not be empty.
  • Must not contain \0, /, or \.

Implementations§

Source§

impl Lock

Source

pub fn new(name: &str) -> Result<Self>

Open (or create) a named lock identified by name.

§Platform behaviour
  • Unix — creates/opens $TMPDIR/<name>.lock (falls back to /tmp/<name>.lock when TMPDIR is unset).
  • Windows — creates/opens a kernel mutex named Global\<name>.
§Errors

Returns Error::InvalidName for illegal names, or Error::Io if the OS operation fails.

Source

pub fn with_path<P: AsRef<Path>>(path: P) -> Result<Self>

Available on Unix only.

Open (or create) a named lock at an explicit filesystem path.

Unlike Lock::new, no .lock suffix is appended and the location is not constrained to $TMPDIR. Parent directories must already exist.

§Errors

Returns Error::Io if the path cannot be opened or created.

Source

pub fn path(&self) -> &Path

Available on Unix only.

Return the filesystem path of the backing lock file (Unix only).

This is the path used by Lock::new or Lock::with_path. It can be used by callers to clean up the lock file when they know it is safe to do so. The library itself intentionally leaves the file in place; deleting it while another process may still be using the lock can break mutual exclusion.

Source

pub fn lock(&self) -> Result<LockGuard>

Acquire the lock, blocking until it is available.

Returns a LockGuard that releases the lock when dropped.

§Errors

Returns Error::Io if the underlying OS call fails.

Source

pub fn try_lock(&self) -> Result<LockGuard>

Try to acquire the lock without blocking.

Returns a LockGuard if the lock is free, or Error::WouldBlock if it is currently held.

§Errors

Returns Error::WouldBlock when the lock is held, or Error::Io for any other OS-level failure.

Trait Implementations§

Source§

impl Clone for Lock

Source§

fn clone(&self) -> Lock

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Lock

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Lock

§

impl RefUnwindSafe for Lock

§

impl Send for Lock

§

impl Sync for Lock

§

impl Unpin for Lock

§

impl UnsafeUnpin for Lock

§

impl UnwindSafe for Lock

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.