Truly independent web browser
LibWeb: Have a test’s claim hold give way to waits on the Render thread
sideshowbarkereb86422da3c2
diff --git a/Libraries/LibWeb/Rust/src/painting/recording_slot.rs b/Libraries/LibWeb/Rust/src/painting/recording_slot.rsindex 930d5ee6935..988d02c0d8e 100644--- a/Libraries/LibWeb/Rust/src/painting/recording_slot.rs+++ b/Libraries/LibWeb/Rust/src/painting/recording_slot.rs@@ -34,6 +34,7 @@ use crate::render_state::{LockstepProof, SampledFrame, TaskBoundary};use crate::stage_thread::InFlight;#[cfg(not(test))]use crate::stage_thread::{ParkedJob, StopWord};+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};/// A display list recording: the frame it records, which it drops before it returns, the rows@@ -327,6 +328,13 @@ enum RecordingHold {Idle,Armed,Holding,+ // The Render thread waits inside its claim of a style update that flies, until the hold goes.+ #[cfg_attr(test, allow(dead_code))]+ HoldingClaim,+ // The claim hold gave way to a job another thread waits for on the Render thread, which then leaves the style+ // update unclaimed, for the host to take back.+ #[cfg_attr(test, allow(dead_code))]+ ClaimYielded,// A layout held is never handed to the Render thread until it goes, so that the thread runs the jobs of other// documents meanwhile, as one the collector finalizes asks of it.#[cfg(not(test))]@@ -339,6 +347,9 @@ enum RecordingHold {static RECORDING_HOLD: Mutex<RecordingHold> = Mutex::new(RecordingHold::Idle);static RECORDING_HOLD_RELEASED: Condvar = Condvar::new();+// Whether the claim hold keeps the Render thread waiting, which a thread about to wait for the Render thread reads+// without taking the lock.+static CLAIM_HOLD_BLOCKS_RENDER_THREAD: AtomicBool = AtomicBool::new(false);fn recording_hold() -> MutexGuard<'static, RecordingHold> {RECORDING_HOLD.lock().unwrap_or_else(PoisonError::into_inner)@@ -346,7 +357,10 @@ fn recording_hold() -> MutexGuard<'static, RecordingHold> {/// Whether a test holds a recording that flies.pub(crate) fn recording_is_held_for_testing() -> bool {- matches!(*recording_hold(), RecordingHold::Holding)+ matches!(+ *recording_hold(),+ RecordingHold::Holding | RecordingHold::HoldingClaim | RecordingHold::ClaimYielded+ )}/// Whether the recording about to fly is the one the test hold is armed for.@@ -396,7 +410,8 @@ pub(crate) fn take_style_flight_claim_hold_for_testing() -> bool {if !matches!(*hold, RecordingHold::ArmedForClaim) {return false;}- *hold = RecordingHold::Holding;+ *hold = RecordingHold::HoldingClaim;+ CLAIM_HOLD_BLOCKS_RENDER_THREAD.store(true, Ordering::Release);true}@@ -406,6 +421,20 @@ pub(crate) fn wait_while_recording_is_held_for_testing() {.unwrap_or_else(PoisonError::into_inner);}+/// Waits while the claim hold keeps the Render thread from claiming a style update, and answers whether to claim it: A+/// hold that gave way to another job leaves the update for the host to take back, as the test holding it expects.+#[cfg(not(test))]+pub(crate) fn wait_while_claim_is_held_for_testing() -> bool {+ let mut hold = RECORDING_HOLD_RELEASED+ .wait_while(recording_hold(), |hold| matches!(hold, RecordingHold::HoldingClaim))+ .unwrap_or_else(PoisonError::into_inner);+ if !matches!(*hold, RecordingHold::ClaimYielded) {+ return true;+ }+ *hold = RecordingHold::Idle;+ false+}+/// Holds the next recording that flies before it reads its frame, until/// [`release_held_recording_for_testing`] or a wait for it.pub(crate) fn hold_next_recording_for_testing() {@@ -428,9 +457,26 @@ pub(crate) fn hold_next_style_flight_claim_for_testing() {/// Lets the recording held for the test go, or disarms the hold no recording has taken yet.pub(crate) fn release_held_recording_for_testing() {*recording_hold() = RecordingHold::Idle;+ CLAIM_HOLD_BLOCKS_RENDER_THREAD.store(false, Ordering::Release);RECORDING_HOLD_RELEASED.notify_all();}+/// Has the claim hold give way, as it keeps the Render thread from every job after the one it holds: A thread that waits+/// for one of those, as the collector finalizing a document waits for its layout to be torn down, would wait forever.+/// The Render thread then leaves the held style update unclaimed, which the host takes back as it would have.+#[cfg(not(test))]+pub(crate) fn yield_claim_hold_for_testing() {+ if !CLAIM_HOLD_BLOCKS_RENDER_THREAD.load(Ordering::Acquire) {+ return;+ }+ let mut hold = recording_hold();+ if matches!(*hold, RecordingHold::HoldingClaim) {+ *hold = RecordingHold::ClaimYielded;+ RECORDING_HOLD_RELEASED.notify_all();+ }+ CLAIM_HOLD_BLOCKS_RENDER_THREAD.store(false, Ordering::Release);+}+/// Records `frame` of the document's `viewport` with `inputs` and `recorder`, which keeps the inputs of a recording that/// publishes, and answers what it recorded, with the trace it left where `trace_recordings` asks for one.pub(crate) fn record_frame(diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rsindex f30adec7d2d..e81abb0b949 100644--- a/Libraries/LibWeb/Rust/src/render_state.rs+++ b/Libraries/LibWeb/Rust/src/render_state.rs@@ -1003,8 +1003,9 @@ pub(crate) fn fly(host: &DocumentHost, job: FrameJob, _license: &crate::paintingcrate::painting::recording_slot::submit_layout(run, |job| owner::hold(job))} else if claim_held {let flight = crate::stage_thread::render_thread().submit_reclaimable(run, |job| {- crate::painting::recording_slot::wait_while_recording_is_held_for_testing();- owner::hold(job);+ if crate::painting::recording_slot::wait_while_claim_is_held_for_testing() {+ owner::hold(job);+ }});(flight, true)} else {diff --git a/Libraries/LibWeb/Rust/src/stage_thread.rs b/Libraries/LibWeb/Rust/src/stage_thread.rsindex 217c5fc99b5..8f15f14379f 100644--- a/Libraries/LibWeb/Rust/src/stage_thread.rs+++ b/Libraries/LibWeb/Rust/src/stage_thread.rs@@ -202,6 +202,12 @@ impl StageThread {if self.is_current() {return job();}+ // A test that holds the Render thread in its claim of a style update gives way to a job another thread waits+ // for, which would otherwise wait behind the hold forever.+ #[cfg(not(test))]+ if std::ptr::eq(self, render_thread()) {+ crate::painting::recording_slot::yield_claim_hold_for_testing();+ }let mut answer = None;let job = JobInFrame::new(|| answer = Some(std::panic::catch_unwind(AssertUnwindSafe(job))),diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.pyindex 731dfbd5549..03178094743 100755--- a/Meta/Linters/check_rust_process_wide_state.py+++ b/Meta/Linters/check_rust_process_wide_state.py@@ -192,6 +192,7 @@ RENDER_STATE_ALLOWED = {f"{RENDER_STATE_CRATE}/src/css/parser/value_parser.rs:LISTS": "built-once immutable lists of initial background values",f"{RENDER_STATE_CRATE}/src/painting/recording_slot.rs:RECORDING_HOLD": "a test's hold on the next recording that flies, behind a mutex; only internals arms it",f"{RENDER_STATE_CRATE}/src/painting/recording_slot.rs:RECORDING_HOLD_RELEASED": "wakes the recording a test held once it lets it go",+ f"{RENDER_STATE_CRATE}/src/painting/recording_slot.rs:CLAIM_HOLD_BLOCKS_RENDER_THREAD": "whether a test's claim hold blocks the Render thread, which a thread about to wait for that thread reads without the hold's mutex",f"{RENDER_STATE_CRATE}/src/stage_thread.rs:THREAD_SETUP": "set once before the first stage thread starts, which runs it; read-only after",f"{RENDER_STATE_CRATE}/src/stage_thread.rs:FLIGHT_FINISHED": "set once before the first job is submitted, which a stage thread calls when one finishes; read-only after",f"{RENDER_STATE_CRATE}/src/stage_thread.rs:RENDER_THREAD": "the process's one Render thread, which every thread hands its jobs to",