JayBeams  0.1
Another project to have fun coding.
thread_config.cpp
Go to the documentation of this file.
1 #include "jb/thread_config.hpp"
2 #include <jb/strtonum.hpp>
3 
4 #include <sched.h>
5 #include <stdexcept>
6 
7 namespace jb {
8 namespace defaults {
9 
10 #ifndef JB_DEFAULT_thread_config_scheduler
11 #define JB_DEFAULT_thread_config_scheduler "OTHER"
12 #endif // JB_DEFAULT_thread_config_scheduler
13 
14 #ifndef JB_DEFAULT_thread_config_priority
15 #define JB_DEFAULT_thread_config_priority "MIN"
16 #endif // JB_DEFAULT_thread_config_priority
17 
18 #ifndef JB_DEFAULT_ignore_setup_errors
19 #define JB_DEFAULT_ignore_setup_errors true
20 #endif // JB_DEFAULT_ignore_setup_errors
21 
25 
26 } // namespace defaults
27 } // namespace jb
28 
30  : name(desc("name").help("The name of this thread"), this)
31  , scheduler(
32  desc("scheduler").help("The scheduling policy for this thread"), this,
34  , priority(
35  desc("priority")
36  .help("The priority for this thread. "
37  "Use MIN/MID/MAX for the minimum, midpoint and maximum "
38  "priorities "
39  "in the scheduling class. "
40  "Use a number to set the specific priority"),
42  , affinity(
43  desc("affinity", "cpu_set")
44  .help("The CPU affinity for this thread. "
45  "If none is set, the thread keeps its default affinity "
46  "settings."),
47  this)
49  desc("ignore-setup-errors")
50  .help("Ignore errors changing thread parameters."),
52 }
53 
55  if (scheduler() == "RR") {
56  return SCHED_RR;
57  }
58  if (scheduler() == "FIFO") {
59  return SCHED_FIFO;
60  }
61  if (scheduler() == "OTHER") {
62  return SCHED_OTHER;
63  }
64  if (scheduler() == "BATCH") {
65  return SCHED_BATCH;
66  }
67  if (scheduler() == "IDLE") {
68  return SCHED_IDLE;
69  }
70  std::string msg("Unknown scheduling policy: ");
71  msg += scheduler();
72  throw std::runtime_error(msg);
73 }
74 
76  int policy = native_scheduling_policy();
77  if (priority() == "MIN") {
78  return sched_get_priority_min(policy);
79  }
80  if (priority() == "MAX") {
81  return sched_get_priority_max(policy);
82  }
83  if (priority() == "MID") {
84  return (sched_get_priority_max(policy) + sched_get_priority_min(policy)) /
85  2;
86  }
87  int prio;
88  if (jb::strtonum(priority(), prio)) {
89  return prio;
90  }
91  std::string msg("Invalid scheduling priority: ");
92  msg += priority();
93  throw std::runtime_error(msg);
94 }
Define defaults for program parameters.
int native_priority() const
#define JB_DEFAULT_thread_config_scheduler
thread_config()
Constructor.
#define JB_DEFAULT_thread_config_priority
std::string thread_config_priority
std::string thread_config_scheduler
int native_scheduling_policy() const
bool strtonum(std::string const &s, T &r)
Generic string to number conversion with validation.
Definition: strtonum.hpp:40
jb::config_attribute< thread_config, std::string > scheduler
bool ignore_setup_errors
#define JB_DEFAULT_ignore_setup_errors
jb::config_attribute< thread_config, std::string > priority
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7