Skip to main content

Cancel

Struct Cancel 

Source
pub struct Cancel(/* private fields */);
Expand description

A handle that stops the runs that watch it.

A run watches a cancel that RunOptions::cancel gave it. Cancel::cancel stops each watching run with its escalation, and the run ends as Stopped(Cancelled). A run that starts with a cancel that is already cancelled starts no child. Clones share one state, so any clone cancels, and a cancel stays cancelled.

Any thread may call Cancel::cancel at any time, before, during or after a run. A cancel that arrives once the run has seen the child’s exit changes nothing, and the run gives the child’s own end. When the run sees a cancel and the exit together, the exit wins. Otherwise the run gives Stopped(Cancelled), which a cancel just after the exit can also give. ChildFinished::status gives the child’s own status in each case.

The run does not poll a flag of the caller’s. A tool whose stop request is a flag, such as one that a signal handler sets, polls that flag on a thread of its own and calls Cancel::cancel from there. A signal handler itself must not call it, because it takes a lock. Such a tool decides what the stop means from its own flag, because a status such as Signal(2) also follows a signal that another process sent.

use std::process::Command;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use crumbkit_process::{Cancel, RunOptions, run};

fn run_until(flag: &Arc<AtomicBool>, command: Command) -> bool {
    let cancel = Cancel::new();
    let done = Arc::new(AtomicBool::new(false));
    let poller = std::thread::spawn({
        let (flag, cancel, done) = (Arc::clone(flag), cancel.clone(), Arc::clone(&done));
        move || {
            while !done.load(Ordering::Relaxed) {
                if flag.load(Ordering::Relaxed) {
                    cancel.cancel();
                    return;
                }
                std::thread::sleep(Duration::from_millis(10));
            }
        }
    });
    let finished = run(command, &RunOptions::new().cancel(&cancel));
    done.store(true, Ordering::Relaxed);
    let _ = poller.join();
    finished.is_ok()
}

Implementations§

Source§

impl Cancel

Source

pub fn new() -> Self

Returns a cancel that is not cancelled.

Source

pub fn cancel(&self)

Cancels every run that watches this cancel or that starts watching it later.

Source

pub fn is_cancelled(&self) -> bool

Returns true once Cancel::cancel has been called on this cancel or a clone of it.

Trait Implementations§

Source§

impl Clone for Cancel

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Cancel

Source§

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

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

impl Default for Cancel

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Forget for Cancel

§

impl Freeze for Cancel

§

impl Move for Cancel

§

impl RefUnwindSafe for Cancel

§

impl Send for Cancel

§

impl Sync for Cancel

§

impl Unpin for Cancel

§

impl UnsafeUnpin for Cancel

§

impl UnwindSafe for Cancel

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.