Expand description
§ARCHIVED ARCHIVED ARCHIVED
This crate is archived and will not be updated.
std::sync::Mutex
got a const
constructor, making this crate unnecessary.
See rustlang/rust#66806.
Don’t use this crate. Just use std::sync::Mutex
.
For folks who continue using this crate,
SafeLock
is now a simple wrapper around std::sync::Mutex
,
so their tests will run a bit faster.
§safe-lock
A simple SafeLock
struct.
§Use Cases
- Run tests sequentially
- Prevent concurrent operations on atomic values
- Prevent concurrent operations on data and systems outside the Rust runtime
§Features
- Const constructor
- Depends only on
std
forbid(unsafe_code)
- 100% test coverage
§Limitations
- Not a
Mutex<T>
. Does not contain a value. - Unoptimized. Uses
AtomicBool
in a spinlock, not fast OS locks. - Not a fair lock. If multiple threads acquire the lock in loops, some may never acquire it.
§Alternatives
rusty-fork
- Run tests in separate processes
std::sync::Mutex
- Part of the Rust standard library: well reviewed, well tested, and well maintained.
- Uses fast OS locks
- Has no const constructor. See rust#66806
and const-eval#3.
You can work around this with unstable
core::lazy::OnceCell
or variousunsafe
crates:lazy_static
,once_cell
,lazycell
, andconquer-once
.
parking_lot
- Well written code. Many hope that it will end up in the Rust standard library someday.
- Contains plenty of
unsafe
try-lock
- Popular
- No dependencies,
no_std
- Uses
unsafe
ruspiro-lock
- Sync and async locks
- No dependencies,
no_std
- Uses
unsafe
flexible-locks
- Lots of
unsafe
- Uses fast OS locks
- Unmaintained
- Lots of
§Related Crates
safina-sync
provides a safe asyncMutex
§Example
Make some tests run sequentially so they don’t interfere with each other:
use safe_lock::SafeLock;
static LOCK: SafeLock = SafeLock::new();
[#test]
fn test1() {
let _guard = LOCK.lock();
// ...
}
[#test]
fn test2() {
let _guard = LOCK.lock();
// ...
}
§Cargo Geiger Safety Report
§Changelog
- v0.1.4
- Make
SafeLock
a wrapper aroundstd::sync::Mutex
since it got a const constructor. - Add archival notice.
- Make
- v0.1.3 - Increase test coverage
- v0.1.2 - Use
Acquire
andRelease
ordering - v0.1.1 - Update docs
- v0.1.0 - Initial version
Structs§
- A lock.
- A handle to the acquired lock. Drop this to release the lock.