Skip to content

Commit

Permalink
std: pass hint to id-based parking functions
Browse files Browse the repository at this point in the history
joboet committed Dec 29, 2022
1 parent a9e5c1a commit 3076f4e
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions library/std/src/sys/sgx/thread_parking.rs
Original file line number Diff line number Diff line change
@@ -7,17 +7,17 @@ pub type ThreadId = fortanix_sgx_abi::Tcs;

pub use super::abi::thread::current;

pub fn park() {
pub fn park(_hint: usize) {
usercalls::wait(EV_UNPARK, WAIT_INDEFINITE).unwrap();
}

pub fn park_timeout(dur: Duration) {
pub fn park_timeout(dur: Duration, _hint: usize) {
let timeout = u128::min(dur.as_nanos(), WAIT_INDEFINITE as u128 - 1) as u64;
if let Err(e) = usercalls::wait(EV_UNPARK, timeout) {
assert!(matches!(e.kind(), ErrorKind::TimedOut | ErrorKind::WouldBlock))
}
}

pub fn unpark(tid: ThreadId) {
pub fn unpark(tid: ThreadId, _hint: usize) {
let _ = usercalls::send(EV_UNPARK, Some(tid));
}
14 changes: 7 additions & 7 deletions library/std/src/sys/unix/thread_parking/netbsd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(target_os = "netbsd")]

use crate::ffi::{c_int, c_void};
use crate::ptr::{null, null_mut};
use crate::ptr;
use crate::time::Duration;
use libc::{_lwp_self, clockid_t, lwpid_t, time_t, timespec, CLOCK_MONOTONIC};

@@ -25,13 +25,13 @@ pub fn current() -> ThreadId {
}

#[inline]
pub fn park() {
pub fn park(hint: usize) {
unsafe {
___lwp_park60(0, 0, null_mut(), 0, null(), null());
___lwp_park60(0, 0, ptr::null_mut(), 0, ptr::invalid(hint), ptr::null());
}
}

pub fn park_timeout(dur: Duration) {
pub fn park_timeout(dur: Duration, hint: usize) {
let mut timeout = timespec {
// Saturate so that the operation will definitely time out
// (even if it is after the heat death of the universe).
@@ -42,13 +42,13 @@ pub fn park_timeout(dur: Duration) {
// Timeout needs to be mutable since it is modified on NetBSD 9.0 and
// above.
unsafe {
___lwp_park60(CLOCK_MONOTONIC, 0, &mut timeout, 0, null(), null());
___lwp_park60(CLOCK_MONOTONIC, 0, &mut timeout, 0, ptr::invalid(hint), ptr::null());
}
}

#[inline]
pub fn unpark(tid: ThreadId) {
pub fn unpark(tid: ThreadId, hint: usize) {
unsafe {
_lwp_unpark(tid, null());
_lwp_unpark(tid, ptr::invalid(hint));
}
}
6 changes: 3 additions & 3 deletions library/std/src/sys_common/thread_parking/id.rs
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ impl Parker {
if state == PARKED {
// Loop to guard against spurious wakeups.
while state == PARKED {
park();
park(self.state.as_mut_ptr().addr());
state = self.state.load(Acquire);
}

@@ -72,7 +72,7 @@ impl Parker {

let state = self.state.fetch_sub(1, Acquire).wrapping_sub(1);
if state == PARKED {
park_timeout(dur);
park_timeout(dur, self.state.as_mut_ptr().addr());
// Swap to ensure that we observe all state changes with acquire
// ordering, even if the state has been changed after the timeout
// occured.
@@ -95,7 +95,7 @@ impl Parker {
// and terminated before this call is made. This call then returns an
// error or wakes up an unrelated thread. The platform API and
// environment does allow this, however.
unpark(tid);
unpark(tid, self.state.as_mut_ptr().addr());
}
}
}

0 comments on commit 3076f4e

Please sign in to comment.