Skip to main content

Crate heavy

Crate heavy 

Source
Expand description

A machine-wide counting semaphore for heavy commands.

A Pool is a directory with one file per slot. Holding a slot is holding an exclusive flock (LockFileEx on Windows) on its file, so the operating system frees the slot when the holder dies, however it dies, and every process on the machine that opens the same directory shares the same slots, whatever checkout or tool it comes from.

use heavy::Pool;

let pool = Pool::open("/tmp/heavy/default", 4)?;
let slot = pool.acquire(|| eprintln!("all slots are busy, waiting"))?;
// ... run the heavy command; the slot is released when `slot` is dropped.
drop(slot);

The queue is not fair: whoever tries first after a slot frees takes it.

Structsยง

Guard
A held slot. The slot is released when the guard is dropped, or when the process ends.
Holder
The process that holds a slot, as it described itself when it took the slot.
Pool
A pool of slots in a directory.
SlotStatus
What Pool::status says about one slot.