ThreadSchedule 2.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;
61 std::optional<ThreadAffinity> affinity;
62};
63
64namespace profiles
65{
70inline auto realtime() -> ThreadProfile
71{
72 return ThreadProfile{"realtime",
73#ifdef _WIN32
75#else
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
106namespace detail
107{
108
113template <typename T>
115{
116 bool ok = true;
117 if (!t.set_scheduling_policy(p.policy, p.priority).has_value())
118 ok = false;
119 if (p.affinity.has_value())
120 {
121 if (!t.set_affinity(*p.affinity).has_value())
122 ok = false;
123 }
124 if (ok)
125 return {};
126 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
127}
128
132template <typename PoolType>
133inline auto apply_profile_to_pool(PoolType& pool, std::string const& name_prefix, ThreadProfile const& p)
135{
136 bool ok = true;
137 if (!pool.configure_threads(name_prefix, p.policy, p.priority).has_value())
138 ok = false;
139 if (p.affinity.has_value())
140 {
141 if (!pool.set_affinity(*p.affinity).has_value())
142 ok = false;
143 }
144 if (ok)
145 return {};
146 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
147}
148
149} // namespace detail
150
159template <typename ThreadLike, std::enable_if_t<is_thread_like_v<ThreadLike>, int> = 0>
160inline auto apply_profile(ThreadLike& t, ThreadProfile const& p) -> expected<void, std::error_code>
161{
162 return detail::apply_profile_to(t, p);
163}
164
172
177{
178 return apply_profile(*t.control, p);
179}
180
185{
186 return detail::apply_profile_to_pool(pool, "pool", p);
187}
188
193{
194 return detail::apply_profile_to_pool(pool, "fast", p);
195}
196
201{
202 return detail::apply_profile_to_pool(pool, "hp", p);
203}
204
209{
210 bool ok = true;
211 if (!reg.set_scheduling_policy(tid, p.policy, p.priority).has_value())
212 ok = false;
213 if (p.affinity.has_value())
214 {
215 if (!reg.set_affinity(tid, *p.affinity).has_value())
216 ok = false;
217 }
218 if (ok)
219 return {};
220 return unexpected(std::make_error_code(std::errc::operation_not_permitted));
221}
222
223} // namespace threadschedule
High-performance thread pool optimized for high-frequency task submission.
Per-thread control handle for OS-level scheduling operations.
Value-semantic wrapper for a thread scheduling priority.
static constexpr auto normal() noexcept -> ThreadPriority
static constexpr auto highest() noexcept -> ThreadPriority
static constexpr auto lowest() noexcept -> ThreadPriority
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_to(T &t, ThreadProfile const &p) -> expected< void, std::error_code >
Apply policy + optional affinity to any type exposing set_scheduling_policy() and set_affinity().
Definition profiles.hpp:114
auto apply_profile_to_pool(PoolType &pool, std::string const &name_prefix, ThreadProfile const &p) -> expected< void, std::error_code >
Apply configure_threads + optional affinity to any pool type.
Definition profiles.hpp:133
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
SchedulingPolicy
Enumeration of available thread scheduling policies.
@ OTHER
Standard round-robin time-sharing.
@ BATCH
For batch style execution.
@ IDLE
For very low priority background tasks.
ThreadPoolBase< IndefiniteWait > ThreadPool
General-purpose thread pool with indefinite blocking wait.
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:160
ThreadWrapperView JThreadWrapperView
ThreadPoolBase< PollingWait<> > FastThreadPool
Thread pool with 10 ms polling wait for lower wake-up latency.
ThreadWrapper JThreadWrapper
Owning wrapper around std::jthread with cooperative cancellation (C++20).
Scheduling policies, thread priority, and CPU affinity types.
Snapshot of metadata for a single registered thread.
Declarative profile bundling scheduling intent for a thread.
Definition profiles.hpp:57
std::optional< ThreadAffinity > affinity
Definition profiles.hpp:61
Type trait that identifies thread-like types.
Definition concepts.hpp:145
Thread pools: HighPerformancePool, ThreadPoolBase, LightweightPoolT, and GlobalPool.
Process-wide thread registry, control blocks, and composite registry.