| Crates.io | declarative_lock |
| lib.rs | declarative_lock |
| version | 0.1.0 |
| created_at | 2025-11-27 15:48:02.048303+00 |
| updated_at | 2025-11-27 15:48:02.048303+00 |
| description | A thread-aware resource locking manager for Rust that enforces declaration-before-locking, guaranteeing deadlock freedom and preventing double-locking by tracking resource usage per thread. |
| homepage | |
| repository | https://github.com/msr1k/declarative_lock |
| max_upload_size | |
| id | 1953986 |
| size | 25,027 |
A thread-aware, deadlock-free resource locking manager for Rust.
declarative_lock enforces declaration-before-locking for resources, guaranteeing deadlock freedom in concurrent environments. By requiring threads to explicitly declare which resources they intend to lock before acquiring any locks, this crate prevents deadlocks caused by lock acquisition order across threads and detects double-locking of the same resource within a single thread.
Deadlock-Free by Design
Separation of Resource Identity and Instance
use declarative_locker::{DeclarativeLock, DeclarativeLocker};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ResourceType {
Database,
SomeOtherResource,
}
let lock = DeclarativeLock::<ResourceType>::new();
// Create two lockers for different resources sharing the same identifier
let db_locker = DeclarativeLocker::new(&lock, ResourceType::Database, db_instance);
let cache_locker = DeclarativeLocker::new(&lock, ResourceType::Database, cache_instance);
// Declare intent to lock Database
let guard = lock.declare(&[ResourceType::Database]).unwrap();
// Both lockers will enforce mutual exclusion on the underlying Database resource
let db_guard = db_locker.lock().unwrap();
// ... use db_guard ...
drop(db_guard);
drop(guard); // Declaration is automatically cleaned up
v0.1.0 (2025/11/27)
Initial Version
MIT