// Copyright © SixtyFPS GmbH // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 #![cfg(target_has_atomic = "ptr")] // Arc is not available. TODO: implement using RawWarker #![warn(missing_docs)] //! This module contains the code that runs futures use crate::api::EventLoopError; use crate::SlintContext; #[cfg(not(feature = "std"))] use alloc::boxed::Box; use alloc::task::Wake; #[cfg(not(feature = "std"))] use alloc::vec::Vec; use core::future::Future; use core::ops::DerefMut; use core::pin::Pin; use core::task::Poll; use portable_atomic as atomic; enum FutureState { Running(Pin>>), Finished(Option), } struct FutureRunnerInner { fut: FutureState, wakers: Vec, } struct FutureRunner { #[cfg(not(feature = "std"))] inner: core::cell::RefCell>, #[cfg(feature = "std")] inner: std::sync::Mutex>, aborted: atomic::AtomicBool, proxy: Box, #[cfg(feature = "std")] thread: std::thread::ThreadId, } impl FutureRunner { fn inner(&self) -> impl DerefMut> + '_ { #[cfg(feature = "std")] return self.inner.lock().unwrap(); #[cfg(not(feature = "std"))] return self.inner.borrow_mut(); } } // # Safety: // The Future might not be Send, but we only poll the future from the main thread. // (We even assert that) // We may access the finished value from another thread only if T is Send // (because JoinHandle only implement Send if T:Send) #[allow(unsafe_code)] unsafe impl Send for FutureRunner {} #[allow(unsafe_code)] unsafe impl Sync for FutureRunner {} impl Wake for FutureRunner { fn wake(self: alloc::sync::Arc) { self.clone().proxy.invoke_from_event_loop(Box::new(move || { #[cfg(feature = "std")] assert_eq!(self.thread, std::thread::current().id(), "the future was moved to a thread despite we checked it was created in the event loop thread"); let waker = self.clone().into(); let mut inner = self.inner(); let mut cx = core::task::Context::from_waker(&waker); if let FutureState::Running(fut) = &mut inner.fut { if self.aborted.load(atomic::Ordering::Relaxed) { inner.fut = FutureState::Finished(None); } else { match fut.as_mut().poll(&mut cx) { Poll::Ready(val) => { inner.fut = FutureState::Finished(Some(val)); for w in core::mem::take(&mut inner.wakers) { w.wake(); } } Poll::Pending => {} } } } })) .expect("No event loop despite we checked"); } } /// The return value of the `spawn_local()` function /// /// Can be used to abort the future, or to get the value from a different thread with `.await` /// /// This trait implements future. Polling it after it finished or aborted may result in a panic. pub struct JoinHandle(alloc::sync::Arc>); impl Future for JoinHandle { type Output = T; fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll { let mut inner = self.0.inner(); match &mut inner.fut { FutureState::Running(_) => { let waker = cx.waker(); if !inner.wakers.iter().any(|w| w.will_wake(waker)) { inner.wakers.push(waker.clone()); } Poll::Pending } FutureState::Finished(x) => { Poll::Ready(x.take().expect("Polling completed or aborted JoinHandle")) } } } } impl JoinHandle { /// If the future hasn't completed yet, this will make the event loop stop polling the corresponding future and it will be dropped /// /// Once this handle has been aborted, it can no longer be polled pub fn abort(self) { self.0.aborted.store(true, atomic::Ordering::Relaxed); } /// Checks if the task associated with this `JoinHandle` has finished. pub fn is_finished(&self) -> bool { matches!(self.0.inner().fut, FutureState::Finished(_)) } } #[cfg(feature = "std")] #[allow(unsafe_code)] // Safety: JoinHandle doesn't access the future, only the unsafe impl Send for JoinHandle {} /// Implementation for [`SlintContext::spawn_local`] pub(crate) fn spawn_local_with_ctx( ctx: &SlintContext, fut: F, ) -> Result, EventLoopError> { let arc = alloc::sync::Arc::new(FutureRunner { #[cfg(feature = "std")] thread: std::thread::current().id(), inner: FutureRunnerInner { fut: FutureState::Running(Box::pin(fut)), wakers: Vec::new() } .into(), aborted: Default::default(), proxy: ctx.event_loop_proxy().ok_or(EventLoopError::NoEventLoopProvider)?, }); arc.wake_by_ref(); Ok(JoinHandle(arc)) }