Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CancellationToken Class Reference

Read-only cooperative cancellation token. More...

#include <thread_pool.H>

Collaboration diagram for Aleph::CancellationToken:
[legend]

Classes

class  ConditionVariableRegistration
 RAII guard for condition variable cancellation registration. More...
 

Public Member Functions

 CancellationToken ()=default
 Default constructor.
 
bool stop_requested () const noexcept
 Return true if cancellation has been requested.
 
bool is_cancellation_requested () const noexcept
 Synonym for stop_requested().
 
void throw_if_cancellation_requested () const
 Throw operation_canceled if cancellation was requested.
 
bool valid () const noexcept
 Return true if the token is connected to a source.
 
ConditionVariableRegistration notify_on_cancel (std::condition_variable &cv) const
 Request notification of a condition variable on cancellation.
 
ConditionVariableRegistration notify_on_cancel (std::condition_variable &cv, std::mutex &mutex) const
 Request notification of a condition variable on cancellation, without missing it.
 

Private Member Functions

 CancellationToken (std::shared_ptr< cancellation_detail::Exact_State > state) noexcept
 

Private Attributes

std::shared_ptr< cancellation_detail::Exact_State > state_
 

Friends

class CancellationSource
 

Detailed Description

Read-only cooperative cancellation token.

Tokens are cheap to copy and can be captured by tasks. They never force a task to stop; instead, user code or higher-level helpers can observe the request and exit gracefully.

Definition at line 292 of file thread_pool.H.

Constructor & Destructor Documentation

◆ CancellationToken() [1/2]

Aleph::CancellationToken::CancellationToken ( std::shared_ptr< cancellation_detail::Exact_State >  state)
inlineexplicitprivatenoexcept

Definition at line 296 of file thread_pool.H.

◆ CancellationToken() [2/2]

Aleph::CancellationToken::CancellationToken ( )
default

Default constructor.

Member Function Documentation

◆ is_cancellation_requested()

bool Aleph::CancellationToken::is_cancellation_requested ( ) const
inlinenoexcept

Synonym for stop_requested().

Definition at line 395 of file thread_pool.H.

References stop_requested().

◆ notify_on_cancel() [1/2]

ConditionVariableRegistration Aleph::CancellationToken::notify_on_cancel ( std::condition_variable &  cv) const
inline

Request notification of a condition variable on cancellation.

This is primarily intended for blocking helpers that need to wake up promptly when cooperative cancellation is requested.

Warning
The notification can come after a waiter has checked stop_requested() and before it waits, and then the waiter misses it; a waiter should wait with a timeout and check the token again.
Deprecated:
Use notify_on_cancel(cv, mutex), which cannot miss the notification. Deprecated in 7.1.0; it may be removed in 8.0.0.

Definition at line 426 of file thread_pool.H.

References state_, and stop_requested().

Referenced by Aleph::BoundedChannel< T >::wait_with_cancellation().

◆ notify_on_cancel() [2/2]

ConditionVariableRegistration Aleph::CancellationToken::notify_on_cancel ( std::condition_variable &  cv,
std::mutex &  mutex 
) const
inline

Request notification of a condition variable on cancellation, without missing it.

mutex is the mutex that the waiters on cv hold while they check their condition and wait. A cancellation takes and releases mutex before it notifies cv, so it cannot come between the check of the token and the wait: a waiter that checks stop_requested() under mutex after this call, and waits on cv for that check, always wakes up. For example:

std::unique_lock<std::mutex> lock(mutex);
registration = token.notify_on_cancel(cv, mutex);
cv.wait(lock, [&] { return ready or token.stop_requested(); });
RAII guard for condition variable cancellation registration.

A cancellation takes mutex to notify the registration, and ending the registration, by destroying or assigning its guard, waits for a notification of it in progress. So, to avoid a deadlock, treat both requesting the cancellation and ending the registration as taking mutex:

  • Not while holding mutex. Declaring the guard before the lock, as above, ends the registration after the lock has released the mutex.
  • Not while holding another mutex that a thread may take while it holds mutex, including in these two ways. For example, two threads that each hold the mutex of one registration and end the other one can deadlock.

Ending a registration waits for any notification of it in progress; after that, the cancellation no longer uses cv and mutex through it.

Parameters
cvCondition variable to notify on cancellation.
mutexMutex that the waiters on cv hold.
Returns
A guard that ends the registration. It is empty if the token has no source or the cancellation was already requested; the check of the token after this call then sees it.
Exceptions
std::bad_allocif the registration cannot be allocated.

Definition at line 479 of file thread_pool.H.

References state_, and stop_requested().

◆ stop_requested()

bool Aleph::CancellationToken::stop_requested ( ) const
inlinenoexcept

Return true if cancellation has been requested.

Definition at line 389 of file thread_pool.H.

References Aleph::and, and state_.

Referenced by is_cancellation_requested(), notify_on_cancel(), notify_on_cancel(), throw_if_cancellation_requested(), and Aleph::BoundedChannel< T >::wait_with_cancellation().

◆ throw_if_cancellation_requested()

void Aleph::CancellationToken::throw_if_cancellation_requested ( ) const
inline

◆ valid()

bool Aleph::CancellationToken::valid ( ) const
inlinenoexcept

Return true if the token is connected to a source.

Definition at line 408 of file thread_pool.H.

References state_.

Friends And Related Symbol Documentation

◆ CancellationSource

friend class CancellationSource
friend

Definition at line 299 of file thread_pool.H.

Member Data Documentation

◆ state_

std::shared_ptr<cancellation_detail::Exact_State> Aleph::CancellationToken::state_
private

Definition at line 294 of file thread_pool.H.

Referenced by notify_on_cancel(), notify_on_cancel(), stop_requested(), and valid().


The documentation for this class was generated from the following file: