mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
add a control chan to workers, use to signal shutdown
This commit is contained in:
parent
ed688fe2c1
commit
947fa8bbb7
6 changed files with 165 additions and 31 deletions
|
@ -86,6 +86,12 @@ impl<'a> Drop for AutoWorkerReset<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Messages sent from the owning global.
|
||||
pub enum DedicatedWorkerControlMsg {
|
||||
/// Shutdown the worker.
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub enum DedicatedWorkerScriptMsg {
|
||||
/// Standard message from a worker.
|
||||
CommonWorker(TrustedWorkerAddress, WorkerScriptMsg),
|
||||
|
@ -96,6 +102,7 @@ pub enum DedicatedWorkerScriptMsg {
|
|||
pub enum MixedMessage {
|
||||
FromWorker(DedicatedWorkerScriptMsg),
|
||||
FromDevtools(DevtoolScriptControlMsg),
|
||||
FromControl(DedicatedWorkerControlMsg),
|
||||
}
|
||||
|
||||
impl QueuedTaskConversion for DedicatedWorkerScriptMsg {
|
||||
|
@ -183,18 +190,23 @@ pub struct DedicatedWorkerGlobalScope {
|
|||
#[ignore_malloc_size_of = "Arc"]
|
||||
image_cache: Arc<dyn ImageCache>,
|
||||
browsing_context: Option<BrowsingContextId>,
|
||||
/// A receiver of control messages,
|
||||
/// currently only used to signal shutdown.
|
||||
#[ignore_malloc_size_of = "Channels are hard"]
|
||||
control_receiver: Receiver<DedicatedWorkerControlMsg>,
|
||||
}
|
||||
|
||||
impl WorkerEventLoopMethods for DedicatedWorkerGlobalScope {
|
||||
type WorkerMsg = DedicatedWorkerScriptMsg;
|
||||
type ControlMsg = DedicatedWorkerControlMsg;
|
||||
type Event = MixedMessage;
|
||||
|
||||
fn task_queue(&self) -> &TaskQueue<DedicatedWorkerScriptMsg> {
|
||||
&self.task_queue
|
||||
}
|
||||
|
||||
fn handle_event(&self, event: MixedMessage) {
|
||||
self.handle_mixed_message(event);
|
||||
fn handle_event(&self, event: MixedMessage) -> bool {
|
||||
self.handle_mixed_message(event)
|
||||
}
|
||||
|
||||
fn handle_worker_post_event(&self, worker: &TrustedWorkerAddress) -> Option<AutoWorkerReset> {
|
||||
|
@ -202,6 +214,10 @@ impl WorkerEventLoopMethods for DedicatedWorkerGlobalScope {
|
|||
Some(ar)
|
||||
}
|
||||
|
||||
fn from_control_msg(&self, msg: DedicatedWorkerControlMsg) -> MixedMessage {
|
||||
MixedMessage::FromControl(msg)
|
||||
}
|
||||
|
||||
fn from_worker_msg(&self, msg: DedicatedWorkerScriptMsg) -> MixedMessage {
|
||||
MixedMessage::FromWorker(msg)
|
||||
}
|
||||
|
@ -209,6 +225,10 @@ impl WorkerEventLoopMethods for DedicatedWorkerGlobalScope {
|
|||
fn from_devtools_msg(&self, msg: DevtoolScriptControlMsg) -> MixedMessage {
|
||||
MixedMessage::FromDevtools(msg)
|
||||
}
|
||||
|
||||
fn control_receiver(&self) -> &Receiver<DedicatedWorkerControlMsg> {
|
||||
&self.control_receiver
|
||||
}
|
||||
}
|
||||
|
||||
impl DedicatedWorkerGlobalScope {
|
||||
|
@ -226,6 +246,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache: Arc<dyn ImageCache>,
|
||||
browsing_context: Option<BrowsingContextId>,
|
||||
gpu_id_hub: Arc<Mutex<Identities>>,
|
||||
control_receiver: Receiver<DedicatedWorkerControlMsg>,
|
||||
) -> DedicatedWorkerGlobalScope {
|
||||
DedicatedWorkerGlobalScope {
|
||||
workerglobalscope: WorkerGlobalScope::new_inherited(
|
||||
|
@ -244,6 +265,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
worker: DomRefCell::new(None),
|
||||
image_cache: image_cache,
|
||||
browsing_context,
|
||||
control_receiver,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -262,6 +284,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache: Arc<dyn ImageCache>,
|
||||
browsing_context: Option<BrowsingContextId>,
|
||||
gpu_id_hub: Arc<Mutex<Identities>>,
|
||||
control_receiver: Receiver<DedicatedWorkerControlMsg>,
|
||||
) -> DomRoot<DedicatedWorkerGlobalScope> {
|
||||
let cx = runtime.cx();
|
||||
let scope = Box::new(DedicatedWorkerGlobalScope::new_inherited(
|
||||
|
@ -278,6 +301,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache,
|
||||
browsing_context,
|
||||
gpu_id_hub,
|
||||
control_receiver,
|
||||
));
|
||||
unsafe { DedicatedWorkerGlobalScopeBinding::Wrap(SafeJSContext::from_ptr(cx), scope) }
|
||||
}
|
||||
|
@ -299,6 +323,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache: Arc<dyn ImageCache>,
|
||||
browsing_context: Option<BrowsingContextId>,
|
||||
gpu_id_hub: Arc<Mutex<Identities>>,
|
||||
control_receiver: Receiver<DedicatedWorkerControlMsg>,
|
||||
) -> JoinHandle<()> {
|
||||
let serialized_worker_url = worker_url.to_string();
|
||||
let name = format!("WebWorker for {}", serialized_worker_url);
|
||||
|
@ -370,6 +395,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache,
|
||||
browsing_context,
|
||||
gpu_id_hub,
|
||||
control_receiver,
|
||||
);
|
||||
// FIXME(njn): workers currently don't have a unique ID suitable for using in reporter
|
||||
// registration (#6631), so we instead use a random number and cross our fingers.
|
||||
|
@ -485,7 +511,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_mixed_message(&self, msg: MixedMessage) {
|
||||
fn handle_mixed_message(&self, msg: MixedMessage) -> bool {
|
||||
// FIXME(#26324): `self.worker` is None in devtools messages.
|
||||
match msg {
|
||||
MixedMessage::FromDevtools(msg) => match msg {
|
||||
|
@ -505,7 +531,11 @@ impl DedicatedWorkerGlobalScope {
|
|||
self.handle_script_event(msg);
|
||||
},
|
||||
MixedMessage::FromWorker(DedicatedWorkerScriptMsg::WakeUp) => {},
|
||||
MixedMessage::FromControl(DedicatedWorkerControlMsg::Exit) => {
|
||||
return false;
|
||||
},
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#runtime-script-errors-2
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue