Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace RwLock<CssRules> with Locked<CssRules>
  • Loading branch information
SimonSapin committed Mar 19, 2017
1 parent b213daa commit f35b4e2
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 52 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/bindings/trace.rs
Expand Up @@ -508,7 +508,7 @@ unsafe impl JSTraceable for RwLock<FontFaceRule> {
}
}

unsafe impl JSTraceable for RwLock<CssRules> {
unsafe impl JSTraceable for StyleLocked<CssRules> {
unsafe fn trace(&self, _trc: *mut JSTracer) {
// Do nothing.
}
Expand Down
5 changes: 2 additions & 3 deletions components/script/dom/cssconditionrule.rs
Expand Up @@ -10,9 +10,8 @@ use dom::cssmediarule::CSSMediaRule;
use dom::cssstylesheet::CSSStyleSheet;
use dom::csssupportsrule::CSSSupportsRule;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::SharedRwLock;
use style::shared_lock::{SharedRwLock, Locked};
use style::stylesheets::CssRules as StyleCssRules;

#[dom_struct]
Expand All @@ -22,7 +21,7 @@ pub struct CSSConditionRule {

impl CSSConditionRule {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet,
rules: Arc<RwLock<StyleCssRules>>) -> CSSConditionRule {
rules: Arc<Locked<StyleCssRules>>) -> CSSConditionRule {
CSSConditionRule {
cssgroupingrule: CSSGroupingRule::new_inherited(parent_stylesheet, rules),
}
Expand Down
7 changes: 3 additions & 4 deletions components/script/dom/cssgroupingrule.rs
Expand Up @@ -12,22 +12,21 @@ use dom::cssrule::CSSRule;
use dom::cssrulelist::{CSSRuleList, RulesSource};
use dom::cssstylesheet::CSSStyleSheet;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::SharedRwLock;
use style::shared_lock::{SharedRwLock, Locked};
use style::stylesheets::CssRules as StyleCssRules;

#[dom_struct]
pub struct CSSGroupingRule {
cssrule: CSSRule,
#[ignore_heap_size_of = "Arc"]
rules: Arc<RwLock<StyleCssRules>>,
rules: Arc<Locked<StyleCssRules>>,
rulelist: MutNullableJS<CSSRuleList>,
}

impl CSSGroupingRule {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet,
rules: Arc<RwLock<StyleCssRules>>) -> CSSGroupingRule {
rules: Arc<Locked<StyleCssRules>>) -> CSSGroupingRule {
CSSGroupingRule {
cssrule: CSSRule::new_inherited(parent_stylesheet),
rules: rules,
Expand Down
16 changes: 11 additions & 5 deletions components/script/dom/cssrulelist.rs
Expand Up @@ -15,6 +15,7 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::Locked;
use style::stylesheets::{CssRules, KeyframesRule, RulesMutateError};

#[allow(unsafe_code)]
Expand Down Expand Up @@ -43,7 +44,7 @@ pub struct CSSRuleList {
}

pub enum RulesSource {
Rules(Arc<RwLock<CssRules>>),
Rules(Arc<Locked<CssRules>>),
Keyframes(Arc<RwLock<KeyframesRule>>),
}

Expand All @@ -52,7 +53,8 @@ impl CSSRuleList {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet, rules: RulesSource) -> CSSRuleList {
let dom_rules = match rules {
RulesSource::Rules(ref rules) => {
rules.read().0.iter().map(|_| MutNullableJS::new(None)).collect()
let guard = parent_stylesheet.shared_lock().read();
rules.read_with(&guard).0.iter().map(|_| MutNullableJS::new(None)).collect()
}
RulesSource::Keyframes(ref rules) => {
rules.read().keyframes.iter().map(|_| MutNullableJS::new(None)).collect()
Expand Down Expand Up @@ -89,7 +91,9 @@ impl CSSRuleList {
let index = idx as usize;

let parent_stylesheet = self.parent_stylesheet.style_stylesheet();
let new_rule = css_rules.write().insert_rule(rule, parent_stylesheet, index, nested)?;
let mut guard = parent_stylesheet.shared_lock.write();
let new_rule = css_rules.write_with(&mut guard)
.insert_rule(rule, parent_stylesheet, index, nested)?;

let parent_stylesheet = &*self.parent_stylesheet;
let dom_rule = CSSRule::new_specific(&window, parent_stylesheet, new_rule);
Expand All @@ -103,7 +107,8 @@ impl CSSRuleList {

match self.rules {
RulesSource::Rules(ref css_rules) => {
css_rules.write().remove_rule(index)?;
let mut guard = self.parent_stylesheet.shared_lock().write();
css_rules.write_with(&mut guard).remove_rule(index)?;
let mut dom_rules = self.dom_rules.borrow_mut();
dom_rules[index].get().map(|r| r.detach());
dom_rules.remove(index);
Expand Down Expand Up @@ -133,9 +138,10 @@ impl CSSRuleList {
let parent_stylesheet = &self.parent_stylesheet;
match self.rules {
RulesSource::Rules(ref rules) => {
let guard = parent_stylesheet.shared_lock().read();
CSSRule::new_specific(self.global().as_window(),
parent_stylesheet,
rules.read().0[idx as usize].clone())
rules.read_with(&guard).0[idx as usize].clone())
}
RulesSource::Keyframes(ref rules) => {
Root::upcast(CSSKeyframeRule::new(self.global().as_window(),
Expand Down
5 changes: 5 additions & 0 deletions components/script/dom/cssstylesheet.rs
Expand Up @@ -16,6 +16,7 @@ use dom::window::Window;
use dom_struct::dom_struct;
use std::cell::Cell;
use std::sync::Arc;
use style::shared_lock::SharedRwLock;
use style::stylesheets::Stylesheet as StyleStyleSheet;

#[dom_struct]
Expand Down Expand Up @@ -72,6 +73,10 @@ impl CSSStyleSheet {
}
}

pub fn shared_lock(&self) -> &SharedRwLock {
&self.style_stylesheet.shared_lock
}

pub fn style_stylesheet(&self) -> &StyleStyleSheet {
&self.style_stylesheet
}
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/htmlmetaelement.rs
Expand Up @@ -101,8 +101,9 @@ impl HTMLMetaElement {
if let Some(translated_rule) = ViewportRule::from_meta(&**content) {
let document = self.upcast::<Node>().owner_doc();
let shared_lock = document.style_shared_lock();
let rule = CssRule::Viewport(Arc::new(RwLock::new(translated_rule)));
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: CssRules::new(vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))]),
rules: CssRules::new(vec![rule], shared_lock),
origin: Origin::Author,
shared_lock: shared_lock.clone(),
base_url: window_from_node(self).get_url(),
Expand Down
2 changes: 2 additions & 0 deletions components/script/stylesheet_loader.rs
Expand Up @@ -175,10 +175,12 @@ impl FetchResponseListener for StylesheetContext {
}
StylesheetContextSource::Import(ref import) => {
let import = import.read();
let mut guard = document.style_shared_lock().write();
Stylesheet::update_from_bytes(&import.stylesheet,
&data,
protocol_encoding_label,
Some(environment_encoding),
&mut guard,
Some(&loader),
win.css_error_reporter(),
ParserContextExtraData::default());
Expand Down
4 changes: 3 additions & 1 deletion components/style/encoding_support.rs
Expand Up @@ -12,7 +12,7 @@ use media_queries::MediaList;
use parser::ParserContextExtraData;
use self::encoding::{EncodingRef, DecoderTrap};
use servo_url::ServoUrl;
use shared_lock::SharedRwLock;
use shared_lock::{SharedRwLock, SharedRwLockWriteGuard};
use std::str;
use stylesheets::{Stylesheet, StylesheetLoader, Origin};

Expand Down Expand Up @@ -78,13 +78,15 @@ impl Stylesheet {
bytes: &[u8],
protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingRef>,
guard: &mut SharedRwLockWriteGuard,
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &ParseErrorReporter,
extra_data: ParserContextExtraData) {
let (string, _) = decode_stylesheet_bytes(
bytes, protocol_encoding_label, environment_encoding);
Self::update_from_str(existing,
&string,
guard,
stylesheet_loader,
error_reporter,
extra_data)
Expand Down
2 changes: 1 addition & 1 deletion components/style/gecko/arc_types.rs
Expand Up @@ -39,7 +39,7 @@ macro_rules! impl_arc_ffi {
}
}

impl_arc_ffi!(RwLock<CssRules> => ServoCssRules
impl_arc_ffi!(Locked<CssRules> => ServoCssRules
[Servo_CssRules_AddRef, Servo_CssRules_Release]);

impl_arc_ffi!(Stylesheet => RawServoStyleSheet
Expand Down
52 changes: 29 additions & 23 deletions components/style/stylesheets.rs
Expand Up @@ -21,7 +21,8 @@ use selector_parser::{SelectorImpl, SelectorParser};
use selectors::parser::SelectorList;
use servo_config::prefs::PREFS;
use servo_url::ServoUrl;
use shared_lock::{SharedRwLock, Locked, SharedRwLockReadGuard, ToCssWithGuard};
use shared_lock::{SharedRwLock, Locked, ToCssWithGuard};
use shared_lock::{SharedRwLockReadGuard, SharedRwLockWriteGuard};
use std::cell::Cell;
use std::fmt;
use std::sync::Arc;
Expand Down Expand Up @@ -87,8 +88,8 @@ impl From<SingleRuleParseError> for RulesMutateError {

impl CssRules {
#[allow(missing_docs)]
pub fn new(rules: Vec<CssRule>) -> Arc<RwLock<CssRules>> {
Arc::new(RwLock::new(CssRules(rules)))
pub fn new(rules: Vec<CssRule>, shared_lock: &SharedRwLock) -> Arc<Locked<CssRules>> {
Arc::new(shared_lock.wrap(CssRules(rules)))
}

fn only_ns_or_import(&self) -> bool {
Expand Down Expand Up @@ -174,7 +175,7 @@ impl CssRules {
pub struct Stylesheet {
/// List of rules in the order they were found (important for
/// cascading order)
pub rules: Arc<RwLock<CssRules>>,
pub rules: Arc<Locked<CssRules>>,
/// List of media associated with the Stylesheet.
pub media: Arc<Locked<MediaList>>,
/// The origin of this stylesheet.
Expand Down Expand Up @@ -302,7 +303,7 @@ impl CssRule {
CssRule::Import(ref lock) => {
let rule = lock.read();
let media = rule.stylesheet.media.read_with(guard);
let rules = rule.stylesheet.rules.read();
let rules = rule.stylesheet.rules.read_with(guard);
// FIXME(emilio): Include the nested rules if the stylesheet is
// loaded.
f(&rules.0, Some(&media))
Expand All @@ -317,14 +318,14 @@ impl CssRule {
CssRule::Media(ref lock) => {
let media_rule = lock.read();
let mq = media_rule.media_queries.read_with(guard);
let rules = &media_rule.rules.read().0;
let rules = &media_rule.rules.read_with(guard).0;
f(rules, Some(&mq))
}
CssRule::Supports(ref lock) => {
let supports_rule = lock.read();
let enabled = supports_rule.enabled;
if enabled {
let rules = &supports_rule.rules.read().0;
let rules = &supports_rule.rules.read_with(guard).0;
f(rules, None)
} else {
f(&[], None)
Expand Down Expand Up @@ -478,7 +479,7 @@ impl ToCssWithGuard for KeyframesRule {
#[derive(Debug)]
pub struct MediaRule {
pub media_queries: Arc<Locked<MediaList>>,
pub rules: Arc<RwLock<CssRules>>,
pub rules: Arc<Locked<CssRules>>,
}

impl ToCssWithGuard for MediaRule {
Expand All @@ -489,7 +490,7 @@ impl ToCssWithGuard for MediaRule {
try!(dest.write_str("@media "));
try!(self.media_queries.read_with(guard).to_css(dest));
try!(dest.write_str(" {"));
for rule in self.rules.read().0.iter() {
for rule in self.rules.read_with(guard).0.iter() {
try!(dest.write_str(" "));
try!(rule.to_css(guard, dest));
}
Expand All @@ -504,7 +505,7 @@ pub struct SupportsRule {
/// The parsed condition
pub condition: SupportsCondition,
/// Child rules
pub rules: Arc<RwLock<CssRules>>,
pub rules: Arc<Locked<CssRules>>,
/// The result of evaluating the condition
pub enabled: bool,
}
Expand All @@ -515,7 +516,7 @@ impl ToCssWithGuard for SupportsRule {
try!(dest.write_str("@supports "));
try!(self.condition.to_css(dest));
try!(dest.write_str(" {"));
for rule in self.rules.read().0.iter() {
for rule in self.rules.read_with(guard).0.iter() {
try!(dest.write_str(" "));
try!(rule.to_css(guard, dest));
}
Expand Down Expand Up @@ -555,10 +556,11 @@ impl Stylesheet {
/// Updates an empty stylesheet from a given string of text.
pub fn update_from_str(existing: &Stylesheet,
css: &str,
guard: &mut SharedRwLockWriteGuard,
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &ParseErrorReporter,
extra_data: ParserContextExtraData) {
let mut rules = existing.rules.write();
let mut rules = existing.rules.write_with(guard);
let mut namespaces = existing.namespaces.write();

assert!(rules.is_empty());
Expand Down Expand Up @@ -613,18 +615,22 @@ impl Stylesheet {
origin: origin,
base_url: base_url,
namespaces: RwLock::new(Namespaces::default()),
rules: CssRules::new(vec![]),
rules: CssRules::new(Vec::new(), &shared_lock),
media: Arc::new(shared_lock.wrap(media)),
shared_lock: shared_lock.clone(),
shared_lock: shared_lock,
dirty_on_viewport_size_change: AtomicBool::new(false),
disabled: AtomicBool::new(false),
};

Self::update_from_str(&s,
css,
stylesheet_loader,
error_reporter,
extra_data);
{
let mut guard = s.shared_lock.write();
Self::update_from_str(&s,
css,
&mut guard,
stylesheet_loader,
error_reporter,
extra_data);
}

s
}
Expand Down Expand Up @@ -666,7 +672,7 @@ impl Stylesheet {
#[inline]
pub fn effective_rules<F>(&self, device: &Device, guard: &SharedRwLockReadGuard, mut f: F)
where F: FnMut(&CssRule) {
effective_rules(&self.rules.read().0, device, guard, &mut f);
effective_rules(&self.rules.read_with(guard).0, device, guard, &mut f);
}

/// Returns whether the stylesheet has been explicitly disabled through the
Expand Down Expand Up @@ -809,7 +815,7 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {
ImportRule {
url: url,
stylesheet: Arc::new(Stylesheet {
rules: Arc::new(RwLock::new(CssRules(vec![]))),
rules: CssRules::new(Vec::new(), self.shared_lock),
media: media,
shared_lock: self.shared_lock.clone(),
origin: self.context.stylesheet_origin,
Expand Down Expand Up @@ -907,7 +913,7 @@ struct NestedRuleParser<'a, 'b: 'a> {
}

impl<'a, 'b> NestedRuleParser<'a, 'b> {
fn parse_nested_rules(&self, input: &mut Parser) -> Arc<RwLock<CssRules>> {
fn parse_nested_rules(&self, input: &mut Parser) -> Arc<Locked<CssRules>> {
let mut iter = RuleListParser::new_for_nested_rule(input, self.clone());
let mut rules = Vec::new();
while let Some(result) = iter.next() {
Expand All @@ -920,7 +926,7 @@ impl<'a, 'b> NestedRuleParser<'a, 'b> {
}
}
}
CssRules::new(rules)
CssRules::new(rules, self.shared_lock)
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/stylist.rs
Expand Up @@ -502,7 +502,7 @@ impl Stylist {
return true
}

mq_eval_changed(guard, &stylesheet.rules.read().0, &self.device, &device)
mq_eval_changed(guard, &stylesheet.rules.read_with(guard).0, &self.device, &device)
});

self.device = Arc::new(device);
Expand Down

0 comments on commit f35b4e2

Please sign in to comment.