distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
RoundRobinQuantum.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_ROUNDROBINQUANTUM_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_ROUNDROBINQUANTUM_HPP_
14 
15 #include "distortos/TickClock.hpp"
16 
17 namespace distortos
18 {
19 
20 namespace internal
21 {
22 
25 {
26 public:
27 
29  using Representation = uint8_t;
30 
32  using Duration = std::chrono::duration<Representation, TickClock::period>;
33 
38  constexpr static Duration getInitial()
39  {
41  }
42 
49  constexpr RoundRobinQuantum() :
51  {
52 
53  }
54 
64  void decrement()
65  {
66  if (isZero() == false)
67  --quantum_;
68  }
69 
76  Duration get() const
77  {
78  return quantum_;
79  }
80 
87  bool isZero() const
88  {
89  return quantum_ == Duration{0};
90  }
91 
98  void reset()
99  {
100  quantum_ = getInitial();
101  }
102 
103 private:
104 
105  static_assert(DISTORTOS_TICK_FREQUENCY > 0, "DISTORTOS_TICK_FREQUENCY must be positive and non-zero!");
106  static_assert(DISTORTOS_ROUND_ROBIN_FREQUENCY > 0,
107  "DISTORTOS_ROUND_ROBIN_FREQUENCY must be positive and non-zero!");
108 
110  constexpr static auto quantumRawInitializer_ = (DISTORTOS_TICK_FREQUENCY + DISTORTOS_ROUND_ROBIN_FREQUENCY / 2) /
111  DISTORTOS_ROUND_ROBIN_FREQUENCY;
112 
113  static_assert(quantumRawInitializer_ > 0 || quantumRawInitializer_ <= UINT8_MAX,
114  "DISTORTOS_TICK_FREQUENCY and DISTORTOS_ROUND_ROBIN_FREQUENCY values produce invalid round-robin quantum!");
115 
118 };
119 
120 } // namespace internal
121 
122 } // namespace distortos
123 
124 #endif // INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_ROUNDROBINQUANTUM_HPP_
RoundRobinQuantum class is a quantum of time for round-robin scheduling.
Definition: RoundRobinQuantum.hpp:24
bool isZero() const
Convenience function to test whether the quantum is already at 0.
Definition: RoundRobinQuantum.hpp:87
constexpr RoundRobinQuantum()
RoundRobinQuantum's constructor.
Definition: RoundRobinQuantum.hpp:49
TickClock class header.
Duration get() const
Gets current value of round-robin's quantum.
Definition: RoundRobinQuantum.hpp:76
uint8_t Representation
type of quantum counter
Definition: RoundRobinQuantum.hpp:29
static constexpr Duration getInitial()
Definition: RoundRobinQuantum.hpp:38
Top-level namespace of distortos project.
Definition: buttons.hpp:33
static constexpr auto quantumRawInitializer_
raw initializer value for round-robin quantum, calculated with rounding to nearest
Definition: RoundRobinQuantum.hpp:110
void decrement()
Decrements round-robin's quantum.
Definition: RoundRobinQuantum.hpp:64
void reset()
Resets value of round-robin's quantum.
Definition: RoundRobinQuantum.hpp:98
Duration quantum_
round-robin quantum
Definition: RoundRobinQuantum.hpp:114
std::chrono::duration< Representation, TickClock::period > Duration
duration type used for quantum
Definition: RoundRobinQuantum.hpp:32