Skip to content

Commit

Permalink
Optimize: Rm pre_exec call on Command (#75)
Browse files Browse the repository at this point in the history
* Rm `imp::Client::pre_run`

On Unix `Client` always clone the fds and now it also records the original fd
used to construct the `Client` and pass it to children, there's no need
to unset `CLOEXEC` before exec anymore.

Signed-off-by: Jiahao XU <[email protected]>

* Fix test: Add back `pre_run` for owned pipe

Signed-off-by: Jiahao XU <[email protected]>

* Fix

Signed-off-by: Jiahao XU <[email protected]>

* Fix

Signed-off-by: Jiahao XU <[email protected]>

---------

Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu authored Jul 5, 2024
1 parent 2729e53 commit 69df38a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,7 @@ impl Client {
Cmd: Command,
F: FnOnce(&mut Cmd) -> io::Result<R>,
{
// Register one-time callback on unix to unset CLO_EXEC
// in child process.
#[cfg(unix)]
self.0.inner.pre_run(&mut cmd);

let arg = self.0.inner.string_arg();
Expand Down
21 changes: 13 additions & 8 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Client {
write: File,
creation_arg: ClientCreationArg,
/// If the Client owns the fifo, then we should remove it on drop.
owns_fifo: bool,
owns: bool,
}

#[derive(Debug)]
Expand All @@ -42,7 +42,8 @@ impl Client {
// Create nonblocking and cloexec pipes
let pipes = create_pipe()?;

let client = unsafe { Self::from_fds(pipes[0], pipes[1]) };
let mut client = unsafe { Self::from_fds(pipes[0], pipes[1]) };
client.owns = true;

client.init(limit)?;

Expand Down Expand Up @@ -84,7 +85,7 @@ impl Client {
read: file.try_clone()?,
write: file,
creation_arg: ClientCreationArg::Fifo(name.into_boxed_path()),
owns_fifo: true,
owns: true,
};

client.init(limit)?;
Expand Down Expand Up @@ -143,7 +144,7 @@ impl Client {
read,
write: open_file_rw(path).ok()?,
creation_arg: ClientCreationArg::Fifo(path.into()),
owns_fifo: false,
owns: false,
})
} else {
None
Expand Down Expand Up @@ -200,7 +201,7 @@ impl Client {
read,
write,
creation_arg,
owns_fifo: false,
owns: false,
});
}

Expand All @@ -211,7 +212,7 @@ impl Client {
read,
write,
creation_arg,
owns_fifo: false,
owns: false,
})
}
_ => None,
Expand All @@ -223,7 +224,7 @@ impl Client {
read: File::from_raw_fd(read),
write: File::from_raw_fd(write),
creation_arg: ClientCreationArg::Fds { read, write },
owns_fifo: false,
owns: false,
}
}

Expand Down Expand Up @@ -301,6 +302,10 @@ impl Client {
where
Cmd: Command,
{
if !self.owns || self.get_fifo().is_some() {
return;
}

let read = self.read.as_raw_fd();
let write = self.write.as_raw_fd();

Expand Down Expand Up @@ -344,7 +349,7 @@ impl Client {
impl Drop for Client {
fn drop(&mut self) {
if let Some(path) = self.get_fifo() {
if self.owns_fifo {
if self.owns {
fs::remove_file(path).ok();
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ impl Client {
);
}

pub fn pre_run<Cmd>(&self, _cmd: &mut Cmd) {
panic!(
"On this platform there is no cross process jobserver support,
so Client::configure_and_run is not supported."
);
}

pub fn available(&self) -> io::Result<usize> {
Ok(*self.count())
}
Expand Down
8 changes: 0 additions & 8 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,6 @@ impl Client {
Cow::Borrowed(&self.name)
}

pub fn pre_run<Cmd>(&self, _cmd: &mut Cmd)
where
Cmd: Command,
{
// nothing to do here, we gave the name of our semaphore to the
// child above
}

pub fn available(&self) -> io::Result<usize> {
// Can't read value of a semaphore on Windows, so
// try to acquire without sleeping, since we can find out the
Expand Down

0 comments on commit 69df38a

Please sign in to comment.