-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows: Implement mutex using futex
Well, the Windows equivalent: `WaitOnAddress`, `WakeByAddressSingle` and `WakeByAddressAll`.
- Loading branch information
1 parent
89b7830
commit 620193b
Showing
12 changed files
with
129 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use super::api; | ||
use crate::sys::c; | ||
use crate::sys::dur2timeout; | ||
use core::ffi::c_void; | ||
use core::mem; | ||
use core::ptr; | ||
use core::time::Duration; | ||
|
||
#[inline(always)] | ||
pub fn wait_on_address<T, U>(address: &T, compare: U, timeout: Option<Duration>) -> bool { | ||
assert_eq!(mem::size_of::<T>(), mem::size_of::<U>()); | ||
unsafe { | ||
let addr = ptr::addr_of!(*address).cast::<c_void>(); | ||
let size = mem::size_of::<T>(); | ||
let compare_addr = ptr::addr_of!(compare).cast::<c_void>(); | ||
let timeout = timeout.map(dur2timeout).unwrap_or(c::INFINITE); | ||
c::WaitOnAddress(addr, compare_addr, size, timeout) == c::TRUE | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn wake_by_address_single<T>(address: &T) -> bool { | ||
unsafe { | ||
let addr = ptr::addr_of!(*address).cast::<c_void>(); | ||
c::WakeByAddressSingle(addr); | ||
false | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn wake_by_address_all<T>(address: &T) { | ||
unsafe { | ||
let addr = ptr::addr_of!(*address).cast::<c_void>(); | ||
c::WakeByAddressAll(addr); | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn futex_wait<T, U>(futex: &T, expected: U, timeout: Option<Duration>) -> bool { | ||
// return false only on timeout | ||
if wait_on_address(futex, expected, timeout) { | ||
true | ||
} else { | ||
api::get_last_error().code != c::ERROR_TIMEOUT | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn futex_wake<T>(futex: &T) -> bool { | ||
wake_by_address_single(futex); | ||
false | ||
} | ||
|
||
#[inline(always)] | ||
pub fn futex_wake_all<T>(futex: &T) { | ||
wake_by_address_all(futex) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters