batch-lock
A thread-safe, key-based lock management system for Rust that provides fine-grained concurrent access control.
Features
- 🔒 Thread-safe key-based locking mechanism
- 🚀 High-performance concurrent implementation using
dashmap
- 📦 Support for both single-key and batch locking operations
- 🛡️ RAII-style lock guards for automatic lock release
- ⚡ Zero-cost abstractions with minimal overhead
- 🔧 Configurable capacity and sharding for optimal performance
Installation
Add this to your Cargo.toml
:
[]
= "0.1"
Usage
Single Key Locking
use LockManager;
// Create a new lock manager
let lock_manager = new;
// Acquire a lock
let guard = lock_manager.lock;
// Critical section - exclusive access guaranteed
// perform operations...
// Lock is automatically released when guard is dropped
Batch Locking
use LockManager;
use BTreeSet;
// Create a new lock manager with custom capacity
let lock_manager = with_capacity;
// Prepare multiple keys
let mut keys = new;
keys.insert;
keys.insert;
// Acquire locks for all keys atomically
let guard = lock_manager.batch_lock;
// Critical section - exclusive access to all keys guaranteed
// perform operations...
// All locks are automatically released when guard is dropped
Custom Capacity and Sharding
use LockManager;
// Create a lock manager with custom capacity and shard count
let lock_manager = with_capacity_and_shard_amount;
Performance Considerations
- Uses
dashmap
for efficient concurrent access - Lock acquisition is fair and FIFO-ordered
- Batch locks are acquired in a consistent order to prevent deadlocks
- Memory usage scales with the number of active locks
- Sharding can be tuned for optimal performance based on workload
Thread Safety
The LockManager
is fully thread-safe and can be safely shared across threads:
use Arc;
use LockManager;
let lock_manager = new;
let lock_manager_clone = lock_manager.clone;
spawn;
API Documentation
For detailed API documentation, please visit docs.rs/batch-lock.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under MIT or Apache-2.0.