Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
pool_traits.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
18#include <type_traits>
19#include <functional>
20#include <chrono>
21#include <algorithm>
22
23namespace kcenon::thread::detail {
24
25 // Concepts are now imported from thread_concepts.h via the using declarations
26 // in that header's detail namespace section
27
31 template<typename F>
32 struct function_traits;
33
34 // Specialization for function pointers
35 template<typename R, typename... Args>
36 struct function_traits<R(*)(Args...)> {
37 using return_type = R;
38 using argument_types = std::tuple<Args...>;
39 static constexpr size_t arity = sizeof...(Args);
40 static constexpr bool is_noexcept = false;
41 };
42
43 // Specialization for noexcept function pointers
44 template<typename R, typename... Args>
45 struct function_traits<R(*)(Args...) noexcept> {
46 using return_type = R;
47 using argument_types = std::tuple<Args...>;
48 static constexpr size_t arity = sizeof...(Args);
49 static constexpr bool is_noexcept = true;
50 };
51
52 // Specialization for member function pointers
53 template<typename C, typename R, typename... Args>
54 struct function_traits<R(C::*)(Args...)> {
55 using return_type = R;
56 using class_type = C;
57 using argument_types = std::tuple<Args...>;
58 static constexpr size_t arity = sizeof...(Args);
59 static constexpr bool is_noexcept = false;
60 };
61
62 // Specialization for const member function pointers
63 template<typename C, typename R, typename... Args>
64 struct function_traits<R(C::*)(Args...) const> {
65 using return_type = R;
66 using class_type = C;
67 using argument_types = std::tuple<Args...>;
68 static constexpr size_t arity = sizeof...(Args);
69 static constexpr bool is_noexcept = false;
70 };
71
72 // Specialization for function objects and lambdas
73 template<typename F>
74 struct function_traits : function_traits<decltype(&F::operator())> {};
75
79 template<typename F>
81
82 template<typename F>
84
85 template<typename F>
87
91 template<size_t ThreadCount>
93 static_assert(ThreadCount > 0, "Thread count must be positive");
94 static_assert(ThreadCount <= 1024, "Thread count is unreasonably high");
95 static constexpr bool value = true;
96 };
97
101#ifdef USE_STD_CONCEPTS
102 template<typename T>
103 requires Callable<T>
104 constexpr auto forward_if_callable(T&& t) -> T&& {
105 return std::forward<T>(t);
106 }
107#else
108 template<typename T>
109 constexpr auto forward_if_callable(T&& t) -> std::enable_if_t<Callable<T>, T&&> {
110 return std::forward<T>(t);
111 }
112#endif
113
118 public:
119#ifdef USE_STD_CONCEPTS
120 template<Callable F>
121#else
122 template<typename F, typename = std::enable_if_t<Callable<F>>>
123#endif
125 : vtable_(&vtable_for<std::decay_t<F>>)
126 {
127 static_assert(sizeof(std::decay_t<F>) <= sizeof(storage_),
128 "Callable is too large for inline storage");
129 new (&storage_) std::decay_t<F>(std::forward<F>(f));
130 }
131
132 void operator()() {
134 }
135
137 if (vtable_) {
139 }
140 }
141
144
146 : vtable_(other.vtable_)
147 {
148 std::copy_n(other.storage_, sizeof(storage_), storage_);
149 other.vtable_ = nullptr;
150 }
151
153 if (this != &other) {
154 if (vtable_) {
156 }
157 vtable_ = other.vtable_;
158 std::copy_n(other.storage_, sizeof(storage_), storage_);
159 other.vtable_ = nullptr;
160 }
161 return *this;
162 }
163
164 private:
165 struct vtable_t {
166 void (*invoke)(void*);
167 void (*destroy)(void*);
168 };
169
170 template<typename F>
171 static constexpr vtable_t vtable_for = {
172 [](void* ptr) { (*static_cast<F*>(ptr))(); },
173 [](void* ptr) { static_cast<F*>(ptr)->~F(); }
174 };
175
177 alignas(std::max_align_t) char storage_[64];
178 };
179
183 template<size_t N>
185 constexpr compile_string(const char (&str)[N]) {
186 std::copy_n(str, N, value);
187 }
188 char value[N];
189 };
190
194 template<typename T>
195 constexpr auto get_type_name() {
196 // This would ideally use std::source_location or compiler intrinsics
197 // For now, return a generic message
198 return "unknown_type";
199 }
200
204 template<typename T, typename = void>
205 struct has_get_method : std::false_type {};
206
207 template<typename T>
208 struct has_get_method<T, std::void_t<decltype(std::declval<T>().get())>>
209 : std::true_type {};
210
211 template<typename T>
213
214} // namespace kcenon::thread::detail
Type eraser for heterogeneous callable storage.
callable_eraser & operator=(callable_eraser &&other) noexcept
callable_eraser(const callable_eraser &)=delete
static constexpr vtable_t vtable_for
callable_eraser & operator=(const callable_eraser &)=delete
callable_eraser(callable_eraser &&other) noexcept
typename function_traits< F >::return_type function_return_t
Helper to get function traits.
Definition pool_traits.h:80
typename function_traits< F >::argument_types function_args_t
Definition pool_traits.h:83
constexpr size_t function_arity_v
Definition pool_traits.h:86
constexpr bool has_get_method_v
constexpr auto forward_if_callable(T &&t) -> std::enable_if_t< Callable< T >, T && >
Template helper for perfect forwarding with type constraints.
constexpr auto get_type_name()
Template for generating descriptive error messages.
STL namespace.
Compile-time string for template error messages.
constexpr compile_string(const char(&str)[N])
Type trait for function signature analysis.
SFINAE helper to detect if a type has a specific member.
Compile-time validation for thread pool configuration.
Definition pool_traits.h:92
C++20 Concepts for thread_system.