22 releases

0.1.21 Jan 30, 2020
0.1.17 Sep 6, 2019
0.1.16 Jul 24, 2019
0.1.13 Mar 15, 2019
0.1.6 Jul 12, 2017
Download history 70413/week @ 2020-11-07 72521/week @ 2020-11-14 72424/week @ 2020-11-21 83488/week @ 2020-11-28 79247/week @ 2020-12-05 78251/week @ 2020-12-12 64295/week @ 2020-12-19 58712/week @ 2020-12-26 89278/week @ 2021-01-02 94568/week @ 2021-01-09 84880/week @ 2021-01-16 96977/week @ 2021-01-23 99348/week @ 2021-01-30 93126/week @ 2021-02-06 93239/week @ 2021-02-13 96299/week @ 2021-02-20

284,421 downloads per month
Used in 1,035 crates (11 directly)

MIT/Apache

44KB
680 lines

jobserver-rs

An implementation of the GNU make jobserver for Rust

Crates.io

Documentation

Usage

First, add this to your Cargo.toml:

[dependencies]
jobserver = "0.1"

Next, add this to your crate:

extern crate jobserver;

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

An implementation of the GNU make jobserver.

This crate is an implementation, in Rust, of the GNU make jobserver for CLI tools that are interoperating with make or otherwise require some form of parallelism limiting across process boundaries. This was originally written for usage in Cargo to both (a) work when cargo is invoked from make (using make's jobserver) and (b) work when cargo invokes build scripts, exporting a jobserver implementation for make processes to transitively use.

The jobserver implementation can be found in detail online but basically boils down to a cross-process semaphore. On Unix this is implemented with the pipe syscall and read/write ends of a pipe and on Windows this is implemented literally with IPC semaphores.

The jobserver protocol in make also dictates when tokens are acquired to run child work, and clients using this crate should take care to implement such details to ensure correct interoperation with make itself.

Examples

Connect to a jobserver that was set up by make or a different process:

use jobserver::Client;

// See API documentation for why this is `unsafe`
let client = match unsafe { Client::from_env() } {
    Some(client) => client,
    None => panic!("client not configured"),
};

Acquire and release token from a jobserver:

use jobserver::Client;

let client = unsafe { Client::from_env().unwrap() };
let token = client.acquire().unwrap(); // blocks until it is available
drop(token); // releases the token when the work is done

Create a new jobserver and configure a child process to have access:

use std::process::Command;
use jobserver::Client;

let client = Client::new(4).expect("failed to create jobserver");
let mut cmd = Command::new("make");
client.configure(&mut cmd);

Caveats

This crate makes no attempt to release tokens back to a jobserver on abnormal exit of a process. If a process which acquires a token is killed with ctrl-c or some similar signal then tokens will not be released and the jobserver may be in a corrupt state.

Note that this is typically ok as ctrl-c means that an entire build process is being torn down, but it's worth being aware of at least!

Windows caveats

There appear to be two implementations of make on Windows. On MSYS2 one typically comes as mingw32-make and the other as make itself. I'm not personally too familiar with what's going on here, but for jobserver-related information the mingw32-make implementation uses Windows semaphores whereas the make program does not. The make program appears to use file descriptors and I'm not really sure how it works, so this crate is not compatible with make on Windows. It is, however, compatible with mingw32-make.

Dependencies