#lock #safe #data #mutex #atomic #prevent

safe-lock

A lock struct with a const fn constructor and no unsafe - ARCHIVED

5 releases

new 0.1.4 Oct 28, 2024
0.1.3 Mar 23, 2021
0.1.2 Mar 22, 2021
0.1.1 Mar 21, 2021
0.1.0 Mar 18, 2021

#82 in Concurrency

Download history 3200/week @ 2024-07-08 3301/week @ 2024-07-15 2262/week @ 2024-07-22 3303/week @ 2024-07-29 2661/week @ 2024-08-05 3499/week @ 2024-08-12 3164/week @ 2024-08-19 3283/week @ 2024-08-26 3506/week @ 2024-09-02 2773/week @ 2024-09-09 2528/week @ 2024-09-16 2988/week @ 2024-09-23 2510/week @ 2024-09-30 2597/week @ 2024-10-07 1861/week @ 2024-10-14 2330/week @ 2024-10-21

9,362 downloads per month
Used in 13 crates (6 directly)

Apache-2.0

11KB

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

Related Crates

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


Metric output format: x/y
    x = unsafe code used by the build
    y = total unsafe code found in the crate

Symbols: 
    🔒  = No `unsafe` usage found, declares #![forbid(unsafe_code)]= No `unsafe` usage found, missing #![forbid(unsafe_code)]
    ☢️  = `unsafe` usage found

Functions  Expressions  Impls  Traits  Methods  Dependency

0/0        0/0          0/0    0/0     0/0      🔒  safe-lock 0.1.4

0/0        0/0          0/0    0/0     0/0    

Changelog

  • v0.1.4
    • Make SafeLock a wrapper around std::sync::Mutex since it got a const constructor.
    • Add archival notice.
  • v0.1.3 - Increase test coverage
  • v0.1.2 - Use Acquire and Release ordering
  • v0.1.1 - Update docs
  • v0.1.0 - Initial version

License: Apache-2.0

No runtime deps