Simple Lock

License builds.sr.ht status Latest version Documentation

A simple locking abstraction for inter-process/thread synchronization in rust.

Features:

By default, no features will be enabled and only the Lock trait and utility functionality will be available. This can be useful if you wish to hand-roll your own Lock, but don't want to re-write the utility functions.

Current implementations:

Examples:

A basic example ```rust use simplelock::*;

// Our application's lock namespace. We use this on creation to allow for // cross-process synchronization. This lets multiple instances of this // application run at once without conflicting. const MYAPPNAME: &str = "myappname";

fn main() -> SimpleLockResult<()> { // Creates the lock based on the "feature" enabled in Cargo.toml. // Will return a SimpleLockError if an error happens when creating // the lock with the default configuration, or if no lock type was // enabled as a "feature". let mut lock = defaultlock(MYAPP_NAME)?;

// One of the utility functions provided, simple critical-section 
// locking. All of the bundled Locks' behaviours, will, by default,
// hang until the lock is available. This can be customized.
let result = lock_until_finished(
    &mut lock,
    || {
        /// Some critical code.
    })?;

// ... Do something with the result.

} ```

If you needed more customization of a lock's behaviour, you may use the LockBuilder to override lock/unlock behaviour.

```rust use simplelock::*;

fn main() -> SimpleLockResult<()> { let mut lock = LockBuilder::default() .withtrylock() // Allow immediate return if lock taken. .withsilentunlock() // Suppress failure to return result. .build(MYAPPNAME)?;

// do something with your lock.

} ```

If you wanted to implement your own lock, you can implement the Lock trait or [ConcreteLock][dock-concretelock] and use it with all the same utility functionality provided by this package. If you do not enable any features, this package is only the trait and utility functions.

Caveats:

If you are ok with the below, then go ahead and use this package.