#lock #key #mutual #exclusion #locking

key-lock

Library for mutual exclusion by keys

1 unstable release

0.1.0 Dec 22, 2022

#5 in #exclusion

Download history 36/week @ 2024-11-13 36/week @ 2024-11-20 29/week @ 2024-11-27 32/week @ 2024-12-04 65/week @ 2024-12-11 82/week @ 2024-12-18 28/week @ 2024-12-25 28/week @ 2025-01-01 21/week @ 2025-01-08 25/week @ 2025-01-15 23/week @ 2025-01-22 74/week @ 2025-01-29 29/week @ 2025-02-05 19/week @ 2025-02-19 52/week @ 2025-02-26

115 downloads per month

MIT license

7KB
105 lines

KeyLock

crates.io page docs.rs page license: MIT

Simple library for mutual exclusion based on keys. Lock and wait for execution by key.

Usage

Import the project using:

cargo add key-lock

Example

use key_lock::KeyLock;

#[tokio::main]
async fn main() {
    // Initialize new lock.
    let lock = KeyLock::new();
    // Lock A, continues immediately.
    let _a = lock.lock("a").await;
    // Lock B, continues immediately.
    let _b = lock.lock("b").await;
    // Try to lock A, but it is already locked. Normal locking would block here.
    assert!(lock.try_lock("a").await.is_err());
}

Lints

This projects uses a bunch of clippy lints for higher code quality and style.

Install cargo-lints using cargo install --git https://github.com/FlixCoder/cargo-lints. The lints are defined in lints.toml and can be checked by running cargo lints clippy --all-targets --workspace.

Dependencies

~2–7.5MB
~47K SLoC