|
| 1 | +//! Thread parking based on SGX events. |
| 2 | +
|
| 3 | +use super::abi::{thread, usercalls}; |
| 4 | +use crate::io::ErrorKind; |
| 5 | +use crate::pin::Pin; |
| 6 | +use crate::ptr::{self, NonNull}; |
| 7 | +use crate::sync::atomic::AtomicPtr; |
| 8 | +use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; |
| 9 | +use crate::time::Duration; |
| 10 | +use fortanix_sgx_abi::{EV_UNPARK, WAIT_INDEFINITE}; |
| 11 | + |
| 12 | +// The TCS structure must be page-aligned (this is checked by EENTER), so these cannot |
| 13 | +// be valid pointers |
| 14 | +const EMPTY: *mut u8 = ptr::invalid_mut(1); |
| 15 | +const NOTIFIED: *mut u8 = ptr::invalid_mut(2); |
| 16 | + |
| 17 | +pub struct Parker { |
| 18 | + /// The park state. One of EMPTY, NOTIFIED or a TCS address. |
| 19 | + /// A state change to NOTIFIED must be done with release ordering |
| 20 | + /// and be observed with acquire ordering so that operations after |
| 21 | + /// `thread::park` returns will not occur before the unpark message |
| 22 | + /// was sent. |
| 23 | + state: AtomicPtr<u8>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Parker { |
| 27 | + /// Construct the thread parker. The UNIX parker implementation |
| 28 | + /// requires this to happen in-place. |
| 29 | + pub unsafe fn new(parker: *mut Parker) { |
| 30 | + unsafe { parker.write(Parker::new_internal()) } |
| 31 | + } |
| 32 | + |
| 33 | + pub(super) fn new_internal() -> Parker { |
| 34 | + Parker { state: AtomicPtr::new(EMPTY) } |
| 35 | + } |
| 36 | + |
| 37 | + // This implementation doesn't require `unsafe` and `Pin`, but other implementations do. |
| 38 | + pub unsafe fn park(self: Pin<&Self>) { |
| 39 | + if self.state.load(Acquire) != NOTIFIED { |
| 40 | + let mut prev = EMPTY; |
| 41 | + loop { |
| 42 | + // Guard against changing TCS addresses by always setting the state to |
| 43 | + // the current value. |
| 44 | + let tcs = thread::current().as_ptr(); |
| 45 | + if self.state.compare_exchange(prev, tcs, Relaxed, Acquire).is_ok() { |
| 46 | + let event = usercalls::wait(EV_UNPARK, WAIT_INDEFINITE).unwrap(); |
| 47 | + assert!(event & EV_UNPARK == EV_UNPARK); |
| 48 | + prev = tcs; |
| 49 | + } else { |
| 50 | + // The state was definitely changed by another thread at this point. |
| 51 | + // The only time this occurs is when the state is changed to NOTIFIED. |
| 52 | + // We observed this change with acquire ordering, so we can simply |
| 53 | + // change the state to EMPTY with a relaxed store. |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // At this point, the token was definately read with acquire ordering, |
| 60 | + // so this can be a relaxed store. |
| 61 | + self.state.store(EMPTY, Relaxed); |
| 62 | + } |
| 63 | + |
| 64 | + // This implementation doesn't require `unsafe` and `Pin`, but other implementations do. |
| 65 | + pub unsafe fn park_timeout(self: Pin<&Self>, dur: Duration) { |
| 66 | + let timeout = u128::min(dur.as_nanos(), WAIT_INDEFINITE as u128 - 1) as u64; |
| 67 | + let tcs = thread::current().as_ptr(); |
| 68 | + |
| 69 | + if self.state.load(Acquire) != NOTIFIED { |
| 70 | + if self.state.compare_exchange(EMPTY, tcs, Relaxed, Acquire).is_ok() { |
| 71 | + match usercalls::wait(EV_UNPARK, timeout) { |
| 72 | + Ok(event) => assert!(event & EV_UNPARK == EV_UNPARK), |
| 73 | + Err(e) => { |
| 74 | + assert!(matches!(e.kind(), ErrorKind::TimedOut | ErrorKind::WouldBlock)) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Swap to provide acquire ordering even if the timeout occurred |
| 79 | + // before the token was set. This situation can result in spurious |
| 80 | + // wakeups on the next call to `park_timeout`, but it is better to let |
| 81 | + // those be handled by the user than do some perhaps unnecessary, but |
| 82 | + // always expensive guarding. |
| 83 | + self.state.swap(EMPTY, Acquire); |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // The token was already read with `acquire` ordering, this can be a store. |
| 89 | + self.state.store(EMPTY, Relaxed); |
| 90 | + } |
| 91 | + |
| 92 | + // This implementation doesn't require `Pin`, but other implementations do. |
| 93 | + pub fn unpark(self: Pin<&Self>) { |
| 94 | + let state = self.state.swap(NOTIFIED, Release); |
| 95 | + |
| 96 | + if !matches!(state, EMPTY | NOTIFIED) { |
| 97 | + // There is a thread waiting, wake it up. |
| 98 | + let tcs = NonNull::new(state).unwrap(); |
| 99 | + // This will fail if the thread has already terminated or its TCS is destroyed |
| 100 | + // by the time the signal is sent, but that is fine. If another thread receives |
| 101 | + // the same TCS, it will receive this notification as a spurious wakeup, but |
| 102 | + // all users of `wait` should and (internally) do guard against those where |
| 103 | + // necessary. |
| 104 | + let _ = usercalls::send(EV_UNPARK, Some(tcs)); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments