Documentation

Init.System.CancelToken

structure IO.CancelToken :

Cancellation token for cooperative task cancellation: request cancellation with CancelToken.set and check for it with CancelToken.isSet. To react to cancellation without polling, register a callback with CancelToken.onSet.

This is a more flexible alternative to Task.cancel as the token can be shared between multiple tasks.

Note: the underlying Promise and the task it produces are kept private. If a future change exposes the underlying task (or the promise) publicly, the set implementation must be audited for races between resolving the promise and writing setRef: callers observing isSet = true and then waiting on the task currently rely on the order chosen in set, and that ordering interacts with the cheap Bool fast path read by the C++ runtime.

Instances For

    Creates a new cancellation token.

    Note: cannot be called from initialize blocks because the underlying task manager is not yet running at that point. Construct lazily on first use instead.

    Equations
    Instances For

      Activates a cancellation token. Idempotent.

      Resolves the underlying promise before setting the Bool flag, so that observing isSet = true implies that any synchronously-chained onSet callbacks have already run. The reverse implication does not hold: a callback running synchronously inside set may briefly observe isSet = false.

      Equations
      Instances For

        Checks whether the cancellation token has been activated.

        Equations
        Instances For

          Registers a callback to run when the cancellation token is set or dropped. The callback runs as a synchronous task dependency, so it executes inline on the thread that calls set (or on the finalizer thread if the token is dropped). If the token is already set when onSet is called, the callback runs immediately.

          Equations
          Instances For