#[doc(hidden)]
pub mod dispatch;
#[cfg(target_family = "wasm")]
mod hot_control;
#[doc(hidden)]
pub mod lifecycle;
mod machinery;
mod method_registry;
mod root_control;
use lifecycle::ReclaimableKey as _;
#[doc(hidden)]
#[cfg(target_family = "wasm")]
pub const __ROOT_POLICY: [u8; 32] =
cdk_rsx_source_policy::source_policy!("root_control.rs", "hot_control.rs", "lifecycle.rs");
pub fn is_idle() -> bool {
machinery::METHODS.inspect(|m| m.is_none_or(|m| m.is_empty()))
&& machinery::TASKS.inspect(|t| t.is_none_or(|t| t.is_empty()))
}
#[doc(hidden)]
pub fn shutdown() -> Result<(), &'static str> {
if !is_idle()
|| machinery::CURRENT_METHOD.get().is_some()
|| machinery::CURRENT_TASK_ID.get().is_some()
|| machinery::RECOVERING.get()
|| machinery::QUERY_EXECUTION.get()
{
return Err("executor shutdown requires drained tasks and no active context");
}
drop(machinery::MIGRATORY_WAKEUPS.take());
drop(machinery::TASKS.take());
drop(machinery::METHODS.take());
Ok(())
}
#[cfg(test)]
mod shutdown_tests {
use super::*;
use std::{cell::Cell, rc::Rc};
#[test]
fn shutdown_frees_empty_backing_and_executor_can_restart() {
let calls = Rc::new(Cell::new(0));
for _ in 0..3 {
let next = calls.clone();
in_tracking_executor_context(|| {
spawn_protected(async move {
next.set(next.get() + 1);
})
});
assert!(is_idle());
shutdown().unwrap();
assert!(machinery::METHODS.inspect(|value| value.is_none()));
assert!(machinery::TASKS.inspect(|value| value.is_none()));
assert!(machinery::MIGRATORY_WAKEUPS.inspect(|value| value.is_none()));
assert!(is_idle()); assert!(machinery::METHODS.inspect(|value| value.is_none()));
}
assert_eq!(calls.get(), 3);
assert_eq!(Rc::strong_count(&calls), 1);
}
#[test]
fn shutdown_refuses_an_active_context_without_cancelling_it() {
in_tracking_executor_context(|| {
assert!(shutdown().is_err());
assert!(!is_idle());
});
assert!(is_idle());
shutdown().unwrap();
}
}
#[doc(inline)]
pub use machinery::{
MethodHandle, TaskHandle, cancel_all_tasks_attached_to_current_method, cancel_task,
extend_current_method_context, in_callback_executor_context_for, in_tracking_executor_context,
in_tracking_query_executor_context, in_trap_recovery_context_for, is_recovering_from_trap,
spawn_migratory, spawn_protected,
};
#[doc(hidden)]
pub use machinery::in_tracking_executor_context_without_poll;
#[doc(hidden)]
pub fn is_query_context() -> bool {
machinery::QUERY_EXECUTION.get()
}
#[cfg(test)]
mod memory_capability_tests {
use super::*;
use std::{
cell::Cell,
future::Future,
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
struct PendingProbe(Rc<Cell<Option<bool>>>);
impl Future for PendingProbe {
type Output = ();
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<()> {
Poll::Pending
}
}
impl Drop for PendingProbe {
fn drop(&mut self) {
self.0.set(Some(is_query_context()));
}
}
#[test]
fn query_fence_covers_cancellation_and_callbacks() {
let observed = Rc::new(Cell::new(None));
in_tracking_query_executor_context(|| {
spawn_protected(PendingProbe(observed.clone()));
});
assert_eq!(observed.get(), Some(true));
assert!(!is_query_context());
let callback = in_tracking_query_executor_context(extend_current_method_context);
in_callback_executor_context_for(callback, || assert!(is_query_context()));
assert!(!is_query_context());
in_tracking_executor_context(|| {
spawn_protected(PendingProbe(observed.clone()));
});
assert_eq!(observed.get(), Some(false));
}
}
#[cfg(target_family = "wasm")]
#[doc(hidden)]
pub unsafe fn __with_store_controls<R>(
f: impl FnOnce(&std::cell::Cell<*const ()>, &std::cell::Cell<bool>) -> R,
) -> R {
unsafe { hot_control::with_store_controls(f) }
}