1 #include "hmbdc/Copyright.hpp" 3 #include "hmbdc/os/Thread.hpp" 4 #include "hmbdc/Exception.hpp" 12 namespace hmbdc {
namespace os {
15 using std::runtime_error::runtime_error;
19 configureCurrentThread(
char const*threadName,
unsigned long cpumask
20 ,
char const* schepolicy =
"SCHED_OTHER",
int priority = 0);
23 setCurrrentThreadSched(
char const* schepolicy,
int priority);
27 configureCurrentThread(
char const*threadName,
unsigned long cpumask
28 ,
char const* schepolicy,
int priority) {
29 int res0 = 0, res1 = 0;
31 if (cpumask && cpumask != 0xfffffffffffffffful) {
32 cpumask &= (1ul << std::thread::hardware_concurrency()) - 1ul;
33 if (cpumask != (1ul << std::thread::hardware_concurrency()) - 1ul) {
36 for (
unsigned long i = 0; i <
sizeof(cpumask) * 8ul; ++i) {
37 if (cpumask & (1ul << i)) {
41 res0 = pthread_setaffinity_np(pthread_self(),
sizeof(mask), &mask);
45 sched_param param = {0};
46 int policy = SCHED_RR;
47 if (std::string(schepolicy) ==
"SCHED_FIFO") {
50 else if (std::string(schepolicy) ==
"SCHED_RR") {
52 }
else if (std::string(schepolicy) ==
"SCHED_IDLE") {
56 policy = SCHED_SPORADIC;
65 param.sched_priority = priority;
66 res1 = pthread_setschedparam(pthread_self(), policy, ¶m);
67 int res2 = pthread_setname_np(pthread_self(), threadName);
68 if (res0 || res1 || res2)
69 HMBDC_THROW(ThreadConfigException,
"cpumask=" << cpumask <<
" pthread_setaffinity_np=" << res0
70 <<
" pthread_setschedparam=" << res1 <<
" pthread_setname_np=" << res2);
75 setCurrrentThreadSched(
char const* schepolicy,
int priority) {
77 sched_param param = {0};
78 int policy = SCHED_RR;
79 if (std::string(schepolicy) ==
"SCHED_FIFO") {
82 else if (std::string(schepolicy) ==
"SCHED_RR") {
84 }
else if (std::string(schepolicy) ==
"SCHED_IDLE") {
91 param.sched_priority = priority;
92 int res1 = pthread_setschedparam(pthread_self(), policy, ¶m);
94 HMBDC_THROW(ThreadConfigException,
" sched_setscheduler=" << res1);
Definition: Thread.hpp:14