Skip to content

Commit

Permalink
Use parking_lot::RwLock instead of DOMRefCell for PropertyDeclaration…
Browse files Browse the repository at this point in the history
…Block
  • Loading branch information
SimonSapin committed Oct 4, 2016
1 parent d986fd2 commit 89a29a7
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 106 deletions.
1 change: 1 addition & 0 deletions components/script/Cargo.toml
Expand Up @@ -51,6 +51,7 @@ net_traits = {path = "../net_traits"}
num-traits = "0.1.32"
offscreen_gl_context = "0.4"
open = "1.1.1"
parking_lot = "0.3"
phf = "0.7.16"
phf_macros = "0.7.16"
plugins = {path = "../plugins"}
Expand Down
12 changes: 6 additions & 6 deletions components/script/dom/cssstyledeclaration.rs
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cssparser::ToCss;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::{self, CSSStyleDeclarationMethods};
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
Expand All @@ -14,6 +13,7 @@ use dom::bindings::str::DOMString;
use dom::element::Element;
use dom::node::{Node, NodeDamage, window_from_node};
use dom::window::Window;
use parking_lot::RwLock;
use std::ascii::AsciiExt;
use std::sync::Arc;
use string_cache::Atom;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
fn Length(&self) -> u32 {
let elem = self.owner.upcast::<Element>();
let len = match *elem.style_attribute().borrow() {
Some(ref declarations) => declarations.borrow().declarations.len(),
Some(ref declarations) => declarations.read().declarations.len(),
None => 0,
};
len as u32
Expand Down Expand Up @@ -120,7 +120,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
if let Some(shorthand) = Shorthand::from_name(&property) {
let style_attribute = owner.style_attribute().borrow();
let style_attribute = if let Some(ref style_attribute) = *style_attribute {
style_attribute.borrow()
style_attribute.read()
} else {
// shorthand.longhands() is never empty, so with no style attribute
// step 2.2.2 would do this:
Expand Down Expand Up @@ -331,7 +331,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
let elem = self.owner.upcast::<Element>();
let style_attribute = elem.style_attribute().borrow();
style_attribute.as_ref().and_then(|declarations| {
declarations.borrow().declarations.get(index).map(|entry| {
declarations.read().declarations.get(index).map(|entry| {
let (ref declaration, importance) = *entry;
let mut css = declaration.to_css_string();
if importance.important() {
Expand All @@ -348,7 +348,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
let style_attribute = elem.style_attribute().borrow();

if let Some(declarations) = style_attribute.as_ref() {
DOMString::from(declarations.borrow().to_css_string())
DOMString::from(declarations.read().to_css_string())
} else {
DOMString::new()
}
Expand All @@ -370,7 +370,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
*element.style_attribute().borrow_mut() = if decl_block.declarations.is_empty() {
None // Step 2
} else {
Some(Arc::new(DOMRefCell::new(decl_block)))
Some(Arc::new(RwLock::new(decl_block)))
};
element.sync_property_with_attrs_style();
let node = element.upcast::<Node>();
Expand Down
31 changes: 14 additions & 17 deletions components/script/dom/element.rs
Expand Up @@ -70,6 +70,7 @@ use html5ever::serialize::SerializeOpts;
use html5ever::serialize::TraversalScope;
use html5ever::serialize::TraversalScope::{ChildrenOnly, IncludeNode};
use html5ever::tree_builder::{LimitedQuirks, NoQuirks, Quirks};
use parking_lot::RwLock;
use selectors::matching::{ElementFlags, MatchingReason, matches};
use selectors::matching::{HAS_EDGE_CHILD_SELECTOR, HAS_SLOW_SELECTOR, HAS_SLOW_SELECTOR_LATER_SIBLINGS};
use selectors::parser::{AttrSelector, NamespaceConstraint, parse_author_origin_selector_list_from_str};
Expand Down Expand Up @@ -110,7 +111,7 @@ pub struct Element {
attrs: DOMRefCell<Vec<JS<Attr>>>,
id_attribute: DOMRefCell<Option<Atom>>,
#[ignore_heap_size_of = "Arc"]
style_attribute: DOMRefCell<Option<Arc<DOMRefCell<PropertyDeclarationBlock>>>>,
style_attribute: DOMRefCell<Option<Arc<RwLock<PropertyDeclarationBlock>>>>,
attr_list: MutNullableHeap<JS<NamedNodeMap>>,
class_list: MutNullableHeap<JS<DOMTokenList>>,
state: Cell<ElementState>,
Expand Down Expand Up @@ -298,7 +299,7 @@ pub trait LayoutElementHelpers {
#[allow(unsafe_code)]
unsafe fn html_element_in_html_document_for_layout(&self) -> bool;
fn id_attribute(&self) -> *const Option<Atom>;
fn style_attribute(&self) -> *const Option<Arc<DOMRefCell<PropertyDeclarationBlock>>>;
fn style_attribute(&self) -> *const Option<Arc<RwLock<PropertyDeclarationBlock>>>;
fn local_name(&self) -> &Atom;
fn namespace(&self) -> &Namespace;
fn get_checked_state_for_layout(&self) -> bool;
Expand Down Expand Up @@ -330,7 +331,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
#[inline]
fn from_declaration(rule: PropertyDeclaration) -> ApplicableDeclarationBlock {
ApplicableDeclarationBlock::from_declarations(
Arc::new(DOMRefCell::new(PropertyDeclarationBlock {
Arc::new(RwLock::new(PropertyDeclarationBlock {
declarations: vec![(rule, Importance::Normal)],
important_count: 0,
})),
Expand Down Expand Up @@ -619,7 +620,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
}

#[allow(unsafe_code)]
fn style_attribute(&self) -> *const Option<Arc<DOMRefCell<PropertyDeclarationBlock>>> {
fn style_attribute(&self) -> *const Option<Arc<RwLock<PropertyDeclarationBlock>>> {
unsafe {
(*self.unsafe_get()).style_attribute.borrow_for_layout()
}
Expand Down Expand Up @@ -708,7 +709,7 @@ impl Element {
self.attrs.borrow()
}

pub fn style_attribute(&self) -> &DOMRefCell<Option<Arc<DOMRefCell<PropertyDeclarationBlock>>>> {
pub fn style_attribute(&self) -> &DOMRefCell<Option<Arc<RwLock<PropertyDeclarationBlock>>>> {
&self.style_attribute
}

Expand Down Expand Up @@ -738,7 +739,7 @@ impl Element {
// therefore, it should not trigger subsequent mutation events
pub fn sync_property_with_attrs_style(&self) {
let style_str = if let &Some(ref declarations) = &*self.style_attribute().borrow() {
declarations.borrow().to_css_string()
declarations.read().to_css_string()
} else {
String::new()
};
Expand Down Expand Up @@ -770,15 +771,15 @@ impl Element {
let mut inline_declarations = element.style_attribute.borrow_mut();
if let &mut Some(ref mut declarations) = &mut *inline_declarations {
let mut importance = None;
let index = declarations.borrow().declarations.iter().position(|&(ref decl, i)| {
let index = declarations.read().declarations.iter().position(|&(ref decl, i)| {
let matching = decl.matches(property);
if matching {
importance = Some(i)
}
matching
});
if let Some(index) = index {
let mut declarations = Arc::make_mut(declarations).borrow_mut();
let mut declarations = declarations.write();
declarations.declarations.remove(index);
if importance.unwrap().important() {
declarations.important_count -= 1;
Expand All @@ -799,9 +800,7 @@ impl Element {
let mut inline_declarations = element.style_attribute().borrow_mut();
if let &mut Some(ref mut declaration_block) = &mut *inline_declarations {
{
// Usually, the reference count will be 1 here. But transitions could make it greater
// than that.
let mut declaration_block = Arc::make_mut(declaration_block).borrow_mut();
let mut declaration_block = declaration_block.write();
let declaration_block = &mut *declaration_block;
let existing_declarations = &mut declaration_block.declarations;

Expand Down Expand Up @@ -836,7 +835,7 @@ impl Element {
0
};

*inline_declarations = Some(Arc::new(DOMRefCell::new(PropertyDeclarationBlock {
*inline_declarations = Some(Arc::new(RwLock::new(PropertyDeclarationBlock {
declarations: declarations.into_iter().map(|d| (d, importance)).collect(),
important_count: important_count,
})));
Expand All @@ -852,9 +851,7 @@ impl Element {
{
let mut inline_declarations = self.style_attribute().borrow_mut();
if let &mut Some(ref mut block) = &mut *inline_declarations {
// Usually, the reference counts of `from` and `to` will be 1 here. But transitions
// could make them greater than that.
let mut block = Arc::make_mut(block).borrow_mut();
let mut block = block.write();
let block = &mut *block;
let declarations = &mut block.declarations;
for &mut (ref declaration, ref mut importance) in declarations {
Expand All @@ -881,7 +878,7 @@ impl Element {
where F: FnOnce(Option<&(PropertyDeclaration, Importance)>) -> R {
let style_attr = self.style_attribute.borrow();
if let Some(ref block) = *style_attr {
let block = block.borrow();
let block = block.read();
f(block.get(property))
} else {
f(None)
Expand Down Expand Up @@ -2131,7 +2128,7 @@ impl VirtualMethods for Element {
*self.style_attribute.borrow_mut() =
mutation.new_value(attr).map(|value| {
let win = window_from_node(self);
Arc::new(DOMRefCell::new(parse_style_attribute(
Arc::new(RwLock::new(parse_style_attribute(
&value,
&doc.base_url(),
win.css_error_reporter(),
Expand Down
4 changes: 2 additions & 2 deletions components/script/layout_wrapper.rs
Expand Up @@ -30,7 +30,6 @@

#![allow(unsafe_code)]

use dom::bindings::cell::DOMRefCell;
use dom::bindings::inheritance::{CharacterDataTypeId, ElementTypeId};
use dom::bindings::inheritance::{HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::LayoutJS;
Expand All @@ -42,6 +41,7 @@ use dom::node::{LayoutNodeHelpers, Node};
use dom::text::Text;
use gfx_traits::ByteIndex;
use msg::constellation_msg::PipelineId;
use parking_lot::RwLock;
use range::Range;
use script_layout_interface::{HTMLCanvasData, LayoutNodeType, TrustedNodeAddress};
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialPersistentLayoutData};
Expand Down Expand Up @@ -462,7 +462,7 @@ impl<'le> TElement for ServoLayoutElement<'le> {
ServoLayoutNode::from_layout_js(self.element.upcast())
}

fn style_attribute(&self) -> Option<&Arc<DOMRefCell<PropertyDeclarationBlock>>> {
fn style_attribute(&self) -> Option<&Arc<RwLock<PropertyDeclarationBlock>>> {
unsafe {
(*self.element.style_attribute()).as_ref()
}
Expand Down
1 change: 1 addition & 0 deletions components/script/lib.rs
Expand Up @@ -64,6 +64,7 @@ extern crate net_traits;
extern crate num_traits;
extern crate offscreen_gl_context;
extern crate open;
extern crate parking_lot;
extern crate phf;
#[macro_use]
extern crate profile_traits;
Expand Down
2 changes: 2 additions & 0 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/style/Cargo.toml
Expand Up @@ -17,7 +17,7 @@ servo = ["serde/unstable", "serde", "serde_macros", "heapsize_plugin",
"style_traits/servo", "app_units/plugins",
"cssparser/heap_size", "cssparser/serde-serialization",
"selectors/unstable", "string_cache",
"url/heap_size", "plugins"]
"url/heap_size", "plugins", "parking_lot/nightly"]
testing = []

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion components/style/animation.rs
Expand Up @@ -383,7 +383,7 @@ fn compute_style_for_animation_step(context: &SharedStyleContext,
// TODO: avoiding this spurious clone might involve having to create
// an Arc in the below (more common case).
KeyframesStepValue::ComputedValues => style_from_cascade.clone(),
KeyframesStepValue::Declarations(ref declarations) => {
KeyframesStepValue::Declarations { block: ref declarations } => {
let declaration_block = ApplicableDeclarationBlock {
mixed_declarations: declarations.clone(),
importance: Importance::Normal,
Expand Down
4 changes: 2 additions & 2 deletions components/style/dom.rs
Expand Up @@ -8,8 +8,8 @@

use atomic_refcell::{AtomicRef, AtomicRefMut};
use data::PersistentStyleData;
use domrefcell::DOMRefCell;
use element_state::ElementState;
use parking_lot::RwLock;
use properties::{ComputedValues, PropertyDeclarationBlock};
use restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
use selector_impl::{ElementExt, PseudoElement};
Expand Down Expand Up @@ -203,7 +203,7 @@ pub trait TElement : PartialEq + Debug + Sized + Copy + Clone + ElementExt + Pre

fn as_node(&self) -> Self::ConcreteNode;

fn style_attribute(&self) -> Option<&Arc<DOMRefCell<PropertyDeclarationBlock>>>;
fn style_attribute(&self) -> Option<&Arc<RwLock<PropertyDeclarationBlock>>>;

fn get_state(&self) -> ElementState;

Expand Down
6 changes: 3 additions & 3 deletions components/style/gecko/wrapper.rs
Expand Up @@ -32,6 +32,7 @@ use gecko_bindings::structs::{nsChangeHint, nsIAtom, nsIContent, nsStyleContext}
use gecko_bindings::structs::OpaqueStyleData;
use gecko_bindings::sugar::ownership::{FFIArcHelpers, HasArcFFI, HasFFI};
use libc::uintptr_t;
use parking_lot::RwLock;
use parser::ParserContextExtraData;
use properties::{ComputedValues, parse_style_attribute};
use properties::PropertyDeclarationBlock;
Expand All @@ -46,7 +47,6 @@ use std::ptr;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicPtr};
use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
use style::domrefcell::DOMRefCell;
use url::Url;

pub struct NonOpaqueStyleData(AtomicRefCell<PersistentStyleData>);
Expand All @@ -59,7 +59,7 @@ impl NonOpaqueStyleData {


pub struct GeckoDeclarationBlock {
pub declarations: Option<Arc<PropertyDeclarationBlock>>,
pub declarations: Option<Arc<RwLock<PropertyDeclarationBlock>>>,
// XXX The following two fields are made atomic to work around the
// ownership system so that they can be changed inside a shared
// instance. It wouldn't provide safety as Rust usually promises,
Expand Down Expand Up @@ -469,7 +469,7 @@ impl<'le> TElement for GeckoElement<'le> {
unsafe { GeckoNode(&*(self.0 as *const _ as *const RawGeckoNode)) }
}

fn style_attribute(&self) -> Option<&Arc<DOMRefCell<PropertyDeclarationBlock>>> {
fn style_attribute(&self) -> Option<&Arc<RwLock<PropertyDeclarationBlock>>> {
let declarations = unsafe { Gecko_GetServoDeclarationBlock(self.0) };
if declarations.is_null() {
None
Expand Down

0 comments on commit 89a29a7

Please sign in to comment.