1use crate::{App, PlatformDispatcher};
2use async_task::Runnable;
3use futures::channel::mpsc;
4use smol::prelude::*;
5use std::{
6 fmt::Debug,
7 marker::PhantomData,
8 mem::{self, ManuallyDrop},
9 num::NonZeroUsize,
10 panic::Location,
11 pin::Pin,
12 rc::Rc,
13 sync::{
14 Arc,
15 atomic::{AtomicUsize, Ordering},
16 },
17 task::{Context, Poll},
18 thread::{self, ThreadId},
19 time::{Duration, Instant},
20};
21use util::TryFutureExt;
22use waker_fn::waker_fn;
23
24#[cfg(any(test, feature = "test-support"))]
25use rand::rngs::StdRng;
26
27/// A pointer to the executor that is currently running,
28/// for spawning background tasks.
29#[derive(Clone)]
30pub struct BackgroundExecutor {
31 #[doc(hidden)]
32 pub dispatcher: Arc<dyn PlatformDispatcher>,
33}
34
35/// A pointer to the executor that is currently running,
36/// for spawning tasks on the main thread.
37///
38/// This is intentionally `!Send` via the `not_send` marker field. This is because
39/// `ForegroundExecutor::spawn` does not require `Send` but checks at runtime that the future is
40/// only polled from the same thread it was spawned from. These checks would fail when spawning
41/// foreground tasks from from background threads.
42#[derive(Clone)]
43pub struct ForegroundExecutor {
44 #[doc(hidden)]
45 pub dispatcher: Arc<dyn PlatformDispatcher>,
46 not_send: PhantomData<Rc<()>>,
47}
48
49/// Task is a primitive that allows work to happen in the background.
50///
51/// It implements [`Future`] so you can `.await` on it.
52///
53/// If you drop a task it will be cancelled immediately. Calling [`Task::detach`] allows
54/// the task to continue running, but with no way to return a value.
55#[must_use]
56#[derive(Debug)]
57pub struct Task<T>(TaskState<T>);
58
59#[derive(Debug)]
60enum TaskState<T> {
61 /// A task that is ready to return a value
62 Ready(Option<T>),
63
64 /// A task that is currently running.
65 Spawned(async_task::Task<T>),
66}
67
68impl<T> Task<T> {
69 /// Creates a new task that will resolve with the value
70 pub fn ready(val: T) -> Self {
71 Task(TaskState::Ready(Some(val)))
72 }
73
74 /// Detaching a task runs it to completion in the background
75 pub fn detach(self) {
76 match self {
77 Task(TaskState::Ready(_)) => {}
78 Task(TaskState::Spawned(task)) => task.detach(),
79 }
80 }
81}
82
83impl<E, T> Task<Result<T, E>>
84where
85 T: 'static,
86 E: 'static + Debug,
87{
88 /// Run the task to completion in the background and log any
89 /// errors that occur.
90 #[track_caller]
91 pub fn detach_and_log_err(self, cx: &App) {
92 let location = core::panic::Location::caller();
93 cx.foreground_executor()
94 .spawn(self.log_tracked_err(*location))
95 .detach();
96 }
97}
98
99impl<T> Future for Task<T> {
100 type Output = T;
101
102 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
103 match unsafe { self.get_unchecked_mut() } {
104 Task(TaskState::Ready(val)) => Poll::Ready(val.take().unwrap()),
105 Task(TaskState::Spawned(task)) => task.poll(cx),
106 }
107 }
108}
109
110/// A task label is an opaque identifier that you can use to
111/// refer to a task in tests.
112#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
113pub struct TaskLabel(NonZeroUsize);
114
115impl Default for TaskLabel {
116 fn default() -> Self {
117 Self::new()
118 }
119}
120
121impl TaskLabel {
122 /// Construct a new task label.
123 pub fn new() -> Self {
124 static NEXT_TASK_LABEL: AtomicUsize = AtomicUsize::new(1);
125 Self(
126 NEXT_TASK_LABEL
127 .fetch_add(1, Ordering::SeqCst)
128 .try_into()
129 .unwrap(),
130 )
131 }
132}
133
134type AnyLocalFuture<R> = Pin<Box<dyn 'static + Future<Output = R>>>;
135
136type AnyFuture<R> = Pin<Box<dyn 'static + Send + Future<Output = R>>>;
137
138/// BackgroundExecutor lets you run things on background threads.
139/// In production this is a thread pool with no ordering guarantees.
140/// In tests this is simulated by running tasks one by one in a deterministic
141/// (but arbitrary) order controlled by the `SEED` environment variable.
142impl BackgroundExecutor {
143 #[doc(hidden)]
144 pub fn new(dispatcher: Arc<dyn PlatformDispatcher>) -> Self {
145 Self { dispatcher }
146 }
147
148 /// Enqueues the given future to be run to completion on a background thread.
149 pub fn spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
150 where
151 R: Send + 'static,
152 {
153 self.spawn_internal::<R>(Box::pin(future), None)
154 }
155
156 /// Enqueues the given future to be run to completion on a background thread.
157 /// The given label can be used to control the priority of the task in tests.
158 pub fn spawn_labeled<R>(
159 &self,
160 label: TaskLabel,
161 future: impl Future<Output = R> + Send + 'static,
162 ) -> Task<R>
163 where
164 R: Send + 'static,
165 {
166 self.spawn_internal::<R>(Box::pin(future), Some(label))
167 }
168
169 fn spawn_internal<R: Send + 'static>(
170 &self,
171 future: AnyFuture<R>,
172 label: Option<TaskLabel>,
173 ) -> Task<R> {
174 let dispatcher = self.dispatcher.clone();
175 let (runnable, task) =
176 async_task::spawn(future, move |runnable| dispatcher.dispatch(runnable, label));
177 runnable.schedule();
178 Task(TaskState::Spawned(task))
179 }
180
181 /// Used by the test harness to run an async test in a synchronous fashion.
182 #[cfg(any(test, feature = "test-support"))]
183 #[track_caller]
184 pub fn block_test<R>(&self, future: impl Future<Output = R>) -> R {
185 if let Ok(value) = self.block_internal(false, future, None) {
186 value
187 } else {
188 unreachable!()
189 }
190 }
191
192 /// Block the current thread until the given future resolves.
193 /// Consider using `block_with_timeout` instead.
194 pub fn block<R>(&self, future: impl Future<Output = R>) -> R {
195 if let Ok(value) = self.block_internal(true, future, None) {
196 value
197 } else {
198 unreachable!()
199 }
200 }
201
202 #[cfg(not(any(test, feature = "test-support")))]
203 pub(crate) fn block_internal<Fut: Future>(
204 &self,
205 _background_only: bool,
206 future: Fut,
207 timeout: Option<Duration>,
208 ) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>> {
209 use std::time::Instant;
210
211 let mut future = Box::pin(future);
212 if timeout == Some(Duration::ZERO) {
213 return Err(future);
214 }
215 let deadline = timeout.map(|timeout| Instant::now() + timeout);
216
217 let parker = parking::Parker::new();
218 let unparker = parker.unparker();
219 let waker = waker_fn(move || {
220 unparker.unpark();
221 });
222 let mut cx = std::task::Context::from_waker(&waker);
223
224 loop {
225 match future.as_mut().poll(&mut cx) {
226 Poll::Ready(result) => return Ok(result),
227 Poll::Pending => {
228 let timeout =
229 deadline.map(|deadline| deadline.saturating_duration_since(Instant::now()));
230 if let Some(timeout) = timeout {
231 if !parker.park_timeout(timeout)
232 && deadline.is_some_and(|deadline| deadline < Instant::now())
233 {
234 return Err(future);
235 }
236 } else {
237 parker.park();
238 }
239 }
240 }
241 }
242 }
243
244 #[cfg(any(test, feature = "test-support"))]
245 #[track_caller]
246 pub(crate) fn block_internal<Fut: Future>(
247 &self,
248 background_only: bool,
249 future: Fut,
250 timeout: Option<Duration>,
251 ) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>> {
252 use std::sync::atomic::AtomicBool;
253
254 use parking::Parker;
255
256 let mut future = Box::pin(future);
257 if timeout == Some(Duration::ZERO) {
258 return Err(future);
259 }
260 let Some(dispatcher) = self.dispatcher.as_test() else {
261 return Err(future);
262 };
263
264 let mut max_ticks = if timeout.is_some() {
265 dispatcher.gen_block_on_ticks()
266 } else {
267 usize::MAX
268 };
269
270 let parker = Parker::new();
271 let unparker = parker.unparker();
272
273 let awoken = Arc::new(AtomicBool::new(false));
274 let waker = waker_fn({
275 let awoken = awoken.clone();
276 let unparker = unparker.clone();
277 move || {
278 awoken.store(true, Ordering::SeqCst);
279 unparker.unpark();
280 }
281 });
282 let mut cx = std::task::Context::from_waker(&waker);
283
284 loop {
285 match future.as_mut().poll(&mut cx) {
286 Poll::Ready(result) => return Ok(result),
287 Poll::Pending => {
288 if max_ticks == 0 {
289 return Err(future);
290 }
291 max_ticks -= 1;
292
293 if !dispatcher.tick(background_only) {
294 if awoken.swap(false, Ordering::SeqCst) {
295 continue;
296 }
297
298 if !dispatcher.parking_allowed() {
299 if dispatcher.advance_clock_to_next_delayed() {
300 continue;
301 }
302 let mut backtrace_message = String::new();
303 let mut waiting_message = String::new();
304 if let Some(backtrace) = dispatcher.waiting_backtrace() {
305 backtrace_message =
306 format!("\nbacktrace of waiting future:\n{:?}", backtrace);
307 }
308 if let Some(waiting_hint) = dispatcher.waiting_hint() {
309 waiting_message = format!("\n waiting on: {}\n", waiting_hint);
310 }
311 panic!(
312 "parked with nothing left to run{waiting_message}{backtrace_message}",
313 )
314 }
315 dispatcher.set_unparker(unparker.clone());
316 parker.park();
317 }
318 }
319 }
320 }
321 }
322
323 /// Block the current thread until the given future resolves
324 /// or `duration` has elapsed.
325 pub fn block_with_timeout<Fut: Future>(
326 &self,
327 duration: Duration,
328 future: Fut,
329 ) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>> {
330 self.block_internal(true, future, Some(duration))
331 }
332
333 /// Scoped lets you start a number of tasks and waits
334 /// for all of them to complete before returning.
335 pub async fn scoped<'scope, F>(&self, scheduler: F)
336 where
337 F: FnOnce(&mut Scope<'scope>),
338 {
339 let mut scope = Scope::new(self.clone());
340 (scheduler)(&mut scope);
341 let spawned = mem::take(&mut scope.futures)
342 .into_iter()
343 .map(|f| self.spawn(f))
344 .collect::<Vec<_>>();
345 for task in spawned {
346 task.await;
347 }
348 }
349
350 /// Get the current time.
351 ///
352 /// Calling this instead of `std::time::Instant::now` allows the use
353 /// of fake timers in tests.
354 pub fn now(&self) -> Instant {
355 self.dispatcher.now()
356 }
357
358 /// Returns a task that will complete after the given duration.
359 /// Depending on other concurrent tasks the elapsed duration may be longer
360 /// than requested.
361 pub fn timer(&self, duration: Duration) -> Task<()> {
362 if duration.is_zero() {
363 return Task::ready(());
364 }
365 let (runnable, task) = async_task::spawn(async move {}, {
366 let dispatcher = self.dispatcher.clone();
367 move |runnable| dispatcher.dispatch_after(duration, runnable)
368 });
369 runnable.schedule();
370 Task(TaskState::Spawned(task))
371 }
372
373 /// in tests, start_waiting lets you indicate which task is waiting (for debugging only)
374 #[cfg(any(test, feature = "test-support"))]
375 pub fn start_waiting(&self) {
376 self.dispatcher.as_test().unwrap().start_waiting();
377 }
378
379 /// in tests, removes the debugging data added by start_waiting
380 #[cfg(any(test, feature = "test-support"))]
381 pub fn finish_waiting(&self) {
382 self.dispatcher.as_test().unwrap().finish_waiting();
383 }
384
385 /// in tests, run an arbitrary number of tasks (determined by the SEED environment variable)
386 #[cfg(any(test, feature = "test-support"))]
387 pub fn simulate_random_delay(&self) -> impl Future<Output = ()> + use<> {
388 self.dispatcher.as_test().unwrap().simulate_random_delay()
389 }
390
391 /// in tests, indicate that a given task from `spawn_labeled` should run after everything else
392 #[cfg(any(test, feature = "test-support"))]
393 pub fn deprioritize(&self, task_label: TaskLabel) {
394 self.dispatcher.as_test().unwrap().deprioritize(task_label)
395 }
396
397 /// in tests, move time forward. This does not run any tasks, but does make `timer`s ready.
398 #[cfg(any(test, feature = "test-support"))]
399 pub fn advance_clock(&self, duration: Duration) {
400 self.dispatcher.as_test().unwrap().advance_clock(duration)
401 }
402
403 /// in tests, run one task.
404 #[cfg(any(test, feature = "test-support"))]
405 pub fn tick(&self) -> bool {
406 self.dispatcher.as_test().unwrap().tick(false)
407 }
408
409 /// in tests, run all tasks that are ready to run. If after doing so
410 /// the test still has outstanding tasks, this will panic. (See also [`Self::allow_parking`])
411 #[cfg(any(test, feature = "test-support"))]
412 pub fn run_until_parked(&self) {
413 self.dispatcher.as_test().unwrap().run_until_parked()
414 }
415
416 /// in tests, prevents `run_until_parked` from panicking if there are outstanding tasks.
417 /// This is useful when you are integrating other (non-GPUI) futures, like disk access, that
418 /// do take real async time to run.
419 #[cfg(any(test, feature = "test-support"))]
420 pub fn allow_parking(&self) {
421 self.dispatcher.as_test().unwrap().allow_parking();
422 }
423
424 /// undoes the effect of [`Self::allow_parking`].
425 #[cfg(any(test, feature = "test-support"))]
426 pub fn forbid_parking(&self) {
427 self.dispatcher.as_test().unwrap().forbid_parking();
428 }
429
430 /// adds detail to the "parked with nothing let to run" message.
431 #[cfg(any(test, feature = "test-support"))]
432 pub fn set_waiting_hint(&self, msg: Option<String>) {
433 self.dispatcher.as_test().unwrap().set_waiting_hint(msg);
434 }
435
436 /// in tests, returns the rng used by the dispatcher and seeded by the `SEED` environment variable
437 #[cfg(any(test, feature = "test-support"))]
438 pub fn rng(&self) -> StdRng {
439 self.dispatcher.as_test().unwrap().rng()
440 }
441
442 /// How many CPUs are available to the dispatcher.
443 pub fn num_cpus(&self) -> usize {
444 #[cfg(any(test, feature = "test-support"))]
445 return 4;
446
447 #[cfg(not(any(test, feature = "test-support")))]
448 return num_cpus::get();
449 }
450
451 /// Whether we're on the main thread.
452 pub fn is_main_thread(&self) -> bool {
453 self.dispatcher.is_main_thread()
454 }
455
456 #[cfg(any(test, feature = "test-support"))]
457 /// in tests, control the number of ticks that `block_with_timeout` will run before timing out.
458 pub fn set_block_on_ticks(&self, range: std::ops::RangeInclusive<usize>) {
459 self.dispatcher.as_test().unwrap().set_block_on_ticks(range);
460 }
461}
462
463/// ForegroundExecutor runs things on the main thread.
464impl ForegroundExecutor {
465 /// Creates a new ForegroundExecutor from the given PlatformDispatcher.
466 pub fn new(dispatcher: Arc<dyn PlatformDispatcher>) -> Self {
467 Self {
468 dispatcher,
469 not_send: PhantomData,
470 }
471 }
472
473 /// Enqueues the given Task to run on the main thread at some point in the future.
474 #[track_caller]
475 pub fn spawn<R>(&self, future: impl Future<Output = R> + 'static) -> Task<R>
476 where
477 R: 'static,
478 {
479 let dispatcher = self.dispatcher.clone();
480
481 #[track_caller]
482 fn inner<R: 'static>(
483 dispatcher: Arc<dyn PlatformDispatcher>,
484 future: AnyLocalFuture<R>,
485 ) -> Task<R> {
486 let (runnable, task) = spawn_local_with_source_location(future, move |runnable| {
487 dispatcher.dispatch_on_main_thread(runnable)
488 });
489 runnable.schedule();
490 Task(TaskState::Spawned(task))
491 }
492 inner::<R>(dispatcher, Box::pin(future))
493 }
494}
495
496/// Variant of `async_task::spawn_local` that includes the source location of the spawn in panics.
497///
498/// Copy-modified from:
499/// <https://github.com/smol-rs/async-task/blob/ca9dbe1db9c422fd765847fa91306e30a6bb58a9/src/runnable.rs#L405>
500#[track_caller]
501fn spawn_local_with_source_location<Fut, S>(
502 future: Fut,
503 schedule: S,
504) -> (Runnable<()>, async_task::Task<Fut::Output, ()>)
505where
506 Fut: Future + 'static,
507 Fut::Output: 'static,
508 S: async_task::Schedule<()> + Send + Sync + 'static,
509{
510 #[inline]
511 fn thread_id() -> ThreadId {
512 std::thread_local! {
513 static ID: ThreadId = thread::current().id();
514 }
515 ID.try_with(|id| *id)
516 .unwrap_or_else(|_| thread::current().id())
517 }
518
519 struct Checked<F> {
520 id: ThreadId,
521 inner: ManuallyDrop<F>,
522 location: &'static Location<'static>,
523 }
524
525 impl<F> Drop for Checked<F> {
526 fn drop(&mut self) {
527 assert!(
528 self.id == thread_id(),
529 "local task dropped by a thread that didn't spawn it. Task spawned at {}",
530 self.location
531 );
532 unsafe { ManuallyDrop::drop(&mut self.inner) };
533 }
534 }
535
536 impl<F: Future> Future for Checked<F> {
537 type Output = F::Output;
538
539 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
540 assert!(
541 self.id == thread_id(),
542 "local task polled by a thread that didn't spawn it. Task spawned at {}",
543 self.location
544 );
545 unsafe { self.map_unchecked_mut(|c| &mut *c.inner).poll(cx) }
546 }
547 }
548
549 // Wrap the future into one that checks which thread it's on.
550 let future = Checked {
551 id: thread_id(),
552 inner: ManuallyDrop::new(future),
553 location: Location::caller(),
554 };
555
556 unsafe { async_task::spawn_unchecked(future, schedule) }
557}
558
559/// Scope manages a set of tasks that are enqueued and waited on together. See [`BackgroundExecutor::scoped`].
560pub struct Scope<'a> {
561 executor: BackgroundExecutor,
562 futures: Vec<Pin<Box<dyn Future<Output = ()> + Send + 'static>>>,
563 tx: Option<mpsc::Sender<()>>,
564 rx: mpsc::Receiver<()>,
565 lifetime: PhantomData<&'a ()>,
566}
567
568impl<'a> Scope<'a> {
569 fn new(executor: BackgroundExecutor) -> Self {
570 let (tx, rx) = mpsc::channel(1);
571 Self {
572 executor,
573 tx: Some(tx),
574 rx,
575 futures: Default::default(),
576 lifetime: PhantomData,
577 }
578 }
579
580 /// How many CPUs are available to the dispatcher.
581 pub fn num_cpus(&self) -> usize {
582 self.executor.num_cpus()
583 }
584
585 /// Spawn a future into this scope.
586 pub fn spawn<F>(&mut self, f: F)
587 where
588 F: Future<Output = ()> + Send + 'a,
589 {
590 let tx = self.tx.clone().unwrap();
591
592 // SAFETY: The 'a lifetime is guaranteed to outlive any of these futures because
593 // dropping this `Scope` blocks until all of the futures have resolved.
594 let f = unsafe {
595 mem::transmute::<
596 Pin<Box<dyn Future<Output = ()> + Send + 'a>>,
597 Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
598 >(Box::pin(async move {
599 f.await;
600 drop(tx);
601 }))
602 };
603 self.futures.push(f);
604 }
605}
606
607impl Drop for Scope<'_> {
608 fn drop(&mut self) {
609 self.tx.take().unwrap();
610
611 // Wait until the channel is closed, which means that all of the spawned
612 // futures have resolved.
613 self.executor.block(self.rx.next());
614 }
615}