# Simple Lock

[](https://builds.sr.ht/~dstar4138/simplelock?)
[](https://crates.io/crates/simplelock)
[](https://docs.rs/simplelock)
**NOTE: Still a WIP package.**
This package provides a simple locking abstraction for inter-process/thread synchronization.
### Features:
By default, no features will be enabled and only the [Lock][doc-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:
* `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).
### Examples:
```rust
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: Lock = default_lock("my_app_name")?;
// One utility function provided, simple critical-section locking.
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][doc-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][cargo-nix] package does not have
implementations for POSIX semaphores yet. If this is unacceptable,
don't enable the "semaphore" feature.
* `no_std` - We currently do not have any non-stdlib available lock
implementations.
[cargo-nix]: https://crates.io/crates/nix
[doc-lock]: https://docs.rs/simplelock/latest/simplelock/trait.Lock.html