[][src]Crate simplelock

SimpleLock is a set of utility functions and a simple Lock trait, which can be used for inter-process synchronization. This differs from many of the other "lock" packages in that it works cross-process using OS/FS/etc mechanisms.

We hope to expand and bundle a number of verified implementations with it, but in the spirit of Rust's "only pay for what you use", they will be gated behind feature flags or enclosed in separate packages.

For example, you can use the FileLock implementation by adding this to your Cargo.toml:

simplelock = { version = "*", features = [ "file" ] }

Presently we have the following implementations available for use:

  • fake - Enables the FakeLock implementation (useful for testing only).
  • file - Enables the FileLock implementation (most common and cross-platform).
  • sema - Enables the SemaphoreLock implementation (minimal libc/nix only).

Un-implemented mechanisms which may come later:

  • smem - Shared Memory.
  • Simple distributed locks:
    • http - URL/web-hook based (i.e. RFC 7232).
    • callback - Customizable callback (i.e. redis dist-lock, etc).

Please request others or hand-roll your own. You need not enable any features and implement the Lock trait directly.


use simplelock::*;

fn main() -> SimpleLockResult<()> {
    // Based on the "feature" enabled, this will create the default lock.
    // Will return a SimpleLockError on error, or if no lock type was enabled.
    let mut lock = default_lock("my_app_name")?;

    // One utility function provided, simple critical-section locking.
    let result = lock_until_finished(
        &mut lock,
        || {
            /// Some critical code.

    // ... Do something with the result.



A dummy lock which is actually just a boolean flag. This is used to test the functionality of higher level functionality where locks would have been useful.


A simple cross-platform file lock.


Builds a lock from a ConcreteLock implementation using the configurations enabled. See below for all possible configurations. Note, by implementing the ConcreteLock implementation you can wrap it with a LockBuilder to create the Lock interface used in all utility functions.


A safe publicly usable object surrounding the wrapper we built since 'nix' doesn't have it yet.



Possible status a Lock can have.


SimpleLock's Error type.



A full implementation of a Lock which can handle all LockBuilder configurations.


The Simple Lock trait.



Get a lock with default configuration based on the feature enabled.


Simple utility function for wrapping a closure with a lock.

Type Definitions


SimpleLock's Result value override to inject our Error type.