contur2
Loading...
Searching...
No Matches
priority.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <cstdint>
11#include <string_view>
12
13namespace contur {
14
19 enum class PriorityLevel : std::int8_t
20 {
22 High = 1,
24 Normal = 3,
26 Low = 5,
27 Idle = 6,
28 };
29
31 constexpr std::int32_t NICE_MIN = -20;
32
34 constexpr std::int32_t NICE_MAX = 19;
35
37 constexpr std::int32_t NICE_DEFAULT = 0;
38
45 struct Priority
46 {
49 std::int32_t nice = NICE_DEFAULT;
50
52 constexpr Priority() noexcept = default;
53
56 constexpr explicit Priority(PriorityLevel level) noexcept
57 : base(level)
58 , effective(level)
60 {}
61
63 constexpr Priority(PriorityLevel base, PriorityLevel effective, std::int32_t nice) noexcept
64 : base(base)
67 {}
68
70 [[nodiscard]] constexpr bool operator==(const Priority &other) const noexcept
71 {
72 return base == other.base && effective == other.effective && nice == other.nice;
73 }
74
76 [[nodiscard]] constexpr bool operator!=(const Priority &other) const noexcept
77 {
78 return !(*this == other);
79 }
80
83 [[nodiscard]] constexpr bool isHigherThan(const Priority &other) const noexcept
84 {
85 if (effective != other.effective)
86 {
87 return static_cast<std::int8_t>(effective) < static_cast<std::int8_t>(other.effective);
88 }
89 return nice < other.nice;
90 }
91
93 [[nodiscard]] static constexpr std::int32_t clampNice(std::int32_t value) noexcept
94 {
95 if (value < NICE_MIN)
96 {
97 return NICE_MIN;
98 }
99 if (value > NICE_MAX)
100 {
101 return NICE_MAX;
102 }
103 return value;
104 }
105 };
106
108 [[nodiscard]] constexpr std::string_view priorityLevelName(PriorityLevel level) noexcept
109 {
110 switch (level)
111 {
113 return "Realtime";
115 return "High";
117 return "AboveNormal";
119 return "Normal";
121 return "BelowNormal";
123 return "Low";
125 return "Idle";
126 }
127 return "Unknown";
128 }
129
130} // namespace contur
Definition block.h:15
PriorityLevel
Discrete priority levels, from highest (Realtime) to lowest (Idle).
Definition priority.h:20
@ Low
Background tasks.
Definition priority.h:26
@ High
Above-normal urgency.
Definition priority.h:22
@ AboveNormal
Slightly above default.
Definition priority.h:23
@ Normal
Default priority for user processes.
Definition priority.h:24
@ Realtime
Highest — time-critical, preempts everything.
Definition priority.h:21
@ BelowNormal
Slightly below default.
Definition priority.h:25
@ Idle
Lowest — runs only when nothing else is ready.
Definition priority.h:27
constexpr std::int32_t NICE_MAX
Maximum nice value (lowest boost).
Definition priority.h:34
constexpr std::string_view priorityLevelName(PriorityLevel level) noexcept
Returns a human-readable name for the given priority level.
Definition priority.h:108
constexpr std::int32_t NICE_MIN
Minimum nice value (highest boost).
Definition priority.h:31
constexpr std::int32_t NICE_DEFAULT
Default nice value (no adjustment).
Definition priority.h:37
constexpr Priority() noexcept=default
Default-constructs a Normal-priority descriptor.
constexpr bool isHigherThan(const Priority &other) const noexcept
Returns true if this priority is higher (numerically lower) than other, based on effective level....
Definition priority.h:83
constexpr bool operator==(const Priority &other) const noexcept
Compares two Priority descriptors for equality.
Definition priority.h:70
std::int32_t nice
Fine-grained adjustment (-20..+19).
Definition priority.h:49
PriorityLevel base
Static priority assigned at creation.
Definition priority.h:47
constexpr bool operator!=(const Priority &other) const noexcept
Compares two Priority descriptors for inequality.
Definition priority.h:76
constexpr Priority(PriorityLevel base, PriorityLevel effective, std::int32_t nice) noexcept
Constructs a priority with base, effective, and nice values.
Definition priority.h:63
static constexpr std::int32_t clampNice(std::int32_t value) noexcept
Clamps a nice value to the valid range [NICE_MIN, NICE_MAX].
Definition priority.h:93
PriorityLevel effective
Dynamic priority used by scheduler.
Definition priority.h:48