Mark3 Realtime Kernel
scheduler.cpp
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 ===========================================================================*/
22 #include "mark3.h"
23 
26 
27 namespace Mark3
28 {
31 ThreadList Scheduler::m_clStopList;
34 
35 //---------------------------------------------------------------------------
37 {
38  for (size_t i = 0; i < m_uNumPriorities; i++) {
41  }
42 }
43 
44 //---------------------------------------------------------------------------
46 {
47  auto uXPrio = m_clPrioMap.HighestPriority();
48  if (0 == uXPrio) {
50  }
51  // Priorities are one-indexed
52  uXPrio--;
53 
54  // Get the thread node at this priority.
55  g_pclNext = m_aclPriorities[uXPrio].GetHead();
56 }
57 
58 //---------------------------------------------------------------------------
59 void Scheduler::Add(Thread* pclThread_)
60 {
61  KERNEL_ASSERT(pclThread_ != nullptr);
62 
63  m_aclPriorities[pclThread_->GetPriority()].Add(pclThread_);
64 }
65 
66 //---------------------------------------------------------------------------
67 void Scheduler::Remove(Thread* pclThread_)
68 {
69  KERNEL_ASSERT(pclThread_ != nullptr);
70 
71  m_aclPriorities[pclThread_->GetPriority()].Remove(pclThread_);
72 }
73 
74 //---------------------------------------------------------------------------
75 bool Scheduler::SetScheduler(bool bEnable_)
76 {
77  const auto cs = CriticalGuard{};
78  auto bRet = m_bEnabled;
79  m_bEnabled = bEnable_;
80  // If there was a queued scheduler evevent, dequeue and trigger an
81  // immediate Yield
83  m_bQueuedSchedule = false;
84  Thread::Yield();
85  }
86  return bRet;
87 }
88 } // namespace Mark3
void SetMapPointer(PriorityMap *pclMap_)
SetMapPointer Set the pointer to a bitmap to use for this threadlist. Once again, only needed when th...
Definition: threadlist.cpp:40
static PriorityMap m_clPrioMap
Priority bitmap lookup structure, 1-bit per thread priority.
Definition: scheduler.h:174
static void Add(Thread *pclThread_)
Add Add a thread to the scheduler at its current priority level.
Definition: scheduler.cpp:59
static bool m_bEnabled
Scheduler&#39;s state - enabled or disabled.
Definition: scheduler.h:162
static bool m_bQueuedSchedule
Variable representing whether or not there&#39;s a queued scheduler operation.
Definition: scheduler.h:165
#define KERNEL_ASSERT(x)
Definition: kerneldebug.h:36
static ThreadList m_clStopList
ThreadList for all stopped threads.
Definition: scheduler.h:168
void Remove(Thread *node_)
Remove Remove the specified thread from the threadlist.
Definition: threadlist.cpp:111
Definition: atomic.cpp:23
static ThreadList m_aclPriorities[m_uNumPriorities]
ThreadLists for all threads at all priorities.
Definition: scheduler.h:171
Mark3::Thread * g_pclCurrent
Definition: scheduler.cpp:25
void SetPriority(PORT_PRIO_TYPE uXPriority_)
SetPriority Set the priority of this threadlist (if used for a scheduler).
Definition: threadlist.cpp:34
Single include file given to users of the Mark3 Kernel API.
The Thread Class. This object providing the fundamental thread control data structures and functions ...
Definition: thread.h:64
Mark3::Thread * g_pclNext
Definition: scheduler.cpp:24
PriorityMapL1< PORT_PRIO_TYPE, KERNEL_NUM_PRIORITIES > PriorityMap
Definition: priomap.h:29
#define KERNEL_NUM_PRIORITIES
Definition: portcfg.h:35
static void Yield(void)
Yield Yield the thread - this forces the system to call the scheduler and determine what thread shoul...
Definition: thread.cpp:304
#define PANIC_NO_READY_THREADS
Definition: paniccodes.h:27
static void Schedule()
Schedule Run the scheduler, determines the next thread to run based on the current state of the threa...
Definition: scheduler.cpp:45
T HighestPriority(void)
HighestPriority Computes the numeric priority of the highest-priority thread represented in the prior...
Definition: priomapl1.h:86
The CriticalGuard class. This class provides an implemention of RAII for critical sections...
Definition: criticalguard.h:38
void Add(Thread *node_)
Add Add a thread to the threadlist.
Definition: threadlist.cpp:47
static void Init()
Init Intiailize the scheduler, must be called before use.
Definition: scheduler.cpp:36
static void Remove(Thread *pclThread_)
Remove Remove a thread from the scheduler at its current priority level.
Definition: scheduler.cpp:67
static constexpr auto m_uNumPriorities
Definition: scheduler.h:159
PORT_PRIO_TYPE GetPriority(void)
GetPriority Return the priority of the current thread.
Definition: thread.h:174
static void Panic(uint16_t u16Cause_)
Panic Cause the kernel to enter its panic state.
Definition: kernel.cpp:70
static bool SetScheduler(bool bEnable_)
SetScheduler Set the active state of the scheduler. When the scheduler is disabled, the next thread is never set; the currently running thread will run forever until the scheduler is enabled again. Care must be taken to ensure that we don&#39;t end up trying to block while the scheduler is disabled, otherwise the system ends up in an unusable state.
Definition: scheduler.cpp:75