Mark3 Realtime Kernel
coroutine.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 =========================================================================== */
20 #include "coroutine.h"
21 #include "cosched.h"
22 #include "criticalguard.h"
23 #include "kernel.h"
24 
25 namespace Mark3 {
26 
27 //---------------------------------------------------------------------------
29 {
32  }
33 
34  const auto cs = CriticalGuard{};
35  m_pclOwner->Remove(this);
36 }
37 
38 //---------------------------------------------------------------------------
39 void Coroutine::Init(PORT_PRIO_TYPE uPriority_, CoroutineHandler pfHandler_, void* pvContext_)
40 {
41  m_bQueued = false;
42  m_pfHandler = pfHandler_;
43  m_pvContext = pvContext_;
44  m_uPriority = uPriority_;
45 
47 
48  const auto cs = CriticalGuard{};
49  m_pclOwner->Add(this);
50 }
51 
52 //---------------------------------------------------------------------------
54 {
55  { // Begin critical section
56  const auto cs = CriticalGuard{};
57  m_pclOwner->Remove(this);
59  m_pclOwner->Add(this);
60  m_bQueued = false;
61  } // end critical section
62 
63  m_pfHandler(this, m_pvContext);
64 }
65 
66 //---------------------------------------------------------------------------
68 {
69  const auto cs = CriticalGuard{};
70 
71  if (m_bQueued) {
72  return;
73  }
74 
75  m_pclOwner->Remove(this);
77  m_pclOwner->Add(this);
78  m_bQueued = true;
79 }
80 
81 //---------------------------------------------------------------------------
83 {
84  const auto cs = CriticalGuard{};
85 
86  m_pclOwner->Remove(this);
87  m_uPriority = uPriority_;
88  if (m_bQueued) {
90  } else {
92  }
93  m_pclOwner->Add(this);
94 }
95 
96 //---------------------------------------------------------------------------
98 {
99  return m_uPriority;
100 }
101 } // namespace Mark3
CoRoutine Scheduler implementation.
CoRoutine implementation.
void * m_pvContext
Definition: coroutine.h:106
static CoList * GetStopList()
GetStopList Get the pointer to the coroutine list managing initialized coroutines that are not awaiti...
Definition: cosched.cpp:45
Kernel initialization and startup class.
CoList * m_pclOwner
Definition: coroutine.h:104
CoroutineHandler m_pfHandler
Definition: coroutine.h:105
PORT_PRIO_TYPE m_uPriority
Definition: coroutine.h:107
#define PANIC_ACTIVE_COROUTINE_DESCOPED
Definition: paniccodes.h:35
void Add(Coroutine *pclCoroutine_)
Add Add a coroutine object to this list.
Definition: colist.cpp:36
#define PORT_PRIO_TYPE
Type used for bitmap in the PriorityMap class.
Definition: portcfg.h:73
void Activate()
Activate Tag the co-routine as pending execution. Has no effect if the co-routine is already pending ...
Definition: coroutine.cpp:67
Definition: atomic.cpp:23
void SetPriority(PORT_PRIO_TYPE uPriority_)
SetPriority Update the scheduling priority of the co-routine. Can be called from within the co-routin...
Definition: coroutine.cpp:82
static CoList * GetCoList(PORT_PRIO_TYPE uPriority_)
GetCoList Retrieve the coroutine list associated with a given priority.
Definition: cosched.cpp:51
void(*)(Coroutine *pclCaller_, void *pvContext_) CoroutineHandler
Definition: coroutine.h:40
PORT_PRIO_TYPE GetPriority()
GetPriority Retrieve the current scheduling priority of the co-routine.
Definition: coroutine.cpp:97
RAII Critical Section Implementation.
void Run()
Run Clear the co-routine's pending execution flag, and execute the coroutine's handler function...
Definition: coroutine.cpp:53
The CriticalGuard class. This class provides an implemention of RAII for critical sections...
Definition: criticalguard.h:38
void Init(PORT_PRIO_TYPE uPriority_, CoroutineHandler pfHandler_, void *pvContext_)
Init Initialize the coroutine object prior to use. Must be called before using the other methods in t...
Definition: coroutine.cpp:39
void Remove(Coroutine *pclCoroutine_)
Remove Remove a given coroutine object from this list.
Definition: colist.cpp:46
static void Panic(uint16_t u16Cause_)
Panic Cause the kernel to enter its panic state.
Definition: kernel.cpp:70