mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
add mechanism to join on service- and dedicated-worker threads
This commit is contained in:
parent
d7d56454b0
commit
ed688fe2c1
6 changed files with 63 additions and 18 deletions
|
@ -135,6 +135,7 @@ use std::path::PathBuf;
|
|||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Instant, SystemTime};
|
||||
use style::animation::ElementAnimationSet;
|
||||
use style::attr::{AttrIdentifier, AttrValue, LengthOrPercentageOrAuto};
|
||||
|
@ -167,6 +168,7 @@ use webxr_api::SwapChainId as WebXRSwapChainId;
|
|||
use webxr_api::{Finger, Hand, Ray, View};
|
||||
|
||||
unsafe_no_jsmanaged_fields!(Tm);
|
||||
unsafe_no_jsmanaged_fields!(JoinHandle<()>);
|
||||
|
||||
/// A trait to allow tracing (only) DOM objects.
|
||||
pub unsafe trait JSTraceable {
|
||||
|
|
|
@ -56,7 +56,7 @@ use servo_url::ServoUrl;
|
|||
use std::mem::replace;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::thread::{self, JoinHandle};
|
||||
use style::thread_state::{self, ThreadState};
|
||||
|
||||
/// Set the `worker` field of a related DedicatedWorkerGlobalScope object to a particular
|
||||
|
@ -299,7 +299,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
image_cache: Arc<dyn ImageCache>,
|
||||
browsing_context: Option<BrowsingContextId>,
|
||||
gpu_id_hub: Arc<Mutex<Identities>>,
|
||||
) {
|
||||
) -> JoinHandle<()> {
|
||||
let serialized_worker_url = worker_url.to_string();
|
||||
let name = format!("WebWorker for {}", serialized_worker_url);
|
||||
let top_level_browsing_context_id = TopLevelBrowsingContextId::installed();
|
||||
|
@ -435,7 +435,7 @@ impl DedicatedWorkerGlobalScope {
|
|||
CommonScriptMsg::CollectReports,
|
||||
);
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
.expect("Thread spawning failed")
|
||||
}
|
||||
|
||||
pub fn image_cache(&self) -> Arc<dyn ImageCache> {
|
||||
|
|
|
@ -112,15 +112,29 @@ use std::ops::Index;
|
|||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread::JoinHandle;
|
||||
use time::{get_time, Timespec};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(JSTraceable)]
|
||||
pub struct AutoCloseWorker(Arc<AtomicBool>);
|
||||
pub struct AutoCloseWorker {
|
||||
closing: Arc<AtomicBool>,
|
||||
join_handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Drop for AutoCloseWorker {
|
||||
/// <https://html.spec.whatwg.org/multipage/#terminate-a-worker>
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, Ordering::SeqCst);
|
||||
// Step 1.
|
||||
self.closing.store(true, Ordering::SeqCst);
|
||||
|
||||
// TODO: step 2 and 3.
|
||||
// Step 4 is unnecessary since we don't use actual ports for dedicated workers.
|
||||
self.join_handle
|
||||
.take()
|
||||
.expect("No handle to join on worker.")
|
||||
.join()
|
||||
.expect("Couldn't join on worker thread.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1794,10 +1808,13 @@ impl GlobalScope {
|
|||
&self.permission_state_invocation_results
|
||||
}
|
||||
|
||||
pub fn track_worker(&self, closing_worker: Arc<AtomicBool>) {
|
||||
pub fn track_worker(&self, closing: Arc<AtomicBool>, join_handle: JoinHandle<()>) {
|
||||
self.list_auto_close_worker
|
||||
.borrow_mut()
|
||||
.push(AutoCloseWorker(closing_worker));
|
||||
.push(AutoCloseWorker {
|
||||
closing,
|
||||
join_handle: Some(join_handle),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn track_event_source(&self, event_source: &EventSource) {
|
||||
|
|
|
@ -45,7 +45,7 @@ use servo_config::pref;
|
|||
use servo_rand::random;
|
||||
use servo_url::ServoUrl;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::time::{Duration, Instant};
|
||||
use style::thread_state::{self, ThreadState};
|
||||
|
||||
|
@ -259,7 +259,7 @@ impl ServiceWorkerGlobalScope {
|
|||
devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl,
|
||||
) {
|
||||
) -> JoinHandle<()> {
|
||||
let ScopeThings {
|
||||
script_url,
|
||||
init,
|
||||
|
@ -361,7 +361,7 @@ impl ServiceWorkerGlobalScope {
|
|||
);
|
||||
scope.clear_js_runtime();
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
.expect("Thread spawning failed")
|
||||
}
|
||||
|
||||
fn handle_mixed_message(&self, msg: MixedMessage) -> bool {
|
||||
|
|
|
@ -87,7 +87,6 @@ impl Worker {
|
|||
let (sender, receiver) = unbounded();
|
||||
let closing = Arc::new(AtomicBool::new(false));
|
||||
let worker = Worker::new(global, sender.clone(), closing.clone());
|
||||
global.track_worker(closing.clone());
|
||||
let worker_ref = Trusted::new(&*worker);
|
||||
|
||||
let worker_load_origin = WorkerScriptLoadOrigin {
|
||||
|
@ -125,7 +124,7 @@ impl Worker {
|
|||
|
||||
let init = prepare_workerscope_init(global, Some(devtools_sender), Some(worker_id));
|
||||
|
||||
DedicatedWorkerGlobalScope::run_worker_scope(
|
||||
let join_handle = DedicatedWorkerGlobalScope::run_worker_scope(
|
||||
init,
|
||||
worker_url,
|
||||
devtools_receiver,
|
||||
|
@ -136,12 +135,14 @@ impl Worker {
|
|||
worker_load_origin,
|
||||
String::from(&*worker_options.name),
|
||||
worker_options.type_,
|
||||
closing,
|
||||
closing.clone(),
|
||||
global.image_cache(),
|
||||
browsing_context,
|
||||
global.wgpu_id_hub(),
|
||||
);
|
||||
|
||||
global.track_worker(closing, join_handle);
|
||||
|
||||
Ok(worker)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue