pub struct BackgroundExecutor { /* private fields */ }Expand description
Runs Send futures on GPUI’s background executor.
Get one from App::background_executor or crate::AsyncApp::background_executor.
Its futures must be Send and 'static, so they cannot borrow entities or contexts. Pass in
plain data and return plain data. Native backends use worker threads; a single-threaded
web build runs these futures on the browser’s main thread. See
executors.
Implementations§
Source§impl BackgroundExecutor
impl BackgroundExecutor
Sourcepub fn new(dispatcher: Arc<dyn PlatformDispatcher>) -> Self
pub fn new(dispatcher: Arc<dyn PlatformDispatcher>) -> Self
Creates an executor that sends its work through dispatcher.
Platform code creates the executor. Apps get one from App::background_executor.
Sourcepub fn scheduler_executor(&self) -> BackgroundExecutor
pub fn scheduler_executor(&self) -> BackgroundExecutor
Returns the underlying scheduler executor, for code that takes one directly.
Sourcepub fn spawn_dedicated<F, Fut>(&self, f: F) -> Task<Fut::Output> ⓘ
pub fn spawn_dedicated<F, Fut>(&self, f: F) -> Task<Fut::Output> ⓘ
Runs the future that f returns on a thread of its own, and returns a task for
its result.
Use this instead of BackgroundExecutor::spawn for work whose polls need more
stack than the shared background threads provide. A dedicated thread gets the
standard library’s default stack of 2 MiB. Dropping the task cancels the work, as it
does for any task.
Sourcepub fn prevent_app_nap(&self, reason: &str) -> ActivityGuard
pub fn prevent_app_nap(&self, reason: &str) -> ActivityGuard
Prevents App Nap-style throttling while the returned guard is held.
This does not prevent the system from entering idle sleep.
Sourcepub fn spawn<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
pub fn spawn<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
Schedules future on this executor and returns a task for its result.
On native targets, use this for computation or blocking I/O that should leave
the main thread free. On a single-threaded web build it uses the main thread,
so each poll must remain short.
The future must be Send and 'static. To get the result on the main thread,
await the task from a foreground future. Dropping the task cancels the future.
§Examples
use gpui::TestApp;
let app = TestApp::new();
let task = app.background_executor().spawn(async { 6 * 7 });
assert_eq!(app.foreground_executor().block_test(task), 42);Sourcepub fn spawn_with_priority<R>(
&self,
priority: Priority,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
pub fn spawn_with_priority<R>(
&self,
priority: Priority,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
Like BackgroundExecutor::spawn, but with a Priority.
Higher priorities are more likely to run first, but the order isn’t guaranteed.
Priority::RealtimeAudio runs the future on a dedicated thread with realtime
scheduling. Use it only for audio work.
Sourcepub async fn scoped<'scope, F>(&self, scheduler: F)
pub async fn scoped<'scope, F>(&self, scheduler: F)
Runs futures that may borrow from the caller in parallel, and waits for all of them to finish.
scheduler receives a Scope and spawns the futures on it. Unlike
BackgroundExecutor::spawn, those futures needn’t be 'static, because the
returned future doesn’t complete until they have. Dropping the returned future
cancels the futures, and blocks until they are destroyed. If scheduler panics, the
futures that haven’t started are dropped, and the panic unwinds without hanging.
§Examples
use std::sync::atomic::{AtomicUsize, Ordering};
use gpui::TestApp;
let app = TestApp::new();
let total = AtomicUsize::new(0);
app.foreground_executor().block_test(app.background_executor().scoped(|scope| {
for n in 1..=3 {
let total = &total;
scope.spawn(async move {
total.fetch_add(n, Ordering::SeqCst);
});
}
}));
assert_eq!(total.into_inner(), 6);Sourcepub async fn scoped_priority<'scope, F>(&self, priority: Priority, scheduler: F)
pub async fn scoped_priority<'scope, F>(&self, priority: Priority, scheduler: F)
Like BackgroundExecutor::scoped, but runs every future in the scope at
priority.
Sourcepub fn now(&self) -> Instant
pub fn now(&self) -> Instant
Returns the current time.
Use this in place of std::time::Instant::now so that tests can control time. In
tests it reads the simulated clock, which moves only when the test advances it.
Sourcepub fn timer(&self, duration: Duration) -> Task<()> ⓘ
pub fn timer(&self, duration: Duration) -> Task<()> ⓘ
Returns a task that completes after duration.
Await it to delay work or to repeat it on an interval. In tests, time moves only
when the test calls BackgroundExecutor::advance_clock. Other work can delay
the wake-up, so the real wait can be longer than duration. A zero duration
returns a task that is already complete.
§Examples
use std::time::Duration;
use gpui::TestApp;
let app = TestApp::new();
let timer = app.background_executor().timer(Duration::from_millis(500));
app.advance_clock(Duration::from_millis(500));
assert!(timer.is_ready());Sourcepub fn simulate_random_delay(&self) -> impl Future<Output = ()> + use<>
pub fn simulate_random_delay(&self) -> impl Future<Output = ()> + use<>
Yields a random number of times, allowing other ready tasks to run between steps.
Await it between steps of a test to shake out ordering bugs. Only available in tests.
§Panics
Panics if this executor isn’t a test executor.
Sourcepub fn advance_clock(&self, duration: Duration)
pub fn advance_clock(&self, duration: Duration)
Moves the simulated clock forward by duration.
Ready tasks run as the clock passes each timer, in time order, so a task that
waits on a timer resumes at the right moment. When the call returns, the clock
reads its earlier value plus duration. Only available in tests.
§Panics
Panics if this executor isn’t a test executor.
Sourcepub fn tick(&self) -> bool
pub fn tick(&self) -> bool
Runs one unit of ready work in tests.
Returns true if it ran a task or fired a timer, and false if there was nothing
to do. Only available in tests.
§Panics
Panics if this executor isn’t a test executor.
Sourcepub fn run_until_parked(&self)
pub fn run_until_parked(&self)
Runs ready tasks in tests until none can make progress.
This doesn’t move the simulated clock. A timer that hasn’t expired stays pending,
until BackgroundExecutor::advance_clock moves time past it. Only available in
tests.
Sourcepub fn allow_parking(&self)
pub fn allow_parking(&self)
Lets blocking waits in tests sleep in real time when no test work can make progress.
By default, a blocking call that cannot progress panics. With parking allowed, it waits
in real time instead, and the test fails if the wait lasts 15 seconds. Use this for tests
that wait on real I/O. Undo it with BackgroundExecutor::forbid_parking.
Sourcepub fn set_block_on_ticks(&self, range: RangeInclusive<usize>)
pub fn set_block_on_ticks(&self, range: RangeInclusive<usize>)
Sets the range that the tick budget of a ForegroundExecutor::block_with_timeout
call is drawn from, in tests.
The default is 1 to 1000 ticks. Only available in tests.
Sourcepub fn forbid_parking(&self)
pub fn forbid_parking(&self)
Undoes BackgroundExecutor::allow_parking, so a blocking call that can’t
progress panics again. Only available in tests.
Sourcepub fn rng(&self) -> SharedRng
pub fn rng(&self) -> SharedRng
Returns the seeded random number generator that the test scheduler uses.
Draw from it when a test makes its own random choices, so that the choices
repeat with the same SEED. Only available in tests.
Sourcepub fn num_cpus(&self) -> usize
pub fn num_cpus(&self) -> usize
Returns the number of CPUs the dispatcher reports.
In tests this is 4 unless it was changed with BackgroundExecutor::set_num_cpus.
Sourcepub fn set_num_cpus(&self, count: usize)
pub fn set_num_cpus(&self, count: usize)
Sets the value that BackgroundExecutor::num_cpus returns, in tests.
§Panics
Panics if this executor isn’t a test executor.
Sourcepub fn is_main_thread(&self) -> bool
pub fn is_main_thread(&self) -> bool
Returns whether the current thread is the main thread.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for BackgroundExecutor
impl !UnwindSafe for BackgroundExecutor
impl Freeze for BackgroundExecutor
impl Send for BackgroundExecutor
impl Sync for BackgroundExecutor
impl Unpin for BackgroundExecutor
impl UnsafeUnpin for BackgroundExecutor
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
Source§type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
Source§fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T, C> ComponentsInto<C> for T
impl<T, C> ComponentsInto<C> for T
Source§fn components_into(self) -> C
fn components_into(self) -> C
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more