diff options
| author | bors <bors@rust-lang.org> | 2017-01-09 07:01:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-01-09 07:01:10 +0000 |
| commit | 7aab3d38a07a89663d1e35f1fd86d7b4659dcef2 (patch) | |
| tree | 9ee6fae0ca6b431bbaba05b2e73d53c09828d77c /src/libstd/sys/windows/process.rs | |
| parent | f5cfe83da96ffeadccfab240d7995b5ade274e6f (diff) | |
| parent | abb91890831d71f5cbe4346a9ab57d432372df65 (diff) | |
| download | rust-7aab3d38a07a89663d1e35f1fd86d7b4659dcef2.tar.gz rust-7aab3d38a07a89663d1e35f1fd86d7b4659dcef2.zip | |
Auto merge of #38866 - alexcrichton:try-wait, r=aturon
std: Add a nonblocking `Child::try_wait` method This commit adds a new method to the `Child` type in the `std::process` module called `try_wait`. This method is the same as `wait` except that it will not block the calling thread and instead only attempt to collect the exit status. On Unix this means that we call `waitpid` with the `WNOHANG` flag and on Windows it just means that we pass a 0 timeout to `WaitForSingleObject`. Currently it's possible to build this method out of tree, but it's unfortunately tricky to do so. Specifically on Unix you essentially lose ownership of the pid for the process once a call to `waitpid` has succeeded. Although `Child` tracks this state internally to be resilient to multiple calls to `wait` or a `kill` after a successful wait, if the child is waited on externally then the state inside of `Child` is not updated. This means that external implementations of this method must be extra careful to essentially not use a `Child`'s methods after a call to `waitpid` has succeeded (even in a nonblocking fashion). By adding this functionality to the standard library it should help canonicalize these external implementations and ensure they can continue to robustly reuse the `Child` type from the standard library without worrying about pid ownership.
Diffstat (limited to 'src/libstd/sys/windows/process.rs')
| -rw-r--r-- | src/libstd/sys/windows/process.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index 7dc8959e1b6..d2ad81023e7 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -340,6 +340,21 @@ impl Process { } } + pub fn try_wait(&mut self) -> io::Result<ExitStatus> { + unsafe { + match c::WaitForSingleObject(self.handle.raw(), 0) { + c::WAIT_OBJECT_0 => {} + c::WAIT_TIMEOUT => { + return Err(io::Error::from_raw_os_error(c::WSAEWOULDBLOCK)) + } + _ => return Err(io::Error::last_os_error()), + } + let mut status = 0; + cvt(c::GetExitCodeProcess(self.handle.raw(), &mut status))?; + Ok(ExitStatus(status)) + } + } + pub fn handle(&self) -> &Handle { &self.handle } pub fn into_handle(self) -> Handle { self.handle } |
