|
|
use std::{ |
|
|
any::{Any, TypeId}, |
|
|
borrow::Cow, |
|
|
future::Future, |
|
|
mem::replace, |
|
|
panic, |
|
|
pin::Pin, |
|
|
sync::Arc, |
|
|
}; |
|
|
|
|
|
use anyhow::{Result, anyhow}; |
|
|
use auto_hash_map::AutoSet; |
|
|
use futures::{StreamExt, TryStreamExt}; |
|
|
use parking_lot::Mutex; |
|
|
use rustc_hash::{FxHashMap, FxHashSet}; |
|
|
use tokio::task_local; |
|
|
use tracing::{Instrument, Span}; |
|
|
|
|
|
use crate::{ |
|
|
self as turbo_tasks, CollectiblesSource, NonLocalValue, ReadRef, ResolvedVc, TryJoinIterExt, |
|
|
debug::ValueDebugFormat, |
|
|
emit, |
|
|
event::{Event, EventListener}, |
|
|
manager::turbo_tasks_future_scope, |
|
|
trace::TraceRawVcs, |
|
|
util::SharedError, |
|
|
}; |
|
|
|
|
|
const APPLY_EFFECTS_CONCURRENCY_LIMIT: usize = 1024; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
trait Effect {} |
|
|
|
|
|
|
|
|
|
|
|
type EffectFuture = Pin<Box<dyn Future<Output = Result<()>> + Send + Sync + 'static>>; |
|
|
|
|
|
|
|
|
struct EffectInner { |
|
|
future: EffectFuture, |
|
|
} |
|
|
|
|
|
enum EffectState { |
|
|
NotStarted(EffectInner), |
|
|
Started(Event), |
|
|
Finished(Result<(), SharedError>), |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(serialization = "none", cell = "new", eq = "manual")] |
|
|
struct EffectInstance { |
|
|
#[turbo_tasks(trace_ignore, debug_ignore)] |
|
|
inner: Mutex<EffectState>, |
|
|
} |
|
|
|
|
|
impl EffectInstance { |
|
|
fn new(future: impl Future<Output = Result<()>> + Send + Sync + 'static) -> Self { |
|
|
Self { |
|
|
inner: Mutex::new(EffectState::NotStarted(EffectInner { |
|
|
future: Box::pin(future), |
|
|
})), |
|
|
} |
|
|
} |
|
|
|
|
|
async fn apply(&self) -> Result<()> { |
|
|
loop { |
|
|
enum State { |
|
|
Started(EventListener), |
|
|
NotStarted(EffectInner), |
|
|
} |
|
|
let state = { |
|
|
let mut guard = self.inner.lock(); |
|
|
match &*guard { |
|
|
EffectState::Started(event) => { |
|
|
let listener = event.listen(); |
|
|
State::Started(listener) |
|
|
} |
|
|
EffectState::Finished(result) => { |
|
|
return result.clone().map_err(Into::into); |
|
|
} |
|
|
EffectState::NotStarted(_) => { |
|
|
let EffectState::NotStarted(inner) = std::mem::replace( |
|
|
&mut *guard, |
|
|
EffectState::Started(Event::new(|| || "Effect".to_string())), |
|
|
) else { |
|
|
unreachable!(); |
|
|
}; |
|
|
State::NotStarted(inner) |
|
|
} |
|
|
} |
|
|
}; |
|
|
match state { |
|
|
State::Started(listener) => { |
|
|
listener.await; |
|
|
} |
|
|
State::NotStarted(EffectInner { future }) => { |
|
|
let join_handle = tokio::spawn(ApplyEffectsContext::in_current_scope( |
|
|
turbo_tasks_future_scope(turbo_tasks::turbo_tasks(), future) |
|
|
.instrument(Span::current()), |
|
|
)); |
|
|
let result = match join_handle.await { |
|
|
Ok(Err(err)) => Err(SharedError::new(err)), |
|
|
Err(err) => { |
|
|
let any = err.into_panic(); |
|
|
let panic = match any.downcast::<String>() { |
|
|
Ok(owned) => Some(Cow::Owned(*owned)), |
|
|
Err(any) => match any.downcast::<&'static str>() { |
|
|
Ok(str) => Some(Cow::Borrowed(*str)), |
|
|
Err(_) => None, |
|
|
}, |
|
|
}; |
|
|
Err(SharedError::new(if let Some(panic) = panic { |
|
|
anyhow!("Task effect panicked: {panic}") |
|
|
} else { |
|
|
anyhow!("Task effect panicked") |
|
|
})) |
|
|
} |
|
|
Ok(Ok(())) => Ok(()), |
|
|
}; |
|
|
let event = { |
|
|
let mut guard = self.inner.lock(); |
|
|
let EffectState::Started(event) = |
|
|
replace(&mut *guard, EffectState::Finished(result.clone())) |
|
|
else { |
|
|
unreachable!(); |
|
|
}; |
|
|
event |
|
|
}; |
|
|
event.notify(usize::MAX); |
|
|
return result.map_err(Into::into); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl Effect for EffectInstance {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn effect(future: impl Future<Output = Result<()>> + Send + Sync + 'static) { |
|
|
emit::<Box<dyn Effect>>(ResolvedVc::upcast( |
|
|
EffectInstance::new(future).resolved_cell(), |
|
|
)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn apply_effects(source: impl CollectiblesSource) -> Result<()> { |
|
|
let effects: AutoSet<ResolvedVc<Box<dyn Effect>>> = source.take_collectibles(); |
|
|
if effects.is_empty() { |
|
|
return Ok(()); |
|
|
} |
|
|
let span = tracing::info_span!("apply effects", count = effects.len()); |
|
|
APPLY_EFFECTS_CONTEXT |
|
|
.scope(Default::default(), async move { |
|
|
|
|
|
futures::stream::iter(effects) |
|
|
.map(Ok) |
|
|
.try_for_each_concurrent(APPLY_EFFECTS_CONCURRENCY_LIMIT, async |effect| { |
|
|
let Some(effect) = ResolvedVc::try_downcast_type::<EffectInstance>(effect) |
|
|
else { |
|
|
panic!("Effect must only be implemented by EffectInstance"); |
|
|
}; |
|
|
effect.await?.apply().await |
|
|
}) |
|
|
.await |
|
|
}) |
|
|
.instrument(span) |
|
|
.await |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn get_effects(source: impl CollectiblesSource) -> Result<Effects> { |
|
|
let effects: AutoSet<ResolvedVc<Box<dyn Effect>>> = source.take_collectibles(); |
|
|
let effects = effects |
|
|
.into_iter() |
|
|
.map(|effect| async move { |
|
|
if let Some(effect) = ResolvedVc::try_downcast_type::<EffectInstance>(effect) { |
|
|
Ok(effect.await?) |
|
|
} else { |
|
|
panic!("Effect must only be implemented by EffectInstance"); |
|
|
} |
|
|
}) |
|
|
.try_join() |
|
|
.await?; |
|
|
Ok(Effects { effects }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(TraceRawVcs, Default, ValueDebugFormat, NonLocalValue)] |
|
|
pub struct Effects { |
|
|
#[turbo_tasks(trace_ignore, debug_ignore)] |
|
|
effects: Vec<ReadRef<EffectInstance>>, |
|
|
} |
|
|
|
|
|
impl PartialEq for Effects { |
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
if self.effects.len() != other.effects.len() { |
|
|
return false; |
|
|
} |
|
|
let effect_ptrs = self |
|
|
.effects |
|
|
.iter() |
|
|
.map(ReadRef::ptr) |
|
|
.collect::<FxHashSet<_>>(); |
|
|
other |
|
|
.effects |
|
|
.iter() |
|
|
.all(|e| effect_ptrs.contains(&ReadRef::ptr(e))) |
|
|
} |
|
|
} |
|
|
|
|
|
impl Eq for Effects {} |
|
|
|
|
|
impl Effects { |
|
|
|
|
|
pub async fn apply(&self) -> Result<()> { |
|
|
let span = tracing::info_span!("apply effects", count = self.effects.len()); |
|
|
APPLY_EFFECTS_CONTEXT |
|
|
.scope(Default::default(), async move { |
|
|
|
|
|
futures::stream::iter(self.effects.iter()) |
|
|
.map(Ok) |
|
|
.try_for_each_concurrent(APPLY_EFFECTS_CONCURRENCY_LIMIT, async |effect| { |
|
|
effect.apply().await |
|
|
}) |
|
|
.await |
|
|
}) |
|
|
.instrument(span) |
|
|
.await |
|
|
} |
|
|
} |
|
|
|
|
|
task_local! { |
|
|
|
|
|
static APPLY_EFFECTS_CONTEXT: Arc<Mutex<ApplyEffectsContext>>; |
|
|
} |
|
|
|
|
|
#[derive(Default)] |
|
|
pub struct ApplyEffectsContext { |
|
|
data: FxHashMap<TypeId, Box<dyn Any + Send + Sync>>, |
|
|
} |
|
|
|
|
|
impl ApplyEffectsContext { |
|
|
fn in_current_scope<F: Future>(f: F) -> impl Future<Output = F::Output> { |
|
|
let current = Self::current(); |
|
|
APPLY_EFFECTS_CONTEXT.scope(current, f) |
|
|
} |
|
|
|
|
|
fn current() -> Arc<Mutex<Self>> { |
|
|
APPLY_EFFECTS_CONTEXT |
|
|
.try_with(|mutex| mutex.clone()) |
|
|
.expect("No effect context found") |
|
|
} |
|
|
|
|
|
fn with_context<T, F: FnOnce(&mut Self) -> T>(f: F) -> T { |
|
|
APPLY_EFFECTS_CONTEXT |
|
|
.try_with(|mutex| f(&mut mutex.lock())) |
|
|
.expect("No effect context found") |
|
|
} |
|
|
|
|
|
pub fn set<T: Any + Send + Sync>(value: T) { |
|
|
Self::with_context(|this| { |
|
|
this.data.insert(TypeId::of::<T>(), Box::new(value)); |
|
|
}) |
|
|
} |
|
|
|
|
|
pub fn with<T: Any + Send + Sync, R>(f: impl FnOnce(&mut T) -> R) -> Option<R> { |
|
|
Self::with_context(|this| { |
|
|
this.data |
|
|
.get_mut(&TypeId::of::<T>()) |
|
|
.map(|value| { |
|
|
|
|
|
unsafe { value.downcast_mut_unchecked() } |
|
|
}) |
|
|
.map(f) |
|
|
}) |
|
|
} |
|
|
|
|
|
pub fn with_or_insert_with<T: Any + Send + Sync, R>( |
|
|
insert_with: impl FnOnce() -> T, |
|
|
f: impl FnOnce(&mut T) -> R, |
|
|
) -> R { |
|
|
Self::with_context(|this| { |
|
|
let value = this.data.entry(TypeId::of::<T>()).or_insert_with(|| { |
|
|
let value = insert_with(); |
|
|
Box::new(value) |
|
|
}); |
|
|
f( |
|
|
|
|
|
unsafe { value.downcast_mut_unchecked() }, |
|
|
) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use crate::{CollectiblesSource, apply_effects, get_effects}; |
|
|
|
|
|
#[test] |
|
|
#[allow(dead_code)] |
|
|
fn is_sync_and_send() { |
|
|
fn assert_sync<T: Sync + Send>(_: T) {} |
|
|
fn check_apply_effects<T: CollectiblesSource + Send + Sync>(t: T) { |
|
|
assert_sync(apply_effects(t)); |
|
|
} |
|
|
fn check_get_effects<T: CollectiblesSource + Send + Sync>(t: T) { |
|
|
assert_sync(get_effects(t)); |
|
|
} |
|
|
} |
|
|
} |
|
|
|