1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! 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:
//!
//! ```toml
//! [dependencies]
//! 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.
//!
//! # 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 = default_lock("my_app_name")?;
//!
//! // One utility function provided, simple critical-section locking.
//! let result = lock_until_finished(
//! &mut lock,
//! || {
//! /// Some critical code.
//! # 42
//! })?;
//!
//! // ... Do something with the result.
//! # assert_eq!(42, result);
//! # Ok(())
//! }
//! ```
//!
//! [`Lock`]: ./trait.Lock.html
//! [`FakeLock`]: ./struct.FakeLock.html
//! [`FileLock`]: ./struct.FileLock.html
//! [`SemaphoreLock`]: ./struct.SemaphoreLock.html
//! [RFC 7232]: https://tools.ietf.org/html/rfc7232
extern crate cfg_if;
// Configure the lock during build.
pub use *;
// SimpleLock error and result overrides.
pub use *;
// Lock implementations
pub use *;
// Lock type hierarchy
pub use *;
// Ease-of-Use / Utility functionality.
pub use *;