mirror of
https://github.com/servo/servo.git
synced 2025-06-12 10:24:43 +00:00
This is a simplification of the internal `TaskQueue` API that moves the `TaskManager` to the `GlobalScope` itself. In addition, the handling of cancellers is moved to the `TaskManager` as well. This means that no arguments other than the `task` are necessary for queueing tasks, which makes the API a lot easier to use and cleaner. `TaskSource` now also keeps a copy of the canceller with it, so that they always know the proper way to cancel any tasks queued on them. There is one complication here. The event loop `sender` for dedicated workers is constantly changing as it is set to `None` when not handling messages. This is because this sender keeps a handle to the main thread's `Worker` object, preventing garbage collection while any messages are still in flight or being handled. This change allows setting the `sender` on the `TaskManager` to `None` to allow proper garbabge collection. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
117 lines
2.7 KiB
Rust
117 lines
2.7 KiB
Rust
/* 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 https://mozilla.org/MPL/2.0/. */
|
|
|
|
//! Machinery for [tasks](https://html.spec.whatwg.org/multipage/#concept-task).
|
|
|
|
use std::fmt;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::Arc;
|
|
|
|
macro_rules! task {
|
|
($name:ident: move || $body:tt) => {{
|
|
#[allow(non_camel_case_types)]
|
|
struct $name<F>(F);
|
|
impl<F> crate::task::TaskOnce for $name<F>
|
|
where
|
|
F: ::std::ops::FnOnce() + Send,
|
|
{
|
|
fn name(&self) -> &'static str {
|
|
stringify!($name)
|
|
}
|
|
|
|
fn run_once(self) {
|
|
(self.0)();
|
|
}
|
|
}
|
|
$name(move || $body)
|
|
}};
|
|
}
|
|
|
|
/// A task that can be run. The name method is for profiling purposes.
|
|
pub trait TaskOnce: Send {
|
|
#[allow(unsafe_code)]
|
|
fn name(&self) -> &'static str {
|
|
::std::any::type_name::<Self>()
|
|
}
|
|
|
|
fn run_once(self);
|
|
}
|
|
|
|
/// A boxed version of `TaskOnce`.
|
|
pub trait TaskBox: Send {
|
|
fn name(&self) -> &'static str;
|
|
|
|
fn run_box(self: Box<Self>);
|
|
}
|
|
|
|
impl<T> TaskBox for T
|
|
where
|
|
T: TaskOnce,
|
|
{
|
|
fn name(&self) -> &'static str {
|
|
TaskOnce::name(self)
|
|
}
|
|
|
|
fn run_box(self: Box<Self>) {
|
|
self.run_once()
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for dyn TaskBox {
|
|
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.
|
|
#[derive(Clone, Default, JSTraceable, MallocSizeOf)]
|
|
pub struct TaskCanceller {
|
|
#[ignore_malloc_size_of = "This is difficult, because only one of them should be measured"]
|
|
pub cancelled: Arc<AtomicBool>,
|
|
}
|
|
|
|
impl TaskCanceller {
|
|
/// Returns a wrapped `task` that will be cancelled if the `TaskCanceller` says so.
|
|
pub(crate) fn wrap_task<T>(&self, task: T) -> impl TaskOnce
|
|
where
|
|
T: TaskOnce,
|
|
{
|
|
CancellableTask {
|
|
cancelled: self.cancelled.clone(),
|
|
inner: task,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A task that can be cancelled by toggling a shared flag.
|
|
pub struct CancellableTask<T: TaskOnce> {
|
|
cancelled: Arc<AtomicBool>,
|
|
inner: T,
|
|
}
|
|
|
|
impl<T> CancellableTask<T>
|
|
where
|
|
T: TaskOnce,
|
|
{
|
|
fn is_cancelled(&self) -> bool {
|
|
self.cancelled.load(Ordering::SeqCst)
|
|
}
|
|
}
|
|
|
|
impl<T> TaskOnce for CancellableTask<T>
|
|
where
|
|
T: TaskOnce,
|
|
{
|
|
fn name(&self) -> &'static str {
|
|
self.inner.name()
|
|
}
|
|
|
|
fn run_once(self) {
|
|
if !self.is_cancelled() {
|
|
self.inner.run_once()
|
|
}
|
|
}
|
|
}
|