Truly independent web browser
LibJS: Wake Atomics.wait() for an interrupt
Andreas Kling8583f1bc16a5
diff --git a/AK/Rust/src/futex.rs b/AK/Rust/src/futex.rsindex 0d1d46c7b1e..92e369febdf 100644--- a/AK/Rust/src/futex.rs+++ b/AK/Rust/src/futex.rs@@ -4,11 +4,14 @@* SPDX-License-Identifier: BSD-2-Clause*/-//! Libraries/LibCore/Futex.cpp: waiting on and waking a word of memory, which Atomics.wait and Atomics.notify use.+//! Libraries/LibCore/Futex.cpp: waiting on and waking a word of memory, which Atomics.wait, Atomics.notify and their+//! WebAssembly counterparts use.-use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};-use core::time::Duration;#[cfg(any(target_os = "macos", target_os = "linux"))]+use core::sync::atomic::AtomicPtr;+use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};+use core::time::Duration;+#[cfg(target_os = "macos")]use std::time::Instant;/// Core::AtomicWaitResult.@@ -17,6 +20,7 @@ pub enum AtomicWaitResult {Woken,NotEqual,TimedOut,+ Interrupted,}/// The word of a shared typed array element that Atomics.wait waits on, which is 4 or 8 bytes wide.@@ -53,23 +57,173 @@ fn word_equals(word: WaitWord<'_>, expected: u64) -> bool {(word.load() & mask) == (expected & mask)}+/// Stops the waits of one agent for good, from any thread. Only a notify ends a wait otherwise, and a wait that ends+/// and starts over would lose its place among the waiters, and any notify that came in between.+///+/// A thread that sleeps in the kernel cannot be woken alone: a wake reaches the waiters of a word. On Linux, a waiter+/// also sleeps on a bit of its interrupt, which is what its interrupt wakes, but only 31 bits go around. On macOS,+/// every waiter of the word wakes. A waiter that such a wake was not for goes back to sleep (see+/// was_woken_by_an_interrupt()).+pub struct WaitInterrupt {+ is_requested: AtomicBool,+ /// The word a thread of this agent sleeps on, for as long as it may be asleep in the kernel.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ sleeping_on: AtomicPtr<u32>,+ /// Where in INTERRUPT_WAKES this interrupt wakes.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ wake_slot: usize,+ /// The thread of this agent that waits in the waiter lists.+ #[cfg(any(test, not(target_os = "linux")))]+ waiter: std::sync::Mutex<Option<std::sync::Arc<waiter_list::Waiter>>>,+}++/// The bit of a wake that a notify is, which every waiter sleeps on.+#[cfg(target_os = "linux")]+const NOTIFY_BIT: u32 = 1;++/// How many interrupts can wake waiters without that reaching a waiter of another one.+#[cfg(target_os = "linux")]+const INTERRUPT_WAKE_SLOTS: usize = 31;+#[cfg(target_os = "macos")]+const INTERRUPT_WAKE_SLOTS: usize = 1;++/// The word that an interrupt is waking, by wake slot, which tells a waiter that a wake may not have been for it.+#[cfg(any(target_os = "macos", target_os = "linux"))]+static INTERRUPT_WAKES: [AtomicPtr<u32>; INTERRUPT_WAKE_SLOTS] =+ [const { AtomicPtr::new(core::ptr::null_mut()) }; INTERRUPT_WAKE_SLOTS];++/// How long an interrupt waits for a thread to stop sleeping before it wakes the word again.+#[cfg(any(target_os = "macos", target_os = "linux"))]+const INTERRUPT_WAKE_INTERVAL: Duration = Duration::from_micros(50);++impl Default for WaitInterrupt {+ fn default() -> Self {+ Self::new()+ }+}++impl WaitInterrupt {+ pub fn new() -> Self {+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ static NEXT_WAKE_SLOT: AtomicU32 = AtomicU32::new(0);+ Self {+ is_requested: AtomicBool::new(false),+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ sleeping_on: AtomicPtr::new(core::ptr::null_mut()),+ // NB: A word can have waiters in other processes. Starting at the process ID spreads those over the bits as+ // well, which is all this can do for them: their waits are out of reach of INTERRUPT_WAKES.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ wake_slot: std::process::id().wrapping_add(NEXT_WAKE_SLOT.fetch_add(1, Ordering::Relaxed)) as usize+ % INTERRUPT_WAKE_SLOTS,+ #[cfg(any(test, not(target_os = "linux")))]+ waiter: std::sync::Mutex::default(),+ }+ }++ /// Whether interrupt() was called.+ pub fn is_requested(&self) -> bool {+ self.is_requested.load(Ordering::SeqCst)+ }++ /// Ends the wait of this agent's thread with AtomicWaitResult::Interrupted, and every later one at once. Blocks until+ /// that thread is awake.+ pub fn interrupt(&self) {+ self.is_requested.store(true, Ordering::SeqCst);+ #[cfg(any(test, not(target_os = "linux")))]+ waiter_list::wake_interrupted(self);+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ self.wake_sleeping_thread();+ }++ // NB: The kernel compares the word before a thread sleeps, but it cannot look at is_requested. A thread that looked at+ // it just before it got set is about to sleep, and a wake would go past it. So the wake has to go on until the+ // thread is known to be awake, which also covers that thread going away: a word that stopped being one only+ // gets a wake that finds nothing, or wakes a waiter that has to cope with this anyway.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ fn wake_sleeping_thread(&self) {+ let interrupt_wake = &INTERRUPT_WAKES[self.wake_slot];+ let mut did_wake = false;+ loop {+ let word = self.sleeping_on.load(Ordering::SeqCst);+ if word.is_null() {+ break;+ }+ interrupt_wake.store(word, Ordering::SeqCst);+ did_wake = true;+ self.wake_word(word);+ std::thread::sleep(INTERRUPT_WAKE_INTERVAL);+ }+ if did_wake {+ interrupt_wake.store(core::ptr::null_mut(), Ordering::SeqCst);+ }+ }++ /// Whether a thread that this interrupt lets sleep on `word` was woken by an interrupt, of this agent or another.+ ///+ /// NB: Telling those apart from a notify only works for an interrupt of this process. A waiter in another process+ /// that an interrupt wakes takes it for a notify. And a notify that arrives right while an interrupt wakes+ /// the word is taken for that interrupt's wake, if it is one for the same bit, so that it gets lost.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ fn was_woken_by_an_interrupt(&self, word: *mut u32) -> bool {+ INTERRUPT_WAKES[self.wake_slot].load(Ordering::SeqCst) == word+ }++ /// Lets interrupt() know that this agent's thread may sleep on `word` until the result goes away. The thread has to+ /// look at is_requested() after this.+ #[cfg(any(target_os = "macos", target_os = "linux"))]+ fn will_sleep_on(&self, word: *mut u32) -> SleepingOn<'_> {+ self.sleeping_on.store(word, Ordering::SeqCst);+ SleepingOn(self)+ }++ #[cfg(target_os = "linux")]+ fn wake_bit(&self) -> u32 {+ NOTIFY_BIT << (self.wake_slot + 1)+ }++ #[cfg(target_os = "linux")]+ fn wake_word(&self, word: *mut u32) {+ wake_waiters_on_bit(word, i32::MAX, self.wake_bit());+ }++ #[cfg(target_os = "macos")]+ fn wake_word(&self, word: *mut u32) {+ if let Some(functions) = os_sync::functions() {+ // SAFETY: A wake of an address only compares it with those that waiters sleep on.+ unsafe {+ (functions.wake_by_address_all)(word.cast(), 4, os_sync::OS_SYNC_WAKE_BY_ADDRESS_SHARED);+ }+ }+ }+}++#[cfg(any(target_os = "macos", target_os = "linux"))]+struct SleepingOn<'a>(&'a WaitInterrupt);++#[cfg(any(target_os = "macos", target_os = "linux"))]+impl Drop for SleepingOn<'_> {+ fn drop(&mut self) {+ self.0.sleeping_on.store(core::ptr::null_mut(), Ordering::SeqCst);+ }+}+// Fallback where there's no wait/wake primitive of the operating system's: Each waiter waits on a condition variable of// its own, in a list per address that atomic_notify() wakes waiters of in the order they began waiting. These lists only// reach the waiters of this process. So, a waiter also polls for a change to the word, which observes the wakes of other// processes that write the word before notifying (as the lock/futex protocol Emscripten uses does). A notify from// another process that leaves the word unchanged wakes no waiter here.-#[cfg(not(target_os = "linux"))]+#[cfg(any(test, not(target_os = "linux")))]mod waiter_list {- use super::{AtomicWaitResult, WaitWord, word_equals};+ use super::{AtomicWaitResult, WaitInterrupt, WaitWord, word_equals};use core::sync::atomic::{AtomicBool, Ordering};use core::time::Duration;use std::collections::{HashMap, VecDeque};- use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};+ use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError};use std::time::Instant;const POLL_INTERVAL: Duration = Duration::from_micros(200);- struct Waiter {+ pub(super) struct Waiter {woken: AtomicBool,condition: Condvar,}@@ -94,12 +248,35 @@ mod waiter_list {}}- pub(super) fn wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+ fn set_waiter_of_interrupt(interrupt: Option<&WaitInterrupt>, waiter: Option<Arc<Waiter>>) {+ if let Some(interrupt) = interrupt {+ *interrupt.waiter.lock().unwrap_or_else(PoisonError::into_inner) = waiter;+ }+ }++ /// Wakes the waiter of an interrupt that is already requested.+ pub(super) fn wake_interrupted(interrupt: &WaitInterrupt) {+ // NB: A waiter looks at the request and goes to sleep while the lists are locked, so this cannot go past it.+ let _lists = lists();+ if let Some(waiter) = &*interrupt.waiter.lock().unwrap_or_else(PoisonError::into_inner) {+ waiter.condition.notify_one();+ }+ }++ pub(super) fn wait(+ word: WaitWord<'_>,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+ ) -> AtomicWaitResult {let address = word.address().addr();let deadline = timeout.and_then(|timeout| Instant::now().checked_add(timeout));// NB: The word is compared while the lists are locked, so a notify that follows a store to it finds this// waiter in its list if the comparison did not see the store.let mut lists = lists();+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::Interrupted;+ }if !word_equals(word, expected) {return AtomicWaitResult::NotEqual;}@@ -108,13 +285,14 @@ mod waiter_list {condition: Condvar::new(),});lists.entry(address).or_default().push_back(waiter.clone());- loop {+ set_waiter_of_interrupt(interrupt, Some(waiter.clone()));+ let result = loop {let interval = match deadline {Some(deadline) => {let remaining = deadline.saturating_duration_since(Instant::now());if remaining.is_zero() {remove(&mut lists, address, &waiter);- return AtomicWaitResult::TimedOut;+ break AtomicWaitResult::TimedOut;}remaining.min(POLL_INTERVAL)}@@ -125,15 +303,21 @@ mod waiter_list {.wait_timeout(lists, interval).unwrap_or_else(|error| error.into_inner()).0;+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ remove(&mut lists, address, &waiter);+ break AtomicWaitResult::Interrupted;+ }// A notify removed the waiter from its list before setting this.if waiter.woken.load(Ordering::Relaxed) {- return AtomicWaitResult::Woken;+ break AtomicWaitResult::Woken;}if !word_equals(word, expected) {remove(&mut lists, address, &waiter);- return AtomicWaitResult::Woken;+ break AtomicWaitResult::Woken;}- }+ };+ set_waiter_of_interrupt(interrupt, None);+ result}pub(super) fn notify(address: *mut u32, max_count: usize) -> usize {@@ -173,6 +357,7 @@ mod os_sync {pub wait_on_address: WaitOnAddress,pub wait_on_address_with_timeout: WaitOnAddressWithTimeout,pub wake_by_address_any: WakeByAddressAny,+ pub wake_by_address_all: WakeByAddressAny,}pub fn functions() -> Option<&'static Functions> {@@ -187,6 +372,7 @@ mod os_sync {let wait_on_address = lookup(c"os_sync_wait_on_address")?;let wait_on_address_with_timeout = lookup(c"os_sync_wait_on_address_with_timeout")?;let wake_by_address_any = lookup(c"os_sync_wake_by_address_any")?;+ let wake_by_address_all = lookup(c"os_sync_wake_by_address_all")?;// SAFETY: The symbols are the libSystem functions with these signatures.unsafe {Some(Functions {@@ -195,6 +381,7 @@ mod os_sync {wait_on_address_with_timeout,),wake_by_address_any: core::mem::transmute::<*mut c_void, WakeByAddressAny>(wake_by_address_any),+ wake_by_address_all: core::mem::transmute::<*mut c_void, WakeByAddressAny>(wake_by_address_all),})}})@@ -208,7 +395,16 @@ fn errno() -> i32 {}#[cfg(target_os = "macos")]-pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+pub fn atomic_wait(+ word: WaitWord<'_>,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+) -> AtomicWaitResult {+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::Interrupted;+ }+// Compare the value in userspace first. os_sync_wait_on_address reports a value mismatch as a *successful* return// (it has no EAGAIN). So, without this, the "not-equal" result would be unreachable; it also faults the shared page// in — avoiding a transient first-touch EFAULT below. A store landing between this read and the kernel's own@@ -227,7 +423,11 @@ pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>)// 4) for both 4-byte and 8-byte waits: the full-width userspace pre-compare above already rejected any full-// value mismatch, so the low half only has to close the lost-wake race at entry — and normalizing the size lets// 4-byte and 8-byte waiters at one address share kernel state (a single spec WaiterList) and cross-wake.+ let _sleeping_on = interrupt.map(|interrupt| interrupt.will_sleep_on(word.address()));loop {+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::Interrupted;+ }let result = if let Some(deadline) = deadline {let remaining = deadline.saturating_duration_since(Instant::now());if remaining.is_zero() {@@ -257,7 +457,14 @@ pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>)};if result >= 0 {- return AtomicWaitResult::Woken;+ if !interrupt.is_some_and(|interrupt| interrupt.was_woken_by_an_interrupt(word.address())) {+ return AtomicWaitResult::Woken;+ }+ // NB: This waiter may go back to sleep, and os_sync would report a value mismatch as a wake.+ if !word_equals(word, expected) && !interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::NotEqual;+ }+ continue;}let error = errno();if error == libc::EINTR {@@ -280,7 +487,7 @@ pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>)}}let remaining = deadline.map(|deadline| deadline.saturating_duration_since(Instant::now()));- waiter_list::wait(word, expected, remaining.or(timeout))+ waiter_list::wait(word, expected, remaining.or(timeout), interrupt)}#[cfg(target_os = "macos")]@@ -313,45 +520,68 @@ pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {woken}+// FUTEX_WAIT_BITSET takes a deadline, on the monotonic clock.#[cfg(target_os = "linux")]-pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+fn monotonic_deadline(timeout: Duration) -> libc::timespec {+ let mut now = libc::timespec { tv_sec: 0, tv_nsec: 0 };+ // SAFETY: The clock always exists, and `now` is a timespec.+ unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &raw mut now) };+ let nanoseconds = now.tv_nsec + libc::c_long::from(timeout.subsec_nanos() as i32);+ let seconds = libc::time_t::try_from(timeout.as_secs()).unwrap_or(libc::time_t::MAX);+ libc::timespec {+ tv_sec: now+ .tv_sec+ .saturating_add(seconds)+ .saturating_add(libc::time_t::from(nanoseconds >= 1_000_000_000)),+ tv_nsec: nanoseconds % 1_000_000_000,+ }+}++#[cfg(target_os = "linux")]+pub fn atomic_wait(+ word: WaitWord<'_>,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+) -> AtomicWaitResult {+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::Interrupted;+ }+// Compare the value in userspace first — so a zero or already-elapsed timeout still yields "not-equal" (which the// spec orders before "timed-out"), and so 8-byte waits share one compare.if !word_equals(word, expected) {return AtomicWaitResult::NotEqual;}- // Anchor the timeout to a fixed deadline — so EINTR retries reduce the remaining time, rather than restarting the- // full timeout (FUTEX_WAIT takes a relative timeout that the kernel doesn't update across a retry).- let deadline = timeout.and_then(|timeout| Instant::now().checked_add(timeout));+ // A fixed deadline — so that neither EINTR nor a wake that wasn't for this waiter restarts the timeout.+ let deadline = timeout.map(monotonic_deadline);+ let deadline_pointer = deadline.as_ref().map_or(core::ptr::null(), core::ptr::from_ref);+ let bits = NOTIFY_BIT | interrupt.map_or(0, WaitInterrupt::wake_bit);+ let _sleeping_on = interrupt.map(|interrupt| interrupt.will_sleep_on(word.address()));loop {- let mut relative_timeout = libc::timespec { tv_sec: 0, tv_nsec: 0 };- let mut timeout_pointer: *const libc::timespec = core::ptr::null();- if let Some(deadline) = deadline {- let remaining = deadline.saturating_duration_since(Instant::now());- if remaining.is_zero() {- return AtomicWaitResult::TimedOut;- }- relative_timeout.tv_sec = remaining.as_secs() as libc::time_t;- relative_timeout.tv_nsec = libc::c_long::from(remaining.subsec_nanos());- timeout_pointer = &raw const relative_timeout;+ if interrupt.is_some_and(WaitInterrupt::is_requested) {+ return AtomicWaitResult::Interrupted;}- // A shared (non-private) FUTEX_WAIT keys on the underlying page — so it coordinates across processes.+ // A shared (non-private) futex keys on the underlying page — so it coordinates across processes.// SAFETY: The address is the aligned word of a shared typed array element.let result = unsafe {libc::syscall(libc::SYS_futex,word.address(),- libc::FUTEX_WAIT,+ libc::FUTEX_WAIT_BITSET,expected as u32,- timeout_pointer,+ deadline_pointer,core::ptr::null::<u32>(),- 0,+ bits,)};if result == 0 {+ if interrupt.is_some_and(|interrupt| interrupt.was_woken_by_an_interrupt(word.address())) {+ continue;+ }return AtomicWaitResult::Woken;}let error = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);@@ -366,35 +596,45 @@ pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>)}}+// Wakes up to `count` waiters of the word with one of `bits` among those they sleep on, and returns how many it woke.#[cfg(target_os = "linux")]-pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {- // FUTEX_WAKE wakes a waiter even when the requested count is zero.- if max_count == 0 {- return 0;- }-- // FUTEX_WAKE wakes waiters keyed on the low 32-bit half at the word's address. That covers both 4-byte and 8-byte waiters- // (8-byte waits key on the low half too), and returns the exact number of waiters woken.- let count = i32::try_from(max_count).unwrap_or(i32::MAX);- // SAFETY: The address is the aligned word of a shared typed array element.+fn wake_waiters_on_bit(word: *mut u32, count: i32, bits: u32) -> usize {+ // SAFETY: Only the address of the word goes to the kernel, which gets nothing out of one that is no longer mapped.let result = unsafe {libc::syscall(libc::SYS_futex,- word.as_ptr(),- libc::FUTEX_WAKE,+ word,+ libc::FUTEX_WAKE_BITSET,count,core::ptr::null::<libc::timespec>(),core::ptr::null::<u32>(),- 0,+ bits,)};usize::try_from(result).unwrap_or(0)}+#[cfg(target_os = "linux")]+pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {+ // FUTEX_WAKE_BITSET wakes a waiter even when the requested count is zero.+ if max_count == 0 {+ return 0;+ }++ // This wakes waiters keyed on the low 32-bit half at the word's address. That covers both 4-byte and 8-byte waiters+ // (8-byte waits key on the low half too), and returns the exact number of waiters woken.+ wake_waiters_on_bit(word.as_ptr(), i32::try_from(max_count).unwrap_or(i32::MAX), NOTIFY_BIT)+}+// No native wait/wake primitive is wired up on this platform.#[cfg(not(any(target_os = "macos", target_os = "linux")))]-pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {- waiter_list::wait(word, expected, timeout)+pub fn atomic_wait(+ word: WaitWord<'_>,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+) -> AtomicWaitResult {+ waiter_list::wait(word, expected, timeout, interrupt)}#[cfg(not(any(target_os = "macos", target_os = "linux")))]@@ -402,10 +642,208 @@ pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {waiter_list::notify(word.as_ptr(), max_count)}-#[cfg(all(test, target_os = "linux"))]-mod linux_tests {+#[cfg(test)]+mod tests {use super::*;+ use std::sync::atomic::AtomicUsize;++ /// Long enough for a thread that was just started to get to sleep.+ const TIME_TO_SLEEP: Duration = Duration::from_millis(30);++ type Wait = fn(&AtomicU32, u64, Option<Duration>, Option<&WaitInterrupt>) -> AtomicWaitResult;+ type Notify = fn(&AtomicU32, usize) -> usize;++ fn wait_in_kernel(+ word: &AtomicU32,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+ ) -> AtomicWaitResult {+ atomic_wait(WaitWord::Word32(word), expected, timeout, interrupt)+ }++ fn wait_in_list(+ word: &AtomicU32,+ expected: u64,+ timeout: Option<Duration>,+ interrupt: Option<&WaitInterrupt>,+ ) -> AtomicWaitResult {+ waiter_list::wait(WaitWord::Word32(word), expected, timeout, interrupt)+ }++ fn notify_in_list(word: &AtomicU32, max_count: usize) -> usize {+ waiter_list::notify(word.as_ptr(), max_count)+ }++ fn for_each_primitive(test: impl Fn(Wait, Notify)) {+ test(wait_in_kernel, atomic_notify);+ test(wait_in_list, notify_in_list);+ }++ #[test]+ fn notifies_wake_waiters_of_a_word_that_did_not_change() {+ for_each_primitive(|wait, notify| {+ let word = AtomicU32::new(0);+ std::thread::scope(|scope| {+ let waiter = scope.spawn(|| wait(&word, 0, None, None));+ // The waiter is among the waiters once a notify wakes it.+ while notify(&word, 1) == 0 {+ std::thread::yield_now();+ }+ assert_eq!(waiter.join().unwrap(), AtomicWaitResult::Woken);+ });+ });+ }++ #[cfg(target_os = "linux")]+ #[test]+ fn a_change_of_the_word_wakes_nobody_in_the_kernel() {+ let word = AtomicU32::new(0);+ std::thread::scope(|scope| {+ let waiter = scope.spawn(|| wait_in_kernel(&word, 0, Some(TIME_TO_SLEEP * 4), None));+ std::thread::sleep(TIME_TO_SLEEP);+ word.store(1, Ordering::SeqCst);+ assert_eq!(waiter.join().unwrap(), AtomicWaitResult::TimedOut);+ });+ }++ #[test]+ fn waits_time_out() {+ for_each_primitive(|wait, notify| {+ let word = AtomicU32::new(0);+ assert_eq!(+ wait(&word, 0, Some(Duration::from_millis(1)), None),+ AtomicWaitResult::TimedOut+ );+ assert_eq!(wait(&word, 0, Some(Duration::ZERO), None), AtomicWaitResult::TimedOut);+ assert_eq!(wait(&word, 1, Some(Duration::ZERO), None), AtomicWaitResult::NotEqual);+ assert_eq!(notify(&word, 1), 0);+ });+ }++ #[test]+ fn notifies_wake_in_the_order_of_the_waits_and_count_what_they_wake() {+ for_each_primitive(|wait, notify| {+ const WAITER_COUNT: usize = 5;+ let word = AtomicU32::new(0);+ let woken_count = AtomicUsize::new(0);+ let positions: [AtomicUsize; WAITER_COUNT] = Default::default();+ std::thread::scope(|scope| {+ for position in &positions {+ let (word, woken_count) = (&word, &woken_count);+ scope.spawn(move || {+ assert_eq!(wait(word, 0, None, None), AtomicWaitResult::Woken);+ position.store(woken_count.fetch_add(1, Ordering::SeqCst) + 1, Ordering::SeqCst);+ });+ std::thread::sleep(TIME_TO_SLEEP);+ }+ for (index, position) in positions.iter().take(2).enumerate() {+ assert_eq!(notify(&word, 1), 1);+ while position.load(Ordering::SeqCst) == 0 {+ std::thread::yield_now();+ }+ assert_eq!(position.load(Ordering::SeqCst), index + 1);+ }+ assert_eq!(notify(&word, 0), 0);+ assert_eq!(notify(&word, usize::MAX), WAITER_COUNT - 2);+ });+ assert_eq!(notify(&word, usize::MAX), 0);+ });+ }++ #[test]+ fn interrupts_end_waits_with_and_without_a_timeout() {+ for_each_primitive(|wait, notify| {+ for timeout in [None, Some(Duration::from_secs(600))] {+ let word = AtomicU32::new(0);+ let interrupt = WaitInterrupt::new();+ std::thread::scope(|scope| {+ let waiter = scope.spawn(|| wait(&word, 0, timeout, Some(&interrupt)));+ std::thread::sleep(TIME_TO_SLEEP);+ interrupt.interrupt();+ assert_eq!(waiter.join().unwrap(), AtomicWaitResult::Interrupted);+ });+ assert_eq!(notify(&word, 1), 0);+ }+ });+ }++ #[test]+ fn interrupted_waits_end_at_once() {+ for_each_primitive(|wait, _| {+ let word = AtomicU32::new(0);+ let interrupt = WaitInterrupt::new();+ interrupt.interrupt();+ assert_eq!(wait(&word, 0, None, Some(&interrupt)), AtomicWaitResult::Interrupted);+ assert_eq!(wait(&word, 1, None, Some(&interrupt)), AtomicWaitResult::Interrupted);+ });+ }++ // An interrupt can come at any point of a wait, which is what a thread on its way to sleep is at risk from.+ #[test]+ fn interrupts_reach_waits_that_are_just_starting() {+ for_each_primitive(|wait, _| {+ for round in 0..2000 {+ let word = AtomicU32::new(0);+ let interrupt = WaitInterrupt::new();+ std::thread::scope(|scope| {+ let waiter = scope.spawn(|| wait(&word, 0, None, Some(&interrupt)));+ for _ in 0..(round % 200) * 20 {+ core::hint::spin_loop();+ }+ interrupt.interrupt();+ assert_eq!(waiter.join().unwrap(), AtomicWaitResult::Interrupted);+ });+ }+ });+ }+ #[test]+ fn interrupts_leave_the_other_waiters_of_a_word_alone() {+ for_each_primitive(|wait, notify| {+ let word = AtomicU32::new(0);+ let interrupted = WaitInterrupt::new();+ let other = WaitInterrupt::new();+ std::thread::scope(|scope| {+ let first = scope.spawn(|| wait(&word, 0, None, Some(&other)));+ std::thread::sleep(TIME_TO_SLEEP);+ let second = scope.spawn(|| wait(&word, 0, None, Some(&interrupted)));+ std::thread::sleep(TIME_TO_SLEEP);+ interrupted.interrupt();+ assert_eq!(second.join().unwrap(), AtomicWaitResult::Interrupted);+ assert!(!first.is_finished());+ assert_eq!(notify(&word, 2), 1);+ assert_eq!(first.join().unwrap(), AtomicWaitResult::Woken);+ });+ });+ }++ #[cfg(target_os = "linux")]+ #[test]+ fn waiters_that_share_a_wake_bit_go_back_to_sleep_without_reporting_anything() {+ let word = AtomicU32::new(0);+ let interrupted = WaitInterrupt::new();+ let mut other = WaitInterrupt::new();+ other.wake_slot = interrupted.wake_slot;+ std::thread::scope(|scope| {+ let (victim, timed) = (+ scope.spawn(|| wait_in_kernel(&word, 0, None, Some(&other))),+ scope.spawn(|| wait_in_kernel(&word, 0, Some(TIME_TO_SLEEP * 6), Some(&other))),+ );+ std::thread::sleep(TIME_TO_SLEEP);+ let target = scope.spawn(|| wait_in_kernel(&word, 0, None, Some(&interrupted)));+ std::thread::sleep(TIME_TO_SLEEP);+ interrupted.interrupt();+ assert_eq!(target.join().unwrap(), AtomicWaitResult::Interrupted);+ std::thread::sleep(TIME_TO_SLEEP);+ assert!(!victim.is_finished());+ assert_eq!(timed.join().unwrap(), AtomicWaitResult::TimedOut);+ assert_eq!(atomic_notify(&word, 1), 1);+ assert_eq!(victim.join().unwrap(), AtomicWaitResult::Woken);+ });+ }++ #[cfg(target_os = "linux")]#[test]fn zero_count_does_not_wake_waiters() {let word = AtomicU64::new(0);@@ -414,7 +852,7 @@ mod linux_tests {let notify_word = unsafe { &*word.as_ptr().cast::<AtomicU32>() };for wait_word in [WaitWord::Word32(notify_word), WaitWord::Word64(&word)] {std::thread::scope(|scope| {- let waiter = scope.spawn(|| atomic_wait(wait_word, 0, Some(Duration::from_millis(100))));+ let waiter = scope.spawn(|| atomic_wait(wait_word, 0, Some(Duration::from_millis(100)), None));while !waiter.is_finished() {assert_eq!(atomic_notify(notify_word, 0), 0);std::thread::yield_now();@@ -423,29 +861,55 @@ mod linux_tests {});}}-}--#[cfg(all(test, not(target_os = "linux")))]-mod tests {- use super::*;+ // SharedArrayBuffers cross processes, where a waiter sleeps on a word that the notifier has at another address.+ #[cfg(target_os = "linux")]#[test]- fn waiter_lists_wake_waiters_of_a_word_that_did_not_change() {- static WORD: AtomicU32 = AtomicU32::new(0);- let waiter = std::thread::spawn(|| waiter_list::wait(WaitWord::Word32(&WORD), 0, None));- // The waiter is in its list once a notify wakes it.- while waiter_list::notify(WORD.as_ptr(), 1) == 0 {- std::thread::yield_now();+ fn notifies_and_interrupts_cross_processes() {+ // SAFETY: A new mapping, which the words below live in and which is never unmapped.+ let shared = unsafe {+ libc::mmap(+ core::ptr::null_mut(),+ 4096,+ libc::PROT_READ | libc::PROT_WRITE,+ libc::MAP_SHARED | libc::MAP_ANONYMOUS,+ -1,+ 0,+ )+ };+ assert_ne!(shared, libc::MAP_FAILED);+ // SAFETY: The mapping is zeroed, aligned and large enough.+ let (word, other_word) = unsafe { (&*shared.cast::<AtomicU32>(), &*shared.cast::<AtomicU32>().add(1)) };++ // SAFETY: The child only sleeps and exits, without anything that another thread could hold a lock of.+ let child = unsafe { libc::fork() };+ assert!(child >= 0);+ if child == 0 {+ let result = atomic_wait(WaitWord::Word32(word), 0, Some(Duration::from_secs(20)), None);+ // SAFETY: Ends the child without running anything of the test harness.+ unsafe { libc::_exit(i32::from(result != AtomicWaitResult::Woken)) };}- assert_eq!(waiter.join().unwrap(), AtomicWaitResult::Woken);- assert_eq!(WORD.load(Ordering::SeqCst), 0);- }- #[test]- fn waiter_lists_time_out() {- static WORD: AtomicU32 = AtomicU32::new(0);- let result = waiter_list::wait(WaitWord::Word32(&WORD), 0, Some(Duration::from_millis(1)));- assert_eq!(result, AtomicWaitResult::TimedOut);- assert_eq!(waiter_list::notify(WORD.as_ptr(), 1), 0);+ let start = std::time::Instant::now();+ while atomic_notify(word, 1) == 0 {+ assert!(+ start.elapsed() < Duration::from_secs(10),+ "the child never started waiting"+ );+ std::thread::sleep(Duration::from_millis(1));+ }+ let mut status = 0;+ // SAFETY: Waits for the child of this thread.+ assert_eq!(unsafe { libc::waitpid(child, &raw mut status, 0) }, child);+ assert!(libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0);++ // A waiter that shares a mapping with another process still has its interrupt.+ let interrupt = WaitInterrupt::new();+ std::thread::scope(|scope| {+ let waiter = scope.spawn(|| atomic_wait(WaitWord::Word32(other_word), 0, None, Some(&interrupt)));+ std::thread::sleep(TIME_TO_SLEEP);+ interrupt.interrupt();+ assert_eq!(waiter.join().unwrap(), AtomicWaitResult::Interrupted);+ });}}diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex 39a3ff8b79f..0ebb81da45c 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -13,7 +13,9 @@ use std::collections::HashMap;use std::collections::HashSet;use std::collections::VecDeque;use std::rc::Rc;+use std::sync::Arc;+use ak::futex::WaitInterrupt;use ak::{Utf16FlyString, Utf16String};use libjs_runtime_macros::Trace;@@ -516,6 +518,8 @@ pub struct Vm {pub jit_native_stack_limit: AtomicUsize,/// Set once script on this VM has to stop for good (see request_interrupt()). Any thread may set it.interrupt_requested: AtomicBool,+ /// What wakes this VM's thread from Atomics.wait() for an interrupt.+ wait_interrupt: Arc<WaitInterrupt>,/// The tables `VmHead::dispatch_tables` points to.pub(crate) own_dispatch_tables: VmDispatchTables,heap: OnceCell<Heap>,@@ -691,6 +695,7 @@ impl Vm {},jit_native_stack_limit: AtomicUsize::new(0),interrupt_requested: AtomicBool::new(false),+ wait_interrupt: Arc::new(WaitInterrupt::new()),own_dispatch_tables: VmDispatchTables::new(),heap: OnceCell::new(),_interpreter_stack_memory: interpreter_stack_memory,@@ -888,8 +893,8 @@ impl Vm {////// Script costs nothing to run for this: the interpreter stops at its next dispatch, as every dispatch table entry/// becomes the fallback handler, and JIT code stops at its next call or loop back edge, which compare the stack- /// pointer with a limit anyway, so raising the limit past every stack pointer does it. Blocking native functions- /// poll the flag.+ /// pointer with a limit anyway, so raising the limit past every stack pointer does it. A thread that sleeps on a word+ /// of shared memory gets woken. This returns once it is awake.////// Script unwinds with interrupt_exception(), past every handler. The flag is never cleared: every later attempt to/// run code fails at once, which is what makes the places in native code that catch exceptions harmless. They can@@ -899,6 +904,12 @@ impl Vm {self.jit_native_stack_limit.store(usize::MAX, Ordering::Relaxed);fence(Ordering::SeqCst);self.own_dispatch_tables.stop_dispatching();+ self.wait_interrupt.interrupt();+ }++ /// What ends a wait of this VM's thread on a word of shared memory.+ pub fn wait_interrupt(&self) -> &Arc<WaitInterrupt> {+ &self.wait_interrupt}}diff --git a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs b/Libraries/LibJS/Rust/src/runtime/array_buffer.rsindex 0d16c31d42a..874cd7b7c4f 100644--- a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs+++ b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs@@ -44,7 +44,7 @@ use crate::runtime::typed_array::TypedArrayBase;use crate::runtime::value::same_value;use crate::runtime::value_conversions;use crate::runtime::value_conversions::MAX_ARRAY_LIKE_INDEX;-use ak::futex::{self, AtomicWaitResult, WaitWord};+use ak::futex::{self, AtomicWaitResult, WaitInterrupt, WaitWord};pub use crate::gc::primitive_storage::cage_base as primitive_storage_cage_base;pub use crate::gc::primitive_storage::{OutOfMemory, ZeroFillNewBytes};@@ -1560,6 +1560,7 @@ impl ArrayBuffer {expected: u64,size: usize,timeout: Option<Duration>,+ interrupt: &WaitInterrupt,) -> AtomicWaitResult {// NB: The data block stays borrowed while the agent waits, since the agent is the only one that could change it.let data_block = self.data_block.borrow();@@ -1568,7 +1569,7 @@ impl ArrayBuffer {8 => WaitWord::Word64(data_block.atomic_at(byte_index)),_ => unreachable!("Atomics.wait waits on 4-byte or 8-byte elements"),};- futex::atomic_wait(word, expected, timeout)+ futex::atomic_wait(word, expected, timeout, Some(interrupt))}/// Core::atomic_notify() on the word at `byte_index`.diff --git a/Libraries/LibJS/Rust/src/runtime/atomics_object.rs b/Libraries/LibJS/Rust/src/runtime/atomics_object.rsindex a45ad00fa50..eca668f1493 100644--- a/Libraries/LibJS/Rust/src/runtime/atomics_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/atomics_object.rs@@ -256,8 +256,10 @@ fn do_wait(value as u64,typed_array.element_size(),wait_timeout,+ vm.wait_interrupt(),);let result = match result {+ AtomicWaitResult::Interrupted => return vm.throw_interrupt(),// 25. If waiterRecord.[[Result]] is "not-equal", return the String "not-equal".AtomicWaitResult::NotEqual => "not-equal",// 26. If waiterRecord.[[Result]] is "timed-out", return the String "timed-out".diff --git a/Libraries/LibWasm/Engine/src/runtime/helpers.rs b/Libraries/LibWasm/Engine/src/runtime/helpers.rsindex 366a76a234a..3c9138dcd45 100644--- a/Libraries/LibWasm/Engine/src/runtime/helpers.rs+++ b/Libraries/LibWasm/Engine/src/runtime/helpers.rs@@ -280,10 +280,11 @@ unsafe fn wait(context: *mut ExecutionContext) -> u64 {}};let timeout = (timeout >= 0).then(|| std::time::Duration::from_nanos(timeout as u64));- match futex::atomic_wait(word, expected, timeout) {+ match futex::atomic_wait(word, expected, timeout, None) {AtomicWaitResult::Woken => 0,AtomicWaitResult::NotEqual => 1,AtomicWaitResult::TimedOut => 2,+ AtomicWaitResult::Interrupted => unreachable!("nothing can interrupt this wait"),}}