-
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.
Rollup merge of #105903 - joboet:unify_parking, r=m-ou-se
Unify id-based thread parking implementations Multiple platforms currently use thread-id-based parking implementations (NetBSD and SGX[^1]). Even though the strategy does not differ, these are duplicated for each platform, as the id is encoded into an atomic thread variable in different ways for each platform. Since `park` is only called by one thread, it is possible to move the thread id into a separate field. By ensuring that the field is only written to once, before any other threads access it, these accesses can be unsynchronized, removing any restrictions on the size and niches of the thread id. This PR also renames the internal `thread_parker` modules to `thread_parking`, as that name now better reflects their contents. I hope this does not add too much reviewing noise. r? `@m-ou-se` `@rustbot` label +T-libs [^1]: SOLID supports this as well, I will switch it over in a follow-up PR.
- Loading branch information
Showing
19 changed files
with
207 additions
and
240 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,23 @@ | ||
use super::abi::usercalls; | ||
use crate::io::ErrorKind; | ||
use crate::time::Duration; | ||
use fortanix_sgx_abi::{EV_UNPARK, WAIT_INDEFINITE}; | ||
|
||
pub type ThreadId = fortanix_sgx_abi::Tcs; | ||
|
||
pub use super::abi::thread::current; | ||
|
||
pub fn park(_hint: usize) { | ||
usercalls::wait(EV_UNPARK, WAIT_INDEFINITE).unwrap(); | ||
} | ||
|
||
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, _hint: usize) { | ||
let _ = usercalls::send(EV_UNPARK, Some(tid)); | ||
} |
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
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,52 @@ | ||
use crate::ffi::{c_int, c_void}; | ||
use crate::ptr; | ||
use crate::time::Duration; | ||
use libc::{_lwp_self, clockid_t, lwpid_t, time_t, timespec, CLOCK_MONOTONIC}; | ||
|
||
extern "C" { | ||
fn ___lwp_park60( | ||
clock_id: clockid_t, | ||
flags: c_int, | ||
ts: *mut timespec, | ||
unpark: lwpid_t, | ||
hint: *const c_void, | ||
unparkhint: *const c_void, | ||
) -> c_int; | ||
fn _lwp_unpark(lwp: lwpid_t, hint: *const c_void) -> c_int; | ||
} | ||
|
||
pub type ThreadId = lwpid_t; | ||
|
||
#[inline] | ||
pub fn current() -> ThreadId { | ||
unsafe { _lwp_self() } | ||
} | ||
|
||
#[inline] | ||
pub fn park(hint: usize) { | ||
unsafe { | ||
___lwp_park60(0, 0, ptr::null_mut(), 0, ptr::invalid(hint), ptr::null()); | ||
} | ||
} | ||
|
||
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). | ||
tv_sec: dur.as_secs().try_into().ok().unwrap_or(time_t::MAX), | ||
tv_nsec: dur.subsec_nanos().into(), | ||
}; | ||
|
||
// Timeout needs to be mutable since it is modified on NetBSD 9.0 and | ||
// above. | ||
unsafe { | ||
___lwp_park60(CLOCK_MONOTONIC, 0, &mut timeout, 0, ptr::invalid(hint), ptr::null()); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn unpark(tid: ThreadId, hint: usize) { | ||
unsafe { | ||
_lwp_unpark(tid, ptr::invalid(hint)); | ||
} | ||
} |
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
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.