Skip to main content

PreemptiveCancellationToken

Struct PreemptiveCancellationToken 

Source
pub struct PreemptiveCancellationToken { /* private fields */ }
Expand description

Preemptive cancellation token.

Cloning is cheap – all clones share the same underlying state.

Implementations§

Source§

impl PreemptiveCancellationToken

Source

pub fn new() -> Self

Create a fresh, non-cancelled token.

Source

pub fn cancel(&self)

Cancel immediately (cooperative – tasks must call check() or is_cancelled()).

Source

pub fn cancel_with(&self, reason: impl Into<String>)

Cancel with a human-readable reason.

Source

pub fn cancel_after(&self, after: Duration)

Schedule automatic cancellation after the given duration.

Spawns a lightweight watchdog thread that sleeps until the deadline, then atomically sets the cancelled flag. Returns immediately.

Source

pub fn cancel_after_with(&self, after: Duration, reason: impl Into<String>)

Schedule automatic cancellation with a reason.

Source

pub fn cancel_at(&self, deadline: Instant)

Schedule automatic cancellation at an absolute Instant.

Source

pub fn deadline_guard(&self, budget: Duration) -> DeadlineGuard

Create a DeadlineGuard that cancels this token if dropped after the budget elapses. The token is also auto-cancelled via a watchdog thread, so even if the guard is never explicitly dropped, cancellation fires.

Source

pub fn is_cancelled(&self) -> bool

true if the task has been cancelled/preempted.

Source

pub fn check(&self) -> Result<(), Preempted>

Returns Ok(()) when not cancelled; Err(Preempted) otherwise.

Designed for use inside task loops:

token.check()?;
Source

pub fn check_and_yield(&self) -> Result<(), Preempted>

Check and yield the current thread to the OS scheduler.

Useful in compute-bound loops – gives the runtime a chance to run the watchdog or other pending tasks, reducing observed cancellation latency without expensive system calls.

Source

pub fn reset(&self)

Reset the token so it can be reused for a new task. Cancels any pending watchdog (watchdog will wake, see cancelled=true, then exit without re-setting because deadline_unix_ms is zeroed).

Source

pub fn reason(&self) -> Option<String>

The reason supplied to cancel_with / cancel_after_with, if any.

Source

pub unsafe fn install_signal_handler()

Install a per-process SIGUSR2 handler that sets a thread-local cancellation flag. Call once at program startup on Linux.

After installation, signal_preempt_thread(handle) will send SIGUSR2 to any thread, making its next check_signal() call return Err.

§Safety

Installs a signal handler via libc. Safe if called at most once and not combined with other SIGUSR2 handlers.

Source

pub fn signal_preempt_thread(pthread_id: pthread_t)

Send SIGUSR2 to a specific thread to trigger signal-based preemption.

The target thread must have called install_signal_handler() first.

Source

pub fn check_signal() -> Result<(), Preempted>

Check the thread-local signal flag set by the SIGUSR2 handler.

Trait Implementations§

Source§

impl Clone for PreemptiveCancellationToken

Source§

fn clone(&self) -> PreemptiveCancellationToken

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PreemptiveCancellationToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PreemptiveCancellationToken

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.