SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_priority_bitmap.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_PRIORITY_BITMAP_H_
11#define STK_PRIORITY_BITMAP_H_
12
18
19#include "stk_common.h"
20
21namespace stk {
22namespace sched {
23
50template <class BitmapT>
52
54namespace priority_bitmap_detail {
55
57template <size_t SIZE>
58struct Scan
59{
60 static __stk_forceinline uint32_t Highest(uint32_t value)
61 {
62 return 31U - CountLeadingZeros(value);
63 }
64};
65
68template <>
69struct Scan<8U>
70{
71 static __stk_forceinline uint32_t Highest(uint64_t value)
72 {
73 const uint32_t hi = static_cast<uint32_t>(value >> 32U);
74
75 return (hi != 0U) ? (63U - CountLeadingZeros(hi))
76 : (31U - CountLeadingZeros(static_cast<uint32_t>(value)));
77 }
78};
79
80} // namespace priority_bitmap_detail
82
83template <class BitmapT>
85{
86 enum { CAPACITY = (sizeof(BitmapT) * 8U) };
87
89 static __stk_forceinline void Set(BitmapT &bitmap, uint32_t bit)
90 {
91 bitmap = static_cast<BitmapT>(bitmap | static_cast<BitmapT>(static_cast<BitmapT>(1U) << bit));
92 }
93
95 static __stk_forceinline void Clear(BitmapT &bitmap, uint32_t bit)
96 {
97 bitmap = static_cast<BitmapT>(bitmap & static_cast<BitmapT>(~static_cast<BitmapT>(static_cast<BitmapT>(1U) << bit)));
98 }
99
101 static __stk_forceinline bool IsSet(const BitmapT &bitmap, uint32_t bit)
102 {
103 return ((bitmap & static_cast<BitmapT>(static_cast<BitmapT>(1U) << bit)) != static_cast<BitmapT>(0U));
104 }
105
107 static __stk_forceinline bool IsEmpty(const BitmapT &bitmap)
108 {
109 return (bitmap == static_cast<BitmapT>(0U));
110 }
111
113 static __stk_forceinline uint32_t HighestSet(const BitmapT &bitmap)
114 {
115 return priority_bitmap_detail::Scan<sizeof(BitmapT)>::Highest(bitmap);
116 }
117};
118
139template <uint32_t BITS>
141{
142public:
143 STK_STATIC_ASSERT_DESC_N(Multiple32, (BITS % 32U) == 0U, "PriorityBitmap: BITS must be a multiple of 32");
144 STK_STATIC_ASSERT_DESC_N(Min32, BITS >= 32U, "PriorityBitmap: BITS must be at least 32");
145 STK_STATIC_ASSERT_DESC_N(Max1024, BITS <= 1024U, "PriorityBitmap: BITS must not exceed 1024 (32-bit summary)");
146
147 enum
148 {
149 CAPACITY = BITS,
150 WORDS = (BITS / 32U)
151 };
152
156
158 __stk_forceinline void Set(uint32_t bit)
159 {
160 STK_ASSERT(bit < BITS);
161
162 const uint32_t word = (bit >> 5U);
163
164 m_words[word] |= (1U << (bit & 31U));
165 m_summary |= (1U << word);
166 }
167
169 __stk_forceinline void Clear(uint32_t bit)
170 {
171 STK_ASSERT(bit < BITS);
172
173 const uint32_t word = (bit >> 5U);
174
175 m_words[word] &= ~(1U << (bit & 31U));
176
177 if (m_words[word] == 0U)
178 {
179 m_summary &= ~(1U << word);
180 }
181 }
182
184 __stk_forceinline bool IsSet(uint32_t bit) const
185 {
186 STK_ASSERT(bit < BITS);
187
188 return ((m_words[bit >> 5U] & (1U << (bit & 31U))) != 0U);
189 }
190
193 {
194 return (m_summary == 0U);
195 }
196
199 {
200 STK_ASSERT(m_summary != 0U);
201
202 const uint32_t word = (31U - CountLeadingZeros(m_summary));
203
204 STK_ASSERT(m_words[word] != 0U);
205
206 return ((word << 5U) + (31U - CountLeadingZeros(m_words[word])));
207 }
208
209private:
210 uint32_t m_summary;
211 uint32_t m_words[WORDS];
212};
213
215template <uint32_t BITS>
217{
218 enum { CAPACITY = BITS };
219
220 static __stk_forceinline void Set(PriorityBitmap<BITS> &bitmap, uint32_t bit)
221 {
222 bitmap.Set(bit);
223 }
224
225 static __stk_forceinline void Clear(PriorityBitmap<BITS> &bitmap, uint32_t bit)
226 {
227 bitmap.Clear(bit);
228 }
229
230 static __stk_forceinline bool IsSet(const PriorityBitmap<BITS> &bitmap, uint32_t bit)
231 {
232 return bitmap.IsSet(bit);
233 }
234
235 static __stk_forceinline bool IsEmpty(const PriorityBitmap<BITS> &bitmap)
236 {
237 return bitmap.IsEmpty();
238 }
239
240 static __stk_forceinline uint32_t HighestSet(const PriorityBitmap<BITS> &bitmap)
241 {
242 return bitmap.HighestSet();
243 }
244};
246
253template <uint16_t MAX_PRIORITIES, uint32_t KIND = ((MAX_PRIORITIES <= 32U) ? 0U : ((MAX_PRIORITIES <= 64U) ? 1U : 2U))>
254struct ReadyBitmapSelect
255{
256 typedef PriorityBitmap<(((MAX_PRIORITIES + 31U) / 32U) * 32U)> Type;
257};
258
260template <uint16_t MAX_PRIORITIES>
262{
263 typedef uint32_t Type;
264};
265
266template <uint16_t MAX_PRIORITIES>
268{
269 typedef uint64_t Type;
270};
272
273} // namespace sched
274} // namespace stk
275
276#endif /* STK_PRIORITY_BITMAP_H_ */
Contains interface definitions of the library.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:270
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
Namespace of STK package.
uint32_t CountLeadingZeros(const uint32_t value) noexcept
Count leading zeros.
Definition stk_defs.h:791
Task-switching strategies (stk::sched::SwitchStrategy*) and their helpers (priority bitmaps,...
Operations on the "ready priorities" bitmap used by SwitchStrategyFixedPriority and SwitchStrategyPre...
static __stk_forceinline void Set(BitmapT &bitmap, uint32_t bit)
Set bit bit.
static __stk_forceinline uint32_t HighestSet(const BitmapT &bitmap)
Index of the most significant set bit.
static __stk_forceinline void Clear(BitmapT &bitmap, uint32_t bit)
Clear bit bit.
static __stk_forceinline bool IsEmpty(const BitmapT &bitmap)
Check that no bit is set.
static __stk_forceinline bool IsSet(const BitmapT &bitmap, uint32_t bit)
Check that bit bit is set.
Two-level bitmap for more priority levels than a native integer can hold (BITS is a multiple of 32 ab...
__stk_forceinline bool IsEmpty() const
Check that no bit is set.
__stk_forceinline bool IsSet(uint32_t bit) const
Check that bit bit is set, [0, BITS).
STK_STATIC_ASSERT_DESC_N(Max1024, BITS<=1024U, "PriorityBitmap: BITS must not exceed 1024 (32-bit summary)")
STK_STATIC_ASSERT_DESC_N(Multiple32,(BITS % 32U)==0U, "PriorityBitmap: BITS must be a multiple of 32")
__stk_forceinline void Set(uint32_t bit)
Set bit bit, [0, BITS).
__stk_forceinline void Clear(uint32_t bit)
Clear bit bit, [0, BITS).
PriorityBitmap()
Construct an empty bitmap.
STK_STATIC_ASSERT_DESC_N(Min32, BITS >=32U, "PriorityBitmap: BITS must be at least 32")
__stk_forceinline uint32_t HighestSet() const
Index of the most significant set bit.
Selects the default bitmap type for a number of priority levels: uint32_t for up to 32,...
PriorityBitmap<(((MAX_PRIORITIES+31U)/32U) *32U)> Type