Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::cancellation_token Class Reference

Provides a mechanism for cooperative cancellation of operations. More...

#include <cancellation_token.h>

Collaboration diagram for kcenon::thread::cancellation_token:
Collaboration graph

Classes

struct  token_state
 

Public Member Functions

 cancellation_token ()
 
 cancellation_token (const cancellation_token &)=default
 
cancellation_tokenoperator= (const cancellation_token &)=default
 
 cancellation_token (cancellation_token &&)=default
 
cancellation_tokenoperator= (cancellation_token &&)=default
 
void cancel ()
 Cancels the operation.
 
bool is_cancelled () const
 Checks if the token has been canceled.
 
common::VoidResult check_cancelled () const
 Checks if the token has been canceled and returns an error result.
 
void register_callback (std::function< void()> callback)
 Registers a callback to be invoked when the token is canceled.
 
 cancellation_token ()
 
 cancellation_token (const cancellation_token &)=default
 
cancellation_tokenoperator= (const cancellation_token &)=default
 
 cancellation_token (cancellation_token &&)=default
 
cancellation_tokenoperator= (cancellation_token &&)=default
 
void cancel ()
 Cancels the operation.
 
bool is_cancelled () const
 Checks if the token has been canceled.
 
common::VoidResult check_cancelled () const
 Checks if the token has been canceled and returns an error result.
 
void register_callback (std::function< void()> callback)
 Registers a callback to be invoked when the token is canceled.
 

Static Public Member Functions

static cancellation_token create ()
 Creates a new cancellation token.
 
static cancellation_token create_linked (std::initializer_list< cancellation_token > tokens)
 Creates a linked token that is canceled when any of the parent tokens are canceled.
 
static cancellation_token create ()
 Creates a new cancellation token.
 
static cancellation_token create_linked (std::initializer_list< cancellation_token > tokens)
 Creates a linked token that is canceled when any of the parent tokens are canceled.
 

Private Member Functions

 cancellation_token (std::shared_ptr< token_state > state)
 
 cancellation_token (std::shared_ptr< token_state > state)
 

Private Attributes

std::shared_ptr< token_statestate_
 

Detailed Description

Provides a mechanism for cooperative cancellation of operations.

Cancellation tokens allow long-running operations to be gracefully canceled. They are particularly useful for worker threads that need to be notified when their work should be aborted.

Definition at line 30 of file cancellation_token.h.

Constructor & Destructor Documentation

◆ cancellation_token() [1/8]

kcenon::thread::cancellation_token::cancellation_token ( std::shared_ptr< token_state > state)
inlineexplicitprivate

Definition at line 42 of file cancellation_token.h.

43 : state_(std::move(state)) {}
std::shared_ptr< token_state > state_

◆ cancellation_token() [2/8]

kcenon::thread::cancellation_token::cancellation_token ( )
inline

Definition at line 47 of file cancellation_token.h.

47: state_(std::make_shared<token_state>()) {}

Referenced by create().

Here is the caller graph for this function:

◆ cancellation_token() [3/8]

kcenon::thread::cancellation_token::cancellation_token ( const cancellation_token & )
default

◆ cancellation_token() [4/8]

kcenon::thread::cancellation_token::cancellation_token ( cancellation_token && )
default

◆ cancellation_token() [5/8]

kcenon::thread::cancellation_token::cancellation_token ( std::shared_ptr< token_state > state)
inlineexplicitprivate

Definition at line 42 of file cancellation_token.h.

43 : state_(std::move(state)) {}

◆ cancellation_token() [6/8]

kcenon::thread::cancellation_token::cancellation_token ( )
inline

Definition at line 47 of file cancellation_token.h.

47: state_(std::make_shared<token_state>()) {}

◆ cancellation_token() [7/8]

kcenon::thread::cancellation_token::cancellation_token ( const cancellation_token & )
default

◆ cancellation_token() [8/8]

kcenon::thread::cancellation_token::cancellation_token ( cancellation_token && )
default

Member Function Documentation

◆ cancel() [1/2]

void kcenon::thread::cancellation_token::cancel ( )
inline

Cancels the operation.

Sets the token to the canceled state and invokes all registered callbacks.

Note
This method is thread-safe and guarantees callbacks are invoked exactly once.

Definition at line 107 of file cancellation_token.h.

107 {
108 std::vector<std::function<void()>> callbacks_to_invoke;
109
110 {
111 std::lock_guard<std::mutex> lock(state_->callback_mutex);
112 bool was_cancelled = state_->is_cancelled.exchange(true);
113 if (!was_cancelled) {
114 // Move callbacks to local vector to avoid holding lock during invocation
115 callbacks_to_invoke = std::move(state_->callbacks);
116 state_->callbacks.clear();
117 }
118 }
119
120 // Invoke callbacks outside the lock to prevent deadlock
121 for (const auto& callback : callbacks_to_invoke) {
122 callback();
123 }
124 }
@ callback
Call user callback for custom decision.

References kcenon::thread::callback, and state_.

Referenced by kcenon::thread::cancellable_future< R >::cancel(), kcenon::thread::cancellable_future< void >::cancel(), and kcenon::thread::thread_pool::stop_unsafe().

Here is the caller graph for this function:

◆ cancel() [2/2]

void kcenon::thread::cancellation_token::cancel ( )
inline

Cancels the operation.

Sets the token to the canceled state and invokes all registered callbacks.

Memory Ordering:

  • Uses memory_order_release to ensure all prior writes are visible to threads that subsequently observe the cancellation
Note
This method is thread-safe and guarantees callbacks are invoked exactly once.

Definition at line 111 of file cancellation_token.h.

111 {
112 std::vector<std::function<void()>> callbacks_to_invoke;
113
114 {
115 std::lock_guard<std::mutex> lock(state_->callback_mutex);
116 bool was_cancelled = state_->is_cancelled.exchange(true, std::memory_order_release);
117 if (!was_cancelled) {
118 // Move callbacks to local vector to avoid holding lock during invocation
119 callbacks_to_invoke = std::move(state_->callbacks);
120 state_->callbacks.clear();
121 }
122 }
123
124 // Invoke callbacks outside the lock to prevent deadlock
125 for (const auto& callback : callbacks_to_invoke) {
126 callback();
127 }
128 }

References kcenon::thread::callback, and state_.

◆ check_cancelled() [1/2]

common::VoidResult kcenon::thread::cancellation_token::check_cancelled ( ) const
inlinenodiscard

Checks if the token has been canceled and returns an error result.

Returns
common::VoidResult — error with operation_canceled if cancelled, success otherwise

Definition at line 139 of file cancellation_token.h.

139 {
140 if (is_cancelled()) {
142 }
143 return common::ok();
144 }
bool is_cancelled() const
Checks if the token has been canceled.
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.

References is_cancelled(), kcenon::thread::make_error_result(), and kcenon::thread::operation_canceled.

Here is the call graph for this function:

◆ check_cancelled() [2/2]

common::VoidResult kcenon::thread::cancellation_token::check_cancelled ( ) const
inlinenodiscard

Checks if the token has been canceled and returns an error result.

Returns
common::VoidResult — error with operation_canceled if cancelled, success otherwise

Definition at line 148 of file cancellation_token.h.

148 {
149 if (is_cancelled()) {
151 }
152 return common::ok();
153 }

References is_cancelled(), kcenon::thread::make_error_result(), and kcenon::thread::operation_canceled.

Here is the call graph for this function:

◆ create() [1/2]

static cancellation_token kcenon::thread::cancellation_token::create ( )
inlinestatic

Creates a new cancellation token.

Returns
A new cancellation token

Definition at line 59 of file cancellation_token.h.

References cancellation_token().

Referenced by create_linked(), and kcenon::thread::thread_pool::start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create() [2/2]

static cancellation_token kcenon::thread::cancellation_token::create ( )
inlinestatic

Creates a new cancellation token.

Returns
A new cancellation token

Definition at line 59 of file cancellation_token.h.

59 {
60 return cancellation_token();
61 }

References cancellation_token().

Here is the call graph for this function:

◆ create_linked() [1/2]

static cancellation_token kcenon::thread::cancellation_token::create_linked ( std::initializer_list< cancellation_token > tokens)
inlinestatic

Creates a linked token that is canceled when any of the parent tokens are canceled.

Parameters
tokensThe parent tokens
Returns
A new token linked to the parents
Note
Uses weak_ptr to avoid circular references and memory leaks

Definition at line 70 of file cancellation_token.h.

70 {
71 auto new_token = create();
72 auto new_state_weak = std::weak_ptr<token_state>(new_token.state_);
73
74 for (const auto& token : tokens) {
75 auto token_copy = token;
76 token_copy.register_callback([new_state_weak]() {
77 if (auto state = new_state_weak.lock()) {
78 // Directly set the cancelled flag and invoke callbacks
79 std::vector<std::function<void()>> callbacks_to_invoke;
80
81 {
82 std::lock_guard<std::mutex> lock(state->callback_mutex);
83 bool was_cancelled = state->is_cancelled.exchange(true);
84 if (!was_cancelled) {
85 callbacks_to_invoke = std::move(state->callbacks);
86 state->callbacks.clear();
87 }
88 }
89
90 for (const auto& callback : callbacks_to_invoke) {
91 callback();
92 }
93 }
94 });
95 }
96
97 return new_token;
98 }
static cancellation_token create()
Creates a new cancellation token.

References kcenon::thread::callback, and create().

Here is the call graph for this function:

◆ create_linked() [2/2]

static cancellation_token kcenon::thread::cancellation_token::create_linked ( std::initializer_list< cancellation_token > tokens)
inlinestatic

Creates a linked token that is canceled when any of the parent tokens are canceled.

Parameters
tokensThe parent tokens
Returns
A new token linked to the parents
Note
Uses weak_ptr to avoid circular references and memory leaks

Definition at line 70 of file cancellation_token.h.

70 {
71 auto new_token = create();
72 auto new_state_weak = std::weak_ptr<token_state>(new_token.state_);
73
74 for (const auto& token : tokens) {
75 auto token_copy = token;
76 token_copy.register_callback([new_state_weak]() {
77 if (auto state = new_state_weak.lock()) {
78 // Directly set the cancelled flag and invoke callbacks
79 std::vector<std::function<void()>> callbacks_to_invoke;
80
81 {
82 std::lock_guard<std::mutex> lock(state->callback_mutex);
83 bool was_cancelled = state->is_cancelled.exchange(true, std::memory_order_release);
84 if (!was_cancelled) {
85 callbacks_to_invoke = std::move(state->callbacks);
86 state->callbacks.clear();
87 }
88 }
89
90 for (const auto& callback : callbacks_to_invoke) {
91 callback();
92 }
93 }
94 });
95 }
96
97 return new_token;
98 }

References kcenon::thread::callback, and create().

Here is the call graph for this function:

◆ is_cancelled() [1/2]

◆ is_cancelled() [2/2]

bool kcenon::thread::cancellation_token::is_cancelled ( ) const
inlinenodiscard

Checks if the token has been canceled.

Memory Ordering:

  • Uses memory_order_acquire to ensure proper synchronization with the cancellation thread's release operation
Returns
true if the token has been canceled, false otherwise

Definition at line 139 of file cancellation_token.h.

139 {
140 return state_->is_cancelled.load(std::memory_order_acquire);
141 }

References state_.

◆ operator=() [1/4]

cancellation_token & kcenon::thread::cancellation_token::operator= ( cancellation_token && )
default

◆ operator=() [2/4]

cancellation_token & kcenon::thread::cancellation_token::operator= ( cancellation_token && )
default

◆ operator=() [3/4]

cancellation_token & kcenon::thread::cancellation_token::operator= ( const cancellation_token & )
default

◆ operator=() [4/4]

cancellation_token & kcenon::thread::cancellation_token::operator= ( const cancellation_token & )
default

◆ register_callback() [1/2]

void kcenon::thread::cancellation_token::register_callback ( std::function< void()> callback)
inline

Registers a callback to be invoked when the token is canceled.

Parameters
callbackThe function to call when the token is canceled

If the token is already canceled, the callback is invoked immediately.

Note
This method is thread-safe and guarantees the callback is called exactly once.

Definition at line 154 of file cancellation_token.h.

154 {
155 std::unique_lock<std::mutex> lock(state_->callback_mutex);
156
157 // Check cancellation state while holding the lock
158 if (state_->is_cancelled.load()) {
159 lock.unlock();
160 callback();
161 return;
162 }
163
164 // Add callback while still holding the lock
165 state_->callbacks.push_back(std::move(callback));
166 }

References kcenon::thread::callback, and state_.

◆ register_callback() [2/2]

void kcenon::thread::cancellation_token::register_callback ( std::function< void()> callback)
inline

Registers a callback to be invoked when the token is canceled.

Parameters
callbackThe function to call when the token is canceled

If the token is already canceled, the callback is invoked immediately.

Memory Ordering:

  • Uses memory_order_acquire when checking cancellation status to synchronize with the cancel() operation
Note
This method is thread-safe and guarantees the callback is called exactly once.

Definition at line 167 of file cancellation_token.h.

167 {
168 std::unique_lock<std::mutex> lock(state_->callback_mutex);
169
170 // Check cancellation state while holding the lock
171 if (state_->is_cancelled.load(std::memory_order_acquire)) {
172 lock.unlock();
173 callback();
174 return;
175 }
176
177 // Add callback while still holding the lock
178 state_->callbacks.push_back(std::move(callback));
179 }

References kcenon::thread::callback, and state_.

Member Data Documentation

◆ state_

std::shared_ptr< token_state > kcenon::thread::cancellation_token::state_
private

Definition at line 39 of file cancellation_token.h.

Referenced by cancel(), is_cancelled(), and register_callback().


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