|
|
use std::{ |
|
|
any::Any, |
|
|
future::Future, |
|
|
hash::BuildHasherDefault, |
|
|
mem::take, |
|
|
pin::Pin, |
|
|
sync::{ |
|
|
Arc, Mutex, RwLock, Weak, |
|
|
atomic::{AtomicBool, AtomicUsize, Ordering}, |
|
|
}, |
|
|
thread, |
|
|
time::{Duration, Instant}, |
|
|
}; |
|
|
|
|
|
use anyhow::{Result, anyhow}; |
|
|
use auto_hash_map::AutoMap; |
|
|
use rustc_hash::FxHasher; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
use smallvec::SmallVec; |
|
|
use tokio::{runtime::Handle, select, sync::mpsc::Receiver, task_local}; |
|
|
use tokio_util::task::TaskTracker; |
|
|
use tracing::{Instrument, Level, Span, info_span, instrument, trace_span}; |
|
|
use turbo_tasks_malloc::TurboMalloc; |
|
|
|
|
|
use crate::{ |
|
|
Completion, InvalidationReason, InvalidationReasonSet, OutputContent, ReadCellOptions, |
|
|
ResolvedVc, SharedReference, TaskId, TaskIdSet, TraitMethod, ValueTypeId, Vc, VcRead, |
|
|
VcValueTrait, VcValueType, |
|
|
backend::{ |
|
|
Backend, CachedTaskType, CellContent, TaskCollectiblesMap, TaskExecutionSpec, |
|
|
TransientTaskType, TurboTasksExecutionError, TypedCellContent, |
|
|
}, |
|
|
capture_future::{self, CaptureFuture}, |
|
|
event::{Event, EventListener}, |
|
|
id::{BackendJobId, ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId}, |
|
|
id_factory::IdFactoryWithReuse, |
|
|
macro_helpers::NativeFunction, |
|
|
magic_any::MagicAny, |
|
|
message_queue::{CompilationEvent, CompilationEventQueue}, |
|
|
raw_vc::{CellId, RawVc}, |
|
|
registry, |
|
|
serialization_invalidation::SerializationInvalidator, |
|
|
task::local_task::{LocalTask, LocalTaskSpec, LocalTaskType}, |
|
|
task_statistics::TaskStatisticsApi, |
|
|
trace::TraceRawVcs, |
|
|
util::{IdFactory, StaticOrArc}, |
|
|
vc::ReadVcFuture, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
pub trait TurboTasksCallApi: Sync + Send { |
|
|
|
|
|
|
|
|
fn dynamic_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc; |
|
|
|
|
|
|
|
|
fn native_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc; |
|
|
|
|
|
|
|
|
fn trait_call( |
|
|
&self, |
|
|
trait_method: &'static TraitMethod, |
|
|
this: RawVc, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc; |
|
|
|
|
|
fn run_once( |
|
|
&self, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId; |
|
|
fn run_once_with_reason( |
|
|
&self, |
|
|
reason: StaticOrArc<dyn InvalidationReason>, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId; |
|
|
fn run_once_process( |
|
|
&self, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { |
|
|
fn invalidate(&self, task: TaskId); |
|
|
fn invalidate_with_reason(&self, task: TaskId, reason: StaticOrArc<dyn InvalidationReason>); |
|
|
|
|
|
fn invalidate_serialization(&self, task: TaskId); |
|
|
|
|
|
|
|
|
|
|
|
fn notify_scheduled_tasks(&self); |
|
|
|
|
|
fn try_read_task_output( |
|
|
&self, |
|
|
task: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<Result<RawVc, EventListener>>; |
|
|
|
|
|
|
|
|
|
|
|
fn try_read_task_output_untracked( |
|
|
&self, |
|
|
task: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<Result<RawVc, EventListener>>; |
|
|
|
|
|
fn try_read_task_cell( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<Result<TypedCellContent, EventListener>>; |
|
|
|
|
|
|
|
|
|
|
|
fn try_read_task_cell_untracked( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<Result<TypedCellContent, EventListener>>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn try_read_local_output( |
|
|
&self, |
|
|
execution_id: ExecutionId, |
|
|
local_task_id: LocalTaskId, |
|
|
) -> Result<Result<RawVc, EventListener>>; |
|
|
|
|
|
fn read_task_collectibles(&self, task: TaskId, trait_id: TraitTypeId) -> TaskCollectiblesMap; |
|
|
|
|
|
fn emit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc); |
|
|
fn unemit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc, count: u32); |
|
|
fn unemit_collectibles(&self, trait_type: TraitTypeId, collectibles: &TaskCollectiblesMap); |
|
|
|
|
|
|
|
|
|
|
|
fn try_read_own_task_cell_untracked( |
|
|
&self, |
|
|
current_task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<TypedCellContent>; |
|
|
|
|
|
fn read_own_task_cell( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<TypedCellContent>; |
|
|
fn update_own_task_cell(&self, task: TaskId, index: CellId, content: CellContent); |
|
|
fn mark_own_task_as_finished(&self, task: TaskId); |
|
|
fn set_own_task_aggregation_number(&self, task: TaskId, aggregation_number: u32); |
|
|
fn mark_own_task_as_session_dependent(&self, task: TaskId); |
|
|
|
|
|
fn connect_task(&self, task: TaskId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn detached_for_testing( |
|
|
&self, |
|
|
f: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>; |
|
|
|
|
|
fn task_statistics(&self) -> &TaskStatisticsApi; |
|
|
|
|
|
fn stop_and_wait(&self) -> Pin<Box<dyn Future<Output = ()> + Send>>; |
|
|
|
|
|
fn subscribe_to_compilation_events( |
|
|
&self, |
|
|
event_types: Option<Vec<String>>, |
|
|
) -> Receiver<Arc<dyn CompilationEvent>>; |
|
|
fn send_compilation_event(&self, event: Arc<dyn CompilationEvent>); |
|
|
} |
|
|
|
|
|
|
|
|
pub struct Unused<T> { |
|
|
inner: T, |
|
|
} |
|
|
|
|
|
impl<T> Unused<T> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn new_unchecked(inner: T) -> Self { |
|
|
Self { inner } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn get_unchecked(&self) -> &T { |
|
|
&self.inner |
|
|
} |
|
|
|
|
|
|
|
|
pub fn into(self) -> T { |
|
|
self.inner |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync + Send { |
|
|
fn pin(&self) -> Arc<dyn TurboTasksBackendApi<B>>; |
|
|
|
|
|
fn get_fresh_persistent_task_id(&self) -> Unused<TaskId>; |
|
|
fn get_fresh_transient_task_id(&self) -> Unused<TaskId>; |
|
|
|
|
|
|
|
|
|
|
|
unsafe fn reuse_persistent_task_id(&self, id: Unused<TaskId>); |
|
|
|
|
|
|
|
|
|
|
|
unsafe fn reuse_transient_task_id(&self, id: Unused<TaskId>); |
|
|
|
|
|
fn schedule(&self, task: TaskId); |
|
|
fn schedule_backend_background_job(&self, id: BackendJobId); |
|
|
fn schedule_backend_foreground_job(&self, id: BackendJobId); |
|
|
|
|
|
fn try_foreground_done(&self) -> Result<(), EventListener>; |
|
|
fn wait_foreground_done_excluding_own<'a>( |
|
|
&'a self, |
|
|
) -> Option<Pin<Box<dyn Future<Output = ()> + Send + 'a>>>; |
|
|
|
|
|
|
|
|
|
|
|
fn schedule_notify_tasks(&self, tasks: &[TaskId]); |
|
|
|
|
|
|
|
|
|
|
|
fn schedule_notify_tasks_set(&self, tasks: &TaskIdSet); |
|
|
|
|
|
|
|
|
fn program_duration_until(&self, instant: Instant) -> Duration; |
|
|
|
|
|
|
|
|
|
|
|
fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState)); |
|
|
|
|
|
|
|
|
|
|
|
fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState)); |
|
|
|
|
|
|
|
|
fn is_idle(&self) -> bool; |
|
|
|
|
|
|
|
|
fn backend(&self) -> &B; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub trait TurboTasksBackendApiExt<B: Backend + 'static>: TurboTasksBackendApi<B> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn read_task_state<T>(&self, func: impl FnOnce(&B::TaskState) -> T) -> T { |
|
|
let mut func = Some(func); |
|
|
let mut out = None; |
|
|
self.read_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts))); |
|
|
out.expect("read_task_state_dyn must call `func`") |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn write_task_state<T>(&self, func: impl FnOnce(&mut B::TaskState) -> T) -> T { |
|
|
let mut func = Some(func); |
|
|
let mut out = None; |
|
|
self.write_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts))); |
|
|
out.expect("write_task_state_dyn must call `func`") |
|
|
} |
|
|
} |
|
|
|
|
|
impl<TT, B> TurboTasksBackendApiExt<B> for TT |
|
|
where |
|
|
TT: TurboTasksBackendApi<B> + ?Sized, |
|
|
B: Backend + 'static, |
|
|
{ |
|
|
} |
|
|
|
|
|
#[allow(clippy::manual_non_exhaustive)] |
|
|
pub struct UpdateInfo { |
|
|
pub duration: Duration, |
|
|
pub tasks: usize, |
|
|
pub reasons: InvalidationReasonSet, |
|
|
#[allow(dead_code)] |
|
|
placeholder_for_future_fields: (), |
|
|
} |
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] |
|
|
pub enum TaskPersistence { |
|
|
|
|
|
Persistent, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Transient, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local, |
|
|
} |
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
|
pub enum ReadConsistency { |
|
|
|
|
|
|
|
|
Eventual, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Strong, |
|
|
} |
|
|
|
|
|
pub struct TurboTasks<B: Backend + 'static> { |
|
|
this: Weak<Self>, |
|
|
backend: B, |
|
|
task_id_factory: IdFactoryWithReuse<TaskId>, |
|
|
transient_task_id_factory: IdFactoryWithReuse<TaskId>, |
|
|
execution_id_factory: IdFactory<ExecutionId>, |
|
|
stopped: AtomicBool, |
|
|
currently_scheduled_tasks: AtomicUsize, |
|
|
currently_scheduled_foreground_jobs: AtomicUsize, |
|
|
currently_scheduled_background_jobs: AtomicUsize, |
|
|
scheduled_tasks: AtomicUsize, |
|
|
start: Mutex<Option<Instant>>, |
|
|
aggregated_update: Mutex<(Option<(Duration, usize)>, InvalidationReasonSet)>, |
|
|
event: Event, |
|
|
event_start: Event, |
|
|
event_foreground: Event, |
|
|
event_background: Event, |
|
|
program_start: Instant, |
|
|
compilation_events: CompilationEventQueue, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct CurrentTaskState { |
|
|
task_id: TaskId, |
|
|
execution_id: ExecutionId, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tasks_to_notify: SmallVec<[TaskId; 4]>, |
|
|
|
|
|
|
|
|
stateful: bool, |
|
|
|
|
|
|
|
|
has_invalidator: bool, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cell_counters: Option<AutoMap<ValueTypeId, u32, BuildHasherDefault<FxHasher>, 8>>, |
|
|
|
|
|
|
|
|
local_tasks: Vec<LocalTask>, |
|
|
|
|
|
|
|
|
|
|
|
local_task_tracker: TaskTracker, |
|
|
|
|
|
backend_state: Box<dyn Any + Send + Sync>, |
|
|
} |
|
|
|
|
|
impl CurrentTaskState { |
|
|
fn new( |
|
|
task_id: TaskId, |
|
|
execution_id: ExecutionId, |
|
|
backend_state: Box<dyn Any + Send + Sync>, |
|
|
) -> Self { |
|
|
Self { |
|
|
task_id, |
|
|
execution_id, |
|
|
tasks_to_notify: SmallVec::new(), |
|
|
stateful: false, |
|
|
has_invalidator: false, |
|
|
cell_counters: Some(AutoMap::default()), |
|
|
local_tasks: Vec::new(), |
|
|
local_task_tracker: TaskTracker::new(), |
|
|
backend_state, |
|
|
} |
|
|
} |
|
|
|
|
|
fn assert_execution_id(&self, expected_execution_id: ExecutionId) { |
|
|
if self.execution_id != expected_execution_id { |
|
|
panic!( |
|
|
"Local tasks can only be scheduled/awaited within the same execution of the \ |
|
|
parent task that created them" |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
fn create_local_task(&mut self, local_task: LocalTask) -> LocalTaskId { |
|
|
self.local_tasks.push(local_task); |
|
|
|
|
|
if cfg!(debug_assertions) { |
|
|
LocalTaskId::try_from(u32::try_from(self.local_tasks.len()).unwrap()).unwrap() |
|
|
} else { |
|
|
unsafe { LocalTaskId::new_unchecked(self.local_tasks.len() as u32) } |
|
|
} |
|
|
} |
|
|
|
|
|
fn get_local_task(&self, local_task_id: LocalTaskId) -> &LocalTask { |
|
|
|
|
|
&self.local_tasks[(*local_task_id as usize) - 1] |
|
|
} |
|
|
|
|
|
fn get_mut_local_task(&mut self, local_task_id: LocalTaskId) -> &mut LocalTask { |
|
|
&mut self.local_tasks[(*local_task_id as usize) - 1] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
task_local! { |
|
|
|
|
|
static TURBO_TASKS: Arc<dyn TurboTasksApi>; |
|
|
|
|
|
static CURRENT_TASK_STATE: Arc<RwLock<CurrentTaskState>>; |
|
|
} |
|
|
|
|
|
impl<B: Backend + 'static> TurboTasks<B> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new(backend: B) -> Arc<Self> { |
|
|
let task_id_factory = IdFactoryWithReuse::new( |
|
|
TaskId::MIN, |
|
|
TaskId::try_from(TRANSIENT_TASK_BIT - 1).unwrap(), |
|
|
); |
|
|
let transient_task_id_factory = |
|
|
IdFactoryWithReuse::new(TaskId::try_from(TRANSIENT_TASK_BIT).unwrap(), TaskId::MAX); |
|
|
let execution_id_factory = IdFactory::new(ExecutionId::MIN, ExecutionId::MAX); |
|
|
let this = Arc::new_cyclic(|this| Self { |
|
|
this: this.clone(), |
|
|
backend, |
|
|
task_id_factory, |
|
|
transient_task_id_factory, |
|
|
execution_id_factory, |
|
|
stopped: AtomicBool::new(false), |
|
|
currently_scheduled_tasks: AtomicUsize::new(0), |
|
|
currently_scheduled_background_jobs: AtomicUsize::new(0), |
|
|
currently_scheduled_foreground_jobs: AtomicUsize::new(0), |
|
|
scheduled_tasks: AtomicUsize::new(0), |
|
|
start: Default::default(), |
|
|
aggregated_update: Default::default(), |
|
|
event: Event::new(|| || "TurboTasks::event".to_string()), |
|
|
event_start: Event::new(|| || "TurboTasks::event_start".to_string()), |
|
|
event_foreground: Event::new(|| || "TurboTasks::event_foreground".to_string()), |
|
|
event_background: Event::new(|| || "TurboTasks::event_background".to_string()), |
|
|
program_start: Instant::now(), |
|
|
compilation_events: CompilationEventQueue::default(), |
|
|
}); |
|
|
this.backend.startup(&*this); |
|
|
this |
|
|
} |
|
|
|
|
|
pub fn pin(&self) -> Arc<Self> { |
|
|
self.this.upgrade().unwrap() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn spawn_root_task<T, F, Fut>(&self, functor: F) -> TaskId |
|
|
where |
|
|
T: ?Sized, |
|
|
F: Fn() -> Fut + Send + Sync + Clone + 'static, |
|
|
Fut: Future<Output = Result<Vc<T>>> + Send, |
|
|
{ |
|
|
let id = self.backend.create_transient_task( |
|
|
TransientTaskType::Root(Box::new(move || { |
|
|
let functor = functor.clone(); |
|
|
Box::pin(async move { |
|
|
let raw_vc = functor().await?.node; |
|
|
raw_vc.to_non_local().await |
|
|
}) |
|
|
})), |
|
|
self, |
|
|
); |
|
|
self.schedule(id); |
|
|
id |
|
|
} |
|
|
|
|
|
pub fn dispose_root_task(&self, task_id: TaskId) { |
|
|
self.backend.dispose_root_task(task_id, self); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[track_caller] |
|
|
pub fn spawn_once_task<T, Fut>(&self, future: Fut) -> TaskId |
|
|
where |
|
|
T: ?Sized, |
|
|
Fut: Future<Output = Result<Vc<T>>> + Send + 'static, |
|
|
{ |
|
|
let id = self.backend.create_transient_task( |
|
|
TransientTaskType::Once(Box::pin(async move { |
|
|
let raw_vc = future.await?.node; |
|
|
raw_vc.to_non_local().await |
|
|
})), |
|
|
self, |
|
|
); |
|
|
self.schedule(id); |
|
|
id |
|
|
} |
|
|
|
|
|
pub async fn run_once<T: TraceRawVcs + Send + 'static>( |
|
|
&self, |
|
|
future: impl Future<Output = Result<T>> + Send + 'static, |
|
|
) -> Result<T> { |
|
|
let (tx, rx) = tokio::sync::oneshot::channel(); |
|
|
let task_id = self.spawn_once_task(async move { |
|
|
let result = future.await?; |
|
|
tx.send(result) |
|
|
.map_err(|_| anyhow!("unable to send result"))?; |
|
|
Ok(Completion::new()) |
|
|
}); |
|
|
|
|
|
|
|
|
let raw_result = |
|
|
read_task_output_untracked(self, task_id, ReadConsistency::Eventual).await?; |
|
|
turbo_tasks_future_scope( |
|
|
self.pin(), |
|
|
ReadVcFuture::<Completion>::from(raw_result.into_read().untracked()), |
|
|
) |
|
|
.await?; |
|
|
|
|
|
Ok(rx.await?) |
|
|
} |
|
|
|
|
|
pub(crate) fn native_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
match persistence { |
|
|
TaskPersistence::Local => { |
|
|
let task_type = LocalTaskSpec { |
|
|
task_type: LocalTaskType::Native { native_fn }, |
|
|
this, |
|
|
arg, |
|
|
}; |
|
|
self.schedule_local_task(task_type, persistence) |
|
|
} |
|
|
TaskPersistence::Transient => { |
|
|
let task_type = CachedTaskType { |
|
|
native_fn, |
|
|
this, |
|
|
arg, |
|
|
}; |
|
|
|
|
|
RawVc::TaskOutput(self.backend.get_or_create_transient_task( |
|
|
task_type, |
|
|
current_task("turbo_function calls"), |
|
|
self, |
|
|
)) |
|
|
} |
|
|
TaskPersistence::Persistent => { |
|
|
let task_type = CachedTaskType { |
|
|
native_fn, |
|
|
this, |
|
|
arg, |
|
|
}; |
|
|
|
|
|
RawVc::TaskOutput(self.backend.get_or_create_persistent_task( |
|
|
task_type, |
|
|
current_task("turbo_function calls"), |
|
|
self, |
|
|
)) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn dynamic_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
if this.is_none_or(|this| this.is_resolved()) && native_fn.arg_meta.is_resolved(&*arg) { |
|
|
return self.native_call(native_fn, this, arg, persistence); |
|
|
} |
|
|
let task_type = LocalTaskSpec { |
|
|
task_type: LocalTaskType::ResolveNative { native_fn }, |
|
|
this, |
|
|
arg, |
|
|
}; |
|
|
self.schedule_local_task(task_type, persistence) |
|
|
} |
|
|
|
|
|
pub fn trait_call( |
|
|
&self, |
|
|
trait_method: &'static TraitMethod, |
|
|
this: RawVc, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
|
|
|
|
|
|
|
|
|
if let RawVc::TaskCell(_, CellId { type_id, .. }) = this { |
|
|
match registry::get_value_type(type_id).get_trait_method(trait_method) { |
|
|
Some(native_fn) => { |
|
|
let arg = native_fn.arg_meta.filter_owned(arg); |
|
|
return self.dynamic_call(native_fn, Some(this), arg, persistence); |
|
|
} |
|
|
None => { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let task_type = LocalTaskSpec { |
|
|
task_type: LocalTaskType::ResolveTrait { trait_method }, |
|
|
this: Some(this), |
|
|
arg, |
|
|
}; |
|
|
|
|
|
self.schedule_local_task(task_type, persistence) |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
pub(crate) fn schedule(&self, task_id: TaskId) { |
|
|
self.begin_primary_job(); |
|
|
self.scheduled_tasks.fetch_add(1, Ordering::AcqRel); |
|
|
|
|
|
let this = self.pin(); |
|
|
let future = async move { |
|
|
let mut schedule_again = true; |
|
|
while schedule_again { |
|
|
let backend_state = this.backend.new_task_state(task_id); |
|
|
|
|
|
let execution_id = this.execution_id_factory.wrapping_get(); |
|
|
let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new( |
|
|
task_id, |
|
|
execution_id, |
|
|
Box::new(backend_state), |
|
|
))); |
|
|
let single_execution_future = async { |
|
|
if this.stopped.load(Ordering::Acquire) { |
|
|
this.backend.task_execution_canceled(task_id, &*this); |
|
|
return false; |
|
|
} |
|
|
|
|
|
let Some(TaskExecutionSpec { future, span }) = |
|
|
this.backend.try_start_task_execution(task_id, &*this) |
|
|
else { |
|
|
return false; |
|
|
}; |
|
|
|
|
|
async { |
|
|
let (result, duration, memory_usage) = CaptureFuture::new(future).await; |
|
|
|
|
|
|
|
|
let ltt = CURRENT_TASK_STATE |
|
|
.with(|ts| ts.read().unwrap().local_task_tracker.clone()); |
|
|
ltt.close(); |
|
|
ltt.wait().await; |
|
|
|
|
|
let result = match result { |
|
|
Ok(Ok(raw_vc)) => Ok(raw_vc), |
|
|
Ok(Err(err)) => Err(err.into()), |
|
|
Err(err) => Err(TurboTasksExecutionError::Panic(Arc::new(err))), |
|
|
}; |
|
|
|
|
|
this.backend.task_execution_result(task_id, result, &*this); |
|
|
let FinishedTaskState { |
|
|
stateful, |
|
|
has_invalidator, |
|
|
} = this.finish_current_task_state(); |
|
|
let cell_counters = CURRENT_TASK_STATE |
|
|
.with(|ts| ts.write().unwrap().cell_counters.take().unwrap()); |
|
|
let schedule_again = this.backend.task_execution_completed( |
|
|
task_id, |
|
|
duration, |
|
|
memory_usage, |
|
|
&cell_counters, |
|
|
stateful, |
|
|
has_invalidator, |
|
|
&*this, |
|
|
); |
|
|
|
|
|
this.notify_scheduled_tasks(); |
|
|
schedule_again |
|
|
} |
|
|
.instrument(span) |
|
|
.await |
|
|
}; |
|
|
schedule_again = CURRENT_TASK_STATE |
|
|
.scope(current_task_state, single_execution_future) |
|
|
.await; |
|
|
} |
|
|
this.finish_primary_job(); |
|
|
anyhow::Ok(()) |
|
|
}; |
|
|
|
|
|
let future = TURBO_TASKS.scope(self.pin(), future).in_current_span(); |
|
|
|
|
|
#[cfg(feature = "tokio_tracing")] |
|
|
{ |
|
|
let description = self.backend.get_task_description(task_id); |
|
|
tokio::task::Builder::new() |
|
|
.name(&description) |
|
|
.spawn(future) |
|
|
.unwrap(); |
|
|
} |
|
|
#[cfg(not(feature = "tokio_tracing"))] |
|
|
tokio::task::spawn(future); |
|
|
} |
|
|
|
|
|
fn schedule_local_task( |
|
|
&self, |
|
|
ty: LocalTaskSpec, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
let task_type = ty.task_type; |
|
|
let (global_task_state, parent_task_id, execution_id, local_task_id) = CURRENT_TASK_STATE |
|
|
.with(|gts| { |
|
|
let mut gts_write = gts.write().unwrap(); |
|
|
let local_task_id = gts_write.create_local_task(LocalTask::Scheduled { |
|
|
done_event: Event::new(move || { |
|
|
move || format!("LocalTask({task_type})::done_event") |
|
|
}), |
|
|
}); |
|
|
( |
|
|
Arc::clone(gts), |
|
|
gts_write.task_id, |
|
|
gts_write.execution_id, |
|
|
local_task_id, |
|
|
) |
|
|
}); |
|
|
|
|
|
#[cfg(feature = "tokio_tracing")] |
|
|
let description = format!( |
|
|
"[local] (parent: {}) {}", |
|
|
self.backend.get_task_description(parent_task_id), |
|
|
ty, |
|
|
); |
|
|
#[cfg(not(feature = "tokio_tracing"))] |
|
|
let _ = parent_task_id; |
|
|
|
|
|
let this = self.pin(); |
|
|
let future = async move { |
|
|
let TaskExecutionSpec { future, span } = |
|
|
crate::task::local_task::get_local_task_execution_spec(&*this, &ty, persistence); |
|
|
async move { |
|
|
let (result, _duration, _memory_usage) = CaptureFuture::new(future).await; |
|
|
|
|
|
let result = match result { |
|
|
Ok(Ok(raw_vc)) => Ok(raw_vc), |
|
|
Ok(Err(err)) => Err(err.into()), |
|
|
Err(err) => Err(TurboTasksExecutionError::Panic(Arc::new(err))), |
|
|
}; |
|
|
|
|
|
let local_task = LocalTask::Done { |
|
|
output: match result { |
|
|
Ok(raw_vc) => OutputContent::Link(raw_vc), |
|
|
Err(err) => OutputContent::Error(err.with_task_context(task_type)), |
|
|
}, |
|
|
}; |
|
|
|
|
|
let done_event = CURRENT_TASK_STATE.with(move |gts| { |
|
|
let mut gts_write = gts.write().unwrap(); |
|
|
let scheduled_task = |
|
|
std::mem::replace(gts_write.get_mut_local_task(local_task_id), local_task); |
|
|
let LocalTask::Scheduled { done_event } = scheduled_task else { |
|
|
panic!("local task finished, but was not in the scheduled state?"); |
|
|
}; |
|
|
done_event |
|
|
}); |
|
|
done_event.notify(usize::MAX) |
|
|
} |
|
|
.instrument(span) |
|
|
.await |
|
|
}; |
|
|
let future = global_task_state |
|
|
.read() |
|
|
.unwrap() |
|
|
.local_task_tracker |
|
|
.track_future(future); |
|
|
let future = CURRENT_TASK_STATE.scope(global_task_state, future); |
|
|
let future = TURBO_TASKS.scope(self.pin(), future).in_current_span(); |
|
|
|
|
|
#[cfg(feature = "tokio_tracing")] |
|
|
tokio::task::Builder::new() |
|
|
.name(&description) |
|
|
.spawn(future) |
|
|
.unwrap(); |
|
|
#[cfg(not(feature = "tokio_tracing"))] |
|
|
tokio::task::spawn(future); |
|
|
|
|
|
RawVc::LocalOutput(execution_id, local_task_id, persistence) |
|
|
} |
|
|
|
|
|
fn begin_primary_job(&self) { |
|
|
if self |
|
|
.currently_scheduled_tasks |
|
|
.fetch_add(1, Ordering::AcqRel) |
|
|
== 0 |
|
|
{ |
|
|
*self.start.lock().unwrap() = Some(Instant::now()); |
|
|
self.event_start.notify(usize::MAX); |
|
|
self.backend.idle_end(self); |
|
|
} |
|
|
} |
|
|
|
|
|
fn begin_foreground_job(&self) { |
|
|
self.begin_primary_job(); |
|
|
self.currently_scheduled_foreground_jobs |
|
|
.fetch_add(1, Ordering::AcqRel); |
|
|
} |
|
|
|
|
|
fn finish_primary_job(&self) { |
|
|
if self |
|
|
.currently_scheduled_tasks |
|
|
.fetch_sub(1, Ordering::AcqRel) |
|
|
== 1 |
|
|
{ |
|
|
self.backend.idle_start(self); |
|
|
|
|
|
|
|
|
let total = self.scheduled_tasks.load(Ordering::Acquire); |
|
|
self.scheduled_tasks.store(0, Ordering::Release); |
|
|
if let Some(start) = *self.start.lock().unwrap() { |
|
|
let (update, _) = &mut *self.aggregated_update.lock().unwrap(); |
|
|
if let Some(update) = update.as_mut() { |
|
|
update.0 += start.elapsed(); |
|
|
update.1 += total; |
|
|
} else { |
|
|
*update = Some((start.elapsed(), total)); |
|
|
} |
|
|
} |
|
|
self.event.notify(usize::MAX); |
|
|
} |
|
|
} |
|
|
|
|
|
fn finish_foreground_job(&self) { |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.fetch_sub(1, Ordering::AcqRel) |
|
|
== 1 |
|
|
{ |
|
|
self.event_foreground.notify(usize::MAX); |
|
|
} |
|
|
self.finish_primary_job(); |
|
|
} |
|
|
|
|
|
pub async fn wait_foreground_done(&self) { |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.load(Ordering::Acquire) |
|
|
== 0 |
|
|
{ |
|
|
return; |
|
|
} |
|
|
let listener = self.event_foreground.listen(); |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.load(Ordering::Acquire) |
|
|
== 0 |
|
|
{ |
|
|
return; |
|
|
} |
|
|
listener |
|
|
.instrument(trace_span!("wait_foreground_done")) |
|
|
.await; |
|
|
} |
|
|
|
|
|
pub fn get_in_progress_count(&self) -> usize { |
|
|
self.currently_scheduled_tasks.load(Ordering::Acquire) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn wait_task_completion( |
|
|
&self, |
|
|
id: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<()> { |
|
|
|
|
|
read_task_output_untracked(self, id, consistency).await?; |
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_or_wait_aggregated_update_info(&self, aggregation: Duration) -> UpdateInfo { |
|
|
self.aggregated_update_info(aggregation, Duration::MAX) |
|
|
.await |
|
|
.unwrap() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn aggregated_update_info( |
|
|
&self, |
|
|
aggregation: Duration, |
|
|
timeout: Duration, |
|
|
) -> Option<UpdateInfo> { |
|
|
let listener = self |
|
|
.event |
|
|
.listen_with_note(|| || "wait for update info".to_string()); |
|
|
let wait_for_finish = { |
|
|
let (update, reason_set) = &mut *self.aggregated_update.lock().unwrap(); |
|
|
if aggregation.is_zero() { |
|
|
if let Some((duration, tasks)) = update.take() { |
|
|
return Some(UpdateInfo { |
|
|
duration, |
|
|
tasks, |
|
|
reasons: take(reason_set), |
|
|
placeholder_for_future_fields: (), |
|
|
}); |
|
|
} else { |
|
|
true |
|
|
} |
|
|
} else { |
|
|
update.is_none() |
|
|
} |
|
|
}; |
|
|
if wait_for_finish { |
|
|
if timeout == Duration::MAX { |
|
|
|
|
|
listener.await; |
|
|
} else { |
|
|
|
|
|
let start_listener = self |
|
|
.event_start |
|
|
.listen_with_note(|| || "wait for update info".to_string()); |
|
|
if self.currently_scheduled_tasks.load(Ordering::Acquire) == 0 { |
|
|
start_listener.await; |
|
|
} else { |
|
|
drop(start_listener); |
|
|
} |
|
|
if timeout.is_zero() || tokio::time::timeout(timeout, listener).await.is_err() { |
|
|
|
|
|
return None; |
|
|
} |
|
|
} |
|
|
} |
|
|
if !aggregation.is_zero() { |
|
|
loop { |
|
|
select! { |
|
|
() = tokio::time::sleep(aggregation) => { |
|
|
break; |
|
|
} |
|
|
() = self.event.listen_with_note(|| || "wait for update info".to_string()) => { |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
let (update, reason_set) = &mut *self.aggregated_update.lock().unwrap(); |
|
|
if let Some((duration, tasks)) = update.take() { |
|
|
Some(UpdateInfo { |
|
|
duration, |
|
|
tasks, |
|
|
reasons: take(reason_set), |
|
|
placeholder_for_future_fields: (), |
|
|
}) |
|
|
} else { |
|
|
panic!("aggregated_update_info must not called concurrently") |
|
|
} |
|
|
} |
|
|
|
|
|
pub async fn wait_background_done(&self) { |
|
|
let listener = self.event_background.listen(); |
|
|
if self |
|
|
.currently_scheduled_background_jobs |
|
|
.load(Ordering::Acquire) |
|
|
!= 0 |
|
|
{ |
|
|
listener.await; |
|
|
} |
|
|
} |
|
|
|
|
|
pub async fn stop_and_wait(&self) { |
|
|
self.backend.stopping(self); |
|
|
self.stopped.store(true, Ordering::Release); |
|
|
{ |
|
|
let listener = self |
|
|
.event |
|
|
.listen_with_note(|| || "wait for stop".to_string()); |
|
|
if self.currently_scheduled_tasks.load(Ordering::Acquire) != 0 { |
|
|
listener.await; |
|
|
} |
|
|
} |
|
|
{ |
|
|
let listener = self.event_background.listen(); |
|
|
if self |
|
|
.currently_scheduled_background_jobs |
|
|
.load(Ordering::Acquire) |
|
|
!= 0 |
|
|
{ |
|
|
listener.await; |
|
|
} |
|
|
} |
|
|
self.backend.stop(self); |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
pub(crate) fn schedule_background_job< |
|
|
T: FnOnce(Arc<TurboTasks<B>>) -> F + Send + 'static, |
|
|
F: Future<Output = ()> + Send + 'static, |
|
|
>( |
|
|
&self, |
|
|
func: T, |
|
|
) { |
|
|
let this = self.pin(); |
|
|
self.currently_scheduled_background_jobs |
|
|
.fetch_add(1, Ordering::AcqRel); |
|
|
tokio::spawn( |
|
|
TURBO_TASKS |
|
|
.scope(this.clone(), async move { |
|
|
while this.currently_scheduled_tasks.load(Ordering::Acquire) != 0 { |
|
|
let listener = this.event.listen_with_note(|| { |
|
|
|| "background job waiting for execution".to_string() |
|
|
}); |
|
|
if this.currently_scheduled_tasks.load(Ordering::Acquire) != 0 { |
|
|
listener.await; |
|
|
} |
|
|
} |
|
|
let this2 = this.clone(); |
|
|
if !this.stopped.load(Ordering::Acquire) { |
|
|
func(this).await; |
|
|
} |
|
|
if this2 |
|
|
.currently_scheduled_background_jobs |
|
|
.fetch_sub(1, Ordering::AcqRel) |
|
|
== 1 |
|
|
{ |
|
|
this2.event_background.notify(usize::MAX); |
|
|
} |
|
|
}) |
|
|
.in_current_span(), |
|
|
); |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
pub(crate) fn schedule_foreground_job< |
|
|
T: FnOnce(Arc<TurboTasks<B>>) -> F + Send + 'static, |
|
|
F: Future<Output = ()> + Send + 'static, |
|
|
>( |
|
|
&self, |
|
|
func: T, |
|
|
) { |
|
|
let this = self.pin(); |
|
|
this.begin_foreground_job(); |
|
|
tokio::spawn( |
|
|
TURBO_TASKS |
|
|
.scope(this.clone(), async move { |
|
|
if !this.stopped.load(Ordering::Acquire) { |
|
|
func(this.clone()).await; |
|
|
} |
|
|
this.finish_foreground_job(); |
|
|
}) |
|
|
.in_current_span(), |
|
|
); |
|
|
} |
|
|
|
|
|
fn finish_current_task_state(&self) -> FinishedTaskState { |
|
|
let (stateful, has_invalidator, tasks) = CURRENT_TASK_STATE.with(|cell| { |
|
|
let CurrentTaskState { |
|
|
tasks_to_notify, |
|
|
stateful, |
|
|
has_invalidator, |
|
|
.. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
(*stateful, *has_invalidator, take(tasks_to_notify)) |
|
|
}); |
|
|
|
|
|
if !tasks.is_empty() { |
|
|
self.backend.invalidate_tasks(&tasks, self); |
|
|
} |
|
|
FinishedTaskState { |
|
|
stateful, |
|
|
has_invalidator, |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn backend(&self) -> &B { |
|
|
&self.backend |
|
|
} |
|
|
} |
|
|
|
|
|
struct FinishedTaskState { |
|
|
|
|
|
stateful: bool, |
|
|
|
|
|
|
|
|
has_invalidator: bool, |
|
|
} |
|
|
|
|
|
impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> { |
|
|
fn dynamic_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
self.dynamic_call(native_fn, this, arg, persistence) |
|
|
} |
|
|
fn native_call( |
|
|
&self, |
|
|
native_fn: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
self.native_call(native_fn, this, arg, persistence) |
|
|
} |
|
|
fn trait_call( |
|
|
&self, |
|
|
trait_method: &'static TraitMethod, |
|
|
this: RawVc, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
self.trait_call(trait_method, this, arg, persistence) |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn run_once( |
|
|
&self, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId { |
|
|
self.spawn_once_task(async move { |
|
|
future.await?; |
|
|
Ok(Completion::new()) |
|
|
}) |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn run_once_with_reason( |
|
|
&self, |
|
|
reason: StaticOrArc<dyn InvalidationReason>, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId { |
|
|
{ |
|
|
let (_, reason_set) = &mut *self.aggregated_update.lock().unwrap(); |
|
|
reason_set.insert(reason); |
|
|
} |
|
|
self.spawn_once_task(async move { |
|
|
future.await?; |
|
|
Ok(Completion::new()) |
|
|
}) |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn run_once_process( |
|
|
&self, |
|
|
future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> TaskId { |
|
|
let this = self.pin(); |
|
|
self.spawn_once_task(async move { |
|
|
this.finish_primary_job(); |
|
|
future.await?; |
|
|
this.begin_primary_job(); |
|
|
Ok(Completion::new()) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> { |
|
|
#[instrument(level = Level::INFO, skip_all, name = "invalidate")] |
|
|
fn invalidate(&self, task: TaskId) { |
|
|
self.backend.invalidate_task(task, self); |
|
|
} |
|
|
|
|
|
#[instrument(level = Level::INFO, skip_all, name = "invalidate", fields(name = display(&reason)))] |
|
|
fn invalidate_with_reason(&self, task: TaskId, reason: StaticOrArc<dyn InvalidationReason>) { |
|
|
{ |
|
|
let (_, reason_set) = &mut *self.aggregated_update.lock().unwrap(); |
|
|
reason_set.insert(reason); |
|
|
} |
|
|
self.backend.invalidate_task(task, self); |
|
|
} |
|
|
|
|
|
fn invalidate_serialization(&self, task: TaskId) { |
|
|
self.backend.invalidate_serialization(task, self); |
|
|
} |
|
|
|
|
|
fn notify_scheduled_tasks(&self) { |
|
|
let _ = CURRENT_TASK_STATE.try_with(|cell| { |
|
|
let tasks = { |
|
|
let CurrentTaskState { |
|
|
tasks_to_notify, .. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
take(tasks_to_notify) |
|
|
}; |
|
|
if tasks.is_empty() { |
|
|
return; |
|
|
} |
|
|
self.backend.invalidate_tasks(&tasks, self); |
|
|
}); |
|
|
} |
|
|
|
|
|
fn try_read_task_output( |
|
|
&self, |
|
|
task: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<Result<RawVc, EventListener>> { |
|
|
self.backend |
|
|
.try_read_task_output(task, current_task("reading Vcs"), consistency, self) |
|
|
} |
|
|
|
|
|
fn try_read_task_output_untracked( |
|
|
&self, |
|
|
task: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<Result<RawVc, EventListener>> { |
|
|
self.backend |
|
|
.try_read_task_output_untracked(task, consistency, self) |
|
|
} |
|
|
|
|
|
fn try_read_task_cell( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<Result<TypedCellContent, EventListener>> { |
|
|
self.backend |
|
|
.try_read_task_cell(task, index, current_task("reading Vcs"), options, self) |
|
|
} |
|
|
|
|
|
fn try_read_task_cell_untracked( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<Result<TypedCellContent, EventListener>> { |
|
|
self.backend |
|
|
.try_read_task_cell_untracked(task, index, options, self) |
|
|
} |
|
|
|
|
|
fn try_read_own_task_cell_untracked( |
|
|
&self, |
|
|
current_task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<TypedCellContent> { |
|
|
self.backend |
|
|
.try_read_own_task_cell_untracked(current_task, index, options, self) |
|
|
} |
|
|
|
|
|
fn try_read_local_output( |
|
|
&self, |
|
|
execution_id: ExecutionId, |
|
|
local_task_id: LocalTaskId, |
|
|
) -> Result<Result<RawVc, EventListener>> { |
|
|
CURRENT_TASK_STATE.with(|gts| { |
|
|
let gts_read = gts.read().unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gts_read.assert_execution_id(execution_id); |
|
|
|
|
|
match gts_read.get_local_task(local_task_id) { |
|
|
LocalTask::Scheduled { done_event } => Ok(Err(done_event.listen())), |
|
|
LocalTask::Done { output } => Ok(Ok(output.as_read_result()?)), |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
fn read_task_collectibles(&self, task: TaskId, trait_id: TraitTypeId) -> TaskCollectiblesMap { |
|
|
self.backend.read_task_collectibles( |
|
|
task, |
|
|
trait_id, |
|
|
current_task("reading collectibles"), |
|
|
self, |
|
|
) |
|
|
} |
|
|
|
|
|
fn emit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc) { |
|
|
self.backend.emit_collectible( |
|
|
trait_type, |
|
|
collectible, |
|
|
current_task("emitting collectible"), |
|
|
self, |
|
|
); |
|
|
} |
|
|
|
|
|
fn unemit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc, count: u32) { |
|
|
self.backend.unemit_collectible( |
|
|
trait_type, |
|
|
collectible, |
|
|
count, |
|
|
current_task("emitting collectible"), |
|
|
self, |
|
|
); |
|
|
} |
|
|
|
|
|
fn unemit_collectibles(&self, trait_type: TraitTypeId, collectibles: &TaskCollectiblesMap) { |
|
|
for (&collectible, &count) in collectibles { |
|
|
if count > 0 { |
|
|
self.backend.unemit_collectible( |
|
|
trait_type, |
|
|
collectible, |
|
|
count as u32, |
|
|
current_task("emitting collectible"), |
|
|
self, |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
fn read_own_task_cell( |
|
|
&self, |
|
|
task: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<TypedCellContent> { |
|
|
|
|
|
self.try_read_own_task_cell_untracked(task, index, options) |
|
|
} |
|
|
|
|
|
fn update_own_task_cell(&self, task: TaskId, index: CellId, content: CellContent) { |
|
|
self.backend.update_task_cell(task, index, content, self); |
|
|
} |
|
|
|
|
|
fn connect_task(&self, task: TaskId) { |
|
|
self.backend |
|
|
.connect_task(task, current_task("connecting task"), self); |
|
|
} |
|
|
|
|
|
fn mark_own_task_as_finished(&self, task: TaskId) { |
|
|
self.backend.mark_own_task_as_finished(task, self); |
|
|
} |
|
|
|
|
|
fn set_own_task_aggregation_number(&self, task: TaskId, aggregation_number: u32) { |
|
|
self.backend |
|
|
.set_own_task_aggregation_number(task, aggregation_number, self); |
|
|
} |
|
|
|
|
|
fn mark_own_task_as_session_dependent(&self, task: TaskId) { |
|
|
self.backend.mark_own_task_as_session_dependent(task, self); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn detached_for_testing( |
|
|
&self, |
|
|
fut: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, |
|
|
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> { |
|
|
|
|
|
|
|
|
let global_task_state = CURRENT_TASK_STATE.with(|ts| ts.clone()); |
|
|
let tracked_fut = { |
|
|
let ts = global_task_state.read().unwrap(); |
|
|
ts.local_task_tracker.track_future(fut) |
|
|
}; |
|
|
Box::pin(TURBO_TASKS.scope( |
|
|
turbo_tasks(), |
|
|
CURRENT_TASK_STATE.scope(global_task_state, tracked_fut), |
|
|
)) |
|
|
} |
|
|
|
|
|
fn task_statistics(&self) -> &TaskStatisticsApi { |
|
|
self.backend.task_statistics() |
|
|
} |
|
|
|
|
|
fn stop_and_wait(&self) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> { |
|
|
let this = self.pin(); |
|
|
Box::pin(async move { |
|
|
this.stop_and_wait().await; |
|
|
}) |
|
|
} |
|
|
|
|
|
fn subscribe_to_compilation_events( |
|
|
&self, |
|
|
event_types: Option<Vec<String>>, |
|
|
) -> Receiver<Arc<dyn CompilationEvent>> { |
|
|
self.compilation_events.subscribe(event_types) |
|
|
} |
|
|
|
|
|
fn send_compilation_event(&self, event: Arc<dyn CompilationEvent>) { |
|
|
if let Err(e) = self.compilation_events.send(event) { |
|
|
tracing::warn!("Failed to send compilation event: {e}"); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> { |
|
|
fn pin(&self) -> Arc<dyn TurboTasksBackendApi<B>> { |
|
|
self.pin() |
|
|
} |
|
|
fn backend(&self) -> &B { |
|
|
&self.backend |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn schedule_backend_background_job(&self, id: BackendJobId) { |
|
|
self.schedule_background_job(move |this| async move { |
|
|
this.backend.run_backend_job(id, &*this).await; |
|
|
}) |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn schedule_backend_foreground_job(&self, id: BackendJobId) { |
|
|
self.schedule_foreground_job(move |this| async move { |
|
|
this.backend.run_backend_job(id, &*this).await; |
|
|
}) |
|
|
} |
|
|
|
|
|
fn try_foreground_done(&self) -> Result<(), EventListener> { |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.load(Ordering::Acquire) |
|
|
== 0 |
|
|
{ |
|
|
return Ok(()); |
|
|
} |
|
|
let listener = self.event_foreground.listen(); |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.load(Ordering::Acquire) |
|
|
== 0 |
|
|
{ |
|
|
return Ok(()); |
|
|
} |
|
|
Err(listener) |
|
|
} |
|
|
|
|
|
fn wait_foreground_done_excluding_own<'a>( |
|
|
&'a self, |
|
|
) -> Option<Pin<Box<dyn Future<Output = ()> + Send + 'a>>> { |
|
|
if self |
|
|
.currently_scheduled_foreground_jobs |
|
|
.load(Ordering::Acquire) |
|
|
== 0 |
|
|
{ |
|
|
return None; |
|
|
} |
|
|
Some(Box::pin(async { |
|
|
self.finish_foreground_job(); |
|
|
self.wait_foreground_done().await; |
|
|
self.begin_foreground_job(); |
|
|
})) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn schedule_notify_tasks(&self, tasks: &[TaskId]) { |
|
|
let result = CURRENT_TASK_STATE.try_with(|cell| { |
|
|
let CurrentTaskState { |
|
|
tasks_to_notify, .. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
tasks_to_notify.extend(tasks.iter().copied()); |
|
|
}); |
|
|
if result.is_err() { |
|
|
let _guard = trace_span!("schedule_notify_tasks", count = tasks.len()).entered(); |
|
|
self.backend.invalidate_tasks(tasks, self); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn schedule_notify_tasks_set(&self, tasks: &TaskIdSet) { |
|
|
let result = CURRENT_TASK_STATE.try_with(|cell| { |
|
|
let CurrentTaskState { |
|
|
tasks_to_notify, .. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
tasks_to_notify.extend(tasks.iter().copied()); |
|
|
}); |
|
|
if result.is_err() { |
|
|
let _guard = trace_span!("schedule_notify_tasks_set", count = tasks.len()).entered(); |
|
|
self.backend.invalidate_tasks_set(tasks, self); |
|
|
}; |
|
|
} |
|
|
|
|
|
#[track_caller] |
|
|
fn schedule(&self, task: TaskId) { |
|
|
self.schedule(task) |
|
|
} |
|
|
|
|
|
fn program_duration_until(&self, instant: Instant) -> Duration { |
|
|
instant - self.program_start |
|
|
} |
|
|
|
|
|
fn get_fresh_persistent_task_id(&self) -> Unused<TaskId> { |
|
|
|
|
|
unsafe { Unused::new_unchecked(self.task_id_factory.get()) } |
|
|
} |
|
|
|
|
|
fn get_fresh_transient_task_id(&self) -> Unused<TaskId> { |
|
|
|
|
|
unsafe { Unused::new_unchecked(self.transient_task_id_factory.get()) } |
|
|
} |
|
|
|
|
|
unsafe fn reuse_persistent_task_id(&self, id: Unused<TaskId>) { |
|
|
unsafe { self.task_id_factory.reuse(id.into()) } |
|
|
} |
|
|
|
|
|
unsafe fn reuse_transient_task_id(&self, id: Unused<TaskId>) { |
|
|
unsafe { self.transient_task_id_factory.reuse(id.into()) } |
|
|
} |
|
|
|
|
|
fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState)) { |
|
|
CURRENT_TASK_STATE |
|
|
.with(move |ts| func(ts.read().unwrap().backend_state.downcast_ref().unwrap())) |
|
|
} |
|
|
|
|
|
fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState)) { |
|
|
CURRENT_TASK_STATE |
|
|
.with(move |ts| func(ts.write().unwrap().backend_state.downcast_mut().unwrap())) |
|
|
} |
|
|
|
|
|
fn is_idle(&self) -> bool { |
|
|
self.currently_scheduled_tasks.load(Ordering::Acquire) == 0 |
|
|
} |
|
|
} |
|
|
|
|
|
pub(crate) fn current_task(from: &str) -> TaskId { |
|
|
match CURRENT_TASK_STATE.try_with(|ts| ts.read().unwrap().task_id) { |
|
|
Ok(id) => id, |
|
|
Err(_) => panic!("{from} can only be used in the context of turbo_tasks task execution"), |
|
|
} |
|
|
} |
|
|
|
|
|
pub async fn run_once<T: Send + 'static>( |
|
|
tt: Arc<dyn TurboTasksApi>, |
|
|
future: impl Future<Output = Result<T>> + Send + 'static, |
|
|
) -> Result<T> { |
|
|
let (tx, rx) = tokio::sync::oneshot::channel(); |
|
|
|
|
|
let task_id = tt.run_once(Box::pin(async move { |
|
|
let result = future.await?; |
|
|
tx.send(result) |
|
|
.map_err(|_| anyhow!("unable to send result"))?; |
|
|
Ok(()) |
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
let raw_result = read_task_output_untracked(&*tt, task_id, ReadConsistency::Eventual).await?; |
|
|
let raw_future = raw_result.into_read().untracked(); |
|
|
turbo_tasks_future_scope(tt, ReadVcFuture::<Completion>::from(raw_future)).await?; |
|
|
|
|
|
Ok(rx.await?) |
|
|
} |
|
|
|
|
|
pub async fn run_once_with_reason<T: Send + 'static>( |
|
|
tt: Arc<dyn TurboTasksApi>, |
|
|
reason: impl InvalidationReason, |
|
|
future: impl Future<Output = Result<T>> + Send + 'static, |
|
|
) -> Result<T> { |
|
|
let (tx, rx) = tokio::sync::oneshot::channel(); |
|
|
|
|
|
let task_id = tt.run_once_with_reason( |
|
|
(Arc::new(reason) as Arc<dyn InvalidationReason>).into(), |
|
|
Box::pin(async move { |
|
|
let result = future.await?; |
|
|
tx.send(result) |
|
|
.map_err(|_| anyhow!("unable to send result"))?; |
|
|
Ok(()) |
|
|
}), |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
let raw_result = read_task_output_untracked(&*tt, task_id, ReadConsistency::Eventual).await?; |
|
|
let raw_future = raw_result.into_read().untracked(); |
|
|
turbo_tasks_future_scope(tt, ReadVcFuture::<Completion>::from(raw_future)).await?; |
|
|
|
|
|
Ok(rx.await?) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn dynamic_call( |
|
|
func: &'static NativeFunction, |
|
|
this: Option<RawVc>, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
with_turbo_tasks(|tt| tt.dynamic_call(func, this, arg, persistence)) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn trait_call( |
|
|
trait_method: &'static TraitMethod, |
|
|
this: RawVc, |
|
|
arg: Box<dyn MagicAny>, |
|
|
persistence: TaskPersistence, |
|
|
) -> RawVc { |
|
|
with_turbo_tasks(|tt| tt.trait_call(trait_method, this, arg, persistence)) |
|
|
} |
|
|
|
|
|
pub fn turbo_tasks() -> Arc<dyn TurboTasksApi> { |
|
|
TURBO_TASKS.with(|arc| arc.clone()) |
|
|
} |
|
|
|
|
|
pub fn with_turbo_tasks<T>(func: impl FnOnce(&Arc<dyn TurboTasksApi>) -> T) -> T { |
|
|
TURBO_TASKS.with(|arc| func(arc)) |
|
|
} |
|
|
|
|
|
pub fn turbo_tasks_scope<T>(tt: Arc<dyn TurboTasksApi>, f: impl FnOnce() -> T) -> T { |
|
|
TURBO_TASKS.sync_scope(tt, f) |
|
|
} |
|
|
|
|
|
pub fn turbo_tasks_future_scope<T>( |
|
|
tt: Arc<dyn TurboTasksApi>, |
|
|
f: impl Future<Output = T>, |
|
|
) -> impl Future<Output = T> { |
|
|
TURBO_TASKS.scope(tt, f) |
|
|
} |
|
|
|
|
|
pub fn with_turbo_tasks_for_testing<T>( |
|
|
tt: Arc<dyn TurboTasksApi>, |
|
|
current_task: TaskId, |
|
|
execution_id: ExecutionId, |
|
|
f: impl Future<Output = T>, |
|
|
) -> impl Future<Output = T> { |
|
|
TURBO_TASKS.scope( |
|
|
tt, |
|
|
CURRENT_TASK_STATE.scope( |
|
|
Arc::new(RwLock::new(CurrentTaskState::new( |
|
|
current_task, |
|
|
execution_id, |
|
|
Box::new(()), |
|
|
))), |
|
|
f, |
|
|
), |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn spawn_detached_for_testing(f: impl Future<Output = Result<()>> + Send + 'static) { |
|
|
tokio::spawn(turbo_tasks().detached_for_testing(Box::pin(f.in_current_span()))); |
|
|
} |
|
|
|
|
|
pub fn current_task_for_testing() -> TaskId { |
|
|
CURRENT_TASK_STATE.with(|ts| ts.read().unwrap().task_id) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn mark_session_dependent() { |
|
|
with_turbo_tasks(|tt| { |
|
|
tt.mark_own_task_as_session_dependent(current_task("turbo_tasks::mark_session_dependent()")) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn mark_root() { |
|
|
with_turbo_tasks(|tt| { |
|
|
tt.set_own_task_aggregation_number(current_task("turbo_tasks::mark_root()"), u32::MAX) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn mark_finished() { |
|
|
with_turbo_tasks(|tt| { |
|
|
tt.mark_own_task_as_finished(current_task("turbo_tasks::mark_finished()")) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn mark_stateful() -> SerializationInvalidator { |
|
|
CURRENT_TASK_STATE.with(|cell| { |
|
|
let CurrentTaskState { |
|
|
stateful, task_id, .. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
*stateful = true; |
|
|
SerializationInvalidator::new(*task_id) |
|
|
}) |
|
|
} |
|
|
|
|
|
pub fn mark_invalidator() { |
|
|
CURRENT_TASK_STATE.with(|cell| { |
|
|
let CurrentTaskState { |
|
|
has_invalidator, .. |
|
|
} = &mut *cell.write().unwrap(); |
|
|
*has_invalidator = true; |
|
|
}) |
|
|
} |
|
|
|
|
|
pub fn prevent_gc() { |
|
|
|
|
|
mark_stateful(); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn notify_scheduled_tasks() { |
|
|
with_turbo_tasks(|tt| tt.notify_scheduled_tasks()) |
|
|
} |
|
|
|
|
|
pub fn emit<T: VcValueTrait + ?Sized>(collectible: ResolvedVc<T>) { |
|
|
with_turbo_tasks(|tt| { |
|
|
let raw_vc = collectible.node.node; |
|
|
tt.emit_collectible(T::get_trait_type_id(), raw_vc) |
|
|
}) |
|
|
} |
|
|
|
|
|
pub async fn spawn_blocking<T: Send + 'static>(func: impl FnOnce() -> T + Send + 'static) -> T { |
|
|
let turbo_tasks = turbo_tasks(); |
|
|
let span = Span::current(); |
|
|
let (result, duration, alloc_info) = tokio::task::spawn_blocking(|| { |
|
|
let _guard = span.entered(); |
|
|
let start = Instant::now(); |
|
|
let start_allocations = TurboMalloc::allocation_counters(); |
|
|
let r = turbo_tasks_scope(turbo_tasks, func); |
|
|
(r, start.elapsed(), start_allocations.until_now()) |
|
|
}) |
|
|
.await |
|
|
.unwrap(); |
|
|
capture_future::add_duration(duration); |
|
|
capture_future::add_allocation_info(alloc_info); |
|
|
result |
|
|
} |
|
|
|
|
|
pub fn spawn_thread(func: impl FnOnce() + Send + 'static) { |
|
|
let handle = Handle::current(); |
|
|
let span = info_span!("thread").or_current(); |
|
|
thread::spawn(move || { |
|
|
let span = span.entered(); |
|
|
let guard = handle.enter(); |
|
|
func(); |
|
|
drop(guard); |
|
|
drop(span); |
|
|
}); |
|
|
} |
|
|
|
|
|
pub(crate) async fn read_task_output( |
|
|
this: &dyn TurboTasksApi, |
|
|
id: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<RawVc> { |
|
|
loop { |
|
|
match this.try_read_task_output(id, consistency)? { |
|
|
Ok(result) => return Ok(result), |
|
|
Err(listener) => listener.await, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn read_task_output_untracked( |
|
|
this: &dyn TurboTasksApi, |
|
|
id: TaskId, |
|
|
consistency: ReadConsistency, |
|
|
) -> Result<RawVc> { |
|
|
loop { |
|
|
match this.try_read_task_output_untracked(id, consistency)? { |
|
|
Ok(result) => return Ok(result), |
|
|
Err(listener) => listener.await, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pub(crate) async fn read_task_cell( |
|
|
this: &dyn TurboTasksApi, |
|
|
id: TaskId, |
|
|
index: CellId, |
|
|
options: ReadCellOptions, |
|
|
) -> Result<TypedCellContent> { |
|
|
loop { |
|
|
match this.try_read_task_cell(id, index, options)? { |
|
|
Ok(result) => return Ok(result), |
|
|
Err(listener) => listener.await, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Serialize, Deserialize)] |
|
|
pub struct CurrentCellRef { |
|
|
current_task: TaskId, |
|
|
index: CellId, |
|
|
} |
|
|
|
|
|
type VcReadRepr<T> = <<T as VcValueType>::Read as VcRead<T>>::Repr; |
|
|
|
|
|
impl CurrentCellRef { |
|
|
|
|
|
pub fn conditional_update<T>(&self, functor: impl FnOnce(Option<&T>) -> Option<T>) |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
self.conditional_update_with_shared_reference(|old_shared_reference| { |
|
|
let old_ref = old_shared_reference |
|
|
.and_then(|sr| sr.0.downcast_ref::<VcReadRepr<T>>()) |
|
|
.map(|content| <T::Read as VcRead<T>>::repr_to_value_ref(content)); |
|
|
let new_value = functor(old_ref)?; |
|
|
Some(SharedReference::new(triomphe::Arc::new( |
|
|
<T::Read as VcRead<T>>::value_to_repr(new_value), |
|
|
))) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn conditional_update_with_shared_reference( |
|
|
&self, |
|
|
functor: impl FnOnce(Option<&SharedReference>) -> Option<SharedReference>, |
|
|
) { |
|
|
let tt = turbo_tasks(); |
|
|
let cell_content = tt |
|
|
.read_own_task_cell(self.current_task, self.index, ReadCellOptions::default()) |
|
|
.ok(); |
|
|
let update = functor(cell_content.as_ref().and_then(|cc| cc.1.0.as_ref())); |
|
|
if let Some(update) = update { |
|
|
tt.update_own_task_cell(self.current_task, self.index, CellContent(Some(update))) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn compare_and_update<T>(&self, new_value: T) |
|
|
where |
|
|
T: PartialEq + VcValueType, |
|
|
{ |
|
|
self.conditional_update(|old_value| { |
|
|
if let Some(old_value) = old_value |
|
|
&& old_value == &new_value |
|
|
{ |
|
|
return None; |
|
|
} |
|
|
Some(new_value) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn compare_and_update_with_shared_reference<T>(&self, new_shared_reference: SharedReference) |
|
|
where |
|
|
T: VcValueType + PartialEq, |
|
|
{ |
|
|
fn extract_sr_value<T: VcValueType>(sr: &SharedReference) -> &T { |
|
|
<T::Read as VcRead<T>>::repr_to_value_ref( |
|
|
sr.0.downcast_ref::<VcReadRepr<T>>() |
|
|
.expect("cannot update SharedReference of different type"), |
|
|
) |
|
|
} |
|
|
self.conditional_update_with_shared_reference(|old_sr| { |
|
|
if let Some(old_sr) = old_sr { |
|
|
let old_value: &T = extract_sr_value(old_sr); |
|
|
let new_value = extract_sr_value(&new_shared_reference); |
|
|
if old_value == new_value { |
|
|
return None; |
|
|
} |
|
|
} |
|
|
Some(new_shared_reference) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
pub fn update<T>(&self, new_value: T) |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
let tt = turbo_tasks(); |
|
|
tt.update_own_task_cell( |
|
|
self.current_task, |
|
|
self.index, |
|
|
CellContent(Some(SharedReference::new(triomphe::Arc::new( |
|
|
<T::Read as VcRead<T>>::value_to_repr(new_value), |
|
|
)))), |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn update_with_shared_reference(&self, shared_ref: SharedReference) { |
|
|
let tt = turbo_tasks(); |
|
|
let content = tt |
|
|
.read_own_task_cell(self.current_task, self.index, ReadCellOptions::default()) |
|
|
.ok(); |
|
|
let update = if let Some(TypedCellContent(_, CellContent(Some(shared_ref_exp)))) = content { |
|
|
|
|
|
shared_ref_exp != shared_ref |
|
|
} else { |
|
|
true |
|
|
}; |
|
|
if update { |
|
|
tt.update_own_task_cell(self.current_task, self.index, CellContent(Some(shared_ref))) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<CurrentCellRef> for RawVc { |
|
|
fn from(cell: CurrentCellRef) -> Self { |
|
|
RawVc::TaskCell(cell.current_task, cell.index) |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn find_cell_by_type(ty: ValueTypeId) -> CurrentCellRef { |
|
|
CURRENT_TASK_STATE.with(|ts| { |
|
|
let current_task = current_task("celling turbo_tasks values"); |
|
|
let mut ts = ts.write().unwrap(); |
|
|
let map = ts.cell_counters.as_mut().unwrap(); |
|
|
let current_index = map.entry(ty).or_default(); |
|
|
let index = *current_index; |
|
|
*current_index += 1; |
|
|
CurrentCellRef { |
|
|
current_task, |
|
|
index: CellId { type_id: ty, index }, |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
pub(crate) async fn read_local_output( |
|
|
this: &dyn TurboTasksApi, |
|
|
execution_id: ExecutionId, |
|
|
local_task_id: LocalTaskId, |
|
|
) -> Result<RawVc> { |
|
|
loop { |
|
|
match this.try_read_local_output(execution_id, local_task_id)? { |
|
|
Ok(raw_vc) => return Ok(raw_vc), |
|
|
Err(event_listener) => event_listener.await, |
|
|
} |
|
|
} |
|
|
} |
|
|
|