mirror of
https://github.com/servo/servo.git
synced 2025-06-08 08:33:26 +00:00
* script: Do not run layout in a thread Instead of spawning a thread for layout that almost always runs synchronously with script, simply run layout in the script thread. This is a resurrection of #28708, taking just the bits that remove the layout thread. It's a complex change and thus is just a first step toward cleaning up the interface between script and layout. Messages are still passed from script to layout via a `process()` method and script proxies some messages to layout from other threads as well. Big changes: 1. Layout is created in the script thread on Document load, thus every live document is guaranteed to have a layout. This isn't completely hidden in the interface, but we can safely `unwrap()` on a Document's layout. 2. Layout configuration is abstracted away into a LayoutConfig struct and the LayoutFactory is a struct passed around by the Constellation. This is to avoid having to monomorphize the entire script thread for each layout. 3. Instead of having the Constellation block on the layout thread to figure out the current epoch and whether there are pending web fonts loading, updates are sent synchronously to the Constellation when rendering to a screenshot. This practically only used by the WPT. A couple tests start to fail, which is probably inevitable since removing the layout thread has introduced timing changes in "exit after load" and screenshot behavior. Co-authored-by: Josh Matthews <josh@joshmatthews.net> * Update test expectations * Fix some issues found during review * Clarify some comments * Address review comments --------- Co-authored-by: Josh Matthews <josh@joshmatthews.net>
126 lines
3.9 KiB
Rust
126 lines
3.9 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
//! A shareable mutable container for the DOM.
|
|
|
|
use std::cell::{BorrowError, BorrowMutError};
|
|
#[cfg(not(feature = "refcell_backtrace"))]
|
|
pub use std::cell::{Ref, RefCell, RefMut};
|
|
|
|
#[cfg(feature = "refcell_backtrace")]
|
|
pub use accountable_refcell::{ref_filter_map, Ref, RefCell, RefMut};
|
|
#[cfg(not(feature = "refcell_backtrace"))]
|
|
pub use ref_filter_map::ref_filter_map;
|
|
|
|
use crate::dom::bindings::root::{assert_in_layout, assert_in_script};
|
|
|
|
/// A mutable field in the DOM.
|
|
///
|
|
/// This extends the API of `std::cell::RefCell` to allow unsafe access in
|
|
/// certain situations, with dynamic checking in debug builds.
|
|
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
|
|
pub struct DomRefCell<T> {
|
|
value: RefCell<T>,
|
|
}
|
|
|
|
// Functionality specific to Servo's `DomRefCell` type
|
|
// ===================================================
|
|
|
|
impl<T> DomRefCell<T> {
|
|
/// Return a reference to the contents.
|
|
///
|
|
/// For use in layout only.
|
|
#[allow(unsafe_code)]
|
|
pub unsafe fn borrow_for_layout(&self) -> &T {
|
|
assert_in_layout();
|
|
self.value
|
|
.try_borrow_unguarded()
|
|
.expect("cell is mutably borrowed")
|
|
}
|
|
|
|
/// Borrow the contents for the purpose of script deallocation.
|
|
///
|
|
#[allow(unsafe_code)]
|
|
pub unsafe fn borrow_for_script_deallocation(&self) -> &mut T {
|
|
assert_in_script();
|
|
&mut *self.value.as_ptr()
|
|
}
|
|
|
|
/// Mutably borrow a cell for layout. Ideally this would use
|
|
/// `RefCell::try_borrow_mut_unguarded` but that doesn't exist yet.
|
|
#[allow(unsafe_code)]
|
|
pub unsafe fn borrow_mut_for_layout(&self) -> &mut T {
|
|
assert_in_layout();
|
|
&mut *self.value.as_ptr()
|
|
}
|
|
}
|
|
|
|
// Functionality duplicated with `std::cell::RefCell`
|
|
// ===================================================
|
|
impl<T> DomRefCell<T> {
|
|
/// Create a new `DomRefCell` containing `value`.
|
|
pub fn new(value: T) -> DomRefCell<T> {
|
|
DomRefCell {
|
|
value: RefCell::new(value),
|
|
}
|
|
}
|
|
|
|
/// Immutably borrows the wrapped value.
|
|
///
|
|
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
|
/// immutable borrows can be taken out at the same time.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if this is called off the script thread.
|
|
///
|
|
/// Panics if the value is currently mutably borrowed.
|
|
pub fn borrow(&self) -> Ref<T> {
|
|
self.value.borrow()
|
|
}
|
|
|
|
/// Mutably borrows the wrapped value.
|
|
///
|
|
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
|
/// cannot be borrowed while this borrow is active.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if this is called off the script thread.
|
|
///
|
|
/// Panics if the value is currently borrowed.
|
|
pub fn borrow_mut(&self) -> RefMut<T> {
|
|
self.value.borrow_mut()
|
|
}
|
|
|
|
/// Attempts to immutably borrow the wrapped value.
|
|
///
|
|
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
|
/// immutable borrows can be taken out at the same time.
|
|
///
|
|
/// Returns `None` if the value is currently mutably borrowed.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if this is called off the script thread.
|
|
pub fn try_borrow(&self) -> Result<Ref<T>, BorrowError> {
|
|
assert_in_script();
|
|
self.value.try_borrow()
|
|
}
|
|
|
|
/// Mutably borrows the wrapped value.
|
|
///
|
|
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
|
/// cannot be borrowed while this borrow is active.
|
|
///
|
|
/// Returns `None` if the value is currently borrowed.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if this is called off the script thread.
|
|
pub fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError> {
|
|
assert_in_script();
|
|
self.value.try_borrow_mut()
|
|
}
|
|
}
|