turbo_tasks/
scope.rs

1use std::sync::Arc;
2
3use crate::{TurboTasksApi, turbo_tasks, turbo_tasks_scope};
4
5/// A wrapper around [`rayon::Scope`] that preserves the [`turbo_tasks_scope`].
6pub struct Scope<'scope, 'a> {
7    scope: &'a rayon::Scope<'scope>,
8    handle: tokio::runtime::Handle,
9    turbo_tasks: Arc<dyn TurboTasksApi>,
10    span: tracing::Span,
11}
12
13impl<'scope> Scope<'scope, '_> {
14    pub fn spawn<Body>(&self, body: Body)
15    where
16        Body: FnOnce(&Scope<'scope, '_>) + Send + 'scope,
17    {
18        let span = self.span.clone();
19        let handle = self.handle.clone();
20        let turbo_tasks = self.turbo_tasks.clone();
21        self.scope.spawn(|scope| {
22            let _span = span.clone().entered();
23            let _guard = handle.enter();
24            turbo_tasks_scope(turbo_tasks.clone(), || {
25                body(&Scope {
26                    scope,
27                    span,
28                    handle,
29                    turbo_tasks,
30                })
31            })
32        });
33    }
34}
35
36/// A wrapper around [`rayon::in_place_scope`] that preserves the [`turbo_tasks_scope`].
37pub fn scope<'scope, Op, R>(op: Op) -> R
38where
39    Op: FnOnce(&Scope<'scope, '_>) -> R,
40{
41    let span = tracing::Span::current();
42    let handle = tokio::runtime::Handle::current();
43    let turbo_tasks = turbo_tasks();
44    rayon::in_place_scope(|scope| {
45        op(&Scope {
46            scope,
47            span,
48            handle,
49            turbo_tasks,
50        })
51    })
52}