Mark3 Realtime Kernel
message.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 namespace Mark3
24 {
25 //---------------------------------------------------------------------------
27 {
28  m_clList.Init();
29 }
30 
31 //---------------------------------------------------------------------------
32 void MessagePool::Push(Message* pclMessage_)
33 {
34  KERNEL_ASSERT(pclMessage_);
35 
36  const auto cs = CriticalGuard{};
37  m_clList.Add(pclMessage_);
38 }
39 
40 //---------------------------------------------------------------------------
42 {
43  const auto cs = CriticalGuard{};
44  auto* pclRet = m_clList.GetHead();
45  if (nullptr != pclRet) {
46  m_clList.Remove(pclRet);
47  }
48  return pclRet;
49 }
50 
51 //------------------------------------------------------------------------
53 {
54  return m_clList.GetHead();
55 }
56 
57 //---------------------------------------------------------------------------
59 {
60  m_clSemaphore.Init(0, 255);
61 }
62 
63 //---------------------------------------------------------------------------
65 {
66  return Receive_i(0);
67 }
68 
69 //---------------------------------------------------------------------------
70 Message* MessageQueue::Receive(uint32_t u32TimeWaitMS_)
71 {
72  return Receive_i(u32TimeWaitMS_);
73 }
74 
75 //---------------------------------------------------------------------------
76 Message* MessageQueue::Receive_i(uint32_t u32TimeWaitMS_)
77 {
78  // Block the current thread on the counting semaphore
79  if (!m_clSemaphore.Pend(u32TimeWaitMS_)) {
80  return nullptr;
81  }
82 
83  const auto cs = CriticalGuard{};
84  // Pop the head of the message queue and return it
85  auto* pclRet = m_clLinkList.GetHead();
86  m_clLinkList.Remove(pclRet);
87 
88  return pclRet;
89 }
90 
91 //---------------------------------------------------------------------------
93 {
94  KERNEL_ASSERT(pclSrc_);
95  {
96  const auto cg = CriticalGuard{};
97  // Add the message to the head of the linked list
98  m_clLinkList.Add(pclSrc_);
99  }
100 
101  // Post the semaphore, waking the blocking thread for the queue.
102  m_clSemaphore.Post();
103 }
104 
105 //---------------------------------------------------------------------------
107 {
108  return m_clSemaphore.GetCount();
109 }
110 } // namespace Mark3
Message * Receive()
Receive.
Definition: message.cpp:64
void Init()
Init.
Definition: message.cpp:58
TypedDoubleLinkList< Message > m_clList
Linked list used to manage the Message objects.
Definition: message.h:201
void Send(Message *pclSrc_)
Send.
Definition: message.cpp:92
#define KERNEL_ASSERT(x)
Definition: kerneldebug.h:36
void Push(Message *pclMessage_)
Push.
Definition: message.cpp:32
Definition: atomic.cpp:23
Message * Receive_i(uint32_t u32TimeWaitMS_)
Receive_i.
Definition: message.cpp:76
Message * Pop()
Pop.
Definition: message.cpp:41
Single include file given to users of the Mark3 Kernel API.
the Message class. This object provides threadsafe message-based IPC services based on exchange of ob...
Definition: message.h:97
uint16_t GetCount()
GetCount.
Definition: message.cpp:106
The CriticalGuard class. This class provides an implemention of RAII for critical sections...
Definition: criticalguard.h:38
void Init()
Init.
Definition: message.cpp:26
Message * GetHead()
GetHead.
Definition: message.cpp:52