Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
enhanced_cancellation_token.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
13#include "cancellation_reason.h"
14#include "error_handling.h"
15
16#include <atomic>
17#include <chrono>
18#include <condition_variable>
19#include <functional>
20#include <memory>
21#include <mutex>
22#include <optional>
23#include <thread>
24#include <unordered_map>
25#include <vector>
26
27namespace kcenon::thread
28{
29 // Forward declarations
30 class enhanced_cancellation_token;
31 class cancellation_callback_guard;
32 class cancellation_scope;
33 class cancellation_context;
34
76 {
77 public:
78 // ====================================================================
79 // Type definitions
80 // ====================================================================
81
83 using callback_handle = std::size_t;
84
86 using callback_type = std::function<void()>;
87
90 std::function<void(const cancellation_reason&)>;
91
92 // ====================================================================
93 // Construction
94 // ====================================================================
95
100
105
110
114 auto operator=(const enhanced_cancellation_token&)
115 -> enhanced_cancellation_token& = default;
116
120 auto operator=(enhanced_cancellation_token&&) noexcept
121 -> enhanced_cancellation_token& = default;
122
127
132 [[nodiscard]] static auto create() -> enhanced_cancellation_token;
133
142 [[nodiscard]] static auto create_with_timeout(std::chrono::milliseconds timeout)
144
153 [[nodiscard]] static auto create_with_deadline(
154 std::chrono::steady_clock::time_point deadline) -> enhanced_cancellation_token;
155
164 [[nodiscard]] static auto create_linked(
165 std::initializer_list<enhanced_cancellation_token> tokens)
167
178 [[nodiscard]] static auto create_linked_with_timeout(
179 const enhanced_cancellation_token& parent,
180 std::chrono::milliseconds timeout) -> enhanced_cancellation_token;
181
182 // ====================================================================
183 // Cancellation
184 // ====================================================================
185
192 auto cancel() -> void;
193
198 auto cancel(const std::string& message) -> void;
199
206 auto cancel(std::exception_ptr ex) -> void;
207
208 // ====================================================================
209 // Query
210 // ====================================================================
211
216 [[nodiscard]] auto is_cancelled() const -> bool;
217
225 [[nodiscard]] auto is_cancellation_requested() const -> bool;
226
231 [[nodiscard]] auto get_reason() const -> std::optional<cancellation_reason>;
232
238 [[nodiscard]] auto check_cancelled() const -> common::VoidResult;
239
240 // ====================================================================
241 // Timeout/Deadline
242 // ====================================================================
243
248 [[nodiscard]] auto has_timeout() const -> bool;
249
254 [[nodiscard]] auto remaining_time() const -> std::chrono::milliseconds;
255
260 [[nodiscard]] auto deadline() const -> std::chrono::steady_clock::time_point;
261
266 auto extend_timeout(std::chrono::milliseconds additional) -> void;
267
268 // ====================================================================
269 // Callbacks
270 // ====================================================================
271
280
290
295 auto unregister_callback(callback_handle handle) -> void;
296
297 // ====================================================================
298 // Waiting
299 // ====================================================================
300
306 auto wait() const -> void;
307
313 [[nodiscard]] auto wait_for(std::chrono::milliseconds timeout) const -> bool;
314
320 [[nodiscard]] auto wait_until(std::chrono::steady_clock::time_point deadline) const
321 -> bool;
322
323 private:
324 struct state;
325 std::shared_ptr<state> state_;
326
327 explicit enhanced_cancellation_token(std::shared_ptr<state> state);
328
329 auto do_cancel(cancellation_reason::type reason_type,
330 const std::string& message,
331 std::optional<std::exception_ptr> ex) -> void;
332
333 static auto start_timeout_timer(std::weak_ptr<state> state_weak,
334 std::chrono::steady_clock::time_point deadline)
335 -> void;
336 };
337
394
421 {
422 public:
428
433
434 // Non-copyable, non-movable
439
444 [[nodiscard]] auto is_cancelled() const -> bool;
445
451 [[nodiscard]] auto check_cancelled() const -> common::VoidResult;
452
457 [[nodiscard]] auto token() const -> const enhanced_cancellation_token&;
458
459 private:
461 };
462
490 {
491 public:
496 [[nodiscard]] static auto current() -> enhanced_cancellation_token;
497
502 static auto push(enhanced_cancellation_token token) -> void;
503
507 static auto pop() -> void;
508
513 class guard
514 {
515 public:
520 explicit guard(enhanced_cancellation_token token);
521
525 ~guard();
526
527 // Non-copyable, non-movable
528 guard(const guard&) = delete;
529 auto operator=(const guard&) -> guard& = delete;
530 guard(guard&&) = delete;
531 auto operator=(guard&&) -> guard& = delete;
532
533 private:
535 };
536
537 private:
539 };
540
541} // namespace kcenon::thread
Exception class for operation cancellation.
Cancellation reason structure for enhanced cancellation tokens.
RAII guard for automatic callback unregistration.
cancellation_callback_guard(const cancellation_callback_guard &)=delete
enhanced_cancellation_token::callback_handle handle_
auto operator=(const cancellation_callback_guard &) -> cancellation_callback_guard &=delete
auto operator=(guard &&) -> guard &=delete
auto operator=(const guard &) -> guard &=delete
Thread-local cancellation context for implicit token propagation.
Structured cancellation scope with check points.
auto operator=(cancellation_scope &&) -> cancellation_scope &=delete
cancellation_scope(const cancellation_scope &)=delete
cancellation_scope(cancellation_scope &&)=delete
auto operator=(const cancellation_scope &) -> cancellation_scope &=delete
~cancellation_scope()=default
Destructor.
Advanced cancellation token with timeout, deadline, and reason support.
std::function< void()> callback_type
Simple callback function type.
static auto create() -> enhanced_cancellation_token
Creates a new cancellation token.
enhanced_cancellation_token(const enhanced_cancellation_token &)=default
Copy constructor (shares state).
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.
enhanced_cancellation_token()
Default constructor creates a new token.
auto has_timeout() const -> bool
Checks if the token has a timeout or deadline.
auto do_cancel(cancellation_reason::type reason_type, const std::string &message, std::optional< std::exception_ptr > ex) -> void
auto wait_for(std::chrono::milliseconds timeout) const -> bool
Waits for cancellation with a timeout.
auto remaining_time() const -> std::chrono::milliseconds
Gets the remaining time before timeout/deadline.
std::function< void(const cancellation_reason &)> callback_with_reason_type
Callback function type with reason parameter.
static auto create_linked(std::initializer_list< enhanced_cancellation_token > tokens) -> enhanced_cancellation_token
Creates a token linked to parent tokens.
auto is_cancelled() const -> bool
Checks if the token has been cancelled.
auto wait_until(std::chrono::steady_clock::time_point deadline) const -> bool
Waits for cancellation until a deadline.
auto unregister_callback(callback_handle handle) -> void
Unregisters a previously registered callback.
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.
auto extend_timeout(std::chrono::milliseconds additional) -> void
Extends the timeout by the specified duration.
auto deadline() const -> std::chrono::steady_clock::time_point
Gets the deadline time point.
std::size_t callback_handle
Callback handle type for registration management.
enhanced_cancellation_token(enhanced_cancellation_token &&) noexcept=default
Move constructor.
static auto start_timeout_timer(std::weak_ptr< state > state_weak, std::chrono::steady_clock::time_point deadline) -> void
auto wait() const -> void
Waits until the token is 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 operator=(const enhanced_cancellation_token &) -> enhanced_cancellation_token &=default
Copy assignment (shares state).
auto register_callback(callback_type callback) -> callback_handle
Registers a callback to be invoked on cancellation.
auto check_cancelled() const -> common::VoidResult
Checks if the token has been cancelled and returns an error result.
@ callback
Call user callback for custom decision.
Error codes and utilities for the thread system.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Holds information about why a cancellation occurred.