fmutex
Mutual exclusion across processes on a file descriptor or path.
- On Unix-like systems this is implemented use
flock(2)
. - On Windows this is implemented using
LockFileEx
.
🚀 Getting started
First add fmutex
to your Cargo manifest.
Now use one of the provided functions to lock a file descriptor (Unix) or handle (Windows) or a file path.
For exclusive locks (only one process can hold the lock):
lock_exclusive()
to acquire an exclusive lock on a file descriptor or handle.try_lock_exclusive()
to attempt to acquire an exclusive lock on a file descriptor or handle.lock_exclusive_path()
to acquire an exclusive lock on a file path.try_lock_exclusive_path()
to attempt to acquire an exclusive lock on a file path.
For shared locks (multiple processes can hold the lock simultaneously, but not when an exclusive lock is held):
lock_shared()
to acquire a shared lock on a file descriptor or handle.try_lock_shared()
to attempt to acquire a shared lock on a file descriptor or handle.lock_shared_path()
to acquire a shared lock on a file path.try_lock_shared_path()
to attempt to acquire a shared lock on a file path.
🤸 Usage
lock_exclusive()
let fd = new.create.write.open?;
// <-- `_guard` dropped here and the lock is released
try_lock_exclusive()
let fd = new.create.write.open?;
match try_lock_exclusive?
lock_exclusive_path()
let path = "path/to/my/file.txt";
// <-- `_guard` dropped here and the lock is released
try_lock_exclusive_path()
let path = "path/to/my/file.txt";
match try_lock_exclusive_path?
lock_shared()
let fd = new.create.write.open?;
// <-- `_guard` dropped here and the lock is released
try_lock_shared()
let fd = new.create.write.open?;
match try_lock_shared?
lock_shared_path()
let path = "path/to/my/file.txt";
// <-- `_guard` dropped here and the lock is released
try_lock_shared_path()
let path = "path/to/my/file.txt";
match try_lock_shared_path?
License
This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.