named-sem

Crates.ionamed-sem
lib.rsnamed-sem
version0.2.0
sourcesrc
created_at2024-01-03 03:26:32.886481
updated_at2024-02-26 08:25:07.785665
descriptionNamed semaphore for Linux & Windows.
homepagehttps://github.com/Evian-Zhang/named-semaphore
repositoryhttps://github.com/Evian-Zhang/named-semaphore
max_upload_size
id1086969
size38,782
EvianZhang (Evian-Zhang)

documentation

https://docs.rs/named-sem

README

Named Semaphore

Named semaphore for Linux & Windows.

To use this crate, add the following to Cargo.toml:

[dependencies]
named-sem = "0.1"

Background

Named semaphore is a process synchronize mechanism provided by OS and libc in Linux & Windows. By giving the semaphore a name, processes with appropriate permissions can share the same semaphore.

In Linux, we use the POSIX semaphore as implementation, and in Windows, we use Semaphore Objects.

Examples

use named_sem::{NamedSemaphore, Error};

# fn do_heavy_things() {}
fn use_named_semaphore() -> Result<(), Error> {
    // In Linux, the semaphore's name should begin with "/"
    let mut semaphore = NamedSemaphore::create("/my-semaphore", 3)?;

    semaphore.wait_then_post(|| {
        do_heavy_things();
    })?;

    Ok(())
}

Usage

A common usage for named semaphore is to control the process count across the system.

For example, we have four large directories, A, B, C and D, which needs to be compressed. While our computer's hardware only allows for two 7z processes to run at the same time. So we can create a named semaphore with initial value 2, and require the semaphore before each 7z run.

There is a small utility, preempt-do, in this repo. Compile it by

cargo build --release --features=commandline --bin preempt-do

Or you can install it by

cargo install named-sem --features=commandline --bin preempt-do

Then you can use the following instructions to do the things above:

preempt-do --name /my-semaphore --count 2 -- 7z a A.7z A
preempt-do --name /my-semaphore --count 2 -- 7z a B.7z B
preempt-do --name /my-semaphore --count 2 -- 7z a C.7z C
preempt-do --name /my-semaphore --count 2 -- 7z a D.7z D

This will allow there to be no more than two 7z processes across the system.

Commit count: 0

cargo fmt