Move Task to its own module

This commit is contained in:
Anthony Ramine 2017-09-17 16:41:54 +02:00
parent 8e78f18d2d
commit 46628fba05
33 changed files with 115 additions and 90 deletions

View file

@ -32,7 +32,6 @@ use dom::promise::Promise;
use js::jsapi::JSAutoCompartment;
use js::jsapi::JSTracer;
use libc;
use script_thread::Task;
use std::cell::RefCell;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::hash_map::HashMap;
@ -41,6 +40,7 @@ use std::marker::PhantomData;
use std::os;
use std::rc::Rc;
use std::sync::{Arc, Weak};
use task::Task;
#[allow(missing_docs)] // FIXME

View file

@ -35,12 +35,12 @@ use ipc_channel::router::ROUTER;
use js::conversions::ConversionResult;
use js::jsapi::{JSAutoCompartment, JSContext, JSObject};
use js::jsval::{ObjectValue, UndefinedValue};
use script_thread::Task;
use std::cell::Ref;
use std::collections::HashMap;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use task::Task;
const KEY_CONVERSION_ERROR: &'static str = "This `manufacturerData` key can not be parsed as unsigned short:";
const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices.";

View file

@ -113,7 +113,7 @@ use net_traits::response::HttpsState;
use num_traits::ToPrimitive;
use script_layout_interface::message::{Msg, ReflowQueryType};
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::{MainThreadScriptMsg, ScriptThread, Task};
use script_thread::{MainThreadScriptMsg, ScriptThread};
use script_traits::{AnimationState, CompositorEvent, DocumentActivity};
use script_traits::{MouseButton, MouseEventType, MozBrowserEvent};
use script_traits::{MsDuration, ScriptMsg, TouchpadPressurePhase};
@ -142,6 +142,7 @@ use style::shared_lock::{SharedRwLock as StyleSharedRwLock, SharedRwLockReadGuar
use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join};
use style::stylesheet_set::StylesheetSet;
use style::stylesheets::{Stylesheet, StylesheetContents, Origin, OriginSet};
use task::Task;
use task_source::TaskSource;
use time;
use timers::OneshotTimerCallback;

View file

@ -87,7 +87,7 @@ use js::jsval::JSVal;
use net_traits::request::CorsSettings;
use ref_filter_map::ref_filter_map;
use script_layout_interface::message::ReflowQueryType;
use script_thread::{ScriptThread, Task};
use script_thread::ScriptThread;
use selectors::attr::{AttrSelectorOperation, NamespaceConstraint, CaseSensitivity};
use selectors::matching::{ElementSelectorFlags, LocalMatchingContext, MatchingContext, MatchingMode};
use selectors::matching::{HAS_EDGE_CHILD_SELECTOR, HAS_SLOW_SELECTOR, HAS_SLOW_SELECTOR_LATER_SIBLINGS};
@ -119,6 +119,7 @@ use style::thread_state;
use style::values::{CSSFloat, Either};
use style::values::{specified, computed};
use stylesheet_loader::StylesheetOwner;
use task::Task;
// TODO: Update focus state when the top-level browsing context gains or loses system focus,
// and when the element enters or leaves a browsing context container.

View file

@ -20,10 +20,10 @@ use dom::node::Node;
use dom::virtualmethods::vtable_for;
use dom::window::Window;
use dom_struct::dom_struct;
use script_thread::Task;
use servo_atoms::Atom;
use std::cell::Cell;
use std::default::Default;
use task::Task;
use time;
#[dom_struct]

View file

@ -27,13 +27,13 @@ use net_traits::{CoreResourceMsg, FetchMetadata, FetchResponseMsg, FetchResponse
use net_traits::request::{CacheMode, CorsSettings, CredentialsMode};
use net_traits::request::{RequestInit, RequestMode};
use network_listener::{NetworkListener, PreInvoke};
use script_thread::Task;
use servo_atoms::Atom;
use servo_url::ServoUrl;
use std::cell::Cell;
use std::mem;
use std::str::{Chars, FromStr};
use std::sync::{Arc, Mutex};
use task::Task;
use task_source::TaskSource;
use timers::OneshotTimerCallback;
use utf8;

View file

@ -29,12 +29,12 @@ use js::jsapi::JSAutoCompartment;
use js::jsapi::JSContext;
use js::jsval::{self, JSVal};
use js::typedarray::{ArrayBuffer, CreateWith};
use script_thread::TaskCanceller;
use servo_atoms::Atom;
use std::cell::Cell;
use std::ptr;
use std::sync::Arc;
use std::thread;
use task::TaskCanceller;
use task_source::TaskSource;
use task_source::file_reading::{FileReadingTask, FileReadingTaskSource};

View file

@ -38,7 +38,7 @@ use msg::constellation_msg::PipelineId;
use net_traits::{CoreResourceThread, ResourceThreads, IpcSend};
use profile_traits::{mem, time};
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
use script_thread::{MainThreadScriptChan, ScriptThread, TaskCanceller};
use script_thread::{MainThreadScriptChan, ScriptThread};
use script_traits::{MsDuration, ScriptToConstellationChan, TimerEvent};
use script_traits::{TimerEventId, TimerSchedulerMsg, TimerSource};
use servo_url::{MutableOrigin, ServoUrl};
@ -47,6 +47,7 @@ use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::ffi::CString;
use std::rc::Rc;
use task::TaskCanceller;
use task_source::file_reading::FileReadingTaskSource;
use task_source::networking::NetworkingTaskSource;
use task_source::performance_timeline::PerformanceTimelineTaskSource;

View file

@ -16,8 +16,8 @@ use dom::node::{Node, window_from_node};
use dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
use script_thread::Task;
use std::cell::Cell;
use task::Task;
use task_source::TaskSource;
#[dom_struct]

View file

@ -49,7 +49,7 @@ use html5ever::{LocalName, Prefix};
use hyper::header::{Charset, ContentDisposition, ContentType, DispositionParam, DispositionType};
use hyper::method::Method;
use msg::constellation_msg::PipelineId;
use script_thread::{MainThreadScriptMsg, Task};
use script_thread::MainThreadScriptMsg;
use script_traits::LoadData;
use servo_rand::random;
use std::borrow::ToOwned;
@ -57,6 +57,7 @@ use std::cell::Cell;
use std::sync::mpsc::Sender;
use style::attr::AttrValue;
use style::str::split_html_space_chars;
use task::Task;
use task_source::TaskSource;
#[derive(Clone, Copy, HeapSizeOf, JSTraceable, PartialEq)]

View file

@ -43,7 +43,7 @@ use js::jsval::{NullValue, UndefinedValue};
use msg::constellation_msg::{FrameType, BrowsingContextId, PipelineId, TopLevelBrowsingContextId, TraversalDirection};
use net_traits::response::HttpsState;
use script_layout_interface::message::ReflowQueryType;
use script_thread::{ScriptThread, Task};
use script_thread::ScriptThread;
use script_traits::{IFrameLoadInfo, IFrameLoadInfoWithData, JsEvalResult, LoadData, UpdatePipelineIdReason};
use script_traits::{MozBrowserEvent, NewLayoutInfo, ScriptMsg};
use script_traits::IFrameSandboxState::{IFrameSandboxed, IFrameUnsandboxed};
@ -54,6 +54,7 @@ use servo_url::ServoUrl;
use std::cell::Cell;
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use style::context::ReflowGoal;
use task::Task;
use task_source::TaskSource;
bitflags! {

View file

@ -48,7 +48,7 @@ use net_traits::image_cache::UsePlaceholder;
use net_traits::request::{RequestInit, Type as RequestType};
use network_listener::{NetworkListener, PreInvoke};
use num_traits::ToPrimitive;
use script_thread::{ScriptThread, Task};
use script_thread::ScriptThread;
use servo_url::ServoUrl;
use servo_url::origin::ImmutableOrigin;
use std::cell::{Cell, RefMut};
@ -56,6 +56,7 @@ use std::default::Default;
use std::i32;
use std::sync::{Arc, Mutex};
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use task::Task;
use task_source::TaskSource;
#[derive(Clone, Copy, HeapSizeOf, JSTraceable)]

View file

@ -35,10 +35,11 @@ use microtask::{Microtask, MicrotaskRunnable};
use net_traits::{FetchResponseListener, FetchMetadata, Metadata, NetworkError};
use net_traits::request::{CredentialsMode, Destination, RequestInit, Type as RequestType};
use network_listener::{NetworkListener, PreInvoke};
use script_thread::{ScriptThread, Task};
use script_thread::ScriptThread;
use servo_url::ServoUrl;
use std::cell::Cell;
use std::sync::{Arc, Mutex};
use task::Task;
use task_source::TaskSource;
use time::{self, Timespec, Duration};

View file

@ -16,10 +16,10 @@ use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use js::jsapi::{HandleValue, JSContext};
use script_thread::Task;
use script_traits::{ScriptMsg, DOMMessage};
use servo_url::ServoUrl;
use std::cell::Cell;
use task::Task;
pub type TrustedServiceWorkerAddress = Trusted<ServiceWorker>;

View file

@ -17,9 +17,9 @@ use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::IpcSend;
use net_traits::storage_thread::{StorageThreadMsg, StorageType};
use script_thread::Task;
use script_traits::ScriptMsg;
use servo_url::ServoUrl;
use task::Task;
use task_source::TaskSource;
#[dom_struct]

View file

@ -36,12 +36,12 @@ use ipc_channel::ipc::{self, IpcSender};
use js::jsapi::JSContext;
use script_runtime::CommonScriptMsg;
use script_runtime::ScriptThreadEventCategory::WebVREvent;
use script_thread::Task;
use std::cell::Cell;
use std::mem;
use std::rc::Rc;
use std::sync::mpsc;
use std::thread;
use task::Task;
use webvr_traits::{WebVRDisplayData, WebVRDisplayEvent, WebVRFrameData, WebVRLayer, WebVRMsg};
#[dom_struct]

View file

@ -31,13 +31,13 @@ use net_traits::CoreResourceMsg::WebsocketConnect;
use net_traits::MessageData;
use script_runtime::CommonScriptMsg;
use script_runtime::ScriptThreadEventCategory::WebSocketEvent;
use script_thread::{Task, TaskCanceller};
use servo_url::ServoUrl;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::cell::Cell;
use std::ptr;
use std::thread;
use task::{Task, TaskCanceller};
use task_source::TaskSource;
use task_source::networking::NetworkingTaskSource;

View file

@ -76,8 +76,8 @@ use script_layout_interface::rpc::{ContentBoxResponse, ContentBoxesResponse, Lay
use script_layout_interface::rpc::{MarginStyleResponse, NodeScrollRootIdResponse};
use script_layout_interface::rpc::{ResolvedStyleResponse, TextIndexResponse};
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, ScriptThreadEventCategory};
use script_thread::{ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg, Task};
use script_thread::{ScriptThread, SendableMainThreadScriptChan, TaskCanceller};
use script_thread::{ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg};
use script_thread::{ScriptThread, SendableMainThreadScriptChan};
use script_traits::{ConstellationControlMsg, DocumentState, LoadData, MozBrowserEvent};
use script_traits::{ScriptToConstellationChan, ScriptMsg, ScrollState, TimerEvent, TimerEventId};
use script_traits::{TimerSchedulerMsg, UntrustedNodeAddress, WindowSizeData, WindowSizeType};
@ -111,6 +111,7 @@ use style::selector_parser::PseudoElement;
use style::str::HTML_SPACE_CHARACTERS;
use style::stylesheets::CssRuleType;
use style_traits::PARSING_MODE_DEFAULT;
use task::{Task, TaskCanceller};
use task_source::dom_manipulation::DOMManipulationTaskSource;
use task_source::file_reading::FileReadingTaskSource;
use task_source::history_traversal::HistoryTraversalTaskSource;

View file

@ -25,12 +25,12 @@ use dom_struct::dom_struct;
use ipc_channel::ipc;
use js::jsapi::{HandleValue, JSAutoCompartment, JSContext, NullHandleValue};
use js::jsval::UndefinedValue;
use script_thread::Task;
use script_traits::WorkerScriptLoadOrigin;
use std::cell::Cell;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Sender, channel};
use task::Task;
pub type TrustedWorkerAddress = Trusted<Worker>;

View file

@ -33,7 +33,6 @@ use js::rust::Runtime;
use net_traits::{IpcSend, load_whole_resource};
use net_traits::request::{CredentialsMode, Destination, RequestInit as NetRequestInit, Type as RequestType};
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, get_reports};
use script_thread::TaskCanceller;
use script_traits::{TimerEvent, TimerEventId};
use script_traits::WorkerGlobalScopeInit;
use servo_url::{MutableOrigin, ServoUrl};
@ -42,6 +41,7 @@ use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Receiver;
use task::TaskCanceller;
use task_source::file_reading::FileReadingTaskSource;
use task_source::networking::NetworkingTaskSource;
use task_source::performance_timeline::PerformanceTimelineTaskSource;

View file

@ -51,7 +51,7 @@ use script_runtime::CommonScriptMsg;
use script_runtime::ScriptThreadEventCategory;
use script_runtime::StackRootTLS;
use script_runtime::new_rt_and_cx;
use script_thread::{MainThreadScriptMsg, ScriptThread, Task};
use script_thread::{MainThreadScriptMsg, ScriptThread};
use servo_rand;
use servo_url::ImmutableOrigin;
use servo_url::ServoUrl;
@ -69,6 +69,7 @@ use std::thread;
use style::thread_state;
use swapper::Swapper;
use swapper::swapper;
use task::Task;
use uuid::Uuid;
// Magic numbers

View file

@ -123,6 +123,7 @@ pub mod script_thread;
mod serviceworker_manager;
mod serviceworkerjob;
mod stylesheet_loader;
mod task;
mod task_source;
pub mod test;
pub mod textinput;

View file

@ -3,8 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use net_traits::{Action, FetchResponseListener, FetchResponseMsg};
use script_thread::{Task, TaskCanceller};
use std::sync::{Arc, Mutex};
use task::{Task, TaskCanceller};
use task_source::TaskSource;
use task_source::networking::NetworkingTaskSource;

View file

@ -23,7 +23,7 @@ use js::panic::wrap_panic;
use js::rust::Runtime;
use microtask::{EnqueuedPromiseCallback, Microtask};
use profile_traits::mem::{Report, ReportKind, ReportsChan};
use script_thread::{STACK_ROOTS, Task, trace_thread};
use script_thread::{STACK_ROOTS, trace_thread};
use servo_config::opts;
use servo_config::prefs::PREFS;
use std::cell::Cell;
@ -35,6 +35,7 @@ use std::os::raw::c_void;
use std::panic::AssertUnwindSafe;
use std::ptr;
use style::thread_state;
use task::Task;
use time::{Tm, now};
/// Common messages used to control the event loops in both the script and the worker

View file

@ -105,15 +105,12 @@ use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use std::cell::Cell;
use std::collections::{hash_map, HashMap, HashSet};
use std::default::Default;
use std::fmt;
use std::intrinsics;
use std::ops::Deref;
use std::option::Option;
use std::ptr;
use std::rc::Rc;
use std::result::Result;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Select, Sender, channel};
use std::thread;
use style::context::ReflowGoal;
@ -206,62 +203,6 @@ impl InProgressLoad {
}
}
/// Encapsulated state required to create cancellable tasks from non-script threads.
pub struct TaskCanceller {
pub cancelled: Option<Arc<AtomicBool>>,
}
impl TaskCanceller {
pub fn wrap_task<T>(&self, task: Box<T>) -> Box<Task + Send>
where
T: Send + Task + 'static,
{
box CancellableTask {
cancelled: self.cancelled.clone(),
inner: task,
}
}
}
/// A task that can be discarded by toggling a shared flag.
pub struct CancellableTask<T: Send + Task> {
cancelled: Option<Arc<AtomicBool>>,
inner: Box<T>,
}
impl<T> CancellableTask<T>
where
T: Send + Task,
{
fn is_cancelled(&self) -> bool {
self.cancelled.as_ref().map_or(false, |cancelled| {
cancelled.load(Ordering::SeqCst)
})
}
}
impl<T> Task for CancellableTask<T>
where
T: Send + Task,
{
fn run(self: Box<Self>) {
if !self.is_cancelled() {
self.inner.run()
}
}
}
pub trait Task {
fn name(&self) -> &'static str { unsafe { intrinsics::type_name::<Self>() } }
fn run(self: Box<Self>);
}
impl fmt::Debug for Task + Send {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple(self.name()).field(&format_args!("...")).finish()
}
}
#[derive(Debug)]
enum MixedMessage {
FromConstellation(ConstellationControlMsg),

View file

@ -18,11 +18,12 @@ use dom::promise::Promise;
use dom::serviceworkerregistration::ServiceWorkerRegistration;
use dom::urlhelper::UrlHelper;
use js::jsapi::JSAutoCompartment;
use script_thread::{ScriptThread, Task};
use script_thread::ScriptThread;
use servo_url::ServoUrl;
use std::cmp::PartialEq;
use std::collections::HashMap;
use std::rc::Rc;
use task::Task;
use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTaskSource;

70
components/script/task.rs Normal file
View file

@ -0,0 +1,70 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Machinery for [tasks](https://html.spec.whatwg.org/multipage/#concept-task).
use std::fmt;
use std::intrinsics;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
/// A task that can be run. The name method is for profiling purposes.
pub trait Task {
#[allow(unsafe_code)]
fn name(&self) -> &'static str { unsafe { intrinsics::type_name::<Self>() } }
fn run(self: Box<Self>);
}
impl fmt::Debug for Task + Send {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple(self.name()).field(&format_args!("...")).finish()
}
}
/// Encapsulated state required to create cancellable tasks from non-script threads.
pub struct TaskCanceller {
pub cancelled: Option<Arc<AtomicBool>>,
}
impl TaskCanceller {
/// Returns a wrapped `task` that will be cancelled if the `TaskCanceller`
/// says so.
pub fn wrap_task<T>(&self, task: Box<T>) -> Box<Task + Send>
where
T: Send + Task + 'static,
{
box CancellableTask {
cancelled: self.cancelled.clone(),
inner: task,
}
}
}
/// A task that can be cancelled by toggling a shared flag.
pub struct CancellableTask<T: Send + Task> {
cancelled: Option<Arc<AtomicBool>>,
inner: Box<T>,
}
impl<T> CancellableTask<T>
where
T: Send + Task,
{
fn is_cancelled(&self) -> bool {
self.cancelled.as_ref().map_or(false, |cancelled| {
cancelled.load(Ordering::SeqCst)
})
}
}
impl<T> Task for CancellableTask<T>
where
T: Send + Task,
{
fn run(self: Box<Self>) {
if !self.is_cancelled() {
self.inner.run()
}
}
}

View file

@ -8,11 +8,12 @@ use dom::event::{EventBubbles, EventCancelable, EventTask, SimpleEventTask};
use dom::eventtarget::EventTarget;
use dom::window::Window;
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::{MainThreadScriptMsg, Task, TaskCanceller};
use script_thread::MainThreadScriptMsg;
use servo_atoms::Atom;
use std::fmt;
use std::result::Result;
use std::sync::mpsc::Sender;
use task::{Task, TaskCanceller};
use task_source::TaskSource;
#[derive(Clone, JSTraceable)]

View file

@ -5,8 +5,8 @@
use dom::domexception::DOMErrorName;
use dom::filereader::{FileReader, TrustedFileReader, GenerationId, ReadMetaData};
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory, ScriptChan};
use script_thread::{Task, TaskCanceller};
use std::sync::Arc;
use task::{Task, TaskCanceller};
use task_source::TaskSource;
#[derive(JSTraceable)]

View file

@ -10,8 +10,8 @@ pub mod performance_timeline;
pub mod user_interaction;
use dom::globalscope::GlobalScope;
use script_thread::{Task, TaskCanceller};
use std::result::Result;
use task::{Task, TaskCanceller};
pub trait TaskSource {
fn queue_with_canceller<T>(

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use script_thread::{Task, TaskCanceller};
use task::{Task, TaskCanceller};
use task_source::TaskSource;
#[derive(JSTraceable)]

View file

@ -10,9 +10,9 @@ use dom::bindings::refcounted::Trusted;
use dom::globalscope::GlobalScope;
use dom::performance::Performance;
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use script_thread::{Task, TaskCanceller};
use std::fmt;
use std::result::Result;
use task::{Task, TaskCanceller};
use task_source::TaskSource;
pub struct NotifyPerformanceObserverTask {

View file

@ -8,11 +8,12 @@ use dom::event::{EventBubbles, EventCancelable, EventTask};
use dom::eventtarget::EventTarget;
use dom::window::Window;
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
use script_thread::{MainThreadScriptMsg, Task, TaskCanceller};
use script_thread::MainThreadScriptMsg;
use servo_atoms::Atom;
use std::fmt;
use std::result::Result;
use std::sync::mpsc::Sender;
use task::{Task, TaskCanceller};
use task_source::TaskSource;
#[derive(Clone, JSTraceable)]