ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
thread_profile.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../result.hpp"
4#include "../scheduling.hpp"
5#include "../thread_affinity.hpp"
6#include "../thread_config.hpp"
7#include "polling_pool.hpp"
8#include "raw_thread_pool.hpp"
10
11#include <optional>
12#include <string>
13#include <system_error>
14#include <utility>
15#include <vector>
16
18{
19
21{
22 std::string name;
24 std::optional<thread_affinity> affinity;
25};
26
27namespace profiles
28{
29[[nodiscard]] inline auto
31{
32 return { "realtime", schedule::realtime_fifo(realtime_priority{ 99 }), std::nullopt };
33}
34
35[[nodiscard]] inline auto
37{
38 return { "low_latency", schedule::low_latency(), std::nullopt };
39}
40
41[[nodiscard]] inline auto
43{
44 return { "throughput", schedule::normal(), std::nullopt };
45}
46
47[[nodiscard]] inline auto
49{
50 return { "background", schedule::background(), std::nullopt };
51}
52} // namespace profiles
53
54namespace profile_detail
55{
56[[nodiscard]] inline auto
57make_config(thread_profile const& profile, std::string name) -> thread_config
58{
59 thread_config config;
60 config.set_name(std::move(name)).set_scheduling(profile.scheduling);
61 if (profile.affinity)
62 config.set_affinity(*profile.affinity);
63 return config;
64}
65
66template <typename ThreadLike>
67auto
68apply_to_thread(ThreadLike& value, thread_profile const& profile) -> result<void>
69{
70 return value.configure(make_config(profile, profile.name));
71}
72
73template <typename Pool>
74auto
75apply_to_pool(Pool& pool, std::string name, thread_profile const& profile) -> result<void>
76{
77 return pool.configure_workers(make_config(profile, std::move(name)));
78}
79} // namespace profile_detail
80
81template <typename ThreadLike>
82auto
83apply_profile(ThreadLike& value, thread_profile const& profile) -> result<void>
84{
85 return profile_detail::apply_to_thread(value, profile);
86}
87
88inline auto
90{
91 return profile_detail::apply_to_pool(pool, "pool", profile);
92}
93
94inline auto
96{
97 return profile_detail::apply_to_pool(pool, "polling", profile);
98}
99
100inline auto
102{
103 return profile_detail::apply_to_pool(pool, "work_stealing", profile);
104}
105
106template <typename ThreadLike>
107auto
108apply_profile_detailed(ThreadLike& value, thread_profile const& profile) -> std::vector<std::error_code>
109{
110 std::vector<std::error_code> errors;
111 thread_config scheduling;
112 scheduling.set_name(profile.name).set_scheduling(profile.scheduling);
113 auto configured = value.configure(scheduling);
114 errors.push_back(configured ? std::error_code{} : configured.error());
115 if (profile.affinity)
116 {
117 auto affinity = value.set_affinity(*profile.affinity);
118 errors.push_back(affinity ? std::error_code{} : affinity.error());
119 }
120 return errors;
121}
122
123} // namespace threadschedule::advanced
Strongly-typed realtime priority in the range $[1, 99]$.
Immutable scheduling request passed to thread creation/configuration APIs.
Portable thread configuration bundle.
auto set_name(std::string name) -> thread_config &
Set thread name.
auto set_affinity(thread_affinity affinity) -> thread_config &
Set CPU affinity mask.
auto make_config(thread_profile const &profile, std::string name) -> thread_config
auto apply_to_thread(ThreadLike &value, thread_profile const &profile) -> result< void >
auto apply_to_pool(Pool &pool, std::string name, thread_profile const &profile) -> result< void >
auto realtime() -> thread_profile
auto background() -> thread_profile
auto throughput() -> thread_profile
auto low_latency() -> thread_profile
auto apply_profile(ThreadLike &value, thread_profile const &profile) -> result< void >
auto apply_profile_detailed(ThreadLike &value, thread_profile const &profile) -> std::vector< std::error_code >
constexpr auto background() noexcept -> scheduling_config
Request background scheduling.
constexpr auto realtime_fifo(realtime_priority priority) noexcept -> scheduling_config
Request realtime FIFO scheduling.
constexpr auto normal() noexcept -> scheduling_config
Request default scheduling.
constexpr auto low_latency() noexcept -> scheduling_config
Request low-latency scheduling.
std::optional< thread_affinity > affinity