-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std: use futex-based locks and thread parker on Hermit
- Loading branch information
Showing
8 changed files
with
53 additions
and
456 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
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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
use super::abi; | ||
use crate::ptr::null; | ||
use crate::sync::atomic::AtomicU32; | ||
use crate::time::Duration; | ||
|
||
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool { | ||
// Calculate the timeout as a relative timespec. | ||
// | ||
// Overflows are rounded up to an infinite timeout (None). | ||
let timespec = timeout.and_then(|dur| { | ||
Some(abi::timespec { | ||
tv_sec: dur.as_secs().try_into().ok()?, | ||
tv_nsec: dur.subsec_nanos().into(), | ||
}) | ||
}); | ||
|
||
let r = unsafe { | ||
abi::futex_wait( | ||
futex.as_mut_ptr(), | ||
expected, | ||
timespec.as_ref().map_or(null(), |t| t as *const abi::timespec), | ||
abi::FUTEX_RELATIVE_TIMEOUT, | ||
) | ||
}; | ||
|
||
r != -abi::errno::ETIMEDOUT | ||
} | ||
|
||
#[inline] | ||
pub fn futex_wake(futex: &AtomicU32) -> bool { | ||
unsafe { abi::futex_wake(futex.as_mut_ptr(), 1) > 0 } | ||
} | ||
|
||
#[inline] | ||
pub fn futex_wake_all(futex: &AtomicU32) { | ||
unsafe { | ||
abi::futex_wake(futex.as_mut_ptr(), i32::MAX); | ||
} | ||
} |
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
Oops, something went wrong.