Mark3 Realtime Kernel
timerlist.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 ===========================================================================*/
23 #include "mark3.h"
24 namespace Mark3
25 {
26 //---------------------------------------------------------------------------
27 void TimerList::Init(void)
28 {
29  m_bTimerActive = false;
30  m_u32NextWakeup = 0;
31  m_clMutex.Init();
32 }
33 
34 //---------------------------------------------------------------------------
35 void TimerList::Add(Timer* pclListNode_)
36 {
37  KERNEL_ASSERT(nullptr != pclListNode_);
38  auto lock = LockGuard { &m_clMutex };
39 
40  pclListNode_->ClearNode();
41  TypedDoubleLinkList<Timer>::Add(pclListNode_);
42 
43  // Set the initial timer value
44  pclListNode_->m_u32TimeLeft = pclListNode_->m_u32Interval;
45 
46  // Set the timer as active.
47  pclListNode_->m_u8Flags |= uTimerFlagActive;
48 }
49 
50 //---------------------------------------------------------------------------
51 void TimerList::Remove(Timer* pclLinkListNode_)
52 {
53  KERNEL_ASSERT(nullptr != pclLinkListNode_);
54  auto lock = LockGuard { &m_clMutex };
55 
56  TypedDoubleLinkList<Timer>::Remove(pclLinkListNode_);
57  pclLinkListNode_->m_u8Flags &= ~uTimerFlagActive;
58 }
59 
60 //---------------------------------------------------------------------------
62 {
63  auto lock = LockGuard { &m_clMutex };
64 
65  auto* pclCurr = GetHead();
66  // Subtract the elapsed time interval from each active timer.
67  while (nullptr != pclCurr) {
68  auto* pclNext = pclCurr->GetNext();
69 
70  // Active timers only...
71  if ((pclCurr->m_u8Flags & uTimerFlagActive) != 0) {
72  pclCurr->m_u32TimeLeft--;
73  if (0 == pclCurr->m_u32TimeLeft) {
74  // Expired -- run the callback. these callbacks must be very fast...
75  if (nullptr != pclCurr->m_pfCallback) {
76  pclCurr->m_pfCallback(pclCurr->m_pclOwner, pclCurr->m_pvData);
77  }
78  if ((pclCurr->m_u8Flags & uTimerFlagOneShot) != 0) {
79  // If this was a one-shot timer, deactivate the timer + remove
80  pclCurr->m_u8Flags |= uTimerFlagExpired;
81  pclCurr->m_u8Flags &= ~uTimerFlagActive;
82  Remove(pclCurr);
83  } else {
84  // Reset the interval timer.
85  pclCurr->m_u32TimeLeft = pclCurr->m_u32Interval;
86  }
87  }
88  }
89  pclCurr = pclNext;
90  }
91 }
92 
93 } // namespace Mark3
uint32_t m_u32Interval
Interval of the timer in timer ticks.
Definition: timer.h:146
uint8_t m_u8Flags
Flags for the timer, defining if the timer is one-shot or repeated.
Definition: timer.h:140
void Remove(Timer *pclLinkListNode_)
Remove Remove a timer from the TimerList, cancelling its expiry.
Definition: timerlist.cpp:51
uint32_t m_u32TimeLeft
Time remaining on the timer.
Definition: timer.h:149
uint32_t m_u32NextWakeup
The time (in system clock ticks) of the next wakeup event.
Definition: timerlist.h:75
void Init()
Init Initialize the TimerList object. Must be called before using the object.
Definition: timerlist.cpp:27
#define KERNEL_ASSERT(x)
Definition: kerneldebug.h:36
Definition: atomic.cpp:23
Single include file given to users of the Mark3 Kernel API.
void Process()
Process Process all timers in the timerlist as a result of the timer expiring. This will select a new...
Definition: timerlist.cpp:61
Mutex m_clMutex
Guards against concurrent access to the timer list - Only needed when running threaded.
Definition: timerlist.h:81
void Init(bool bRecursive_=true)
Init Initialize a mutex object for use - must call this function before using the object...
Definition: mutex.cpp:93
static constexpr auto uTimerFlagOneShot
Timer is one-shot.
Definition: timer.h:34
The Timer Class. This class provides kernel-managed timers, used to provide high-precision delays...
Definition: timer.h:68
bool m_bTimerActive
Whether or not the timer is active.
Definition: timerlist.h:78
static constexpr auto uTimerFlagActive
Timer is currently active.
Definition: timer.h:35
void Add(Timer *pclListNode_)
Add Add a timer to the TimerList.
Definition: timerlist.cpp:35
The LockGuard class. This class provides RAII locks based on Mark3&#39;s kernel Mutex object...
Definition: lockguard.h:32
static constexpr auto uTimerFlagExpired
Timer is actually expired.
Definition: timer.h:37