filock
Thread-safe, closure-based file locking for Rust using flock(2).
Features
- Closure-based API — locks are held for exactly the closure's duration, preventing leaks
- Thread-safe —
FileStoreisSend + Sync - Deadlock-free — by construction within the same process
- Condvar-based waiting — no spin-waiting, efficient blocking
- Non-blocking try-locks —
try_with_read/try_with_write - Timeout support —
with_read_timeout/with_write_timeout - Minimal dependencies — only
libc - Advisory locking — works across processes using
flock(2)
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
use FileStore;
use ;
API
Blocking locks
// Shared (read) — multiple threads can hold simultaneously
store.with_read?;
// Exclusive (write) — only one holder at a time
store.with_write?;
Non-blocking try-locks
// Returns Some(result) if lock acquired, None if contended
let result = store.try_with_write?;
match result
Timeout locks
use Duration;
// Returns Err(FileLockError::Timeout) if deadline exceeded
store.with_write_timeout?;
Read-modify-write
store.modify?;
Introspection
if store.is_locked?
Lock Compatibility
| Held \ Requested | Shared | Exclusive |
|---|---|---|
| None | ✅ Grant | ✅ Grant |
| Shared (other) | ✅ Grant | ❌ Block |
| Shared (self) | ✅ Grant | ❌ No upgrade |
| Exclusive (other) | ❌ Block | ❌ Block |
| Exclusive (self) | ❌ No downgrade | ❌ No re-entrant |
Thread Safety
- Within the same process: full thread safety with Condvar-based blocking
- Across processes: advisory
flock(2)locking (cooperative processes only)
License
MIT — see LICENSE for details.