Truly independent web browser
LibWeb: Take turns at the render state slot in order of arrival
Andreas Kling4c10f989f693
diff --git a/Libraries/LibWeb/Rust/src/render_state/owner.rs b/Libraries/LibWeb/Rust/src/render_state/owner.rsindex 08a2003516d..aecf5e30ee6 100644--- a/Libraries/LibWeb/Rust/src/render_state/owner.rs+++ b/Libraries/LibWeb/Rust/src/render_state/owner.rs@@ -18,7 +18,7 @@ use std::cell::{Cell, RefCell, UnsafeCell};use std::collections::HashMap;use std::sync::Arc;use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};-use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError};+use std::sync::{Condvar, LazyLock, Mutex, PoisonError};/// The host's name for its document's render state, which it mints without asking the owner.#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]@@ -69,9 +69,44 @@ impl SharedWithHost {}}+/// The turns threads take at the slot, in the order they ask for them. A thread that holds the slot over and over, as+/// the ticks of a clock that falls behind do, would otherwise take it ahead of a thread waiting to wake, for as long as+/// it goes on.+#[derive(Default)]+struct Turns {+ next: AtomicU64,+ serving: AtomicU64,+ waiting: Mutex<()>,+ turn_ended: Condvar,+}++impl Turns {+ fn take(&self) {+ // NB: These operations are sequentially consistent, or this thread could read the turn as not yet its own while+ // the holder ending it reads no ticket past its own, and wakes nobody.+ let ticket = self.next.fetch_add(1, Ordering::SeqCst);+ if self.serving.load(Ordering::SeqCst) == ticket {+ return;+ }+ let mut waiting = self.waiting.lock().unwrap_or_else(PoisonError::into_inner);+ while self.serving.load(Ordering::SeqCst) != ticket {+ waiting = self.turn_ended.wait(waiting).unwrap_or_else(PoisonError::into_inner);+ }+ }++ fn end(&self) {+ let serving = self.serving.fetch_add(1, Ordering::SeqCst) + 1;+ if self.next.load(Ordering::SeqCst) != serving {+ // NB: A waiter holds the lock from reading the turn until it sleeps, so this reaches it.+ drop(self.waiting.lock().unwrap_or_else(PoisonError::into_inner));+ self.turn_ended.notify_all();+ }+ }+}+/// The slot every document's render state lives in, which one thread holds at a time.struct Slot {- held: Mutex<()>,+ held: Turns,/// Each document's render state and clock lanes, which only the thread that holds the slot reaches. An entry is/// boxed, so a job reaching it keeps reaching it while the job makes another document's state.states: RefCell<HashMap<DocumentId, Box<DocumentEntry>>>,@@ -95,7 +130,7 @@ const _: () = {};static SLOT: LazyLock<Slot> = LazyLock::new(|| Slot {- held: Mutex::new(()),+ held: Turns::default(),states: RefCell::new(HashMap::new()),process_style: Arc::default(),tsan_key: AtomicBool::new(false),@@ -130,15 +165,17 @@ pub(super) fn is_held_here() -> bool {/// Runs `job` holding the slot, which the calling thread takes first where it does not hold it yet, waiting for/// whichever thread holds it to let go.pub(super) fn hold<R>(job: impl FnOnce() -> R) -> R {- struct Holding(Option<MutexGuard<'static, ()>>);+ struct Holding {+ took_the_slot: bool,+ }impl Drop for Holding {fn drop(&mut self) {HOLDS.set(HOLDS.get() - 1);- let Some(guard) = self.0.take() else {+ if !self.took_the_slot {return;- };+ }crate::stage_thread::tsan::release(&SLOT.tsan_key);- drop(guard);+ SLOT.held.end();// NB: Taken out before dropping them, since a drop may hold the slot again.let dropped = DROPPED_AFTER_HOLD.take();// A lane's drop resumes the panic of the recording it waits for, which a thread that is already unwinding@@ -150,13 +187,13 @@ pub(super) fn hold<R>(job: impl FnOnce() -> R) -> R {}}}- let taken = (!is_held_here()).then(|| {- let guard = SLOT.held.lock().unwrap_or_else(PoisonError::into_inner);+ let took_the_slot = !is_held_here();+ if took_the_slot {+ SLOT.held.take();crate::stage_thread::tsan::acquire(&SLOT.tsan_key);- guard- });+ }HOLDS.set(HOLDS.get() + 1);- let _holding = Holding(taken);+ let _holding = Holding { took_the_slot };job()}