use core::any::TypeId;
use core::cell::{Cell, RefCell};
use core::future::Future;
use core::pin::Pin;
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use core::task::{Context, Poll, Waker};
use core::time::Duration;
use std::boxed::Box;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::task::Wake;
use std::vec::Vec;
use crate::element::View;
use crate::pod::{ActionScope, DragEvent, Handle, PodId, PodPool};
use super::backend::SchedulerBackend;
use super::scheduler::SchedulerCapabilities;
use super::std_scheduler::{BackgroundExecutor, NativeStdScheduler, Timer};
use super::task::{Task, TaskShared};
#[derive(Copy, Clone)]
struct ActiveEngineCtx {
pool_ptr: *mut PodPool,
now_ms: u32,
}
impl ActiveEngineCtx {
const EMPTY: Self = Self {
pool_ptr: core::ptr::null_mut(),
now_ms: 0,
};
}
std::thread_local! {
static ACTIVE_ENGINE_CTX: Cell<ActiveEngineCtx> = const { Cell::new(ActiveEngineCtx::EMPTY) };
}
struct ActiveEngineGuard {
prev: ActiveEngineCtx,
}
impl ActiveEngineGuard {
#[inline]
fn enter(pool: &mut PodPool, now_ms: u32) -> Self {
let prev = ACTIVE_ENGINE_CTX.with(|c| {
c.replace(ActiveEngineCtx {
pool_ptr: pool as *mut PodPool,
now_ms,
})
});
Self { prev }
}
}
impl Drop for ActiveEngineGuard {
#[inline]
fn drop(&mut self) {
ACTIVE_ENGINE_CTX.with(|c| c.set(self.prev));
}
}
type WakeUiFn = Arc<dyn Fn() + Send + Sync>;
pub(crate) struct ForegroundShared {
next_id: AtomicU64,
ready_queue: Mutex<VecDeque<u64>>,
cancelled_ids: Mutex<Vec<u64>>,
wake_ui: Mutex<Option<WakeUiFn>>,
backend: Mutex<Arc<dyn SchedulerBackend>>,
}
impl ForegroundShared {
fn new(backend: Arc<dyn SchedulerBackend>) -> Arc<Self> {
Arc::new(Self {
next_id: AtomicU64::new(1),
ready_queue: Mutex::new(VecDeque::new()),
cancelled_ids: Mutex::new(Vec::new()),
wake_ui: Mutex::new(None),
backend: Mutex::new(backend),
})
}
fn backend(&self) -> Arc<dyn SchedulerBackend> {
let guard = self.backend.lock().unwrap_or_else(|e| e.into_inner());
Arc::clone(&guard)
}
fn set_backend(&self, backend: Arc<dyn SchedulerBackend>) {
let mut guard = self.backend.lock().unwrap_or_else(|e| e.into_inner());
*guard = backend;
}
fn set_wake_ui(&self, waker: WakeUiFn) {
let mut guard = self.wake_ui.lock().unwrap_or_else(|e| e.into_inner());
*guard = Some(waker);
}
fn notify_ui(&self) {
let cb = {
let guard = self.wake_ui.lock().unwrap_or_else(|e| e.into_inner());
guard.as_ref().map(Arc::clone)
};
if let Some(cb) = cb {
cb();
}
}
fn enqueue_ready(&self, task_id: u64) {
{
let mut q = self.ready_queue.lock().unwrap_or_else(|e| e.into_inner());
q.push_back(task_id);
}
self.notify_ui();
}
fn enqueue_cancel(&self, task_id: u64) {
{
let mut c = self.cancelled_ids.lock().unwrap_or_else(|e| e.into_inner());
c.push(task_id);
}
self.notify_ui();
}
fn has_ready(&self) -> bool {
let q = self.ready_queue.lock().unwrap_or_else(|e| e.into_inner());
!q.is_empty()
}
fn has_cancelled(&self) -> bool {
let c = self
.cancelled_ids
.lock()
.unwrap_or_else(|e| e.into_inner());
!c.is_empty()
}
}
struct FgTaskWaker {
shared: Arc<ForegroundShared>,
task_id: u64,
queued: Arc<AtomicBool>,
cancelled: Arc<AtomicBool>,
}
impl FgTaskWaker {
fn schedule(&self) {
if self.cancelled.load(Ordering::Acquire) {
return;
}
if !self.queued.swap(true, Ordering::AcqRel) {
self.shared.enqueue_ready(self.task_id);
}
}
}
impl Wake for FgTaskWaker {
fn wake(self: Arc<Self>) {
self.schedule();
}
fn wake_by_ref(self: &Arc<Self>) {
self.schedule();
}
}
struct FgTaskEntry {
future: Pin<Box<dyn Future<Output = ()>>>,
cancelled: Arc<AtomicBool>,
queued: Arc<AtomicBool>,
}
pub(crate) struct ForegroundLocal {
tasks: HashMap<u64, FgTaskEntry>,
defers: VecDeque<Box<dyn FnOnce(&mut ActionScope)>>,
}
impl ForegroundLocal {
fn new() -> Self {
Self {
tasks: HashMap::new(),
defers: VecDeque::new(),
}
}
}
pub struct ForegroundExecutor {
shared: Arc<ForegroundShared>,
local: Rc<RefCell<ForegroundLocal>>,
}
impl Default for ForegroundExecutor {
fn default() -> Self {
Self::new()
}
}
impl ForegroundExecutor {
pub fn new() -> Self {
Self::with_backend(NativeStdScheduler::global())
}
pub fn with_backend(backend: Arc<dyn SchedulerBackend>) -> Self {
Self {
shared: ForegroundShared::new(backend),
local: Rc::new(RefCell::new(ForegroundLocal::new())),
}
}
pub fn set_backend(&self, backend: Arc<dyn SchedulerBackend>) {
self.shared.set_backend(backend);
}
#[inline]
pub fn backend(&self) -> Arc<dyn SchedulerBackend> {
self.shared.backend()
}
#[inline]
pub fn background_executor(&self) -> BackgroundExecutor {
BackgroundExecutor::with_backend(self.shared.backend())
}
#[inline]
pub fn scheduler_name(&self) -> &'static str {
self.shared.backend().name()
}
#[inline]
pub fn scheduler_capabilities(&self) -> SchedulerCapabilities {
self.shared.backend().capabilities()
}
pub fn set_wake_ui(&self, waker: impl Fn() + Send + Sync + 'static) {
self.shared.set_wake_ui(Arc::new(waker));
}
#[inline]
pub fn async_app(&self) -> AsyncApp {
AsyncApp {
shared: Arc::clone(&self.shared),
local: Rc::clone(&self.local),
}
}
pub fn spawn<T, F, Fut>(&self, f: F) -> Task<T>
where
T: 'static,
F: FnOnce(AsyncApp) -> Fut,
Fut: Future<Output = T> + 'static,
{
self.async_app().spawn(f)
}
pub fn defer<F>(&self, f: F)
where
F: FnOnce(&mut ActionScope) + 'static,
{
self.local.borrow_mut().defers.push_back(Box::new(f));
self.shared.notify_ui();
}
#[inline]
pub fn has_ready_work(&self) -> bool {
self.shared.has_ready() || !self.local.borrow().defers.is_empty()
}
#[inline]
pub(crate) fn has_work_to_poll(&self) -> bool {
self.has_ready_work() || self.shared.has_cancelled()
}
#[inline]
pub fn active_task_count(&self) -> usize {
self.local.borrow().tasks.len()
}
pub fn poll_ready(&self, pool: &mut PodPool, now_ms: u32) -> bool {
let _guard = ActiveEngineGuard::enter(pool, now_ms);
let mut did_work = false;
for _round in 0..64 {
let cancelled_batch: Vec<u64> = {
let mut c = self
.shared
.cancelled_ids
.lock()
.unwrap_or_else(|e| e.into_inner());
core::mem::take(&mut *c)
};
if !cancelled_batch.is_empty() {
let mut loc = self.local.borrow_mut();
for id in cancelled_batch {
loc.tasks.remove(&id);
}
}
let ready_batch: Vec<u64> = {
let mut q = self
.shared
.ready_queue
.lock()
.unwrap_or_else(|e| e.into_inner());
q.drain(..).collect()
};
let mut progressed_this_round = false;
for task_id in ready_batch {
let entry_opt = self.local.borrow_mut().tasks.remove(&task_id);
let Some(mut entry) = entry_opt else {
continue;
};
entry.queued.store(false, Ordering::Release);
if entry.cancelled.load(Ordering::Acquire) {
continue;
}
did_work = true;
progressed_this_round = true;
let waker = Waker::from(Arc::new(FgTaskWaker {
shared: Arc::clone(&self.shared),
task_id,
queued: Arc::clone(&entry.queued),
cancelled: Arc::clone(&entry.cancelled),
}));
let mut cx = Context::from_waker(&waker);
match entry.future.as_mut().poll(&mut cx) {
Poll::Ready(()) => {}
Poll::Pending => {
if !entry.cancelled.load(Ordering::Acquire) {
self.local.borrow_mut().tasks.insert(task_id, entry);
}
}
}
}
loop {
let next_defer = self.local.borrow_mut().defers.pop_front();
let Some(cb) = next_defer else {
break;
};
did_work = true;
progressed_this_round = true;
let ctx = ACTIVE_ENGINE_CTX.with(|c| c.get());
if !ctx.pool_ptr.is_null() {
let pool_ref = unsafe { &mut *ctx.pool_ptr };
let mut acx = ActionScope::new(
PodId(u16::MAX),
pool_ref,
(0, 0),
DragEvent::default(),
ctx.now_ms,
)
.with_async_app(self.async_app());
cb(&mut acx);
acx.flush_signal_dirty_pods();
}
}
let ctx = ACTIVE_ENGINE_CTX.with(|c| c.get());
if !ctx.pool_ptr.is_null() {
unsafe { &mut *ctx.pool_ptr }.flush_signal_dirty_pods();
}
if !progressed_this_round || !self.has_ready_work() {
break;
}
}
did_work
}
}
#[derive(Clone)]
pub struct AsyncApp {
shared: Arc<ForegroundShared>,
local: Rc<RefCell<ForegroundLocal>>,
}
impl AsyncApp {
#[inline]
pub fn background_executor(&self) -> BackgroundExecutor {
BackgroundExecutor::with_backend(self.shared.backend())
}
#[inline]
pub fn background_spawn<T, Fut>(&self, fut: Fut) -> Task<T>
where
T: Send + 'static,
Fut: Future<Output = T> + Send + 'static,
{
self.background_executor().spawn(fut)
}
#[inline]
pub fn timer(&self, duration: Duration) -> Timer {
self.background_executor().timer(duration)
}
#[inline]
pub fn scheduler_name(&self) -> &'static str {
self.shared.backend().name()
}
pub fn spawn<T, F, Fut>(&self, f: F) -> Task<T>
where
T: 'static,
F: FnOnce(AsyncApp) -> Fut,
Fut: Future<Output = T> + 'static,
{
let task_id = self.shared.next_id.fetch_add(1, Ordering::Relaxed);
let cancelled = Arc::new(AtomicBool::new(false));
let queued = Arc::new(AtomicBool::new(true));
let shared_for_cancel = Arc::clone(&self.shared);
let on_cancel: Arc<dyn Fn() + Send + Sync> = Arc::new(move || {
shared_for_cancel.enqueue_cancel(task_id);
});
let task_shared = TaskShared::new(Arc::clone(&cancelled), Some(on_cancel));
let task_out = Arc::clone(&task_shared);
let fut = f(self.clone());
let wrapped = async move {
let output = fut.await;
if !task_out.is_cancelled() {
task_out.complete(output);
}
};
self.local.borrow_mut().tasks.insert(
task_id,
FgTaskEntry {
future: Box::pin(wrapped),
cancelled,
queued,
},
);
self.shared.enqueue_ready(task_id);
Task::from_shared(task_shared)
}
pub fn spawn_for<V, T, F, Fut>(&self, handle: Handle<V>, f: F) -> Task<T>
where
V: View,
T: 'static,
F: FnOnce(Handle<V>, AsyncApp) -> Fut,
Fut: Future<Output = T> + 'static,
{
self.spawn(move |cx| f(handle, cx))
}
#[inline]
pub fn defer<F>(&self, f: F)
where
F: FnOnce(&mut ActionScope) + 'static,
{
self.local.borrow_mut().defers.push_back(Box::new(f));
self.shared.notify_ui();
}
pub fn update<V: View, R>(
&mut self,
handle: Handle<V>,
f: impl FnOnce(&mut V, &mut ActionScope) -> R,
) -> Option<R> {
let ctx = ACTIVE_ENGINE_CTX.with(|c| c.get());
if ctx.pool_ptr.is_null() {
return None;
}
let pool = unsafe { &mut *ctx.pool_ptr };
let target_ptr = pool.acquire_raw_mut(handle.id, Some(handle.generation), TypeId::of::<V>())?;
let mut acx = ActionScope::new(
handle.id,
pool,
(0, 0),
DragEvent::default(),
ctx.now_ms,
)
.with_async_app(self.clone());
let view_ref = unsafe { &mut *(target_ptr as *mut V) };
let res = f(view_ref, &mut acx);
let skipped = acx.notify_skipped;
acx.release_view_borrow(handle.id);
if core::mem::size_of::<V>() > 0 && !skipped {
acx.notify();
}
acx.flush_signal_dirty_pods();
Some(res)
}
pub fn read<V: View, R>(&self, handle: Handle<V>, f: impl FnOnce(&V) -> R) -> Option<R> {
let ctx = ACTIVE_ENGINE_CTX.with(|c| c.get());
if ctx.pool_ptr.is_null() {
return None;
}
let pool = unsafe { &*ctx.pool_ptr };
let view = pool.get(handle)?;
Some(f(view))
}
pub fn notify_handle<V: View>(&mut self, handle: Handle<V>) {
let ctx = ACTIVE_ENGINE_CTX.with(|c| c.get());
if ctx.pool_ptr.is_null() {
return;
}
let pool = unsafe { &mut *ctx.pool_ptr };
if pool.get(handle).is_some() {
pool.mark_dirty(handle.id);
}
}
#[inline]
pub fn now_ms(&self) -> u32 {
ACTIVE_ENGINE_CTX.with(|c| c.get().now_ms)
}
}