simplelock 0.1.0

Simple abstractions for inter-process synchronization.
Documentation

Simple Lock

This package provides a simple locking abstraction for inter-process/thread synchronization.

It also provides a number of common implementations for locking (File Locks, POSIX Semaphores, etc).

The trait is also generic with the ability to customize the failure scenarios based on use-case.

Features:

By default, no features will be enabled and only the Lock trait and utility functionality will be available.

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

Un-implemented mechanisms:

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

Examples:

use simplelock::{
    // The lock interface.
    Lock,

    // Based on the "feature" enabled, this will create the default lock.
    default_lock,

    // Ease of use function to lock a critical closure.
    lock_until_finished,

    // Result/Error types for stack-traces etc.
    SimpleLockResult,
};

fn main() -> SimpleLockResult<()> {
    // Will return an Error if no lock type was enabled.
    let lock: Lock = default_lock("my_app_name")?;
    
    let result = lock_until_finished(
        lock,
        || {
            /// Some critical code.
        })?;
    // ... Do something with the result.
}

If you wanted to implement your own lock, you can implement the Lock trait 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.

  • unsafe - The "semaphore" lock implementation has unsafe code, this is because the "nix" package does not have implementations for POSIX semaphores yet. If this is unacceptible, don't enable the "semaphore" feature.
  • windows - The "file" lock implementation should in theory work, but unverified as I don't have a windows machine.
  • no_std - We currently do not have any non-stdlib available lock implementations.