Skip to content

Cross-platform mutual exclusion across processes on a file or path

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

rossmacarthur/fmutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fmutex

Crates.io Version Docs.rs Latest Build Status

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.

cargo add fmutex

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):

For shared locks (multiple processes can hold the lock simultaneously, but not when an exclusive lock is held):

🤸 Usage

let fd = fs::OpenOptions::new().create(true).write(true).open(&path)?;

{
    let _guard = fmutex::lock_exclusive(&fd)?;

    // do mutually exclusive stuff here

} // <-- `_guard` dropped here and the lock is released
let fd = fs::OpenOptions::new().create(true).write(true).open(&path)?;

match fmutex::try_lock_exclusive(&fd)? {
    Some(_guard) => {

        // do mutually exclusive stuff here

    } // <-- `_guard` dropped here and the lock is released

    None => {
        eprintln!("the lock could not be acquired!");
    }
}
let path = "path/to/my/file.txt";

{
    let _guard = fmutex::lock_exclusive_path(path)?;

    // do mutually exclusive stuff here

} // <-- `_guard` dropped here and the lock is released
let path = "path/to/my/file.txt";

match fmutex::try_lock_exclusive_path(path)? {
    Some(_guard) => {

        // do mutually exclusive stuff here

    } // <-- `_guard` dropped here and the lock is released

    None => {
        eprintln!("the lock could not be acquired!");
    }
}
let fd = fs::OpenOptions::new().create(true).write(true).open(&path)?;

{
    let _guard = fmutex::lock_shared(&fd)?;

    // do shared read-only operations here
    // other processes can also acquire shared locks simultaneously

} // <-- `_guard` dropped here and the lock is released
let fd = fs::OpenOptions::new().create(true).write(true).open(&path)?;

match fmutex::try_lock_shared(&fd)? {
    Some(_guard) => {

        // do shared read-only operations here
        // other processes can also acquire shared locks simultaneously

    } // <-- `_guard` dropped here and the lock is released

    None => {
        eprintln!("the shared lock could not be acquired (file is exclusively locked)!");
    }
}
let path = "path/to/my/file.txt";

{
    let _guard = fmutex::lock_shared_path(path)?;

    // do shared read-only operations here
    // other processes can also acquire shared locks simultaneously

} // <-- `_guard` dropped here and the lock is released
let path = "path/to/my/file.txt";

match fmutex::try_lock_shared_path(path)? {
    Some(_guard) => {

        // do shared read-only operations here
        // other processes can also acquire shared locks simultaneously

    } // <-- `_guard` dropped here and the lock is released

    None => {
        eprintln!("the shared lock could not be acquired (file is exclusively locked)!");
    }
}

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.

About

Cross-platform mutual exclusion across processes on a file or path

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages