Skip to main content

Crate ipc_lock

Crate ipc_lock 

Source
Expand description

Cross-process named locks.

ipc-lock provides mutual exclusion that works across both threads and processes on the same machine.

§How it works

Two locking layers work together:

  1. OS-level — keeps different processes out.

    • Unix: flock(2) via std::fs::File::lock on a file under $TMPDIR.
    • Windows: a Global\ named kernel mutex via CreateMutexW. If the previous owner terminates without releasing the mutex, the next waiter acquires it successfully but LockGuard::is_abandoned returns true.
  2. Thread-level — keeps different threads in the same process from entering concurrently, because flock and CreateMutexW are process-granular primitives that allow re-entry from the same process without blocking. Implemented with a Mutex<bool> gate and a Condvar.

§Example

use ipc_lock::{Lock, Result};

fn main() -> Result<()> {
    let lock = Lock::new("my-app")?;
    let _guard = lock.lock()?;   // blocks until available
    // critical section …
    Ok(())                        // _guard dropped → lock released
}

Lock is cheap to clone — all clones share the same underlying state.

let lock = Lock::new("shared")?;
let other = lock.clone();        // cheap Arc clone

Structs§

Lock
A cross-process named lock.
LockGuard
RAII guard returned by Lock::lock and Lock::try_lock.

Enums§

Error
Errors that can occur when using Lock.

Type Aliases§

Result
Specialized Result for this crate.