diff --git a/src/api.rs b/src/api.rs index b1f1ca1..8951a64 100644 --- a/src/api.rs +++ b/src/api.rs @@ -107,11 +107,9 @@ fn start_server_observed( continue; } worker.refresh_viewport(&worker_namespace, &worker_notify); - let wait = if worker - .repl - .as_ref() - .is_some_and(|(_, runtime)| runtime.has_pending_refresh()) - { + let wait = if worker.repl.as_ref().is_some_and(|(_, runtime)| { + runtime.has_pending_refresh() || runtime.has_cooperative_tasks() + }) { std::time::Duration::from_millis(1) } else { std::time::Duration::from_millis(100) diff --git a/src/endor_host.rs b/src/endor_host.rs index 4c1fdc3..b7ccfe2 100644 --- a/src/endor_host.rs +++ b/src/endor_host.rs @@ -555,6 +555,10 @@ impl ReplSession { result } + pub fn has_cooperative_tasks(&self) -> bool { + self.bridge.has_cooperative_tasks() + } + pub fn has_pending_refresh(&self) -> bool { self.refresh.is_some() } diff --git a/src/tasks.rs b/src/tasks.rs index ce4e28c..6b3513f 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,7 +1,10 @@ //! Owner-managed asynchronous work. Only weak identities enter the scheduler; //! callbacks and scopes are ordinary traced edges in the Aster object graph. use std::{ - sync::{Arc, Mutex}, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering}, + }, time::{Duration, Instant}, }; @@ -215,6 +218,7 @@ struct After { provider: crate::io_tasks::Provider, signature: endor_core::CallSignature, index: Index, + runnable: Arc, desktop: ObjectRef, } impl NativeCallable for After { @@ -407,6 +411,9 @@ impl NativeCallable for After { live.retain(|old| Some(*old) != previous_weak); live.push(weak); *index = live; + if self.operation == Operation::Spawn { + self.runnable.store(true, Ordering::Release); + } Ok(NativeResult::Return(Value::Object(task))) } } @@ -512,10 +519,14 @@ impl NativeCallable for Method { pub struct Scheduler { index: Index, + runnable: Arc, desktop: ObjectRef, provider: crate::io_tasks::Provider, } impl Scheduler { + pub fn has_cooperative_work(&self) -> bool { + self.runnable.load(Ordering::Acquire) + } /// Mutation boundaries collect unreachable owner/callback cycles before /// this weak lookup. Idle timer polling does not rescan the entire heap. pub fn poll(&self, vm: &mut Vm, limit: usize) -> Result { @@ -679,6 +690,7 @@ impl Scheduler { .map_err(|_| anyhow::anyhow!("task index poisoned"))?; let count = index.len(); index.clear(); + self.runnable.store(false, Ordering::Release); Ok(count) } fn prune(&self, vm: &mut Vm) -> Result<()> { @@ -687,11 +699,14 @@ impl Scheduler { .lock() .map_err(|_| anyhow::anyhow!("task index poisoned"))?; let mut live = Vec::new(); + let mut runnable = false; for weak in index.iter().copied() { if let Some(task) = vm.upgrade(weak)? { - let pending = vm - .payload::(task, "Task") - .map(|task| matches!(task.state, State::Pending | State::Running)); + let pending = vm.payload::(task, "Task").map(|task| { + let pending = matches!(task.state, State::Pending | State::Running); + runnable |= pending && task.cooperative; + pending + }); vm.release(task)?; if pending? { live.push(weak); @@ -699,6 +714,7 @@ impl Scheduler { } } *index = live; + self.runnable.store(runnable, Ordering::Release); Ok(()) } } @@ -815,6 +831,7 @@ pub fn install( desktop: ObjectRef, ) -> Result { let index = Arc::new(Mutex::new(Vec::new())); + let runnable = Arc::new(AtomicBool::new(false)); let definition = desktop_type(vm.object_store(), "Task", desktop)?; let provider = crate::io_tasks::Provider::default(); for (name, operation) in [ @@ -858,6 +875,7 @@ pub fn install( signature }, index: index.clone(), + runnable: runnable.clone(), desktop, }, vec![desktop], @@ -898,6 +916,7 @@ pub fn install( } Ok(Scheduler { index, + runnable, desktop, provider, }) diff --git a/src/ui_bridge.rs b/src/ui_bridge.rs index 42f59c1..e1610ba 100644 --- a/src/ui_bridge.rs +++ b/src/ui_bridge.rs @@ -1812,6 +1812,10 @@ fn project_tabs( } impl UiBridge { + pub fn has_cooperative_tasks(&self) -> bool { + self.tasks.has_cooperative_work() + } + pub fn poll_tasks(&self, vm: &mut Vm, limit: usize) -> Result { self.tasks.poll(vm, limit) }