Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_conditions.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
12#pragma once
13
16
17#include <string>
18#include <array>
19#include <cstdint>
20#include <string_view>
21
22namespace kcenon::thread
23{
40 enum class thread_conditions : uint8_t
41 {
42 Created,
43 Waiting,
44 Working,
45 Stopping,
46 Stopped,
47 };
48
49 namespace job_detail
50 {
57 constexpr std::array thread_conditions_strings
58 = { "created", "waiting", "working", "stopping", "stopped" };
59
64
71 static_assert(thread_conditions_count
72 == static_cast<size_t>(thread_conditions::Stopped) + 1,
73 "thread_conditions_strings and thread_conditions enum are out of sync");
74 }
75
88 [[nodiscard]] constexpr std::string_view to_string(thread_conditions condition)
89 {
90 auto index = static_cast<size_t>(condition);
93 : "UNKNOWN";
94 }
95
108 [[nodiscard]] inline auto get_all_thread_conditions(void) -> std::vector<thread_conditions>
109 {
112 }
113} // namespace kcenon::thread
114
115// ----------------------------------------------------------------------------
116// Formatter specializations for thread_conditions
117// ----------------------------------------------------------------------------
118
130template <>
131struct std::formatter<kcenon::thread::thread_conditions> : std::formatter<std::string_view>
132{
140 template <typename FormatContext>
141 auto format(const kcenon::thread::thread_conditions& thread_condition, FormatContext& ctx) const
142 {
143 return std::formatter<std::string_view>::format(kcenon::thread::to_string(thread_condition),
144 ctx);
145 }
146};
147
153template <>
154struct std::formatter<kcenon::thread::thread_conditions, wchar_t>
155 : std::formatter<std::wstring_view, wchar_t>
156{
164 template <typename FormatContext>
165 auto format(const kcenon::thread::thread_conditions& thread_condition, FormatContext& ctx) const
166 {
167 auto str = kcenon::thread::to_string(thread_condition);
169 return std::formatter<std::wstring_view, wchar_t>::format(wstr, ctx);
170 }
171};
static auto to_wstring(const std::string &value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
Converts a std::string (system-encoded) to a std::wstring.
String encoding conversion, Base64 encoding/decoding utilities.
Generic formatter for enum types using user-provided converter functors.
constexpr size_t thread_conditions_count
Total number of states defined in thread_conditions.
constexpr std::array thread_conditions_strings
Array of string representations corresponding to each thread_conditions state.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
thread_conditions
Enumeration of various states in a thread's lifecycle.
@ Created
Thread created but not started.
@ Waiting
Thread waiting for work or tasks.
@ Stopping
Thread in the process of stopping.
@ Working
Thread currently processing a task.
@ Stopped
Thread fully stopped.
auto get_all_thread_conditions(void) -> std::vector< thread_conditions >
Retrieves a vector containing all possible thread_conditions values.
constexpr std::string_view to_string(log_level_v2 level) noexcept
Convert log_level_v2 to string representation.
Definition log_level.h:40
auto format(const kcenon::thread::thread_conditions &thread_condition, FormatContext &ctx) const
Formats a thread_conditions value as a wide string.
auto format(const kcenon::thread::thread_conditions &thread_condition, FormatContext &ctx) const
Formats a thread_conditions value as a string.