|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Advanced cancellation token with timeout, deadline, and reason support. More...
#include <enhanced_cancellation_token.h>

Classes | |
| struct | state |
Public Types | |
| using | callback_handle = std::size_t |
| Callback handle type for registration management. | |
| using | callback_type = std::function<void()> |
| Simple callback function type. | |
| using | callback_with_reason_type |
| Callback function type with reason parameter. | |
Public Member Functions | |
| enhanced_cancellation_token () | |
| Default constructor creates a new token. | |
| enhanced_cancellation_token (const enhanced_cancellation_token &)=default | |
| Copy constructor (shares state). | |
| enhanced_cancellation_token (enhanced_cancellation_token &&) noexcept=default | |
| Move constructor. | |
| auto | operator= (const enhanced_cancellation_token &) -> enhanced_cancellation_token &=default |
| Copy assignment (shares state). | |
| auto | operator= (enhanced_cancellation_token &&) noexcept -> enhanced_cancellation_token &=default |
| Move assignment. | |
| ~enhanced_cancellation_token () | |
| Destructor. | |
| auto | cancel () -> void |
| Cancels the token. | |
| auto | cancel (const std::string &message) -> void |
| Cancels the token with a message. | |
| auto | cancel (std::exception_ptr ex) -> void |
| Cancels the token with an exception. | |
| auto | is_cancelled () const -> bool |
| Checks if the token has been cancelled. | |
| auto | is_cancellation_requested () const -> bool |
| Checks if cancellation has been requested. | |
| auto | get_reason () const -> std::optional< cancellation_reason > |
| Gets the cancellation reason. | |
| auto | check_cancelled () const -> common::VoidResult |
| Checks if the token has been cancelled and returns an error result. | |
| auto | has_timeout () const -> bool |
| Checks if the token has a timeout or deadline. | |
| auto | remaining_time () const -> std::chrono::milliseconds |
| Gets the remaining time before timeout/deadline. | |
| auto | deadline () const -> std::chrono::steady_clock::time_point |
| Gets the deadline time point. | |
| auto | extend_timeout (std::chrono::milliseconds additional) -> void |
| Extends the timeout by the specified duration. | |
| auto | register_callback (callback_type callback) -> callback_handle |
| Registers a callback to be invoked on cancellation. | |
| auto | register_callback (callback_with_reason_type callback) -> callback_handle |
| Registers a callback that receives the cancellation reason. | |
| auto | unregister_callback (callback_handle handle) -> void |
| Unregisters a previously registered callback. | |
| auto | wait () const -> void |
| Waits until the token is cancelled. | |
| auto | wait_for (std::chrono::milliseconds timeout) const -> bool |
| Waits for cancellation with a timeout. | |
| auto | wait_until (std::chrono::steady_clock::time_point deadline) const -> bool |
| Waits for cancellation until a deadline. | |
Static Public Member Functions | |
| static auto | create () -> enhanced_cancellation_token |
| Creates a new cancellation token. | |
| static auto | create_with_timeout (std::chrono::milliseconds timeout) -> enhanced_cancellation_token |
| Creates a token that auto-cancels after the specified timeout. | |
| static auto | create_with_deadline (std::chrono::steady_clock::time_point deadline) -> enhanced_cancellation_token |
| Creates a token that auto-cancels at the specified deadline. | |
| static auto | create_linked (std::initializer_list< enhanced_cancellation_token > tokens) -> enhanced_cancellation_token |
| Creates a token linked to parent tokens. | |
| static auto | create_linked_with_timeout (const enhanced_cancellation_token &parent, std::chrono::milliseconds timeout) -> enhanced_cancellation_token |
| Creates a linked token with additional timeout. | |
Private Member Functions | |
| enhanced_cancellation_token (std::shared_ptr< state > state) | |
| auto | do_cancel (cancellation_reason::type reason_type, const std::string &message, std::optional< std::exception_ptr > ex) -> void |
Static Private Member Functions | |
| static auto | start_timeout_timer (std::weak_ptr< state > state_weak, std::chrono::steady_clock::time_point deadline) -> void |
Private Attributes | |
| std::shared_ptr< state > | state_ |
Advanced cancellation token with timeout, deadline, and reason support.
Extends the basic cancellation_token with additional features:
Definition at line 75 of file enhanced_cancellation_token.h.
| using kcenon::thread::enhanced_cancellation_token::callback_handle = std::size_t |
Callback handle type for registration management.
Definition at line 83 of file enhanced_cancellation_token.h.
| using kcenon::thread::enhanced_cancellation_token::callback_type = std::function<void()> |
Simple callback function type.
Definition at line 86 of file enhanced_cancellation_token.h.
Callback function type with reason parameter.
Definition at line 89 of file enhanced_cancellation_token.h.
| kcenon::thread::enhanced_cancellation_token::enhanced_cancellation_token | ( | ) |
Default constructor creates a new token.
Definition at line 40 of file enhanced_cancellation_token.cpp.
|
default |
Copy constructor (shares state).
|
defaultnoexcept |
Move constructor.
| kcenon::thread::enhanced_cancellation_token::~enhanced_cancellation_token | ( | ) |
Destructor.
Definition at line 51 of file enhanced_cancellation_token.cpp.
References state_.
|
explicitprivate |
Definition at line 45 of file enhanced_cancellation_token.cpp.
| auto kcenon::thread::enhanced_cancellation_token::cancel | ( | ) | -> void |
Cancels the token.
Sets the token to cancelled state with reason_type::user_requested and invokes all registered callbacks.
Definition at line 219 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::user_requested.
| auto kcenon::thread::enhanced_cancellation_token::cancel | ( | const std::string & | message | ) | -> void |
Cancels the token with a message.
| message | Human-readable reason for cancellation. |
Definition at line 224 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::user_requested.
| auto kcenon::thread::enhanced_cancellation_token::cancel | ( | std::exception_ptr | ex | ) | -> void |
Cancels the token with an exception.
| ex | Exception that caused the cancellation. |
The cancellation reason_type will be set to error.
Definition at line 229 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::error.
|
nodiscard |
Checks if the token has been cancelled and returns an error result.
Definition at line 300 of file enhanced_cancellation_token.cpp.
References get_reason(), is_cancelled(), kcenon::thread::make_error_result(), and kcenon::thread::operation_canceled.
Referenced by kcenon::thread::cancellation_scope::check_cancelled().


|
staticnodiscard |
Creates a new cancellation token.
Definition at line 61 of file enhanced_cancellation_token.cpp.
Referenced by kcenon::thread::cancellation_context::current().

|
staticnodiscard |
Creates a token linked to parent tokens.
| tokens | Parent tokens to link with. |
The new token will be cancelled when any of the parent tokens are cancelled, with reason_type::parent_cancelled.
Definition at line 88 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::cancel_time, kcenon::thread::cancellation_reason::message, kcenon::thread::cancellation_reason::parent_cancelled, and kcenon::thread::cancellation_reason::reason_type.
|
staticnodiscard |
Creates a linked token with additional timeout.
| parent | Parent token to link with. |
| timeout | Additional timeout duration. |
The token cancels when either:
Definition at line 150 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::cancel_time, kcenon::thread::cancellation_reason::message, kcenon::thread::cancellation_reason::parent_cancelled, and kcenon::thread::cancellation_reason::reason_type.
|
staticnodiscard |
Creates a token that auto-cancels at the specified deadline.
| deadline | Time point at which the token auto-cancels. |
The token will automatically cancel with reason_type::deadline when the specified time point is reached.
Definition at line 73 of file enhanced_cancellation_token.cpp.
|
staticnodiscard |
Creates a token that auto-cancels after the specified timeout.
| timeout | Duration after which the token auto-cancels. |
The token will automatically cancel with reason_type::timeout when the specified duration elapses from the creation time.
Definition at line 66 of file enhanced_cancellation_token.cpp.
|
nodiscard |
Gets the deadline time point.
Definition at line 340 of file enhanced_cancellation_token.cpp.
References state_.
|
private |
Definition at line 234 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::cancel_time, kcenon::thread::cancellation_reason::exception, kcenon::thread::cancellation_reason::message, and kcenon::thread::cancellation_reason::reason_type.
| auto kcenon::thread::enhanced_cancellation_token::extend_timeout | ( | std::chrono::milliseconds | additional | ) | -> void |
Extends the timeout by the specified duration.
| additional | Additional time to add to the deadline. |
Definition at line 346 of file enhanced_cancellation_token.cpp.
|
nodiscard |
Gets the cancellation reason.
Definition at line 293 of file enhanced_cancellation_token.cpp.
References state_.
Referenced by check_cancelled().

|
nodiscard |
Checks if the token has a timeout or deadline.
Definition at line 315 of file enhanced_cancellation_token.cpp.
References state_.
Referenced by remaining_time().

|
nodiscard |
Checks if cancellation has been requested.
This is functionally equivalent to is_cancelled() for this implementation.
Definition at line 288 of file enhanced_cancellation_token.cpp.
References is_cancelled().

|
nodiscard |
Checks if the token has been cancelled.
Definition at line 283 of file enhanced_cancellation_token.cpp.
References is_cancelled(), and state_.
Referenced by check_cancelled(), is_cancellation_requested(), kcenon::thread::cancellation_scope::is_cancelled(), and is_cancelled().


|
default |
Copy assignment (shares state).
|
defaultnoexcept |
Move assignment.
|
nodiscard |
Registers a callback to be invoked on cancellation.
| callback | Function to call when cancelled. |
If already cancelled, the callback is invoked immediately.
Definition at line 356 of file enhanced_cancellation_token.cpp.
References kcenon::thread::callback.
|
nodiscard |
Registers a callback that receives the cancellation reason.
| callback | Function to call with reason when cancelled. |
If already cancelled, the callback is invoked immediately.
Definition at line 374 of file enhanced_cancellation_token.cpp.
References kcenon::thread::callback, and kcenon::thread::cancellation_reason::user_requested.
|
nodiscard |
Gets the remaining time before timeout/deadline.
Definition at line 320 of file enhanced_cancellation_token.cpp.
References has_timeout(), and state_.

|
staticprivate |
Definition at line 444 of file enhanced_cancellation_token.cpp.
References kcenon::thread::cancellation_reason::cancel_time, kcenon::thread::cancellation_reason::message, kcenon::thread::cancellation_reason::reason_type, and kcenon::thread::cancellation_reason::timeout.
| auto kcenon::thread::enhanced_cancellation_token::unregister_callback | ( | callback_handle | handle | ) | -> void |
Unregisters a previously registered callback.
| handle | The handle returned from register_callback. |
Definition at line 403 of file enhanced_cancellation_token.cpp.
Referenced by kcenon::thread::cancellation_callback_guard::~cancellation_callback_guard().

| auto kcenon::thread::enhanced_cancellation_token::wait | ( | ) | const -> void |
Waits until the token is cancelled.
Blocks the calling thread until the token is cancelled.
Definition at line 416 of file enhanced_cancellation_token.cpp.
References state_.
|
nodiscard |
Waits for cancellation with a timeout.
| timeout | Maximum time to wait. |
Definition at line 424 of file enhanced_cancellation_token.cpp.
|
nodiscard |
Waits for cancellation until a deadline.
| deadline | Maximum time point to wait until. |
Definition at line 434 of file enhanced_cancellation_token.cpp.
|
private |
Definition at line 325 of file enhanced_cancellation_token.h.
Referenced by deadline(), get_reason(), has_timeout(), is_cancelled(), remaining_time(), wait(), and ~enhanced_cancellation_token().