diff --git a/gen-windows-sys-binding/windows_sys.list b/gen-windows-sys-binding/windows_sys.list index 6c84d7f98..f5ed55e16 100644 --- a/gen-windows-sys-binding/windows_sys.list +++ b/gen-windows-sys-binding/windows_sys.list @@ -9,6 +9,9 @@ Windows.Win32.Foundation.S_OK Windows.Win32.Foundation.FALSE Windows.Win32.Foundation.HANDLE Windows.Win32.Foundation.WAIT_OBJECT_0 +Windows.Win32.Foundation.WAIT_TIMEOUT +Windows.Win32.Foundation.WAIT_FAILED +Windows.Win32.Foundation.WAIT_ABANDONED Windows.Win32.System.Com.SAFEARRAY Windows.Win32.System.Com.SAFEARRAYBOUND diff --git a/src/job_token/windows.rs b/src/job_token/windows.rs index 81cb8b1db..e77c9d74c 100644 --- a/src/job_token/windows.rs +++ b/src/job_token/windows.rs @@ -5,9 +5,13 @@ use std::{ use crate::windows_sys::{ OpenSemaphoreA, ReleaseSemaphore, WaitForSingleObject, FALSE, HANDLE, SEMAPHORE_MODIFY_STATE, - THREAD_SYNCHRONIZE, WAIT_OBJECT_0, + THREAD_SYNCHRONIZE, WAIT_ABANDONED, WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT, }; +const WAIT_ABANDOEND_ERR_MSG: &str = r#" The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread and the mutex state is set to nonsignaled. + +If the mutex was protecting persistent state information, you should check it for consistency."#; + pub(super) struct JobServerClient { sem: HANDLE, } @@ -41,13 +45,12 @@ impl JobServerClient { } pub(super) fn try_acquire(&self) -> io::Result> { - let r = unsafe { WaitForSingleObject(self.sem, 0) }; - if r == WAIT_OBJECT_0 { - Ok(Some(())) - } else if r == 0 { - Err(io::Error::last_os_error()) - } else { - Ok(None) + match unsafe { WaitForSingleObject(self.sem, 0) } { + WAIT_OBJECT_0 => Ok(Some(())), + WAIT_TIMEOUT => Ok(None), + WAIT_FAILED => Err(io::Error::last_os_error()), + WAIT_ABANDONED => Err(io::Error::new(io::ErrorKind::Other, WAIT_ABANDOEND_ERR_MSG)), + _ => unreachable!("Unexpected return value from WaitForSingleObject"), } } diff --git a/src/windows_sys.rs b/src/windows_sys.rs index eea210853..20a256076 100644 --- a/src/windows_sys.rs +++ b/src/windows_sys.rs @@ -202,7 +202,10 @@ pub const S_FALSE: HRESULT = 1i32; pub const S_OK: HRESULT = 0i32; pub type THREAD_ACCESS_RIGHTS = u32; pub const THREAD_SYNCHRONIZE: THREAD_ACCESS_RIGHTS = 1048576u32; +pub const WAIT_ABANDONED: WIN32_ERROR = 128u32; +pub const WAIT_FAILED: WIN32_ERROR = 4294967295u32; pub const WAIT_OBJECT_0: WIN32_ERROR = 0u32; +pub const WAIT_TIMEOUT: WIN32_ERROR = 258u32; pub type WIN32_ERROR = u32; /// Adapted from