Truly independent web browser
AK+LibJS+LibWasm: Share one copy of the futex code
Andreas Kling65bee30e22c4
diff --git a/AK/Rust/Cargo.toml b/AK/Rust/Cargo.tomlindex af2e67019ad..eea0a493874 100644--- a/AK/Rust/Cargo.toml+++ b/AK/Rust/Cargo.toml@@ -6,6 +6,11 @@ edition = "2024"[lib]crate-type = ["rlib"]+# After changing dependencies, regenerate the Flatpak sources:+# python3 Meta/CMake/flatpak/generate-cargo-sources.py+[dependencies]+libc = "0.2"+[build-dependencies]cc = "1"diff --git a/Libraries/LibJS/Rust/src/futex.rs b/AK/Rust/src/futex.rssimilarity index 96%rename from Libraries/LibJS/Rust/src/futex.rsrename to AK/Rust/src/futex.rsindex 9d5151c8ae9..0d1d46c7b1e 100644--- a/Libraries/LibJS/Rust/src/futex.rs+++ b/AK/Rust/src/futex.rs@@ -21,7 +21,7 @@ pub enum AtomicWaitResult {/// The word of a shared typed array element that Atomics.wait waits on, which is 4 or 8 bytes wide.#[derive(Clone, Copy)]-pub(crate) enum WaitWord<'a> {+pub enum WaitWord<'a> {Word32(&'a AtomicU32),Word64(&'a AtomicU64),}@@ -208,7 +208,7 @@ fn errno() -> i32 {}#[cfg(target_os = "macos")]-pub(crate) fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {// 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@@ -284,7 +284,7 @@ pub(crate) fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Dur}#[cfg(target_os = "macos")]-pub(crate) fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {+pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {// Waiters key on the low 32-bit half — so, wake with size 4 regardless of the notifying view's element size. That// lets a 4-byte notify wake an 8-byte waiter (and vice versa) at the same address — which the spec's single per-// address WaiterList requires.@@ -314,7 +314,7 @@ pub(crate) fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {}#[cfg(target_os = "linux")]-pub(crate) fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {// 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) {@@ -367,7 +367,7 @@ pub(crate) fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Dur}#[cfg(target_os = "linux")]-pub(crate) fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {+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;@@ -393,12 +393,12 @@ pub(crate) fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {// No native wait/wake primitive is wired up on this platform.#[cfg(not(any(target_os = "macos", target_os = "linux")))]-pub(crate) fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {+pub fn atomic_wait(word: WaitWord<'_>, expected: u64, timeout: Option<Duration>) -> AtomicWaitResult {waiter_list::wait(word, expected, timeout)}#[cfg(not(any(target_os = "macos", target_os = "linux")))]-pub(crate) fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {+pub fn atomic_notify(word: &AtomicU32, max_count: usize) -> usize {waiter_list::notify(word.as_ptr(), max_count)}diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rsindex 82cf03c4ded..f6bc4760c4d 100644--- a/AK/Rust/src/lib.rs+++ b/AK/Rust/src/lib.rs@@ -6,6 +6,7 @@//! Rust views of shared AK data structures.+pub mod futex;mod generic_lexer;pub mod math;pub mod perf_map;diff --git a/Cargo.lock b/Cargo.lockindex c5fb5d97a9b..a93c642deee 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -86,6 +86,7 @@ name = "ak"version = "0.1.0"dependencies = ["cc",+ "libc",][[package]]diff --git a/Libraries/LibJS/Rust/src/lib.rs b/Libraries/LibJS/Rust/src/lib.rsindex 589243f54af..e69a93c2aa6 100644--- a/Libraries/LibJS/Rust/src/lib.rs+++ b/Libraries/LibJS/Rust/src/lib.rs@@ -105,7 +105,6 @@ pub mod fast_hash;#[cfg(not(test))]pub mod frontend_host;#[cfg(any(not(test), target_os = "linux"))]-pub mod futex;#[cfg(not(test))]pub mod gc;#[cfg(not(test))]diff --git a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs b/Libraries/LibJS/Rust/src/runtime/array_buffer.rsindex 6f103f3d03b..0d16c31d42a 100644--- a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs+++ b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs@@ -16,7 +16,6 @@ use core::time::Duration;use libjs_runtime_macros::Trace;use num_bigint::BigInt as NumBigInt;-use crate::futex::{self, AtomicWaitResult, WaitWord};use crate::gc::capi::{GC_PRIMITIVE_STORAGE_NULL_HANDLE, GCPrimitiveStorageHandle, gc_cell_type_info};use crate::gc::class::{ExternalMemorySize, Finalize, GcCell, define_cell};use crate::gc::foreign::ForeignCellSlot;@@ -45,6 +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};pub use crate::gc::primitive_storage::cage_base as primitive_storage_cage_base;pub use crate::gc::primitive_storage::{OutOfMemory, ZeroFillNewBytes};diff --git a/Libraries/LibJS/Rust/src/runtime/atomics_object.rs b/Libraries/LibJS/Rust/src/runtime/atomics_object.rsindex 8fd174f2e9f..a45ad00fa50 100644--- a/Libraries/LibJS/Rust/src/runtime/atomics_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/atomics_object.rs@@ -8,7 +8,6 @@ use core::time::Duration;use libjs_runtime_macros::Trace;-use crate::futex::AtomicWaitResult;use crate::gc::class::{GcCell, define_cell};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;@@ -29,6 +28,7 @@ use crate::runtime::typed_array::{ContentType, Kind, TypedArrayBase, TypedArrayWithBufferWitness, is_typed_array_out_of_bounds,make_typed_array_with_buffer_witness_record, typed_array_from, typed_array_length, validate_typed_array,};+use ak::futex::AtomicWaitResult;#[repr(C)]#[derive(Trace)]diff --git a/Libraries/LibWasm/Engine/src/lib.rs b/Libraries/LibWasm/Engine/src/lib.rsindex a2777480eae..a6a2f929aff 100644--- a/Libraries/LibWasm/Engine/src/lib.rs+++ b/Libraries/LibWasm/Engine/src/lib.rs@@ -19,10 +19,6 @@ mod rust_demangle;#[path = "../../../RustPanic.rs"]mod rust_panic;-// NB: Atomics.wait and Atomics.notify's waiting and waking, which memory.atomic.wait and memory.atomic.notify share.-#[path = "../../../LibJS/Rust/src/futex.rs"]-mod futex;-#[cfg(feature = "capi")]pub mod capi;pub mod runtime;diff --git a/Libraries/LibWasm/Engine/src/runtime/helpers.rs b/Libraries/LibWasm/Engine/src/runtime/helpers.rsindex aace3a65775..366a76a234a 100644--- a/Libraries/LibWasm/Engine/src/runtime/helpers.rs+++ b/Libraries/LibWasm/Engine/src/runtime/helpers.rs@@ -12,8 +12,8 @@ use crate::abi::{ExecutionContext, FRAME_FUNCTION, FRAME_HEADER_SIZE, FRAME_INSTANCE, FRAME_RETURN_IP, FRAME_SAVED_FP, MemoryData,TableData,};-use crate::futex::{self, AtomicWaitResult, WaitWord};use crate::validator::{CatchKind, NO_EXCEPTION_LOCAL, NOT_DELEGATED};+use ak::futex::{self, AtomicWaitResult, WaitWord};export_runtime_functions! {wasm_runtime_memory_grow => memory_grow,@@ -269,7 +269,7 @@ unsafe fn wait(context: *mut ExecutionContext) -> u64 {));return crate::abi::WAIT_CANNOT_BLOCK;}- // NB: The waits and wakes are those of Atomics.wait and Atomics.notify (LibJS's futex), whose waiter lists are+ // NB: The waits and wakes are those of Atomics.wait and Atomics.notify (ak::futex), whose waiter lists are// the operating system's, per address: JavaScript and WebAssembly wake each other, as the memory model's// single WaiterList per location requires.let word = unsafe {