Skip to content

Commit

Permalink
Replace RwLock<StyleRule> with Locked<StyleRule>
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Mar 19, 2017
1 parent 57724e5 commit aeffca2
Show file tree
Hide file tree
Showing 33 changed files with 279 additions and 334 deletions.
2 changes: 0 additions & 2 deletions 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/layout/block.rs
Expand Up @@ -446,7 +446,7 @@ fn translate_including_floats(cur_b: &mut Au, delta: Au, floats: &mut Floats) {
///
/// Note that flows with position 'fixed' just form a flat list as they all
/// have the Root flow as their CB.
pub struct AbsoluteAssignBSizesTraversal<'a>(pub &'a SharedStyleContext);
pub struct AbsoluteAssignBSizesTraversal<'a>(pub &'a SharedStyleContext<'a>);

impl<'a> PreorderFlowTraversal for AbsoluteAssignBSizesTraversal<'a> {
#[inline]
Expand Down
27 changes: 14 additions & 13 deletions components/layout/construct.rs
Expand Up @@ -311,7 +311,7 @@ impl InlineFragmentsAccumulator {
/// An object that knows how to create flows.
pub struct FlowConstructor<'a, N: ThreadSafeLayoutNode> {
/// The layout context.
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
/// Satisfy the compiler about the unused parameters, which we use to improve the ergonomics of
/// the ensuing impl {} by removing the need to parameterize all the methods individually.
phantom2: PhantomData<N>,
Expand All @@ -320,7 +320,7 @@ pub struct FlowConstructor<'a, N: ThreadSafeLayoutNode> {
impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode>
FlowConstructor<'a, ConcreteThreadSafeLayoutNode> {
/// Creates a new flow constructor.
pub fn new(layout_context: &'a LayoutContext) -> Self {
pub fn new(layout_context: &'a LayoutContext<'a>) -> Self {
FlowConstructor {
layout_context: layout_context,
phantom2: PhantomData,
Expand Down Expand Up @@ -660,10 +660,9 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode>

let mut style = node.style(self.style_context());
if node_is_input_or_text_area {
style = self.style_context()
.stylist
.style_for_anonymous_box(&PseudoElement::ServoInputText,
&style)
let context = self.style_context();
style = context.stylist.style_for_anonymous_box(
&context.guards, &PseudoElement::ServoInputText, &style)
}

self.create_fragments_for_node_text_content(&mut initial_fragments, node, &style)
Expand Down Expand Up @@ -1096,11 +1095,14 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode>
-> ConstructionResult {
let mut legalizer = Legalizer::new();

let table_style = node.style(self.style_context());
let wrapper_style = self.style_context()
.stylist
.style_for_anonymous_box(&PseudoElement::ServoTableWrapper,
&table_style);
let table_style;
let wrapper_style;
{
let context = self.style_context();
table_style = node.style(context);
wrapper_style = context.stylist.style_for_anonymous_box(
&context.guards, &PseudoElement::ServoTableWrapper, &table_style);
}
let wrapper_fragment =
Fragment::from_opaque_node_and_style(node.opaque(),
PseudoElementType::Normal,
Expand Down Expand Up @@ -2080,8 +2082,7 @@ impl Legalizer {
let reference_block = reference.as_block();
let mut new_style = reference_block.fragment.style.clone();
for pseudo in pseudos {
new_style = context.stylist.style_for_anonymous_box(pseudo,
&new_style)
new_style = context.stylist.style_for_anonymous_box(&context.guards, pseudo, &new_style)
}
let fragment = reference_block.fragment
.create_similar_anonymous_fragment(new_style,
Expand Down
8 changes: 4 additions & 4 deletions components/layout/context.rs
Expand Up @@ -75,9 +75,9 @@ pub fn heap_size_of_persistent_local_context() -> usize {
}

/// Layout information shared among all workers. This must be thread-safe.
pub struct LayoutContext {
pub struct LayoutContext<'a> {
/// Bits shared by the layout and style system.
pub style_context: SharedStyleContext,
pub style_context: SharedStyleContext<'a>,

/// The shared image cache thread.
pub image_cache_thread: Mutex<ImageCacheThread>,
Expand All @@ -95,7 +95,7 @@ pub struct LayoutContext {
pub pending_images: Option<Mutex<Vec<PendingImage>>>
}

impl Drop for LayoutContext {
impl<'a> Drop for LayoutContext<'a> {
fn drop(&mut self) {
if !thread::panicking() {
if let Some(ref pending_images) = self.pending_images {
Expand All @@ -105,7 +105,7 @@ impl Drop for LayoutContext {
}
}

impl LayoutContext {
impl<'a> LayoutContext<'a> {
#[inline(always)]
pub fn shared_context(&self) -> &SharedStyleContext {
&self.style_context
Expand Down
2 changes: 1 addition & 1 deletion components/layout/display_list_builder.rs
Expand Up @@ -121,7 +121,7 @@ fn get_cyclic<T>(arr: &[T], index: usize) -> &T {
}

pub struct DisplayListBuildState<'a> {
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
pub root_stacking_context: StackingContext,
pub items: HashMap<StackingContextId, Vec<DisplayItem>>,
pub stacking_context_children: HashMap<StackingContextId, Vec<StackingContext>>,
Expand Down
2 changes: 1 addition & 1 deletion components/layout/generated_content.rs
Expand Up @@ -97,7 +97,7 @@ static KATAKANA_IROHA: [char; 47] = [
/// The generated content resolution traversal.
pub struct ResolveGeneratedContent<'a> {
/// The layout context.
layout_context: &'a LayoutContext,
layout_context: &'a LayoutContext<'a>,
/// The counter representing an ordered list item.
list_item: Counter,
/// Named CSS counters.
Expand Down
24 changes: 12 additions & 12 deletions components/layout/traversal.rs
Expand Up @@ -22,20 +22,20 @@ use style::traversal::PerLevelTraversalData;
use wrapper::{GetRawData, LayoutNodeHelpers, LayoutNodeLayoutData};
use wrapper::ThreadSafeLayoutNodeHelpers;

pub struct RecalcStyleAndConstructFlows {
context: LayoutContext,
pub struct RecalcStyleAndConstructFlows<'a> {
context: LayoutContext<'a>,
driver: TraversalDriver,
}

impl RecalcStyleAndConstructFlows {
pub fn layout_context(&self) -> &LayoutContext {
impl<'a> RecalcStyleAndConstructFlows<'a> {
pub fn layout_context(&self) -> &LayoutContext<'a> {
&self.context
}
}

impl RecalcStyleAndConstructFlows {
impl<'a> RecalcStyleAndConstructFlows<'a> {
/// Creates a traversal context, taking ownership of the shared layout context.
pub fn new(context: LayoutContext, driver: TraversalDriver) -> Self {
pub fn new(context: LayoutContext<'a>, driver: TraversalDriver) -> Self {
RecalcStyleAndConstructFlows {
context: context,
driver: driver,
Expand All @@ -44,13 +44,13 @@ impl RecalcStyleAndConstructFlows {

/// Consumes this traversal context, returning ownership of the shared layout
/// context to the caller.
pub fn destroy(self) -> LayoutContext {
pub fn destroy(self) -> LayoutContext<'a> {
self.context
}
}

#[allow(unsafe_code)]
impl<E> DomTraversal<E> for RecalcStyleAndConstructFlows
impl<'a, E> DomTraversal<E> for RecalcStyleAndConstructFlows<'a>
where E: TElement,
E::ConcreteNode: LayoutNode,
{
Expand Down Expand Up @@ -152,7 +152,7 @@ fn construct_flows_at<N>(context: &LayoutContext,
/// The bubble-inline-sizes traversal, the first part of layout computation. This computes
/// preferred and intrinsic inline-sizes and bubbles them up the tree.
pub struct BubbleISizes<'a> {
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
}

impl<'a> PostorderFlowTraversal for BubbleISizes<'a> {
Expand All @@ -171,7 +171,7 @@ impl<'a> PostorderFlowTraversal for BubbleISizes<'a> {
/// The assign-inline-sizes traversal. In Gecko this corresponds to `Reflow`.
#[derive(Copy, Clone)]
pub struct AssignISizes<'a> {
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
}

impl<'a> PreorderFlowTraversal for AssignISizes<'a> {
Expand All @@ -191,7 +191,7 @@ impl<'a> PreorderFlowTraversal for AssignISizes<'a> {
/// positions. In Gecko this corresponds to `Reflow`.
#[derive(Copy, Clone)]
pub struct AssignBSizes<'a> {
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
}

impl<'a> PostorderFlowTraversal for AssignBSizes<'a> {
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'a> PostorderFlowTraversal for AssignBSizes<'a> {

#[derive(Copy, Clone)]
pub struct ComputeAbsolutePositions<'a> {
pub layout_context: &'a LayoutContext,
pub layout_context: &'a LayoutContext<'a>,
}

impl<'a> PreorderFlowTraversal for ComputeAbsolutePositions<'a> {
Expand Down
42 changes: 29 additions & 13 deletions components/layout_thread/lib.rs
Expand Up @@ -114,8 +114,9 @@ use style::error_reporting::StdoutErrorReporter;
use style::logical_geometry::LogicalPoint;
use style::media_queries::{Device, MediaType};
use style::parser::ParserContextExtraData;
use style::servo::AUTHOR_SHARED_LOCK;
use style::servo::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION, STORE_OVERFLOW};
use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard};
use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ReadGuards};
use style::stylesheets::{Origin, Stylesheet, UserAgentStylesheets};
use style::stylist::Stylist;
use style::thread_state;
Expand Down Expand Up @@ -215,7 +216,7 @@ pub struct LayoutThread {
WebRenderImageInfo,
BuildHasherDefault<FnvHasher>>>>,

// Webrender interface.
/// Webrender interface.
webrender_api: webrender_traits::RenderApi,

/// The timer object to control the timing of the animations. This should
Expand Down Expand Up @@ -498,16 +499,18 @@ impl LayoutThread {
}

// Create a layout context for use in building display lists, hit testing, &c.
fn build_layout_context(&self,
rw_data: &LayoutThreadData,
request_images: bool)
-> LayoutContext {
fn build_layout_context<'a>(&self,
guards: ReadGuards<'a>,
rw_data: &LayoutThreadData,
request_images: bool)
-> LayoutContext<'a> {
let thread_local_style_context_creation_data =
ThreadLocalStyleContextCreationInfo::new(self.new_animations_sender.clone());

LayoutContext {
style_context: SharedStyleContext {
stylist: rw_data.stylist.clone(),
guards: guards,
running_animations: self.running_animations.clone(),
expired_animations: self.expired_animations.clone(),
error_reporter: Box::new(self.error_reporter.clone()),
Expand Down Expand Up @@ -941,7 +944,6 @@ impl LayoutThread {
possibly_locked_rw_data: &mut RwData<'a, 'b>) {
let document = unsafe { ServoLayoutNode::new(&data.document) };
let document = document.as_document().unwrap();
let style_guard = document.style_shared_lock().read();
self.quirks_mode = Some(document.quirks_mode());

// FIXME(pcwalton): Combine `ReflowGoal` and `ReflowQueryType`. Then remove this assert.
Expand Down Expand Up @@ -1017,9 +1019,11 @@ impl LayoutThread {
Au::from_f32_px(initial_viewport.height));

// Calculate the actual viewport as per DEVICE-ADAPT § 6

let author_guard = document.style_shared_lock().read();
let device = Device::new(MediaType::Screen, initial_viewport);
Arc::get_mut(&mut rw_data.stylist).unwrap()
.set_device(device, &style_guard, &data.document_stylesheets);
.set_device(device, &author_guard, &data.document_stylesheets);

self.viewport_size =
rw_data.stylist.viewport_constraints().map_or(current_screen_size, |constraints| {
Expand Down Expand Up @@ -1063,10 +1067,16 @@ impl LayoutThread {
}

// If the entire flow tree is invalid, then it will be reflowed anyhow.
let ua_stylesheets = &*UA_STYLESHEETS;
let ua_or_user_guard = ua_stylesheets.shared_lock.read();
let guards = ReadGuards {
author: &author_guard,
ua_or_user: &ua_or_user_guard,
};
let needs_dirtying = Arc::get_mut(&mut rw_data.stylist).unwrap().update(
&data.document_stylesheets,
&style_guard,
Some(&*UA_STYLESHEETS),
&guards,
Some(ua_stylesheets),
data.stylesheets_changed);
let needs_reflow = viewport_size_changed && !needs_dirtying;
if needs_dirtying {
Expand Down Expand Up @@ -1113,7 +1123,7 @@ impl LayoutThread {
}

// Create a layout context for use throughout the following passes.
let mut layout_context = self.build_layout_context(&*rw_data, true);
let mut layout_context = self.build_layout_context(guards.clone(), &*rw_data, true);

// NB: Type inference falls apart here for some reason, so we need to be very verbose. :-(
let traversal_driver = if self.parallel_flag && self.parallel_traversal.is_some() {
Expand Down Expand Up @@ -1172,7 +1182,7 @@ impl LayoutThread {
}

if opts::get().dump_rule_tree {
layout_context.style_context.stylist.rule_tree.dump_stdout();
layout_context.style_context.stylist.rule_tree.dump_stdout(&guards);
}

// GC the rule tree if some heuristics are met.
Expand Down Expand Up @@ -1341,7 +1351,13 @@ impl LayoutThread {
page_clip_rect: max_rect(),
};

let mut layout_context = self.build_layout_context(&*rw_data, false);
let author_guard = AUTHOR_SHARED_LOCK.read();
let ua_or_user_guard = UA_STYLESHEETS.shared_lock.read();
let guards = ReadGuards {
author: &author_guard,
ua_or_user: &ua_or_user_guard,
};
let mut layout_context = self.build_layout_context(guards, &*rw_data, false);

if let Some(mut root_flow) = self.root_flow.clone() {
// Perform an abbreviated style recalc that operates without access to the DOM.
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/bindings/trace.rs
Expand Up @@ -550,7 +550,7 @@ unsafe impl JSTraceable for StyleLocked<NamespaceRule> {
}
}

unsafe impl JSTraceable for RwLock<StyleRule> {
unsafe impl JSTraceable for StyleLocked<StyleRule> {
unsafe fn trace(&self, _trc: *mut JSTracer) {
// Do nothing.
}
Expand Down

0 comments on commit aeffca2

Please sign in to comment.