Auto merge of #18418 - servo:tasks, r=emilio

Simplify some task-related code

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18418)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-09-08 06:01:22 -05:00 committed by GitHub
commit 84ac9f384a
3 changed files with 47 additions and 65 deletions

View file

@ -114,12 +114,12 @@ use std::sync::mpsc::{Receiver, Select, Sender, channel};
use std::thread; use std::thread;
use style::context::ReflowGoal; use style::context::ReflowGoal;
use style::thread_state; use style::thread_state;
use task_source::dom_manipulation::{DOMManipulationTask, DOMManipulationTaskSource}; use task_source::dom_manipulation::DOMManipulationTaskSource;
use task_source::file_reading::FileReadingTaskSource; use task_source::file_reading::FileReadingTaskSource;
use task_source::history_traversal::HistoryTraversalTaskSource; use task_source::history_traversal::HistoryTraversalTaskSource;
use task_source::networking::NetworkingTaskSource; use task_source::networking::NetworkingTaskSource;
use task_source::performance_timeline::PerformanceTimelineTaskSource; use task_source::performance_timeline::PerformanceTimelineTaskSource;
use task_source::user_interaction::{UserInteractionTask, UserInteractionTaskSource}; use task_source::user_interaction::UserInteractionTaskSource;
use time::{get_time, precise_time_ns, Tm}; use time::{get_time, precise_time_ns, Tm};
use url::Position; use url::Position;
use webdriver_handlers; use webdriver_handlers;
@ -251,7 +251,9 @@ where
pub trait Runnable { pub trait Runnable {
fn name(&self) -> &'static str { unsafe { intrinsics::type_name::<Self>() } } fn name(&self) -> &'static str { unsafe { intrinsics::type_name::<Self>() } }
fn handler(self: Box<Self>) {} fn handler(self: Box<Self>) {
panic!("This should probably be redefined.")
}
fn main_thread_handler(self: Box<Self>, _script_thread: &ScriptThread) { self.handler(); } fn main_thread_handler(self: Box<Self>, _script_thread: &ScriptThread) { self.handler(); }
} }
@ -276,10 +278,6 @@ pub enum MainThreadScriptMsg {
/// dispatched to ScriptThread). Allows for a replace bool to be passed. If true, /// dispatched to ScriptThread). Allows for a replace bool to be passed. If true,
/// the current entry will be replaced instead of a new entry being added. /// the current entry will be replaced instead of a new entry being added.
Navigate(PipelineId, LoadData, bool), Navigate(PipelineId, LoadData, bool),
/// Tasks that originate from the DOM manipulation task source
DOMManipulation(DOMManipulationTask),
/// Tasks that originate from the user interaction task source
UserInteraction(UserInteractionTask),
/// Notifies the script thread that a new worklet has been loaded, and thus the page should be /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
/// reflowed. /// reflowed.
WorkletLoaded(PipelineId), WorkletLoaded(PipelineId),
@ -1285,23 +1283,21 @@ impl ScriptThread {
fn handle_msg_from_script(&self, msg: MainThreadScriptMsg) { fn handle_msg_from_script(&self, msg: MainThreadScriptMsg) {
match msg { match msg {
MainThreadScriptMsg::Navigate(parent_pipeline_id, load_data, replace) => MainThreadScriptMsg::Navigate(parent_pipeline_id, load_data, replace) => {
self.handle_navigate(parent_pipeline_id, None, load_data, replace), self.handle_navigate(parent_pipeline_id, None, load_data, replace)
MainThreadScriptMsg::ExitWindow(id) => },
self.handle_exit_window_msg(id), MainThreadScriptMsg::ExitWindow(id) => {
self.handle_exit_window_msg(id)
},
MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable)) => { MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable)) => {
// The category of the runnable is ignored by the pattern, however
// it is still respected by profiling (see categorize_msg).
runnable.main_thread_handler(self) runnable.main_thread_handler(self)
} }
MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(reports_chan)) => MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(chan)) => {
self.collect_reports(reports_chan), self.collect_reports(chan)
MainThreadScriptMsg::WorkletLoaded(pipeline_id) => },
self.handle_worklet_loaded(pipeline_id), MainThreadScriptMsg::WorkletLoaded(pipeline_id) => {
MainThreadScriptMsg::DOMManipulation(task) => self.handle_worklet_loaded(pipeline_id)
task.handle_task(self), },
MainThreadScriptMsg::UserInteraction(task) =>
task.handle_task(self),
} }
} }

View file

@ -7,7 +7,8 @@ use dom::bindings::refcounted::Trusted;
use dom::event::{EventBubbles, EventCancelable, EventRunnable, SimpleEventRunnable}; use dom::event::{EventBubbles, EventCancelable, EventRunnable, SimpleEventRunnable};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::window::Window; use dom::window::Window;
use script_thread::{MainThreadScriptMsg, Runnable, RunnableWrapper, ScriptThread}; use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::{MainThreadScriptMsg, Runnable, RunnableWrapper};
use servo_atoms::Atom; use servo_atoms::Atom;
use std::fmt; use std::fmt;
use std::result::Result; use std::result::Result;
@ -24,13 +25,19 @@ impl fmt::Debug for DOMManipulationTaskSource {
} }
impl TaskSource for DOMManipulationTaskSource { impl TaskSource for DOMManipulationTaskSource {
fn queue_with_wrapper<T>(&self, fn queue_with_wrapper<T>(
msg: Box<T>, &self,
wrapper: &RunnableWrapper) msg: Box<T>,
-> Result<(), ()> wrapper: &RunnableWrapper,
where T: Runnable + Send + 'static { ) -> Result<(), ()>
let msg = DOMManipulationTask(wrapper.wrap_runnable(msg)); where
self.0.send(MainThreadScriptMsg::DOMManipulation(msg)).map_err(|_| ()) T: Runnable + Send + 'static,
{
let msg = MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(
ScriptThreadEventCategory::ScriptEvent,
wrapper.wrap_runnable(msg),
));
self.0.send(msg).map_err(|_| ())
} }
} }
@ -60,17 +67,3 @@ impl DOMManipulationTaskSource {
let _ = self.queue(runnable, window.upcast()); let _ = self.queue(runnable, window.upcast());
} }
} }
pub struct DOMManipulationTask(pub Box<Runnable + Send>);
impl fmt::Debug for DOMManipulationTask {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DOMManipulationTask(...)")
}
}
impl DOMManipulationTask {
pub fn handle_task(self, script_thread: &ScriptThread) {
self.0.main_thread_handler(script_thread);
}
}

View file

@ -7,7 +7,8 @@ use dom::bindings::refcounted::Trusted;
use dom::event::{EventBubbles, EventCancelable, EventRunnable}; use dom::event::{EventBubbles, EventCancelable, EventRunnable};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::window::Window; use dom::window::Window;
use script_thread::{MainThreadScriptMsg, Runnable, RunnableWrapper, ScriptThread}; use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::{MainThreadScriptMsg, Runnable, RunnableWrapper};
use servo_atoms::Atom; use servo_atoms::Atom;
use std::fmt; use std::fmt;
use std::result::Result; use std::result::Result;
@ -24,13 +25,19 @@ impl fmt::Debug for UserInteractionTaskSource {
} }
impl TaskSource for UserInteractionTaskSource { impl TaskSource for UserInteractionTaskSource {
fn queue_with_wrapper<T>(&self, fn queue_with_wrapper<T>(
msg: Box<T>, &self,
wrapper: &RunnableWrapper) msg: Box<T>,
-> Result<(), ()> wrapper: &RunnableWrapper,
where T: Runnable + Send + 'static { ) -> Result<(), ()>
let msg = UserInteractionTask(wrapper.wrap_runnable(msg)); where
self.0.send(MainThreadScriptMsg::UserInteraction(msg)).map_err(|_| ()) T: Runnable + Send + 'static,
{
let msg = MainThreadScriptMsg::Common(CommonScriptMsg::RunnableMsg(
ScriptThreadEventCategory::InputEvent,
wrapper.wrap_runnable(msg),
));
self.0.send(msg).map_err(|_| ())
} }
} }
@ -51,17 +58,3 @@ impl UserInteractionTaskSource {
let _ = self.queue(runnable, window.upcast()); let _ = self.queue(runnable, window.upcast());
} }
} }
pub struct UserInteractionTask(pub Box<Runnable + Send>);
impl fmt::Debug for UserInteractionTask {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UserInteractionTask(...)")
}
}
impl UserInteractionTask {
pub fn handle_task(self, script_thread: &ScriptThread) {
self.0.main_thread_handler(script_thread);
}
}