Skip to content

Commit 2002de4

Browse files
committed
Use mitochondria::OnceCell to store ScriptThread in TLS
1 parent 1b68f79 commit 2002de4

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ libc = "0.2"
5353
log = "0.3.5"
5454
mime = "0.2.1"
5555
mime_guess = "1.8.0"
56+
mitochondria = "1.1.2"
5657
msg = {path = "../msg"}
5758
net_traits = {path = "../net_traits"}
5859
num-traits = "0.1.32"

components/script/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern crate log;
6363
#[macro_use]
6464
extern crate mime;
6565
extern crate mime_guess;
66+
extern crate mitochondria;
6667
extern crate msg;
6768
extern crate net_traits;
6869
extern crate num_traits;

components/script/script_thread.rs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use js::jsval::UndefinedValue;
7070
use js::rust::Runtime;
7171
use layout_wrapper::ServoLayoutNode;
7272
use mem::heap_size_of_self_and_children;
73+
use mitochondria::OnceCell;
7374
use msg::constellation_msg::{FrameId, FrameType, PipelineId, PipelineNamespace};
7475
use net_traits::{CoreResourceMsg, FetchMetadata, FetchResponseListener};
7576
use net_traits::{IpcSend, Metadata, ReferrerPolicy, ResourceThreads};
@@ -120,13 +121,13 @@ use webdriver_handlers;
120121
use webvr_traits::WebVRMsg;
121122

122123
thread_local!(pub static STACK_ROOTS: Cell<Option<RootCollectionPtr>> = Cell::new(None));
123-
thread_local!(static SCRIPT_THREAD_ROOT: Cell<Option<*const ScriptThread>> = Cell::new(None));
124+
thread_local!(static SCRIPT_THREAD_ROOT: OnceCell<ScriptThread> = OnceCell::new());
124125

125126
pub unsafe fn trace_thread(tr: *mut JSTracer) {
126127
SCRIPT_THREAD_ROOT.with(|root| {
127-
if let Some(script_thread) = root.get() {
128+
if let Some(script_thread) = root.as_ref() {
128129
debug!("tracing fields of ScriptThread");
129-
(*script_thread).trace(tr);
130+
script_thread.trace(tr);
130131
}
131132
});
132133
}
@@ -534,29 +535,26 @@ impl ScriptThreadFactory for ScriptThread {
534535
let parent_info = state.parent_info;
535536
let mem_profiler_chan = state.mem_profiler_chan.clone();
536537
let window_size = state.window_size;
537-
let script_thread = ScriptThread::new(state,
538-
script_port,
539-
script_chan.clone());
540538

541539
SCRIPT_THREAD_ROOT.with(|root| {
542-
root.set(Some(&script_thread as *const _));
543-
});
540+
let script_thread = root.init_once(|| ScriptThread::new(state, script_port, script_chan.clone()));
544541

545-
let mut failsafe = ScriptMemoryFailsafe::new(&script_thread);
542+
let mut failsafe = ScriptMemoryFailsafe::new(&script_thread);
546543

547-
let origin = Origin::new(&load_data.url);
548-
let new_load = InProgressLoad::new(id, frame_id, parent_info, layout_chan, window_size,
549-
load_data.url.clone(), origin);
550-
script_thread.start_page_load(new_load, load_data);
544+
let origin = Origin::new(&load_data.url);
545+
let new_load = InProgressLoad::new(id, frame_id, parent_info, layout_chan, window_size,
546+
load_data.url.clone(), origin);
547+
script_thread.start_page_load(new_load, load_data);
551548

552-
let reporter_name = format!("script-reporter-{}", id);
553-
mem_profiler_chan.run_with_memory_reporting(|| {
554-
script_thread.start();
555-
let _ = script_thread.content_process_shutdown_chan.send(());
556-
}, reporter_name, script_chan, CommonScriptMsg::CollectReports);
549+
let reporter_name = format!("script-reporter-{}", id);
550+
mem_profiler_chan.run_with_memory_reporting(|| {
551+
script_thread.start();
552+
let _ = script_thread.content_process_shutdown_chan.send(());
553+
}, reporter_name, script_chan, CommonScriptMsg::CollectReports);
557554

558-
// This must always be the very last operation performed before the thread completes
559-
failsafe.neuter();
555+
// This must always be the very last operation performed before the thread completes
556+
failsafe.neuter();
557+
});
560558
}).expect("Thread spawning failed");
561559

562560
(sender, receiver)
@@ -567,24 +565,23 @@ impl ScriptThread {
567565
pub fn page_headers_available(id: &PipelineId, metadata: Option<Metadata>)
568566
-> Option<Root<ServoParser>> {
569567
SCRIPT_THREAD_ROOT.with(|root| {
570-
let script_thread = unsafe { &*root.get().unwrap() };
568+
let script_thread = root.as_ref().unwrap();
571569
script_thread.handle_page_headers_available(id, metadata)
572570
})
573571
}
574572

575573
#[allow(unrooted_must_root)]
576574
pub fn schedule_job(job: Job, global: &GlobalScope) {
577575
SCRIPT_THREAD_ROOT.with(|root| {
578-
let script_thread = unsafe { &*root.get().unwrap() };
576+
let script_thread = root.as_ref().unwrap();
579577
let job_queue = &*script_thread.job_queue_map;
580578
job_queue.schedule_job(job, global, &script_thread);
581579
});
582580
}
583581

584582
pub fn process_event(msg: CommonScriptMsg) {
585583
SCRIPT_THREAD_ROOT.with(|root| {
586-
if let Some(script_thread) = root.get() {
587-
let script_thread = unsafe { &*script_thread };
584+
if let Some(script_thread) = root.as_ref() {
588585
script_thread.handle_msg_from_script(MainThreadScriptMsg::Common(msg));
589586
}
590587
});
@@ -594,8 +591,7 @@ impl ScriptThread {
594591
pub fn await_stable_state<T: Runnable + Send + 'static>(task: T) {
595592
//TODO use microtasks when they exist
596593
SCRIPT_THREAD_ROOT.with(|root| {
597-
if let Some(script_thread) = root.get() {
598-
let script_thread = unsafe { &*script_thread };
594+
if let Some(script_thread) = root.as_ref() {
599595
let _ = script_thread.chan.send(CommonScriptMsg::RunnableMsg(
600596
ScriptThreadEventCategory::DomEvent,
601597
box task));
@@ -605,8 +601,7 @@ impl ScriptThread {
605601

606602
pub fn process_attach_layout(new_layout_info: NewLayoutInfo, origin: Origin) {
607603
SCRIPT_THREAD_ROOT.with(|root| {
608-
if let Some(script_thread) = root.get() {
609-
let script_thread = unsafe { &*script_thread };
604+
if let Some(script_thread) = root.as_ref() {
610605
script_thread.profile_event(ScriptThreadEventCategory::AttachLayout, || {
611606
script_thread.handle_new_layout(new_layout_info, origin);
612607
})
@@ -615,10 +610,11 @@ impl ScriptThread {
615610
}
616611

617612
pub fn find_document(id: PipelineId) -> Option<Root<Document>> {
618-
SCRIPT_THREAD_ROOT.with(|root| root.get().and_then(|script_thread| {
619-
let script_thread = unsafe { &*script_thread };
620-
script_thread.documents.borrow().find_document(id)
621-
}))
613+
SCRIPT_THREAD_ROOT.with(|root| {
614+
root.as_ref().and_then(|script_thread| {
615+
script_thread.documents.borrow().find_document(id)
616+
})
617+
})
622618
}
623619

624620
/// Creates a new script thread.
@@ -2167,14 +2163,14 @@ impl ScriptThread {
21672163

21682164
pub fn enqueue_promise_job(job: EnqueuedPromiseCallback, global: &GlobalScope) {
21692165
SCRIPT_THREAD_ROOT.with(|root| {
2170-
let script_thread = unsafe { &*root.get().unwrap() };
2166+
let script_thread = root.as_ref().unwrap();
21712167
script_thread.promise_job_queue.enqueue(job, global);
21722168
});
21732169
}
21742170

21752171
pub fn flush_promise_jobs(global: &GlobalScope) {
21762172
SCRIPT_THREAD_ROOT.with(|root| {
2177-
let script_thread = unsafe { &*root.get().unwrap() };
2173+
let script_thread = root.as_ref().unwrap();
21782174
let _ = script_thread.dom_manipulation_task_source.queue(
21792175
box FlushPromiseJobs, global);
21802176
})
@@ -2192,14 +2188,6 @@ impl Runnable for FlushPromiseJobs {
21922188
}
21932189
}
21942190

2195-
impl Drop for ScriptThread {
2196-
fn drop(&mut self) {
2197-
SCRIPT_THREAD_ROOT.with(|root| {
2198-
root.set(None);
2199-
});
2200-
}
2201-
}
2202-
22032191
/// Shuts down layout for the given window.
22042192
fn shut_down_layout(window: &Window) {
22052193
// Tell the layout thread to begin shutting down, and wait until it

0 commit comments

Comments
 (0)