diff --git a/components/layout_thread/lib.rs b/components/layout_thread/lib.rs index 1fbc41cab06..05666dac383 100644 --- a/components/layout_thread/lib.rs +++ b/components/layout_thread/lib.rs @@ -1332,10 +1332,10 @@ impl LayoutThread { data.stylesheets_changed, ); - let pool; + let pool = STYLE_THREAD_POOL.lock().unwrap(); + let thread_pool = pool.pool(); let (thread_pool, num_threads) = if self.parallel_flag { - pool = STYLE_THREAD_POOL.pool(); - (pool.as_ref(), STYLE_THREAD_POOL.num_threads.unwrap_or(1)) + (thread_pool.as_ref(), pool.num_threads.unwrap_or(1)) } else { (None, 1) }; @@ -1419,6 +1419,7 @@ impl LayoutThread { Some(&document), &mut rw_data, &mut layout_context, + thread_pool, ); } @@ -1626,6 +1627,7 @@ impl LayoutThread { document: Option<&ServoLayoutDocument>, rw_data: &mut LayoutThreadData, context: &mut LayoutContext, + thread_pool: Option<&rayon::ThreadPool>, ) { Self::cancel_animations_for_nodes_not_in_flow_tree( &mut *(context.style_context.animations.sets.write()), @@ -1683,14 +1685,6 @@ impl LayoutThread { || { let profiler_metadata = self.profiler_metadata(); - let pool; - let thread_pool = if self.parallel_flag { - pool = STYLE_THREAD_POOL.pool(); - pool.as_ref() - } else { - None - }; - if let Some(pool) = thread_pool { // Parallel mode. LayoutThread::solve_constraints_parallel( diff --git a/components/layout_thread_2020/lib.rs b/components/layout_thread_2020/lib.rs index 06675f0fe3b..006d18fc67e 100644 --- a/components/layout_thread_2020/lib.rs +++ b/components/layout_thread_2020/lib.rs @@ -517,6 +517,7 @@ impl LayoutThread { animation_timeline_value: f64, animations: &DocumentAnimationSet, stylesheets_changed: bool, + use_rayon: bool, ) -> LayoutContext<'a> { let traversal_flags = match stylesheets_changed { true => TraversalFlags::ForCSSRuleChanges, @@ -541,7 +542,7 @@ impl LayoutThread { font_cache_thread: Mutex::new(self.font_cache_thread.clone()), webrender_image_cache: self.webrender_image_cache.clone(), pending_images: Mutex::new(vec![]), - use_rayon: STYLE_THREAD_POOL.pool().is_some(), + use_rayon, } } @@ -989,6 +990,10 @@ impl LayoutThread { self.stylist.flush(&guards, Some(root_element), Some(&map)); + let rayon_pool = STYLE_THREAD_POOL.lock().unwrap(); + let rayon_pool = rayon_pool.pool(); + let rayon_pool = rayon_pool.as_ref(); + // Create a layout context for use throughout the following passes. let mut layout_context = self.build_layout_context( guards.clone(), @@ -997,6 +1002,7 @@ impl LayoutThread { data.animation_timeline_value, &data.animations, data.stylesheets_changed, + rayon_pool.is_some(), ); let dirty_root = unsafe { @@ -1012,9 +1018,6 @@ impl LayoutThread { RecalcStyle::pre_traverse(dirty_root, shared) }; - let rayon_pool = STYLE_THREAD_POOL.pool(); - let rayon_pool = rayon_pool.as_ref(); - if token.should_traverse() { let dirty_root: ServoLayoutNode = driver::traverse_dom(&traversal, token, rayon_pool).as_node(); diff --git a/components/style/global_style_data.rs b/components/style/global_style_data.rs index 6a22c5a11e9..80626267cdd 100644 --- a/components/style/global_style_data.rs +++ b/components/style/global_style_data.rs @@ -16,6 +16,7 @@ use parking_lot::{RwLock, RwLockReadGuard}; use rayon; use std::env; use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Mutex; /// Global style data pub struct GlobalStyleData { @@ -74,7 +75,7 @@ impl StyleThreadPool { } { // Drop the pool. - let _ = STYLE_THREAD_POOL.style_thread_pool.write().take(); + let _ = STYLE_THREAD_POOL.lock().unwrap().style_thread_pool.write().take(); } // Spin until all our threads are done. This will usually be pretty // fast, as on shutdown there should be basically no threads left @@ -104,7 +105,7 @@ impl StyleThreadPool { lazy_static! { /// Global thread pool - pub static ref STYLE_THREAD_POOL: StyleThreadPool = { + pub static ref STYLE_THREAD_POOL: Mutex = { let stylo_threads = env::var("STYLO_THREADS") .map(|s| s.parse::().expect("invalid STYLO_THREADS value")); let mut num_threads = match stylo_threads { @@ -157,14 +158,14 @@ lazy_static! { workers.ok() }; - StyleThreadPool { + Mutex::new(StyleThreadPool { num_threads: if num_threads > 0 { Some(num_threads) } else { None }, style_thread_pool: RwLock::new(pool), - } + }) }; /// Global style data diff --git a/components/style/parallel.rs b/components/style/parallel.rs index 82f003cff71..e578c6bbe51 100644 --- a/components/style/parallel.rs +++ b/components/style/parallel.rs @@ -32,8 +32,14 @@ use rayon; use smallvec::SmallVec; /// The minimum stack size for a thread in the styling pool, in kilobytes. +#[cfg(feature = "gecko")] pub const STYLE_THREAD_STACK_SIZE_KB: usize = 256; +/// The minimum stack size for a thread in the styling pool, in kilobytes. +/// Servo requires a bigger stack in debug builds. +#[cfg(feature = "servo")] +pub const STYLE_THREAD_STACK_SIZE_KB: usize = 512; + /// The stack margin. If we get this deep in the stack, we will skip recursive /// optimizations to ensure that there is sufficient room for non-recursive work. ///