#lock-files #lock #deadlock #signal

bin+lib alive_lock_file

A simple crate to create lock files without creating dead locks

3 releases

0.1.2 Jul 30, 2024
0.1.1 Jul 22, 2024
0.1.0 Jul 22, 2024

#816 in Filesystem

MIT license

8KB
112 lines

alive_lock_file

crates.io docs.rs license

A simple crate to create lock files without creating dead locks. For that, it use 2 things:

  • join the provided path to the $XDG_RUNTIME_DIR env variable. This directory get cleanned automatically by the system, and is mount as a ramfs.
  • intercept signals

Usage

use alive_lock_file::{init_signals, LockFileState};

fn main() {
    // intercept the `SIGINT` and `SIGTERM` signals.
    init_signals();

    match LockFileState::try_lock("file.lock").unwrap() {
        LockFileState::Lock(_lock) => {
            // while _lock is in scope, `file.lock` will not be removed
        }
        LockFileState::AlreadyLocked => {}
    };
}

Dependencies

~2–12MB
~84K SLoC