Skip to main content

rustymath_core/
interrupt.rs

1//! The process's interrupt check. Long computations call [`check`] between steps, about every [`BETWEEN_CHECKS`](crate::BETWEEN_CHECKS) items, and stop with its error. A frontend installs its check with [`install`]: basedpl's stops the running evaluation at an interrupt or a passed deadline, and rustymath's Python bindings' raise the exception of a pending signal. Until a frontend installs one, `check` passes.
2use crate::error::Error;
3use std::sync::OnceLock;
4
5static HOOK: OnceLock<fn() -> Result<(), Error>> = OnceLock::new();
6
7/// Makes `hook` the process's interrupt check. The first check installed stays, and a later call does nothing.
8pub fn install(hook: fn() -> Result<(), Error>) { HOOK.get_or_init(|| hook); }
9
10/// The installed check's result: an error of kind Interrupt or Timeout stops the computation that called it. `Ok` until a frontend installs a check.
11#[inline]
12pub fn check() -> Result<(), Error> { HOOK.get().map_or(Ok(()), |hook| hook()) }