Rename PrivateStyleData to PersistentStyleData and use AtomicRefCell instead of RefCell as a container.

This allows us to eliminate the unsafe borrows. \o/

MozReview-Commit-ID: 29hpGaWUFQz
This commit is contained in:
Bobby Holley 2016-09-29 17:09:06 -07:00
parent 5bcc4192bf
commit 687e1f701c
12 changed files with 96 additions and 142 deletions

View file

@ -16,7 +16,7 @@
use app_units::Au; use app_units::Au;
use block::BlockFlow; use block::BlockFlow;
use context::LayoutContext; use context::LayoutContext;
use data::{HAS_NEWLY_CONSTRUCTED_FLOW, PrivateLayoutData}; use data::{HAS_NEWLY_CONSTRUCTED_FLOW, PersistentLayoutData};
use flex::FlexFlow; use flex::FlexFlow;
use floats::FloatKind; use floats::FloatKind;
use flow::{self, AbsoluteDescendants, IS_ABSOLUTELY_POSITIONED, ImmutableFlowUtils}; use flow::{self, AbsoluteDescendants, IS_ABSOLUTELY_POSITIONED, ImmutableFlowUtils};
@ -1679,7 +1679,7 @@ trait NodeUtils {
/// Returns true if this node doesn't render its kids and false otherwise. /// Returns true if this node doesn't render its kids and false otherwise.
fn is_replaced_content(&self) -> bool; fn is_replaced_content(&self) -> bool;
fn construction_result_mut(self, layout_data: &mut PrivateLayoutData) -> &mut ConstructionResult; fn construction_result_mut(self, layout_data: &mut PersistentLayoutData) -> &mut ConstructionResult;
/// Sets the construction result of a flow. /// Sets the construction result of a flow.
fn set_flow_construction_result(self, result: ConstructionResult); fn set_flow_construction_result(self, result: ConstructionResult);
@ -1708,7 +1708,7 @@ impl<ConcreteThreadSafeLayoutNode> NodeUtils for ConcreteThreadSafeLayoutNode
} }
} }
fn construction_result_mut(self, data: &mut PrivateLayoutData) -> &mut ConstructionResult { fn construction_result_mut(self, data: &mut PersistentLayoutData) -> &mut ConstructionResult {
match self.get_pseudo_element_type() { match self.get_pseudo_element_type() {
PseudoElementType::Before(_) => &mut data.before_flow_construction_result, PseudoElementType::Before(_) => &mut data.before_flow_construction_result,
PseudoElementType::After (_) => &mut data.after_flow_construction_result, PseudoElementType::After (_) => &mut data.after_flow_construction_result,

View file

@ -4,15 +4,15 @@
use construct::ConstructionResult; use construct::ConstructionResult;
use script_layout_interface::restyle_damage::RestyleDamage; use script_layout_interface::restyle_damage::RestyleDamage;
use style::data::PrivateStyleData; use style::data::PersistentStyleData;
/// Data that layout associates with a node. /// Data that layout associates with a node.
pub struct PrivateLayoutData { pub struct PersistentLayoutData {
/// Data that the style system associates with a node. When the /// Data that the style system associates with a node. When the
/// style system is being used standalone, this is all that hangs /// style system is being used standalone, this is all that hangs
/// off the node. This must be first to permit the various /// off the node. This must be first to permit the various
/// transmuations between PrivateStyleData PrivateLayoutData. /// transmutations between PersistentStyleData and PersistentLayoutData.
pub style_data: PrivateStyleData, pub style_data: PersistentStyleData,
/// Description of how to account for recent style changes. /// Description of how to account for recent style changes.
pub restyle_damage: RestyleDamage, pub restyle_damage: RestyleDamage,
@ -34,11 +34,11 @@ pub struct PrivateLayoutData {
pub flags: LayoutDataFlags, pub flags: LayoutDataFlags,
} }
impl PrivateLayoutData { impl PersistentLayoutData {
/// Creates new layout data. /// Creates new layout data.
pub fn new() -> PrivateLayoutData { pub fn new() -> PersistentLayoutData {
PrivateLayoutData { PersistentLayoutData {
style_data: PrivateStyleData::new(), style_data: PersistentStyleData::new(),
restyle_damage: RestyleDamage::empty(), restyle_damage: RestyleDamage::empty(),
flow_construction_result: ConstructionResult::None, flow_construction_result: ConstructionResult::None,
before_flow_construction_result: ConstructionResult::None, before_flow_construction_result: ConstructionResult::None,

View file

@ -31,33 +31,25 @@
#![allow(unsafe_code)] #![allow(unsafe_code)]
use core::nonzero::NonZero; use core::nonzero::NonZero;
use data::{LayoutDataFlags, PrivateLayoutData}; use data::{LayoutDataFlags, PersistentLayoutData};
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData}; use script_layout_interface::{OpaqueStyleAndLayoutData, PartialPersistentLayoutData};
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode}; use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
use style::atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use style::computed_values::content::{self, ContentItem}; use style::computed_values::content::{self, ContentItem};
use style::refcell::{Ref, RefCell, RefMut};
pub type NonOpaqueStyleAndLayoutData = *mut RefCell<PrivateLayoutData>; pub type NonOpaqueStyleAndLayoutData = *mut AtomicRefCell<PersistentLayoutData>;
pub trait LayoutNodeLayoutData { pub trait LayoutNodeLayoutData {
/// Similar to borrow_data*, but returns the full PrivateLayoutData rather /// Similar to borrow_data*, but returns the full PersistentLayoutData rather
/// than only the PrivateStyleData. /// than only the PersistentStyleData.
unsafe fn borrow_layout_data_unchecked(&self) -> Option<*const PrivateLayoutData>; fn borrow_layout_data(&self) -> Option<AtomicRef<PersistentLayoutData>>;
fn borrow_layout_data(&self) -> Option<Ref<PrivateLayoutData>>; fn mutate_layout_data(&self) -> Option<AtomicRefMut<PersistentLayoutData>>;
fn mutate_layout_data(&self) -> Option<RefMut<PrivateLayoutData>>;
fn initialize_data(self); fn initialize_data(self);
fn flow_debug_id(self) -> usize; fn flow_debug_id(self) -> usize;
} }
impl<T: LayoutNode> LayoutNodeLayoutData for T { impl<T: LayoutNode> LayoutNodeLayoutData for T {
unsafe fn borrow_layout_data_unchecked(&self) -> Option<*const PrivateLayoutData> { fn borrow_layout_data(&self) -> Option<AtomicRef<PersistentLayoutData>> {
self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
&(*(*container).as_unsafe_cell().get()) as *const PrivateLayoutData
})
}
fn borrow_layout_data(&self) -> Option<Ref<PrivateLayoutData>> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|opaque| { self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData; let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
@ -66,7 +58,7 @@ impl<T: LayoutNode> LayoutNodeLayoutData for T {
} }
} }
fn mutate_layout_data(&self) -> Option<RefMut<PrivateLayoutData>> { fn mutate_layout_data(&self) -> Option<AtomicRefMut<PersistentLayoutData>> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|opaque| { self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData; let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
@ -76,11 +68,11 @@ impl<T: LayoutNode> LayoutNodeLayoutData for T {
} }
fn initialize_data(self) { fn initialize_data(self) {
if unsafe { self.borrow_data_unchecked() }.is_none() { if self.borrow_data().is_none() {
let ptr: NonOpaqueStyleAndLayoutData = let ptr: NonOpaqueStyleAndLayoutData =
Box::into_raw(box RefCell::new(PrivateLayoutData::new())); Box::into_raw(box AtomicRefCell::new(PersistentLayoutData::new()));
let opaque = OpaqueStyleAndLayoutData { let opaque = OpaqueStyleAndLayoutData {
ptr: unsafe { NonZero::new(ptr as *mut RefCell<PartialStyleAndLayoutData>) } ptr: unsafe { NonZero::new(ptr as *mut AtomicRefCell<PartialPersistentLayoutData>) }
}; };
self.init_style_and_layout_data(opaque); self.init_style_and_layout_data(opaque);
} }
@ -94,19 +86,17 @@ impl<T: LayoutNode> LayoutNodeLayoutData for T {
pub trait ThreadSafeLayoutNodeHelpers { pub trait ThreadSafeLayoutNodeHelpers {
fn flow_debug_id(self) -> usize; fn flow_debug_id(self) -> usize;
unsafe fn borrow_layout_data_unchecked(&self) -> Option<*const PrivateLayoutData>;
/// Borrows the layout data immutably. Fails on a conflicting borrow. /// Borrows the layout data immutably. Fails on a conflicting borrow.
/// ///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases. /// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)] #[inline(always)]
fn borrow_layout_data(&self) -> Option<Ref<PrivateLayoutData>>; fn borrow_layout_data(&self) -> Option<AtomicRef<PersistentLayoutData>>;
/// Borrows the layout data mutably. Fails on a conflicting borrow. /// Borrows the layout data mutably. Fails on a conflicting borrow.
/// ///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases. /// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)] #[inline(always)]
fn mutate_layout_data(&self) -> Option<RefMut<PrivateLayoutData>>; fn mutate_layout_data(&self) -> Option<AtomicRefMut<PersistentLayoutData>>;
/// Returns the layout data flags for this node. /// Returns the layout data flags for this node.
fn flags(self) -> LayoutDataFlags; fn flags(self) -> LayoutDataFlags;
@ -129,14 +119,7 @@ impl<T: ThreadSafeLayoutNode> ThreadSafeLayoutNodeHelpers for T {
self.borrow_layout_data().map_or(0, |d| d.flow_construction_result.debug_id()) self.borrow_layout_data().map_or(0, |d| d.flow_construction_result.debug_id())
} }
unsafe fn borrow_layout_data_unchecked(&self) -> Option<*const PrivateLayoutData> { fn borrow_layout_data(&self) -> Option<AtomicRef<PersistentLayoutData>> {
self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
&(*(*container).as_unsafe_cell().get()) as *const PrivateLayoutData
})
}
fn borrow_layout_data(&self) -> Option<Ref<PrivateLayoutData>> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|opaque| { self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData; let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
@ -145,7 +128,7 @@ impl<T: ThreadSafeLayoutNode> ThreadSafeLayoutNodeHelpers for T {
} }
} }
fn mutate_layout_data(&self) -> Option<RefMut<PrivateLayoutData>> { fn mutate_layout_data(&self) -> Option<AtomicRefMut<PersistentLayoutData>> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|opaque| { self.get_style_and_layout_data().map(|opaque| {
let container = *opaque.ptr as NonOpaqueStyleAndLayoutData; let container = *opaque.ptr as NonOpaqueStyleAndLayoutData;
@ -155,9 +138,7 @@ impl<T: ThreadSafeLayoutNode> ThreadSafeLayoutNodeHelpers for T {
} }
fn flags(self) -> LayoutDataFlags { fn flags(self) -> LayoutDataFlags {
unsafe { self.borrow_layout_data().as_ref().unwrap().flags
(*self.borrow_layout_data_unchecked().unwrap()).flags
}
} }
fn insert_flags(self, new_flags: LayoutDataFlags) { fn insert_flags(self, new_flags: LayoutDataFlags) {

View file

@ -86,7 +86,7 @@ use profile_traits::mem::{self, Report, ReportKind, ReportsChan};
use profile_traits::time::{self, TimerMetadata, profile}; use profile_traits::time::{self, TimerMetadata, profile};
use profile_traits::time::{TimerMetadataFrameType, TimerMetadataReflowType}; use profile_traits::time::{TimerMetadataFrameType, TimerMetadataReflowType};
use script::layout_wrapper::{ServoLayoutDocument, ServoLayoutNode}; use script::layout_wrapper::{ServoLayoutDocument, ServoLayoutNode};
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData}; use script_layout_interface::{OpaqueStyleAndLayoutData, PartialPersistentLayoutData};
use script_layout_interface::message::{Msg, NewLayoutThreadInfo, Reflow, ReflowQueryType, ScriptReflow}; use script_layout_interface::message::{Msg, NewLayoutThreadInfo, Reflow, ReflowQueryType, ScriptReflow};
use script_layout_interface::reporter::CSSErrorReporter; use script_layout_interface::reporter::CSSErrorReporter;
use script_layout_interface::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION}; use script_layout_interface::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION};
@ -104,6 +104,7 @@ use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel}; use std::sync::mpsc::{Receiver, Sender, channel};
use style::animation::Animation; use style::animation::Animation;
use style::atomic_refcell::AtomicRefCell;
use style::computed_values::{filter, mix_blend_mode}; use style::computed_values::{filter, mix_blend_mode};
use style::context::{LocalStyleContextCreationInfo, ReflowGoal, SharedStyleContext}; use style::context::{LocalStyleContextCreationInfo, ReflowGoal, SharedStyleContext};
use style::dom::{TDocument, TElement, TNode}; use style::dom::{TDocument, TElement, TNode};
@ -112,7 +113,6 @@ use style::logical_geometry::LogicalPoint;
use style::media_queries::{Device, MediaType}; use style::media_queries::{Device, MediaType};
use style::parallel::WorkQueueData; use style::parallel::WorkQueueData;
use style::parser::ParserContextExtraData; use style::parser::ParserContextExtraData;
use style::refcell::RefCell;
use style::selector_matching::Stylist; use style::selector_matching::Stylist;
use style::stylesheets::{CSSRuleIteratorExt, Origin, Stylesheet, UserAgentStylesheets}; use style::stylesheets::{CSSRuleIteratorExt, Origin, Stylesheet, UserAgentStylesheets};
use style::thread_state; use style::thread_state;
@ -1594,7 +1594,7 @@ impl LayoutThread {
/// Handles a message to destroy layout data. Layout data must be destroyed on *this* thread /// Handles a message to destroy layout data. Layout data must be destroyed on *this* thread
/// because the struct type is transmuted to a different type on the script side. /// because the struct type is transmuted to a different type on the script side.
unsafe fn handle_reap_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData) { unsafe fn handle_reap_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData) {
let ptr: *mut RefCell<PartialStyleAndLayoutData> = *data.ptr; let ptr: *mut AtomicRefCell<PartialPersistentLayoutData> = *data.ptr;
let non_opaque: NonOpaqueStyleAndLayoutData = ptr as *mut _; let non_opaque: NonOpaqueStyleAndLayoutData = ptr as *mut _;
let _ = Box::from_raw(non_opaque); let _ = Box::from_raw(non_opaque);
} }

View file

@ -43,7 +43,7 @@ use gfx_traits::ByteIndex;
use msg::constellation_msg::PipelineId; use msg::constellation_msg::PipelineId;
use range::Range; use range::Range;
use script_layout_interface::{HTMLCanvasData, LayoutNodeType, TrustedNodeAddress}; use script_layout_interface::{HTMLCanvasData, LayoutNodeType, TrustedNodeAddress};
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData}; use script_layout_interface::{OpaqueStyleAndLayoutData, PartialPersistentLayoutData};
use script_layout_interface::restyle_damage::RestyleDamage; use script_layout_interface::restyle_damage::RestyleDamage;
use script_layout_interface::wrapper_traits::{DangerousThreadSafeLayoutNode, LayoutNode, PseudoElementType}; use script_layout_interface::wrapper_traits::{DangerousThreadSafeLayoutNode, LayoutNode, PseudoElementType};
use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode}; use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode};
@ -54,15 +54,15 @@ use std::marker::PhantomData;
use std::mem::transmute; use std::mem::transmute;
use std::sync::Arc; use std::sync::Arc;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
use style::atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use style::attr::AttrValue; use style::attr::AttrValue;
use style::computed_values::display; use style::computed_values::display;
use style::context::SharedStyleContext; use style::context::SharedStyleContext;
use style::data::PrivateStyleData; use style::data::PersistentStyleData;
use style::dom::{LayoutIterator, NodeInfo, OpaqueNode, PresentationalHintsSynthetizer, TDocument, TElement, TNode}; use style::dom::{LayoutIterator, NodeInfo, OpaqueNode, PresentationalHintsSynthetizer, TDocument, TElement, TNode};
use style::dom::UnsafeNode; use style::dom::UnsafeNode;
use style::element_state::*; use style::element_state::*;
use style::properties::{ComputedValues, PropertyDeclarationBlock}; use style::properties::{ComputedValues, PropertyDeclarationBlock};
use style::refcell::{Ref, RefCell, RefMut};
use style::selector_impl::{ElementSnapshot, NonTSPseudoClass, PseudoElement, ServoSelectorImpl}; use style::selector_impl::{ElementSnapshot, NonTSPseudoClass, PseudoElement, ServoSelectorImpl};
use style::selector_matching::ApplicableDeclarationBlock; use style::selector_matching::ApplicableDeclarationBlock;
use style::sink::Push; use style::sink::Push;
@ -220,30 +220,20 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
self.node.set_flag(CAN_BE_FRAGMENTED, value) self.node.set_flag(CAN_BE_FRAGMENTED, value)
} }
unsafe fn borrow_data_unchecked(&self) -> Option<*const PrivateStyleData> { fn borrow_data(&self) -> Option<AtomicRef<PersistentStyleData>> {
self.get_style_data().map(|d| { self.get_style_data().map(|d| d.borrow())
&(*d.as_unsafe_cell().get()).style_data as *const _
})
} }
fn borrow_data(&self) -> Option<Ref<PrivateStyleData>> { fn mutate_data(&self) -> Option<AtomicRefMut<PersistentStyleData>> {
self.get_style_data().map(|d| { self.get_style_data().map(|d| d.borrow_mut())
Ref::map(d.borrow(), |d| &d.style_data)
})
}
fn mutate_data(&self) -> Option<RefMut<PrivateStyleData>> {
self.get_style_data().map(|d| {
RefMut::map(d.borrow_mut(), |d| &mut d.style_data)
})
} }
fn restyle_damage(self) -> RestyleDamage { fn restyle_damage(self) -> RestyleDamage {
self.get_style_data().unwrap().borrow().restyle_damage self.get_partial_layout_data().unwrap().borrow().restyle_damage
} }
fn set_restyle_damage(self, damage: RestyleDamage) { fn set_restyle_damage(self, damage: RestyleDamage) {
self.get_style_data().unwrap().borrow_mut().restyle_damage = damage; self.get_partial_layout_data().unwrap().borrow_mut().restyle_damage = damage;
} }
fn parent_node(&self) -> Option<ServoLayoutNode<'ln>> { fn parent_node(&self) -> Option<ServoLayoutNode<'ln>> {
@ -309,10 +299,12 @@ impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
self.script_type_id().into() self.script_type_id().into()
} }
fn get_style_data(&self) -> Option<&RefCell<PartialStyleAndLayoutData>> { fn get_style_data(&self) -> Option<&AtomicRefCell<PersistentStyleData>> {
unsafe { unsafe {
self.get_jsmanaged().get_style_and_layout_data().map(|d| { self.get_jsmanaged().get_style_and_layout_data().map(|d| {
&**d.ptr let ppld: &AtomicRefCell<PartialPersistentLayoutData> = &**d.ptr;
let psd: &AtomicRefCell<PersistentStyleData> = transmute(ppld);
psd
}) })
} }
} }
@ -331,6 +323,14 @@ impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
} }
impl<'ln> ServoLayoutNode<'ln> { impl<'ln> ServoLayoutNode<'ln> {
fn get_partial_layout_data(&self) -> Option<&AtomicRefCell<PartialPersistentLayoutData>> {
unsafe {
self.get_jsmanaged().get_style_and_layout_data().map(|d| {
&**d.ptr
})
}
}
fn dump_indent(self, indent: u32) { fn dump_indent(self, indent: u32) {
let mut s = String::new(); let mut s = String::new();
for _ in 0..indent { for _ in 0..indent {
@ -871,7 +871,7 @@ impl<'ln> ThreadSafeLayoutNode for ServoThreadSafeLayoutNode<'ln> {
} }
} }
fn get_style_data(&self) -> Option<&RefCell<PartialStyleAndLayoutData>> { fn get_style_data(&self) -> Option<&AtomicRefCell<PersistentStyleData>> {
self.node.get_style_data() self.node.get_style_data()
} }
} }

View file

@ -52,11 +52,11 @@ use core::nonzero::NonZero;
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::IpcSender;
use libc::c_void; use libc::c_void;
use restyle_damage::RestyleDamage; use restyle_damage::RestyleDamage;
use style::data::PrivateStyleData; use style::atomic_refcell::AtomicRefCell;
use style::refcell::RefCell; use style::data::PersistentStyleData;
pub struct PartialStyleAndLayoutData { pub struct PartialPersistentLayoutData {
pub style_data: PrivateStyleData, pub style_data: PersistentStyleData,
pub restyle_damage: RestyleDamage, pub restyle_damage: RestyleDamage,
} }
@ -64,7 +64,7 @@ pub struct PartialStyleAndLayoutData {
pub struct OpaqueStyleAndLayoutData { pub struct OpaqueStyleAndLayoutData {
#[ignore_heap_size_of = "TODO(#6910) Box value that should be counted but \ #[ignore_heap_size_of = "TODO(#6910) Box value that should be counted but \
the type lives in layout"] the type lives in layout"]
pub ptr: NonZero<*mut RefCell<PartialStyleAndLayoutData>> pub ptr: NonZero<*mut AtomicRefCell<PartialPersistentLayoutData>>
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]

View file

@ -5,7 +5,6 @@
use HTMLCanvasData; use HTMLCanvasData;
use LayoutNodeType; use LayoutNodeType;
use OpaqueStyleAndLayoutData; use OpaqueStyleAndLayoutData;
use PartialStyleAndLayoutData;
use gfx_traits::{ByteIndex, LayerId, LayerType}; use gfx_traits::{ByteIndex, LayerId, LayerType};
use msg::constellation_msg::PipelineId; use msg::constellation_msg::PipelineId;
use range::Range; use range::Range;
@ -13,12 +12,13 @@ use restyle_damage::RestyleDamage;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
use style::atomic_refcell::AtomicRefCell;
use style::computed_values::display; use style::computed_values::display;
use style::context::SharedStyleContext; use style::context::SharedStyleContext;
use style::data::PersistentStyleData;
use style::dom::{LayoutIterator, NodeInfo, PresentationalHintsSynthetizer, TNode}; use style::dom::{LayoutIterator, NodeInfo, PresentationalHintsSynthetizer, TNode};
use style::dom::OpaqueNode; use style::dom::OpaqueNode;
use style::properties::ServoComputedValues; use style::properties::ServoComputedValues;
use style::refcell::RefCell;
use style::selector_impl::{PseudoElement, PseudoElementCascadeType, ServoSelectorImpl}; use style::selector_impl::{PseudoElement, PseudoElementCascadeType, ServoSelectorImpl};
use url::Url; use url::Url;
@ -76,7 +76,7 @@ pub trait LayoutNode: TNode {
/// Returns the type ID of this node. /// Returns the type ID of this node.
fn type_id(&self) -> LayoutNodeType; fn type_id(&self) -> LayoutNodeType;
fn get_style_data(&self) -> Option<&RefCell<PartialStyleAndLayoutData>>; fn get_style_data(&self) -> Option<&AtomicRefCell<PersistentStyleData>>;
fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData); fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData);
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>; fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;
@ -183,7 +183,6 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
if self.get_style_data() if self.get_style_data()
.unwrap() .unwrap()
.borrow() .borrow()
.style_data
.per_pseudo .per_pseudo
.contains_key(&PseudoElement::Before) { .contains_key(&PseudoElement::Before) {
Some(self.with_pseudo(PseudoElementType::Before(None))) Some(self.with_pseudo(PseudoElementType::Before(None)))
@ -197,7 +196,6 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
if self.get_style_data() if self.get_style_data()
.unwrap() .unwrap()
.borrow() .borrow()
.style_data
.per_pseudo .per_pseudo
.contains_key(&PseudoElement::After) { .contains_key(&PseudoElement::After) {
Some(self.with_pseudo(PseudoElementType::After(None))) Some(self.with_pseudo(PseudoElementType::After(None)))
@ -244,7 +242,7 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
match self.get_pseudo_element_type() { match self.get_pseudo_element_type() {
PseudoElementType::Normal => { PseudoElementType::Normal => {
self.get_style_data().unwrap().borrow() self.get_style_data().unwrap().borrow()
.style_data.style.as_ref().unwrap().clone() .style.as_ref().unwrap().clone()
}, },
other => { other => {
// Precompute non-eagerly-cascaded pseudo-element styles if not // Precompute non-eagerly-cascaded pseudo-element styles if not
@ -257,14 +255,13 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
if !self.get_style_data() if !self.get_style_data()
.unwrap() .unwrap()
.borrow() .borrow()
.style_data
.per_pseudo.contains_key(&style_pseudo) { .per_pseudo.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut(); let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style = let new_style =
context.stylist context.stylist
.precomputed_values_for_pseudo(&style_pseudo, .precomputed_values_for_pseudo(&style_pseudo,
data.style_data.style.as_ref()); data.style.as_ref());
data.style_data.per_pseudo data.per_pseudo
.insert(style_pseudo.clone(), new_style.unwrap()); .insert(style_pseudo.clone(), new_style.unwrap());
} }
} }
@ -273,7 +270,6 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
if !self.get_style_data() if !self.get_style_data()
.unwrap() .unwrap()
.borrow() .borrow()
.style_data
.per_pseudo.contains_key(&style_pseudo) { .per_pseudo.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut(); let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style = let new_style =
@ -281,15 +277,15 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
.lazily_compute_pseudo_element_style( .lazily_compute_pseudo_element_style(
&self.as_element(), &self.as_element(),
&style_pseudo, &style_pseudo,
data.style_data.style.as_ref().unwrap()); data.style.as_ref().unwrap());
data.style_data.per_pseudo data.per_pseudo
.insert(style_pseudo.clone(), new_style.unwrap()); .insert(style_pseudo.clone(), new_style.unwrap());
} }
} }
} }
self.get_style_data().unwrap().borrow() self.get_style_data().unwrap().borrow()
.style_data.per_pseudo.get(&style_pseudo) .per_pseudo.get(&style_pseudo)
.unwrap().clone() .unwrap().clone()
} }
} }
@ -307,18 +303,18 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
let data = self.get_style_data().unwrap().borrow(); let data = self.get_style_data().unwrap().borrow();
match self.get_pseudo_element_type() { match self.get_pseudo_element_type() {
PseudoElementType::Normal PseudoElementType::Normal
=> data.style_data.style.as_ref().unwrap().clone(), => data.style.as_ref().unwrap().clone(),
other other
=> data.style_data.per_pseudo.get(&other.style_pseudo_element()).unwrap().clone(), => data.per_pseudo.get(&other.style_pseudo_element()).unwrap().clone(),
} }
} }
#[inline] #[inline]
fn selected_style(&self, _context: &SharedStyleContext) -> Arc<ServoComputedValues> { fn selected_style(&self, _context: &SharedStyleContext) -> Arc<ServoComputedValues> {
let data = self.get_style_data().unwrap().borrow(); let data = self.get_style_data().unwrap().borrow();
data.style_data.per_pseudo data.per_pseudo
.get(&PseudoElement::Selection) .get(&PseudoElement::Selection)
.unwrap_or(data.style_data.style.as_ref().unwrap()).clone() .unwrap_or(data.style.as_ref().unwrap()).clone()
} }
/// Removes the style from this node. /// Removes the style from this node.
@ -329,10 +325,10 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
match self.get_pseudo_element_type() { match self.get_pseudo_element_type() {
PseudoElementType::Normal => { PseudoElementType::Normal => {
data.style_data.style = None; data.style = None;
} }
other => { other => {
data.style_data.per_pseudo.remove(&other.style_pseudo_element()); data.per_pseudo.remove(&other.style_pseudo_element());
} }
}; };
} }
@ -387,7 +383,7 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
LayerId::new_of_type(LayerType::OverflowScroll, self.opaque().id() as usize) LayerId::new_of_type(LayerType::OverflowScroll, self.opaque().id() as usize)
} }
fn get_style_data(&self) -> Option<&RefCell<PartialStyleAndLayoutData>>; fn get_style_data(&self) -> Option<&AtomicRefCell<PersistentStyleData>>;
} }
// This trait is only public so that it can be implemented by the gecko wrapper. // This trait is only public so that it can be implemented by the gecko wrapper.

View file

@ -11,7 +11,7 @@ use std::hash::BuildHasherDefault;
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::AtomicIsize; use std::sync::atomic::AtomicIsize;
pub struct PrivateStyleData { pub struct PersistentStyleData {
/// The results of CSS styling for this node. /// The results of CSS styling for this node.
pub style: Option<Arc<ComputedValues>>, pub style: Option<Arc<ComputedValues>>,
@ -23,9 +23,9 @@ pub struct PrivateStyleData {
pub parallel: DomParallelInfo, pub parallel: DomParallelInfo,
} }
impl PrivateStyleData { impl PersistentStyleData {
pub fn new() -> Self { pub fn new() -> Self {
PrivateStyleData { PersistentStyleData {
style: None, style: None,
per_pseudo: HashMap::with_hasher(Default::default()), per_pseudo: HashMap::with_hasher(Default::default()),
parallel: DomParallelInfo::new(), parallel: DomParallelInfo::new(),

View file

@ -6,11 +6,10 @@
#![allow(unsafe_code)] #![allow(unsafe_code)]
use context::SharedStyleContext; use atomic_refcell::{AtomicRef, AtomicRefMut};
use data::PrivateStyleData; use data::PersistentStyleData;
use element_state::ElementState; use element_state::ElementState;
use properties::{ComputedValues, PropertyDeclarationBlock}; use properties::{ComputedValues, PropertyDeclarationBlock};
use refcell::{Ref, RefMut};
use restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint}; use restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
use selector_impl::{ElementExt, PseudoElement}; use selector_impl::{ElementExt, PseudoElement};
use selector_matching::ApplicableDeclarationBlock; use selector_matching::ApplicableDeclarationBlock;
@ -139,17 +138,13 @@ pub trait TNode : Sized + Copy + Clone + NodeInfo {
unsafe fn set_can_be_fragmented(&self, value: bool); unsafe fn set_can_be_fragmented(&self, value: bool);
/// Borrows the PrivateStyleData without checks. /// Borrows the style data immutably. Fails on a conflicting borrow.
#[inline(always)] #[inline(always)]
unsafe fn borrow_data_unchecked(&self) -> Option<*const PrivateStyleData>; fn borrow_data(&self) -> Option<AtomicRef<PersistentStyleData>>;
/// Borrows the PrivateStyleData immutably. Fails on a conflicting borrow. /// Borrows the style data mutably. Fails on a conflicting borrow.
#[inline(always)] #[inline(always)]
fn borrow_data(&self) -> Option<Ref<PrivateStyleData>>; fn mutate_data(&self) -> Option<AtomicRefMut<PersistentStyleData>>;
/// Borrows the PrivateStyleData mutably. Fails on a conflicting borrow.
#[inline(always)]
fn mutate_data(&self) -> Option<RefMut<PrivateStyleData>>;
/// Get the description of how to account for recent style changes. /// Get the description of how to account for recent style changes.
fn restyle_damage(self) -> Self::ConcreteRestyleDamage; fn restyle_damage(self) -> Self::ConcreteRestyleDamage;

View file

@ -5,7 +5,8 @@
#![allow(unsafe_code)] #![allow(unsafe_code)]
use data::PrivateStyleData; use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use data::PersistentStyleData;
use dom::{LayoutIterator, NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode}; use dom::{LayoutIterator, NodeInfo, TDocument, TElement, TNode, TRestyleDamage, UnsafeNode};
use dom::{OpaqueNode, PresentationalHintsSynthetizer}; use dom::{OpaqueNode, PresentationalHintsSynthetizer};
use element_state::ElementState; use element_state::ElementState;
@ -34,7 +35,6 @@ use libc::uintptr_t;
use parser::ParserContextExtraData; use parser::ParserContextExtraData;
use properties::{ComputedValues, parse_style_attribute}; use properties::{ComputedValues, parse_style_attribute};
use properties::PropertyDeclarationBlock; use properties::PropertyDeclarationBlock;
use refcell::{Ref, RefCell, RefMut};
use selector_impl::ElementExt; use selector_impl::ElementExt;
use selector_matching::ApplicableDeclarationBlock; use selector_matching::ApplicableDeclarationBlock;
use selectors::Element; use selectors::Element;
@ -48,11 +48,11 @@ use std::sync::atomic::{AtomicBool, AtomicPtr};
use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace}; use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
use url::Url; use url::Url;
pub struct NonOpaqueStyleData(RefCell<PrivateStyleData>); pub struct NonOpaqueStyleData(AtomicRefCell<PersistentStyleData>);
impl NonOpaqueStyleData { impl NonOpaqueStyleData {
pub fn new() -> Self { pub fn new() -> Self {
NonOpaqueStyleData(RefCell::new(PrivateStyleData::new())) NonOpaqueStyleData(AtomicRefCell::new(PersistentStyleData::new()))
} }
} }
@ -302,18 +302,12 @@ impl<'ln> TNode for GeckoNode<'ln> {
} }
#[inline(always)] #[inline(always)]
unsafe fn borrow_data_unchecked(&self) -> Option<*const PrivateStyleData> { fn borrow_data(&self) -> Option<AtomicRef<PersistentStyleData>> {
self.get_node_data().as_ref().map(|d| d.0.as_unsafe_cell().get()
as *const PrivateStyleData)
}
#[inline(always)]
fn borrow_data(&self) -> Option<Ref<PrivateStyleData>> {
self.get_node_data().as_ref().map(|d| d.0.borrow()) self.get_node_data().as_ref().map(|d| d.0.borrow())
} }
#[inline(always)] #[inline(always)]
fn mutate_data(&self) -> Option<RefMut<PrivateStyleData>> { fn mutate_data(&self) -> Option<AtomicRefMut<PersistentStyleData>> {
self.get_node_data().as_ref().map(|d| d.0.borrow_mut()) self.get_node_data().as_ref().map(|d| d.0.borrow_mut())
} }

View file

@ -11,7 +11,7 @@ use arc_ptr_eq;
use cache::{LRUCache, SimpleHashCache}; use cache::{LRUCache, SimpleHashCache};
use cascade_info::CascadeInfo; use cascade_info::CascadeInfo;
use context::{SharedStyleContext, StyleContext}; use context::{SharedStyleContext, StyleContext};
use data::PrivateStyleData; use data::PersistentStyleData;
use dom::{NodeInfo, TElement, TNode, TRestyleDamage, UnsafeNode}; use dom::{NodeInfo, TElement, TNode, TRestyleDamage, UnsafeNode};
use properties::{ComputedValues, PropertyDeclarationBlock, cascade}; use properties::{ComputedValues, PropertyDeclarationBlock, cascade};
use properties::longhands::display::computed_value as display; use properties::longhands::display::computed_value as display;
@ -874,19 +874,9 @@ pub trait MatchMethods : TNode {
-> RestyleResult -> RestyleResult
where Ctx: StyleContext<'a> where Ctx: StyleContext<'a>
{ {
// Get our parent's style. This must be unsafe so that we don't touch the parent's // Get our parent's style.
// borrow flags. let parent_node_data = parent.as_ref().and_then(|x| x.borrow_data());
// let parent_style = parent_node_data.as_ref().map(|x| x.style.as_ref().unwrap());
// FIXME(pcwalton): Isolate this unsafety into the `wrapper` module to allow
// enforced safe, race-free access to the parent style.
let parent_style = match parent {
Some(parent_node) => {
let parent_style = (*parent_node.borrow_data_unchecked().unwrap()).style.as_ref().unwrap();
Some(parent_style)
}
None => None,
};
// In the case we're styling a text node, we don't need to compute the // In the case we're styling a text node, we don't need to compute the
// restyle damage, since it's a subset of the restyle damage of the // restyle damage, since it's a subset of the restyle damage of the
@ -945,7 +935,7 @@ pub trait MatchMethods : TNode {
fn compute_damage_and_cascade_pseudos<'a, Ctx>(&self, fn compute_damage_and_cascade_pseudos<'a, Ctx>(&self,
final_style: Arc<ComputedValues>, final_style: Arc<ComputedValues>,
data: &mut PrivateStyleData, data: &mut PersistentStyleData,
context: &Ctx, context: &Ctx,
applicable_declarations: &ApplicableDeclarations, applicable_declarations: &ApplicableDeclarations,
mut applicable_declarations_cache: &mut ApplicableDeclarationsCache) mut applicable_declarations_cache: &mut ApplicableDeclarationsCache)

View file

@ -161,9 +161,7 @@ fn bottom_up_dom<N, C>(root: OpaqueNode,
Some(parent) => parent, Some(parent) => parent,
}; };
let parent_data = unsafe { let parent_data = parent.borrow_data().unwrap();
&*parent.borrow_data_unchecked().unwrap()
};
if parent_data if parent_data
.parallel .parallel