Skip to content

Commit

Permalink
Fix windows implementation: Match all return value explicitly
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed Oct 23, 2023
1 parent 70160b4 commit 638e74e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions gen-windows-sys-binding/windows_sys.list
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/job_token/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -41,13 +45,12 @@ impl JobServerClient {
}

pub(super) fn try_acquire(&self) -> io::Result<Option<()>> {
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"),
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 638e74e

Please sign in to comment.