ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
profiles.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include "concepts.hpp"
14#include "scheduler_policy.hpp"
15#include "thread_pool.hpp"
16#include "thread_registry.hpp"
17#include <optional>
18#include <string>
19
20namespace threadschedule
21{
22
23template <>
24struct is_thread_like<ThreadWrapper> : std::true_type
25{
26};
27
28template <>
29struct is_thread_like<ThreadWrapperView> : std::true_type
30{
31};
32
33#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
34template <>
35struct is_thread_like<JThreadWrapper> : std::true_type
36{
37};
38
39template <>
40struct is_thread_like<JThreadWrapperView> : std::true_type
41{
42};
43#endif
44
57{
58 std::string name;
59 SchedulingPolicy policy;
60 ThreadPriority priority;
61 std::optional<ThreadAffinity> affinity; // optional pinning
62};
63
64namespace profiles
65{
70inline auto realtime() -> ThreadProfile
71{
72 return ThreadProfile{"realtime",
73#ifdef _WIN32
74 SchedulingPolicy::OTHER,
75#else
76 SchedulingPolicy::FIFO,
77#endif
78 ThreadPriority::highest(), std::nullopt};
79}
80
84inline auto low_latency() -> ThreadProfile
85{
86 return ThreadProfile{"low_latency", SchedulingPolicy::RR, ThreadPriority{5}, std::nullopt};
87}
88
92inline auto throughput() -> ThreadProfile
93{
94 return ThreadProfile{"throughput", SchedulingPolicy::BATCH, ThreadPriority::normal(), std::nullopt};
95}
96
100inline auto background() -> ThreadProfile
101{
102 return ThreadProfile{"background", SchedulingPolicy::IDLE, ThreadPriority::lowest(), std::nullopt};
103}
104} // namespace profiles
105
118template <typename ThreadLike, std::enable_if_t<is_thread_like_v<ThreadLike>, int> = 0>
119inline auto apply_profile(ThreadLike& t, ThreadProfile const& p) -> expected<void, std::error_code>
120{
121 bool ok = true;
122 if (!t.set_scheduling_policy(p.policy, p.priority).has_value())
123 ok = false;
124 if (p.affinity.has_value())
125 {
126 if (!t.set_affinity(*p.affinity).has_value())
127 ok = false;
128 }
129 if (ok)
130 return {};
131 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
132}
133
142{
143 bool ok = true;
144 if (!t.set_scheduling_policy(p.policy, p.priority).has_value())
145 ok = false;
146 if (p.affinity.has_value())
147 {
148 if (!t.set_affinity(*p.affinity).has_value())
149 ok = false;
150 }
151 if (ok)
152 return {};
153 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
154}
155
169{
170 return apply_profile(*t.control, p);
171}
172
184{
185 bool ok = true;
186 // Name prefix left to caller via configure_threads; here just policy/priority
187 if (!pool.configure_threads("pool", p.policy, p.priority))
188 ok = false;
189 if (p.affinity.has_value())
190 {
191 if (!pool.set_affinity(*p.affinity))
192 ok = false;
193 }
194 if (ok)
195 return {};
196 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
197}
198
210{
211 bool ok = true;
212 if (!pool.configure_threads("hp", p.policy, p.priority).has_value())
213 ok = false;
214 if (p.affinity.has_value())
215 {
216 if (!pool.set_affinity(*p.affinity).has_value())
217 ok = false;
218 }
219 if (ok)
220 return {};
221 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
222}
223
233{
234 bool ok = true;
235 if (!reg.set_scheduling_policy(tid, p.policy, p.priority).has_value())
236 ok = false;
237 if (p.affinity.has_value())
238 {
239 if (!reg.set_affinity(tid, *p.affinity).has_value())
240 ok = false;
241 }
242 if (ok)
243 return {};
244 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
245}
246
247} // namespace threadschedule
High-performance thread pool optimized for high-frequency task submission.
Per-thread control handle for OS-level scheduling operations.
Simple, general-purpose thread pool.
Value-semantic wrapper for a thread scheduling priority.
Central registry of threads indexed by OS-level thread ID (Tid).
Non-owning view over an externally managed std::thread.
Owning wrapper around std::thread with RAII join-on-destroy semantics.
A result type that holds either a value of type T or an error of type E.
Definition expected.hpp:215
Exception thrown by expected::value() when the object is in the error state.
Definition expected.hpp:162
C++20 concepts, type traits, and SFINAE helpers for the threading library.
auto apply_profile(ThreadLike &t, ThreadProfile const &p) -> expected< void, std::error_code >
Apply a profile to a thread wrapper or view.
Definition profiles.hpp:119
auto throughput() -> ThreadProfile
Throughput-oriented profile favoring batch scheduling.
Definition profiles.hpp:92
auto background() -> ThreadProfile
Background profile for very low priority work.
Definition profiles.hpp:100
auto low_latency() -> ThreadProfile
Low-latency interactive profile using RR scheduling.
Definition profiles.hpp:84
auto realtime() -> ThreadProfile
Highest priority profile. Uses FIFO on Linux (if permitted), falls back to OTHER on Windows.
Definition profiles.hpp:70
Snapshot of metadata for a single registered thread.
Declarative profile bundling scheduling intent for a thread.
Definition profiles.hpp:57
Type trait that identifies thread-like types.
Definition concepts.hpp:145