#lock #hierarchy #level #hierarchies #mutex #higher-level #rw-lock

lock-hierarchy

Prevent dead locks by enforcing lock hierarchies

4 releases

new 0.2.0 Feb 12, 2025
0.1.3 Feb 7, 2025
0.1.2 Sep 20, 2022
0.1.1 Sep 20, 2022

#313 in Concurrency

Download history 60/week @ 2024-10-25 89/week @ 2024-11-01 150/week @ 2024-11-08 121/week @ 2024-11-15 107/week @ 2024-11-22 75/week @ 2024-11-29 129/week @ 2024-12-06 175/week @ 2024-12-13 67/week @ 2024-12-20 123/week @ 2024-12-27 206/week @ 2025-01-03 100/week @ 2025-01-10 79/week @ 2025-01-17 151/week @ 2025-01-24 107/week @ 2025-01-31 489/week @ 2025-02-07

853 downloads per month

MIT license

23KB
513 lines

Lock hierarchy

This Rust crate offers debug assertions for violations of lock hierarchies. No runtime overhead or protection occurs for release builds.

Usage

use lock_hierarchy::Mutex;

let mutex_a = Mutex::new(()); // Level 0
let mutex_b = Mutex::with_level((), 0); // also level 0
// Fine, first mutex in thread
let _guard_a = mutex_a.lock().unwrap();
// Must panic, lock hierarchy violation
let _guard_b = mutex_b.lock().unwrap();
use lock_hierarchy::Mutex;

let mutex_a = Mutex::with_level((), 1); // Level 1
let mutex_b = Mutex::new(()); // level 0
// Fine, first mutex in thread
let _guard_a = mutex_a.lock().unwrap();
// Fine: 0 is lower level than 1
let _guard_b = mutex_b.lock().unwrap();

lib.rs:

This crate offers debug assertions for violations of lock hierarchies. No runtime overhead or protection occurs for release builds.

Each lock is assigned a level. Locks with higher levels must be acquired before locks with lower levels. Both [RwLock] and [Mutex] use the same hierarchy.

No runtime deps