Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
cancellation_reason.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
17 // end of cancellation
18
19#include <chrono>
20#include <exception>
21#include <optional>
22#include <string>
23
24namespace kcenon::thread
25{
51 {
56 enum class type
57 {
58 none,
60 timeout,
61 deadline,
64 error
65 };
66
69
71 std::string message;
72
74 std::chrono::steady_clock::time_point cancel_time;
75
77 std::optional<std::exception_ptr> exception;
78
83 [[nodiscard]] auto to_string() const -> std::string
84 {
85 std::string type_str;
86 switch (reason_type)
87 {
88 case type::none:
89 type_str = "none";
90 break;
92 type_str = "user_requested";
93 break;
94 case type::timeout:
95 type_str = "timeout";
96 break;
97 case type::deadline:
98 type_str = "deadline";
99 break;
101 type_str = "parent_cancelled";
102 break;
104 type_str = "pool_shutdown";
105 break;
106 case type::error:
107 type_str = "error";
108 break;
109 }
110
111 std::string result = "cancellation_reason{type=" + type_str;
112 if (!message.empty())
113 {
114 result += ", message=\"" + message + "\"";
115 }
116 result += "}";
117 return result;
118 }
119
125 [[nodiscard]] static auto type_to_string(type t) -> std::string
126 {
127 switch (t)
128 {
129 case type::none:
130 return "none";
132 return "user_requested";
133 case type::timeout:
134 return "timeout";
135 case type::deadline:
136 return "deadline";
138 return "parent_cancelled";
140 return "pool_shutdown";
141 case type::error:
142 return "error";
143 }
144 return "unknown";
145 }
146 };
147
148} // namespace kcenon::thread
Represents an error in the thread system.
A template class representing either a value or an error.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Holds information about why a cancellation occurred.
static auto type_to_string(type t) -> std::string
Converts the reason type enum to a string.
std::chrono::steady_clock::time_point cancel_time
Time point when the cancellation occurred.
type reason_type
The type of cancellation that occurred.
auto to_string() const -> std::string
Converts the cancellation reason to a human-readable string.
std::optional< std::exception_ptr > exception
Optional exception that triggered the cancellation.
std::string message
Human-readable message describing the cancellation.
type
The type of cancellation that occurred.
@ pool_shutdown
Thread pool is shutting down.
@ parent_cancelled
Parent token was cancelled.
@ deadline
Deadline time point reached.
@ none
No cancellation (default state)
@ user_requested
Explicit cancel() call by user.
@ timeout
Timeout duration expired.
@ error
Cancellation triggered by an error.