Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Thread.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Thread.hpp"
18
19#include <toolbox/sys/Error.hpp>
22
23namespace toolbox {
24inline namespace sys {
25using namespace std;
26namespace {
27pair<int, int> split_range(string_view s) noexcept
28{
29 auto [first, last] = split_pair(s, '-');
30 const auto i = ston<int>(first);
31 return {i, last.empty() ? i : ston<int>(last)};
32}
33} // namespace
34
35cpu_set_t parse_cpu_set(string_view s) noexcept
36{
38 CPU_ZERO(&bs);
39
40 Tokeniser toks{s, ","sv};
41 while (!toks.empty()) {
42 auto [i, j] = split_range(toks.top());
43 for (; i <= j; ++i) {
44 CPU_SET(i, &bs);
45 }
46 toks.pop();
47 }
48 return bs;
49}
50
51int parse_sched_policy(string_view s)
52{
53 int policy;
54 if (s == "other"sv) {
56 } else if (s == "fifo"sv) {
58 } else if (s == "rr"sv) {
60 } else if (s == "batch"sv) {
62 } else if (s == "idle"sv) {
64 } else {
65 throw invalid_argument{"invalid sched policy"};
66 }
67 return policy;
68}
69
71{
72 const auto tid = pthread_self();
73 if (!config.name.empty()) {
74 if (const auto err = pthread_setname_np(tid, config.name.c_str()); err != 0) {
75 throw system_error{make_error(err), "pthread_setname_np"};
76 }
77 }
78 if (!config.affinity.empty()) {
79 const auto bs = parse_cpu_set(config.affinity);
80 if (const auto err = pthread_setaffinity_np(tid, sizeof(bs), &bs); err != 0) {
81 throw system_error{make_error(err), "pthread_setaffinity_np"};
82 }
83 }
84 if (!config.sched_policy.empty()) {
85 const auto policy = parse_sched_policy(config.sched_policy);
87 if (priority == -1) {
88 throw system_error{make_error(priority), "sched_get_priority_max"};
89 }
90 sched_param param{.sched_priority = priority};
91 if (const auto err = pthread_setschedparam(tid, policy, &param); err != 0) {
92 throw system_error{make_error(err), "pthread_setschedparam"};
93 }
94 }
95}
96
97} // namespace sys
98} // namespace toolbox
STL namespace.
void set_thread_attrs(const ThreadConfig &config)
Definition Thread.cpp:70
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
int parse_sched_policy(string_view s)
Definition Thread.cpp:51
cpu_set_t parse_cpu_set(string_view s) noexcept
Definition Thread.cpp:35
pair< string_view, string_view > split_pair(string_view s, char delim) noexcept
Definition String.cpp:51
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92
ThreadConfig holds the thread attributes.
Definition Thread.hpp:29