Mark3 Realtime Kernel
priomapl1.h
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 ===========================================================================*/
20 #pragma once
21 
22 #include "kerneltypes.h"
23 #include "mark3cfg.h"
24 #include "threadport.h"
25 
26 namespace Mark3
27 {
28 //---------------------------------------------------------------------------
44 template <typename T, size_t C>
46 {
47 public:
53  {
54  m_uXPriorityMap = 0;
55  }
56 
62  void Set(T uXPrio_)
63  {
64  auto uXPrioBit = PrioBit(uXPrio_);
65  m_uXPriorityMap |= (1 << uXPrioBit);
66  }
67 
73  void Clear(T uXPrio_)
74  {
75  auto uXPrioBit = PrioBit(uXPrio_);
76  m_uXPriorityMap &= ~(1 << uXPrioBit);
77  }
78 
87  {
88  auto uXPrio = PriorityFromBitmap(m_uXPriorityMap);
89  return uXPrio;
90  }
91 
92 private:
93  static inline T PrioBit(T prio) { return prio & m_uXPrioMapBitMask; }
94 
95  static inline T PrioMapWordIndex(T prio) { return prio >> m_uXPrioMapWordShift; }
96 
97  static inline T PriorityFromBitmap(T uXPrio_)
98  {
99 #if PORT_USE_HW_CLZ
100  // Support hardware-accelerated Count-leading-zeros instruction
101  return m_uXPrioMapBits - PORT_CLZ(uXPrio_);
102 #else
103  // Default un-optimized count-leading zeros operation
104  T uXMask = 1 << (m_uXPrioMapBits - 1);
105  auto u8Zeros = T { 0 };
106 
107  while (uXMask) {
108  if (uXMask & uXPrio_) {
109  return (m_uXPrioMapBits - u8Zeros);
110  }
111 
112  uXMask >>= 1;
113  u8Zeros++;
114  }
115  return 0;
116 #endif
117  }
118 
119  static constexpr size_t m_uXPrioMapShiftLUT[9] = {0, 3, 4, 0, 5, 0, 0, 0, 6};
120  static constexpr auto m_uXPrioMapWordShift = T { m_uXPrioMapShiftLUT[sizeof(T)] };
121  static constexpr auto m_uXPrioMapBits = T { 8 * sizeof(T) };
122  static constexpr auto m_uXPrioMapBitMask = T { (1 << m_uXPrioMapWordShift) - 1 };
123 
125 };
126 } // namespace Mark3
static T PriorityFromBitmap(T uXPrio_)
Definition: priomapl1.h:97
Basic data type primatives used throughout the OS.
static T PrioBit(T prio)
Definition: priomapl1.h:93
static T PrioMapWordIndex(T prio)
Definition: priomapl1.h:95
PriorityMapL1()
PriorityMap Initialize the priority map object, clearing the bitamp data to all 0&#39;s.
Definition: priomapl1.h:52
static constexpr auto m_uXPrioMapWordShift
Definition: priomapl1.h:120
Definition: atomic.cpp:23
Mark3 Kernel Configuration This file is used to configure the kernel for your specific application in...
uint8_t PORT_CLZ(uint8_t in_)
Definition: threadport.h:144
static constexpr auto m_uXPrioMapBits
Definition: priomapl1.h:121
static constexpr auto m_uXPrioMapBitMask
Definition: priomapl1.h:122
ATMega1284p Multithreading support.
The PriorityMapL1 class This class implements a priority bitmap data structure. Each bit in the objec...
Definition: priomapl1.h:45
T HighestPriority(void)
HighestPriority Computes the numeric priority of the highest-priority thread represented in the prior...
Definition: priomapl1.h:86
void Set(T uXPrio_)
Set Set the priority map bitmap data, at all levels, for the given priority.
Definition: priomapl1.h:62
void Clear(T uXPrio_)
Clear Clear the priority map bitmap data, at all levels, for the given priority.
Definition: priomapl1.h:73
static constexpr size_t m_uXPrioMapShiftLUT[9]
Definition: priomapl1.h:119