rust-lang / jobserver-rs

Apache License 2.0
69 stars 39 forks source link

feature: Add new API `Client::{try_acquire, support_try_acquire}` for non-blocking operation #72

Closed NobodyXu closed 4 months ago

NobodyXu commented 6 months ago

On unix, when the named fifo is used and on Windows, it is possible to acquire without blocking.

For named fifo on unix, we can simply set pipe to be non-blocking before reading.

https://github.com/rust-lang/cc-rs/blob/2f587f5561fb9c3c02e5c9e186b2bea98691608a/src/parallel/job_token/unix.rs#L113

On windows, we can use WaitForSingleObject(self.sem, 0):

https://github.com/rust-lang/cc-rs/blob/2f587f5561fb9c3c02e5c9e186b2bea98691608a/src/parallel/job_token/windows.rs#L44

On Linux, we can also applies an optimization, to turn an annoymous pipe into a named fifo:

https://github.com/rust-lang/cc-rs/blob/2f587f5561fb9c3c02e5c9e186b2bea98691608a/src/parallel/job_token/unix.rs#L77

Though that optimization is verified to be incorrect on macOS, opening /dev/fd/$fd seems to return the same file description.

NobodyXu commented 6 months ago

My proposal is add two new APIs to Client:

impl Client {
    fn try_acquire(&self) -> io::Result<Option<Acquired>>;

    fn supports_try_acquire(&self) -> bool;
}