Replace RwLock<MediaList> with shared_lock::Locked<MediaList>

This commit is contained in:
Simon Sapin 2017-03-14 16:09:41 +01:00
parent 8feb9e8047
commit c5a7294e05
18 changed files with 117 additions and 42 deletions

View file

@ -115,6 +115,7 @@ use style::logical_geometry::LogicalPoint;
use style::media_queries::{Device, MediaType}; use style::media_queries::{Device, MediaType};
use style::parser::ParserContextExtraData; use style::parser::ParserContextExtraData;
use style::servo::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION, STORE_OVERFLOW}; use style::servo::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION, STORE_OVERFLOW};
use style::shared_lock::SharedRwLock;
use style::stylesheets::{Origin, Stylesheet, UserAgentStylesheets}; use style::stylesheets::{Origin, Stylesheet, UserAgentStylesheets};
use style::stylist::Stylist; use style::stylist::Stylist;
use style::thread_state; use style::thread_state;
@ -934,6 +935,7 @@ impl LayoutThread {
possibly_locked_rw_data: &mut RwData<'a, 'b>) { possibly_locked_rw_data: &mut RwData<'a, 'b>) {
let document = unsafe { ServoLayoutNode::new(&data.document) }; let document = unsafe { ServoLayoutNode::new(&data.document) };
let document = document.as_document().unwrap(); let document = document.as_document().unwrap();
let style_guard = document.style_shared_lock().read();
self.quirks_mode = Some(document.quirks_mode()); self.quirks_mode = Some(document.quirks_mode());
// FIXME(pcwalton): Combine `ReflowGoal` and `ReflowQueryType`. Then remove this assert. // FIXME(pcwalton): Combine `ReflowGoal` and `ReflowQueryType`. Then remove this assert.
@ -1010,7 +1012,8 @@ impl LayoutThread {
// Calculate the actual viewport as per DEVICE-ADAPT § 6 // Calculate the actual viewport as per DEVICE-ADAPT § 6
let device = Device::new(MediaType::Screen, initial_viewport); let device = Device::new(MediaType::Screen, initial_viewport);
Arc::get_mut(&mut rw_data.stylist).unwrap().set_device(device, &data.document_stylesheets); Arc::get_mut(&mut rw_data.stylist).unwrap()
.set_device(device, &style_guard, &data.document_stylesheets);
self.viewport_size = self.viewport_size =
rw_data.stylist.viewport_constraints().map_or(current_screen_size, |constraints| { rw_data.stylist.viewport_constraints().map_or(current_screen_size, |constraints| {
@ -1528,7 +1531,8 @@ fn get_root_flow_background_color(flow: &mut Flow) -> webrender_traits::ColorF {
} }
fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> { fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
fn parse_ua_stylesheet(filename: &'static str) -> Result<Stylesheet, &'static str> { fn parse_ua_stylesheet(shared_lock: &SharedRwLock, filename: &'static str)
-> Result<Stylesheet, &'static str> {
let res = try!(read_resource_file(filename).map_err(|_| filename)); let res = try!(read_resource_file(filename).map_err(|_| filename));
Ok(Stylesheet::from_bytes( Ok(Stylesheet::from_bytes(
&res, &res,
@ -1537,26 +1541,29 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
None, None,
Origin::UserAgent, Origin::UserAgent,
Default::default(), Default::default(),
shared_lock.clone(),
None, None,
&StdoutErrorReporter, &StdoutErrorReporter,
ParserContextExtraData::default())) ParserContextExtraData::default()))
} }
let shared_lock = SharedRwLock::new();
let mut user_or_user_agent_stylesheets = vec!(); let mut user_or_user_agent_stylesheets = vec!();
// FIXME: presentational-hints.css should be at author origin with zero specificity. // FIXME: presentational-hints.css should be at author origin with zero specificity.
// (Does it make a difference?) // (Does it make a difference?)
for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] { for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] {
user_or_user_agent_stylesheets.push(try!(parse_ua_stylesheet(filename))); user_or_user_agent_stylesheets.push(try!(parse_ua_stylesheet(&shared_lock, filename)));
} }
for &(ref contents, ref url) in &opts::get().user_stylesheets { for &(ref contents, ref url) in &opts::get().user_stylesheets {
user_or_user_agent_stylesheets.push(Stylesheet::from_bytes( user_or_user_agent_stylesheets.push(Stylesheet::from_bytes(
&contents, url.clone(), None, None, Origin::User, Default::default(), &contents, url.clone(), None, None, Origin::User, Default::default(),
None, &StdoutErrorReporter, ParserContextExtraData::default())); shared_lock.clone(), None, &StdoutErrorReporter, ParserContextExtraData::default()));
} }
let quirks_mode_stylesheet = try!(parse_ua_stylesheet("quirks-mode.css")); let quirks_mode_stylesheet = try!(parse_ua_stylesheet(&shared_lock, "quirks-mode.css"));
Ok(UserAgentStylesheets { Ok(UserAgentStylesheets {
shared_lock: shared_lock,
user_or_user_agent_stylesheets: user_or_user_agent_stylesheets, user_or_user_agent_stylesheets: user_or_user_agent_stylesheets,
quirks_mode_stylesheet: quirks_mode_stylesheet, quirks_mode_stylesheet: quirks_mode_stylesheet,
}) })

View file

@ -99,6 +99,7 @@ use style::keyframes::Keyframe;
use style::media_queries::MediaList; use style::media_queries::MediaList;
use style::properties::PropertyDeclarationBlock; use style::properties::PropertyDeclarationBlock;
use style::selector_parser::{PseudoElement, Snapshot}; use style::selector_parser::{PseudoElement, Snapshot};
use style::shared_lock::SharedRwLock as StyleSharedRwLock;
use style::stylesheets::{CssRules, KeyframesRule, MediaRule, NamespaceRule, StyleRule, ImportRule}; use style::stylesheets::{CssRules, KeyframesRule, MediaRule, NamespaceRule, StyleRule, ImportRule};
use style::stylesheets::SupportsRule; use style::stylesheets::SupportsRule;
use style::values::specified::Length; use style::values::specified::Length;
@ -360,6 +361,7 @@ unsafe_no_jsmanaged_fields!(HttpsState);
unsafe_no_jsmanaged_fields!(Request); unsafe_no_jsmanaged_fields!(Request);
unsafe_no_jsmanaged_fields!(RequestInit); unsafe_no_jsmanaged_fields!(RequestInit);
unsafe_no_jsmanaged_fields!(SharedRt); unsafe_no_jsmanaged_fields!(SharedRt);
unsafe_no_jsmanaged_fields!(StyleSharedRwLock);
unsafe_no_jsmanaged_fields!(TouchpadPressurePhase); unsafe_no_jsmanaged_fields!(TouchpadPressurePhase);
unsafe_no_jsmanaged_fields!(USVString); unsafe_no_jsmanaged_fields!(USVString);
unsafe_no_jsmanaged_fields!(ReferrerPolicy); unsafe_no_jsmanaged_fields!(ReferrerPolicy);

View file

@ -134,6 +134,7 @@ use style::attr::AttrValue;
use style::context::{QuirksMode, ReflowGoal}; use style::context::{QuirksMode, ReflowGoal};
use style::restyle_hints::{RestyleHint, RESTYLE_STYLE_ATTRIBUTE}; use style::restyle_hints::{RestyleHint, RESTYLE_STYLE_ATTRIBUTE};
use style::selector_parser::{RestyleDamage, Snapshot}; use style::selector_parser::{RestyleDamage, Snapshot};
use style::shared_lock::SharedRwLock as StyleSharedRwLock;
use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join}; use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join};
use style::stylesheets::Stylesheet; use style::stylesheets::Stylesheet;
use task_source::TaskSource; use task_source::TaskSource;
@ -220,6 +221,9 @@ pub struct Document {
scripts: MutNullableJS<HTMLCollection>, scripts: MutNullableJS<HTMLCollection>,
anchors: MutNullableJS<HTMLCollection>, anchors: MutNullableJS<HTMLCollection>,
applets: MutNullableJS<HTMLCollection>, applets: MutNullableJS<HTMLCollection>,
/// Lock use for style attributes and author-origin stylesheet objects in this document.
/// Can be acquired once for accessing many objects.
style_shared_lock: StyleSharedRwLock,
/// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed. /// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed.
stylesheets: DOMRefCell<Option<Vec<StylesheetInDocument>>>, stylesheets: DOMRefCell<Option<Vec<StylesheetInDocument>>>,
/// Whether the list of stylesheets has changed since the last reflow was triggered. /// Whether the list of stylesheets has changed since the last reflow was triggered.
@ -1964,6 +1968,7 @@ pub trait LayoutDocumentHelpers {
unsafe fn needs_paint_from_layout(&self); unsafe fn needs_paint_from_layout(&self);
unsafe fn will_paint(&self); unsafe fn will_paint(&self);
unsafe fn quirks_mode(&self) -> QuirksMode; unsafe fn quirks_mode(&self) -> QuirksMode;
unsafe fn style_shared_lock(&self) -> &StyleSharedRwLock;
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
@ -2000,6 +2005,11 @@ impl LayoutDocumentHelpers for LayoutJS<Document> {
unsafe fn quirks_mode(&self) -> QuirksMode { unsafe fn quirks_mode(&self) -> QuirksMode {
(*self.unsafe_get()).quirks_mode() (*self.unsafe_get()).quirks_mode()
} }
#[inline]
unsafe fn style_shared_lock(&self) -> &StyleSharedRwLock {
(*self.unsafe_get()).style_shared_lock()
}
} }
// https://html.spec.whatwg.org/multipage/#is-a-registrable-domain-suffix-of-or-is-equal-to // https://html.spec.whatwg.org/multipage/#is-a-registrable-domain-suffix-of-or-is-equal-to
@ -2121,6 +2131,7 @@ impl Document {
scripts: Default::default(), scripts: Default::default(),
anchors: Default::default(), anchors: Default::default(),
applets: Default::default(), applets: Default::default(),
style_shared_lock: StyleSharedRwLock::new(),
stylesheets: DOMRefCell::new(None), stylesheets: DOMRefCell::new(None),
stylesheets_changed_since_reflow: Cell::new(false), stylesheets_changed_since_reflow: Cell::new(false),
stylesheet_list: MutNullableJS::new(None), stylesheet_list: MutNullableJS::new(None),
@ -2250,6 +2261,11 @@ impl Document {
}; };
} }
/// Return a reference to the per-document shared lock used in stylesheets.
pub fn style_shared_lock(&self) -> &StyleSharedRwLock {
&self.style_shared_lock
}
/// Returns the list of stylesheets associated with nodes in the document. /// Returns the list of stylesheets associated with nodes in the document.
pub fn stylesheets(&self) -> Vec<Arc<Stylesheet>> { pub fn stylesheets(&self) -> Vec<Arc<Stylesheet>> {
self.ensure_stylesheets(); self.ensure_stylesheets();

View file

@ -99,12 +99,15 @@ impl HTMLMetaElement {
let content = content.value(); let content = content.value();
if !content.is_empty() { if !content.is_empty() {
if let Some(translated_rule) = ViewportRule::from_meta(&**content) { if let Some(translated_rule) = ViewportRule::from_meta(&**content) {
let document = self.upcast::<Node>().owner_doc();
let shared_lock = document.style_shared_lock();
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet { *self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: CssRules::new(vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))]), rules: CssRules::new(vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))]),
origin: Origin::Author, origin: Origin::Author,
shared_lock: shared_lock.clone(),
base_url: window_from_node(self).get_url(), base_url: window_from_node(self).get_url(),
namespaces: Default::default(), namespaces: Default::default(),
media: Default::default(), media: Arc::new(shared_lock.wrap(Default::default())),
// Viewport constraints are always recomputed on resize; they don't need to // Viewport constraints are always recomputed on resize; they don't need to
// force all styles to be recomputed. // force all styles to be recomputed.
dirty_on_viewport_size_change: AtomicBool::new(false), dirty_on_viewport_size_change: AtomicBool::new(false),

View file

@ -84,9 +84,10 @@ impl HTMLStyleElement {
let data = node.GetTextContent().expect("Element.textContent must be a string"); let data = node.GetTextContent().expect("Element.textContent must be a string");
let mq = parse_media_query_list(&mut CssParser::new(&mq_str)); let mq = parse_media_query_list(&mut CssParser::new(&mq_str));
let shared_lock = node.owner_doc().style_shared_lock().clone();
let loader = StylesheetLoader::for_element(self.upcast()); let loader = StylesheetLoader::for_element(self.upcast());
let sheet = Stylesheet::from_str(&data, url, Origin::Author, mq, let sheet = Stylesheet::from_str(&data, url, Origin::Author, mq,
Some(&loader), shared_lock, Some(&loader),
win.css_error_reporter(), win.css_error_reporter(),
ParserContextExtraData::default()); ParserContextExtraData::default());

View file

@ -69,6 +69,7 @@ use style::dom::UnsafeNode;
use style::element_state::*; use style::element_state::*;
use style::properties::{ComputedValues, PropertyDeclarationBlock}; use style::properties::{ComputedValues, PropertyDeclarationBlock};
use style::selector_parser::{NonTSPseudoClass, PseudoElement, SelectorImpl}; use style::selector_parser::{NonTSPseudoClass, PseudoElement, SelectorImpl};
use style::shared_lock::SharedRwLock as StyleSharedRwLock;
use style::sink::Push; use style::sink::Push;
use style::str::is_whitespace; use style::str::is_whitespace;
use style::stylist::ApplicableDeclarationBlock; use style::stylist::ApplicableDeclarationBlock;
@ -330,6 +331,10 @@ impl<'ld> ServoLayoutDocument<'ld> {
unsafe { self.document.quirks_mode() } unsafe { self.document.quirks_mode() }
} }
pub fn style_shared_lock(&self) -> &StyleSharedRwLock {
unsafe { self.document.style_shared_lock() }
}
pub fn from_layout_js(doc: LayoutJS<Document>) -> ServoLayoutDocument<'ld> { pub fn from_layout_js(doc: LayoutJS<Document>) -> ServoLayoutDocument<'ld> {
ServoLayoutDocument { ServoLayoutDocument {
document: doc, document: doc,

View file

@ -152,12 +152,14 @@ impl FetchResponseListener for StylesheetContext {
let is_stylesheet_load_applicable = let is_stylesheet_load_applicable =
self.request_generation_id.map_or(true, |gen| gen == link.get_request_generation_id()); self.request_generation_id.map_or(true, |gen| gen == link.get_request_generation_id());
if is_stylesheet_load_applicable { if is_stylesheet_load_applicable {
let shared_lock = document.style_shared_lock().clone();
let sheet = let sheet =
Arc::new(Stylesheet::from_bytes(&data, final_url, Arc::new(Stylesheet::from_bytes(&data, final_url,
protocol_encoding_label, protocol_encoding_label,
Some(environment_encoding), Some(environment_encoding),
Origin::Author, Origin::Author,
media.take().unwrap(), media.take().unwrap(),
shared_lock,
Some(&loader), Some(&loader),
win.css_error_reporter(), win.css_error_reporter(),
ParserContextExtraData::default())); ParserContextExtraData::default()));

View file

@ -12,6 +12,7 @@ use media_queries::MediaList;
use parser::ParserContextExtraData; use parser::ParserContextExtraData;
use self::encoding::{EncodingRef, DecoderTrap}; use self::encoding::{EncodingRef, DecoderTrap};
use servo_url::ServoUrl; use servo_url::ServoUrl;
use shared_lock::SharedRwLock;
use std::str; use std::str;
use stylesheets::{Stylesheet, StylesheetLoader, Origin}; use stylesheets::{Stylesheet, StylesheetLoader, Origin};
@ -54,6 +55,7 @@ impl Stylesheet {
environment_encoding: Option<EncodingRef>, environment_encoding: Option<EncodingRef>,
origin: Origin, origin: Origin,
media: MediaList, media: MediaList,
shared_lock: SharedRwLock,
stylesheet_loader: Option<&StylesheetLoader>, stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &ParseErrorReporter, error_reporter: &ParseErrorReporter,
extra_data: ParserContextExtraData) extra_data: ParserContextExtraData)
@ -64,6 +66,7 @@ impl Stylesheet {
base_url, base_url,
origin, origin,
media, media,
shared_lock,
stylesheet_loader, stylesheet_loader,
error_reporter, error_reporter,
extra_data) extra_data)

View file

@ -21,6 +21,7 @@ use selector_parser::{SelectorImpl, SelectorParser};
use selectors::parser::SelectorList; use selectors::parser::SelectorList;
use servo_config::prefs::PREFS; use servo_config::prefs::PREFS;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use shared_lock::{SharedRwLock, Locked, SharedRwLockReadGuard};
use std::cell::Cell; use std::cell::Cell;
use std::fmt; use std::fmt;
use std::sync::Arc; use std::sync::Arc;
@ -175,11 +176,13 @@ pub struct Stylesheet {
/// cascading order) /// cascading order)
pub rules: Arc<RwLock<CssRules>>, pub rules: Arc<RwLock<CssRules>>,
/// List of media associated with the Stylesheet. /// List of media associated with the Stylesheet.
pub media: Arc<RwLock<MediaList>>, pub media: Arc<Locked<MediaList>>,
/// The origin of this stylesheet. /// The origin of this stylesheet.
pub origin: Origin, pub origin: Origin,
/// The base url this stylesheet should use. /// The base url this stylesheet should use.
pub base_url: ServoUrl, pub base_url: ServoUrl,
/// The lock used for objects inside this stylesheet
pub shared_lock: SharedRwLock,
/// The namespaces that apply to this stylesheet. /// The namespaces that apply to this stylesheet.
pub namespaces: RwLock<Namespaces>, pub namespaces: RwLock<Namespaces>,
/// Whether this stylesheet would be dirty when the viewport size changes. /// Whether this stylesheet would be dirty when the viewport size changes.
@ -191,6 +194,8 @@ pub struct Stylesheet {
/// This structure holds the user-agent and user stylesheets. /// This structure holds the user-agent and user stylesheets.
pub struct UserAgentStylesheets { pub struct UserAgentStylesheets {
/// The lock used for user-agent stylesheets.
pub shared_lock: SharedRwLock,
/// The user or user agent stylesheets. /// The user or user agent stylesheets.
pub user_or_user_agent_stylesheets: Vec<Stylesheet>, pub user_or_user_agent_stylesheets: Vec<Stylesheet>,
/// The quirks mode stylesheet. /// The quirks mode stylesheet.
@ -291,12 +296,12 @@ impl CssRule {
/// used for others. /// used for others.
/// ///
/// This will not recurse down unsupported @supports rules /// This will not recurse down unsupported @supports rules
pub fn with_nested_rules_and_mq<F, R>(&self, mut f: F) -> R pub fn with_nested_rules_and_mq<F, R>(&self, guard: &SharedRwLockReadGuard, mut f: F) -> R
where F: FnMut(&[CssRule], Option<&MediaList>) -> R { where F: FnMut(&[CssRule], Option<&MediaList>) -> R {
match *self { match *self {
CssRule::Import(ref lock) => { CssRule::Import(ref lock) => {
let rule = lock.read(); let rule = lock.read();
let media = rule.stylesheet.media.read(); let media = rule.stylesheet.media.read_with(guard);
let rules = rule.stylesheet.rules.read(); let rules = rule.stylesheet.rules.read();
// FIXME(emilio): Include the nested rules if the stylesheet is // FIXME(emilio): Include the nested rules if the stylesheet is
// loaded. // loaded.
@ -349,6 +354,7 @@ impl CssRule {
let mut rule_parser = TopLevelRuleParser { let mut rule_parser = TopLevelRuleParser {
stylesheet_origin: parent_stylesheet.origin, stylesheet_origin: parent_stylesheet.origin,
context: context, context: context,
shared_lock: &parent_stylesheet.shared_lock,
loader: None, loader: None,
state: Cell::new(state), state: Cell::new(state),
namespaces: &mut namespaces, namespaces: &mut namespaces,
@ -424,7 +430,8 @@ impl ToCss for ImportRule {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("@import ")); try!(dest.write_str("@import "));
try!(self.url.to_css(dest)); try!(self.url.to_css(dest));
let media = self.stylesheet.media.read(); let guard = self.stylesheet.shared_lock.read(); // FIXME: have the caller pass this?
let media = self.stylesheet.media.read_with(&guard);
if !media.is_empty() { if !media.is_empty() {
try!(dest.write_str(" ")); try!(dest.write_str(" "));
try!(media.to_css(dest)); try!(media.to_css(dest));
@ -554,6 +561,7 @@ impl Stylesheet {
let rule_parser = TopLevelRuleParser { let rule_parser = TopLevelRuleParser {
stylesheet_origin: existing.origin, stylesheet_origin: existing.origin,
namespaces: &mut namespaces, namespaces: &mut namespaces,
shared_lock: &existing.shared_lock,
loader: stylesheet_loader, loader: stylesheet_loader,
context: ParserContext::new_with_extra_data(existing.origin, context: ParserContext::new_with_extra_data(existing.origin,
&existing.base_url, &existing.base_url,
@ -591,6 +599,7 @@ impl Stylesheet {
base_url: ServoUrl, base_url: ServoUrl,
origin: Origin, origin: Origin,
media: MediaList, media: MediaList,
shared_lock: SharedRwLock,
stylesheet_loader: Option<&StylesheetLoader>, stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &ParseErrorReporter, error_reporter: &ParseErrorReporter,
extra_data: ParserContextExtraData) -> Stylesheet { extra_data: ParserContextExtraData) -> Stylesheet {
@ -599,7 +608,8 @@ impl Stylesheet {
base_url: base_url, base_url: base_url,
namespaces: RwLock::new(Namespaces::default()), namespaces: RwLock::new(Namespaces::default()),
rules: CssRules::new(vec![]), rules: CssRules::new(vec![]),
media: Arc::new(RwLock::new(media)), media: Arc::new(shared_lock.wrap(media)),
shared_lock: shared_lock.clone(),
dirty_on_viewport_size_change: AtomicBool::new(false), dirty_on_viewport_size_change: AtomicBool::new(false),
disabled: AtomicBool::new(false), disabled: AtomicBool::new(false),
}; };
@ -638,7 +648,8 @@ impl Stylesheet {
/// ///
/// Always true if no associated MediaList exists. /// Always true if no associated MediaList exists.
pub fn is_effective_for_device(&self, device: &Device) -> bool { pub fn is_effective_for_device(&self, device: &Device) -> bool {
self.media.read().evaluate(device) let guard = self.shared_lock.read(); // FIXME: have the caller pass this?
self.media.read_with(&guard).evaluate(device)
} }
/// Return an iterator over the effective rules within the style-sheet, as /// Return an iterator over the effective rules within the style-sheet, as
@ -649,7 +660,8 @@ impl Stylesheet {
/// examined. /// examined.
#[inline] #[inline]
pub fn effective_rules<F>(&self, device: &Device, mut f: F) where F: FnMut(&CssRule) { pub fn effective_rules<F>(&self, device: &Device, mut f: F) where F: FnMut(&CssRule) {
effective_rules(&self.rules.read().0, device, &mut f); let guard = self.shared_lock.read(); // FIXME: have the caller pass this?
effective_rules(&self.rules.read().0, device, &guard, &mut f);
} }
/// Returns whether the stylesheet has been explicitly disabled through the /// Returns whether the stylesheet has been explicitly disabled through the
@ -670,16 +682,17 @@ impl Stylesheet {
} }
} }
fn effective_rules<F>(rules: &[CssRule], device: &Device, f: &mut F) where F: FnMut(&CssRule) { fn effective_rules<F>(rules: &[CssRule], device: &Device, guard: &SharedRwLockReadGuard, f: &mut F)
where F: FnMut(&CssRule) {
for rule in rules { for rule in rules {
f(rule); f(rule);
rule.with_nested_rules_and_mq(|rules, mq| { rule.with_nested_rules_and_mq(guard, |rules, mq| {
if let Some(media_queries) = mq { if let Some(media_queries) = mq {
if !media_queries.evaluate(device) { if !media_queries.evaluate(device) {
return return
} }
} }
effective_rules(rules, device, f) effective_rules(rules, device, guard, f)
}) })
} }
} }
@ -724,6 +737,7 @@ pub trait StylesheetLoader {
struct TopLevelRuleParser<'a> { struct TopLevelRuleParser<'a> {
stylesheet_origin: Origin, stylesheet_origin: Origin,
namespaces: &'a mut Namespaces, namespaces: &'a mut Namespaces,
shared_lock: &'a SharedRwLock,
loader: Option<&'a StylesheetLoader>, loader: Option<&'a StylesheetLoader>,
context: ParserContext<'a>, context: ParserContext<'a>,
state: Cell<State>, state: Cell<State>,
@ -780,7 +794,7 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {
&self.context)); &self.context));
let media = let media =
Arc::new(RwLock::new(parse_media_query_list(input))); Arc::new(self.shared_lock.wrap(parse_media_query_list(input)));
let is_valid_url = url.url().is_some(); let is_valid_url = url.url().is_some();
@ -790,6 +804,7 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {
stylesheet: Arc::new(Stylesheet { stylesheet: Arc::new(Stylesheet {
rules: Arc::new(RwLock::new(CssRules(vec![]))), rules: Arc::new(RwLock::new(CssRules(vec![]))),
media: media, media: media,
shared_lock: self.shared_lock.clone(),
origin: self.context.stylesheet_origin, origin: self.context.stylesheet_origin,
base_url: self.context.base_url.clone(), base_url: self.context.base_url.clone(),
namespaces: RwLock::new(Namespaces::default()), namespaces: RwLock::new(Namespaces::default()),

View file

@ -28,6 +28,7 @@ use selectors::matching::{AFFECTED_BY_STYLE_ATTRIBUTE, AFFECTED_BY_PRESENTATIONA
use selectors::matching::{ElementSelectorFlags, StyleRelations, matches_complex_selector}; use selectors::matching::{ElementSelectorFlags, StyleRelations, matches_complex_selector};
use selectors::parser::{Selector, SimpleSelector, LocalName as LocalNameSelector, ComplexSelector}; use selectors::parser::{Selector, SimpleSelector, LocalName as LocalNameSelector, ComplexSelector};
use selectors::parser::SelectorMethods; use selectors::parser::SelectorMethods;
#[cfg(feature = "servo")] use shared_lock::SharedRwLockReadGuard;
use sink::Push; use sink::Push;
use smallvec::VecLike; use smallvec::VecLike;
use std::borrow::Borrow; use std::borrow::Borrow;
@ -461,7 +462,8 @@ impl Stylist {
/// FIXME(emilio): The semantics of the device for Servo and Gecko are /// FIXME(emilio): The semantics of the device for Servo and Gecko are
/// different enough we may want to unify them. /// different enough we may want to unify them.
#[cfg(feature = "servo")] #[cfg(feature = "servo")]
pub fn set_device(&mut self, mut device: Device, stylesheets: &[Arc<Stylesheet>]) { pub fn set_device(&mut self, mut device: Device, guard: &SharedRwLockReadGuard,
stylesheets: &[Arc<Stylesheet>]) {
let cascaded_rule = ViewportRule { let cascaded_rule = ViewportRule {
declarations: viewport::Cascade::from_stylesheets(stylesheets, &device).finish(), declarations: viewport::Cascade::from_stylesheets(stylesheets, &device).finish(),
}; };
@ -473,15 +475,16 @@ impl Stylist {
device.account_for_viewport_rule(constraints); device.account_for_viewport_rule(constraints);
} }
fn mq_eval_changed(rules: &[CssRule], before: &Device, after: &Device) -> bool { fn mq_eval_changed(guard: &SharedRwLockReadGuard, rules: &[CssRule],
before: &Device, after: &Device) -> bool {
for rule in rules { for rule in rules {
let changed = rule.with_nested_rules_and_mq(|rules, mq| { let changed = rule.with_nested_rules_and_mq(guard, |rules, mq| {
if let Some(mq) = mq { if let Some(mq) = mq {
if mq.evaluate(before) != mq.evaluate(after) { if mq.evaluate(before) != mq.evaluate(after) {
return true return true
} }
} }
mq_eval_changed(rules, before, after) mq_eval_changed(guard, rules, before, after)
}); });
if changed { if changed {
return true return true
@ -490,12 +493,12 @@ impl Stylist {
false false
} }
self.is_device_dirty |= stylesheets.iter().any(|stylesheet| { self.is_device_dirty |= stylesheets.iter().any(|stylesheet| {
let mq = stylesheet.media.read(); let mq = stylesheet.media.read_with(guard);
if mq.evaluate(&self.device) != mq.evaluate(&device) { if mq.evaluate(&self.device) != mq.evaluate(&device) {
return true return true
} }
mq_eval_changed(&stylesheet.rules.read().0, &self.device, &device) mq_eval_changed(guard, &stylesheet.rules.read().0, &self.device, &device)
}); });
self.device = Arc::new(device); self.device = Arc::new(device);

View file

@ -76,6 +76,7 @@ use style::properties::parse_one_declaration;
use style::restyle_hints::{self, RestyleHint}; use style::restyle_hints::{self, RestyleHint};
use style::selector_parser::PseudoElementCascadeType; use style::selector_parser::PseudoElementCascadeType;
use style::sequential; use style::sequential;
use style::shared_lock::SharedRwLock;
use style::string_cache::Atom; use style::string_cache::Atom;
use style::stylesheets::{CssRule, CssRules, ImportRule, MediaRule, NamespaceRule}; use style::stylesheets::{CssRule, CssRules, ImportRule, MediaRule, NamespaceRule};
use style::stylesheets::{Origin, Stylesheet, StyleRule}; use style::stylesheets::{Origin, Stylesheet, StyleRule};
@ -85,7 +86,7 @@ use style::thread_state;
use style::timer::Timer; use style::timer::Timer;
use style::traversal::{resolve_style, DomTraversal, TraversalDriver}; use style::traversal::{resolve_style, DomTraversal, TraversalDriver};
use style_traits::ToCss; use style_traits::ToCss;
use stylesheet_loader::StylesheetLoader; use super::stylesheet_loader::StylesheetLoader;
/* /*
* For Gecko->Servo function calls, we need to redeclare the same signature that was declared in * For Gecko->Servo function calls, we need to redeclare the same signature that was declared in
@ -95,12 +96,15 @@ use stylesheet_loader::StylesheetLoader;
* depend on but good enough for our purposes. * depend on but good enough for our purposes.
*/ */
struct GlobalStyleData { pub struct GlobalStyleData {
// How many threads parallel styling can use. // How many threads parallel styling can use.
pub num_threads: usize, pub num_threads: usize,
// The parallel styling thread pool. // The parallel styling thread pool.
pub style_thread_pool: Option<rayon::ThreadPool>, pub style_thread_pool: Option<rayon::ThreadPool>,
// Shared RWLock for CSSOM objects
pub shared_lock: SharedRwLock,
} }
impl GlobalStyleData { impl GlobalStyleData {
@ -124,12 +128,13 @@ impl GlobalStyleData {
GlobalStyleData { GlobalStyleData {
num_threads: num_threads, num_threads: num_threads,
style_thread_pool: pool, style_thread_pool: pool,
shared_lock: SharedRwLock::new(),
} }
} }
} }
lazy_static! { lazy_static! {
static ref GLOBAL_STYLE_DATA: GlobalStyleData = { pub static ref GLOBAL_STYLE_DATA: GlobalStyleData = {
GlobalStyleData::new() GlobalStyleData::new()
}; };
} }
@ -335,8 +340,9 @@ pub extern "C" fn Servo_StyleSheet_Empty(mode: SheetParsingMode) -> RawServoStyl
SheetParsingMode::eUserSheetFeatures => Origin::User, SheetParsingMode::eUserSheetFeatures => Origin::User,
SheetParsingMode::eAgentSheetFeatures => Origin::UserAgent, SheetParsingMode::eAgentSheetFeatures => Origin::UserAgent,
}; };
let shared_lock = GLOBAL_STYLE_DATA.shared_lock.clone();
Arc::new(Stylesheet::from_str( Arc::new(Stylesheet::from_str(
"", url, origin, Default::default(), None, "", url, origin, Default::default(), shared_lock, None,
&StdoutErrorReporter, extra_data) &StdoutErrorReporter, extra_data)
).into_strong() ).into_strong()
} }
@ -378,8 +384,9 @@ pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(loader: *mut Loader,
Some(ref s) => Some(s), Some(ref s) => Some(s),
}; };
let shared_lock = GLOBAL_STYLE_DATA.shared_lock.clone();
Arc::new(Stylesheet::from_str( Arc::new(Stylesheet::from_str(
input, url, origin, Default::default(), loader, input, url, origin, Default::default(), shared_lock, loader,
&StdoutErrorReporter, extra_data) &StdoutErrorReporter, extra_data)
).into_strong() ).into_strong()
} }

View file

@ -9,6 +9,7 @@ use style::gecko_bindings::structs::{Loader, ServoStyleSheet};
use style::gecko_bindings::sugar::ownership::HasArcFFI; use style::gecko_bindings::sugar::ownership::HasArcFFI;
use style::stylesheets::{ImportRule, StylesheetLoader as StyleStylesheetLoader}; use style::stylesheets::{ImportRule, StylesheetLoader as StyleStylesheetLoader};
use style_traits::ToCss; use style_traits::ToCss;
use super::glue::GLOBAL_STYLE_DATA;
pub struct StylesheetLoader(*mut Loader, *mut ServoStyleSheet); pub struct StylesheetLoader(*mut Loader, *mut ServoStyleSheet);
@ -20,6 +21,8 @@ impl StylesheetLoader {
impl StyleStylesheetLoader for StylesheetLoader { impl StyleStylesheetLoader for StylesheetLoader {
fn request_stylesheet(&self, import_rule: &Arc<RwLock<ImportRule>>) { fn request_stylesheet(&self, import_rule: &Arc<RwLock<ImportRule>>) {
let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read();
let import = import_rule.read(); let import = import_rule.read();
let (spec_bytes, spec_len) = import.url.as_slice_components() let (spec_bytes, spec_len) = import.url.as_slice_components()
.expect("Import only loads valid URLs"); .expect("Import only loads valid URLs");
@ -32,7 +35,7 @@ impl StyleStylesheetLoader for StylesheetLoader {
// evaluate them on the main thread. // evaluate them on the main thread.
// //
// Meanwhile, this works. // Meanwhile, this works.
let media = import.stylesheet.media.read().to_css_string(); let media = import.stylesheet.media.read_with(&guard).to_css_string();
unsafe { unsafe {
Gecko_LoadStyleSheet(self.0, Gecko_LoadStyleSheet(self.0,

View file

@ -11,6 +11,7 @@ use style::error_reporting::ParseErrorReporter;
use style::media_queries::*; use style::media_queries::*;
use style::parser::ParserContextExtraData; use style::parser::ParserContextExtraData;
use style::servo::media_queries::*; use style::servo::media_queries::*;
use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard};
use style::stylesheets::{Stylesheet, Origin, CssRule}; use style::stylesheets::{Stylesheet, Origin, CssRule};
use style::values::specified; use style::values::specified;
use style_traits::ToCss; use style_traits::ToCss;
@ -29,26 +30,27 @@ fn test_media_rule<F>(css: &str, callback: F)
let url = ServoUrl::parse("http://localhost").unwrap(); let url = ServoUrl::parse("http://localhost").unwrap();
let css_str = css.to_owned(); let css_str = css.to_owned();
let stylesheet = Stylesheet::from_str( let stylesheet = Stylesheet::from_str(
css, url, Origin::Author, Default::default(), css, url, Origin::Author, Default::default(), SharedRwLock::new(),
None, &CSSErrorReporterTest, None, &CSSErrorReporterTest,
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut rule_count = 0; let mut rule_count = 0;
media_queries(&stylesheet.rules.read().0, &mut |mq| { let guard = stylesheet.shared_lock.read();
media_queries(&guard, &stylesheet.rules.read().0, &mut |mq| {
rule_count += 1; rule_count += 1;
callback(mq, css); callback(mq, css);
}); });
assert!(rule_count > 0, css_str); assert!(rule_count > 0, css_str);
} }
fn media_queries<F>(rules: &[CssRule], f: &mut F) fn media_queries<F>(guard: &SharedRwLockReadGuard, rules: &[CssRule], f: &mut F)
where F: FnMut(&MediaList), where F: FnMut(&MediaList),
{ {
for rule in rules { for rule in rules {
rule.with_nested_rules_and_mq(|rules, mq| { rule.with_nested_rules_and_mq(guard, |rules, mq| {
if let Some(mq) = mq { if let Some(mq) = mq {
f(mq) f(mq)
} }
media_queries(rules, f) media_queries(guard, rules, f)
}) })
} }
} }
@ -56,7 +58,7 @@ fn media_queries<F>(rules: &[CssRule], f: &mut F)
fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) { fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) {
let url = ServoUrl::parse("http://localhost").unwrap(); let url = ServoUrl::parse("http://localhost").unwrap();
let ss = Stylesheet::from_str( let ss = Stylesheet::from_str(
css, url, Origin::Author, Default::default(), css, url, Origin::Author, Default::default(), SharedRwLock::new(),
None, &CSSErrorReporterTest, None, &CSSErrorReporterTest,
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut rule_count = 0; let mut rule_count = 0;

View file

@ -12,6 +12,7 @@ use style::media_queries::MediaList;
use style::parser::ParserContextExtraData; use style::parser::ParserContextExtraData;
use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock}; use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock};
use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource}; use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource};
use style::shared_lock::SharedRwLock;
use style::stylesheets::{Origin, Stylesheet, CssRule}; use style::stylesheets::{Origin, Stylesheet, CssRule};
use test::{self, Bencher}; use test::{self, Bencher};
@ -44,6 +45,7 @@ fn parse_rules(css: &str) -> Vec<(StyleSource, CascadeLevel)> {
MediaList { MediaList {
media_queries: vec![], media_queries: vec![],
}, },
SharedRwLock::new(),
None, None,
&ErrorringErrorReporter, &ErrorringErrorReporter,
ParserContextExtraData {}); ParserContextExtraData {});

View file

@ -20,6 +20,7 @@ use style::properties::Importance;
use style::properties::{CSSWideKeyword, DeclaredValueOwned, PropertyDeclaration, PropertyDeclarationBlock}; use style::properties::{CSSWideKeyword, DeclaredValueOwned, PropertyDeclaration, PropertyDeclarationBlock};
use style::properties::longhands; use style::properties::longhands;
use style::properties::longhands::animation_play_state; use style::properties::longhands::animation_play_state;
use style::shared_lock::SharedRwLock;
use style::stylesheets::{Origin, Namespaces}; use style::stylesheets::{Origin, Namespaces};
use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, CssRules, StyleRule, KeyframesRule}; use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, CssRules, StyleRule, KeyframesRule};
use style::values::specified::{LengthOrPercentageOrAuto, Percentage}; use style::values::specified::{LengthOrPercentageOrAuto, Percentage};
@ -62,14 +63,15 @@ fn test_parse_stylesheet() {
}"; }";
let url = ServoUrl::parse("about::test").unwrap(); let url = ServoUrl::parse("about::test").unwrap();
let stylesheet = Stylesheet::from_str(css, url.clone(), Origin::UserAgent, Default::default(), let stylesheet = Stylesheet::from_str(css, url.clone(), Origin::UserAgent, Default::default(),
None, SharedRwLock::new(), None,
&CSSErrorReporterTest, &CSSErrorReporterTest,
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut namespaces = Namespaces::default(); let mut namespaces = Namespaces::default();
namespaces.default = Some(ns!(html)); namespaces.default = Some(ns!(html));
let expected = Stylesheet { let expected = Stylesheet {
origin: Origin::UserAgent, origin: Origin::UserAgent,
media: Default::default(), media: Arc::new(stylesheet.shared_lock.wrap(Default::default())),
shared_lock: stylesheet.shared_lock.clone(),
namespaces: RwLock::new(namespaces), namespaces: RwLock::new(namespaces),
base_url: url, base_url: url,
dirty_on_viewport_size_change: AtomicBool::new(false), dirty_on_viewport_size_change: AtomicBool::new(false),
@ -324,7 +326,7 @@ fn test_report_error_stylesheet() {
let errors = error_reporter.errors.clone(); let errors = error_reporter.errors.clone();
Stylesheet::from_str(css, url.clone(), Origin::UserAgent, Default::default(), Stylesheet::from_str(css, url.clone(), Origin::UserAgent, Default::default(),
None, SharedRwLock::new(), None,
&error_reporter, &error_reporter,
ParserContextExtraData::default()); ParserContextExtraData::default());

View file

@ -9,6 +9,7 @@ use servo_config::prefs::{PREFS, PrefValue};
use servo_url::ServoUrl; use servo_url::ServoUrl;
use style::media_queries::{Device, MediaType}; use style::media_queries::{Device, MediaType};
use style::parser::{ParserContext, ParserContextExtraData}; use style::parser::{ParserContext, ParserContextExtraData};
use style::shared_lock::SharedRwLock;
use style::stylesheets::{Stylesheet, Origin}; use style::stylesheets::{Stylesheet, Origin};
use style::values::specified::LengthOrPercentageOrAuto::{self, Auto}; use style::values::specified::LengthOrPercentageOrAuto::{self, Auto};
use style::values::specified::NoCalcLength::{self, ViewportPercentage}; use style::values::specified::NoCalcLength::{self, ViewportPercentage};
@ -24,6 +25,7 @@ macro_rules! stylesheet {
ServoUrl::parse("http://localhost").unwrap(), ServoUrl::parse("http://localhost").unwrap(),
Origin::$origin, Origin::$origin,
Default::default(), Default::default(),
SharedRwLock::new(),
None, None,
&$error_reporter, &$error_reporter,
ParserContextExtraData::default() ParserContextExtraData::default()

View file

@ -21,8 +21,5 @@ extern crate style_traits;
mod sanity_checks; mod sanity_checks;
mod size_of; mod size_of;
#[path = "../../../ports/geckolib/stylesheet_loader.rs"]
mod stylesheet_loader;
mod servo_function_signatures; mod servo_function_signatures;

View file

@ -10,6 +10,9 @@ use style::gecko_properties::*;
include!(concat!(env!("OUT_DIR"), "/check_bindings.rs")); include!(concat!(env!("OUT_DIR"), "/check_bindings.rs"));
#[path = "../../../ports/geckolib/stylesheet_loader.rs"]
mod stylesheet_loader;
#[allow(non_snake_case, unused_unsafe, private_no_mangle_fns)] #[allow(non_snake_case, unused_unsafe, private_no_mangle_fns)]
mod glue { mod glue {
// this module pretends to be glue.rs, with the safe functions swapped for unsafe ones. This is // this module pretends to be glue.rs, with the safe functions swapped for unsafe ones. This is