#lock #safe #testing #struct #atomic #constructor

safe-lock

A lock struct with a const fn constructor and no unsafe

4 releases

0.1.3 Mar 23, 2021
0.1.2 Mar 22, 2021
0.1.1 Mar 21, 2021
0.1.0 Mar 18, 2021

#852 in Concurrency

Download history 4787/week @ 2023-12-13 3108/week @ 2023-12-20 2509/week @ 2023-12-27 3171/week @ 2024-01-03 3084/week @ 2024-01-10 3364/week @ 2024-01-17 3698/week @ 2024-01-24 2632/week @ 2024-01-31 3236/week @ 2024-02-07 2554/week @ 2024-02-14 2424/week @ 2024-02-21 2562/week @ 2024-02-28 3519/week @ 2024-03-06 4409/week @ 2024-03-13 4470/week @ 2024-03-20 3074/week @ 2024-03-27

16,185 downloads per month
Used in 14 crates (5 directly)

Apache-2.0

13KB
58 lines

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

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

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.3

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

Changelog

  • v0.1.3 - Increase test coverage
  • v0.1.2 - Use Acquire and Release ordering
  • v0.1.1 - Update docs
  • v0.1.0 - Initial version

Happy Contributors 🙂

Fixing bugs and adding features is easy and fast. Send us a pull request and we intend to:

  • Always respond within 24 hours
  • Provide clear & concrete feedback
  • Immediately make a new release for your accepted change

License: Apache-2.0

No runtime deps