Mark3 Realtime Kernel
mailbox.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 "mark3cfg.h"
23 #include "kerneltypes.h"
24 #include "ithreadport.h"
25 #include "ksemaphore.h"
26 
27 namespace Mark3
28 {
35 class Mailbox
36 {
37 public:
38  void* operator new(size_t sz, void* pv) { return reinterpret_cast<Mailbox*>(pv); }
39  ~Mailbox();
40 
50  void Init(void* pvBuffer_, uint16_t u16BufferSize_, uint16_t u16ElementSize_);
51 
64  static Mailbox* Init(uint16_t u16BufferSize_, uint16_t u16ElementSize_);
65 
78  bool Send(void* pvData_);
79 
92  bool SendTail(void* pvData_);
93 
107  bool Send(void* pvData_, uint32_t u32TimeoutMS_);
108 
122  bool SendTail(void* pvData_, uint32_t u32TimeoutMS_);
123 
132  void Receive(void* pvData_);
133 
142  void ReceiveTail(void* pvData_);
143 
155  bool Receive(void* pvData_, uint32_t u32TimeoutMS_);
156 
168  bool ReceiveTail(void* pvData_, uint32_t u32TimeoutMS_);
169 
170  uint16_t GetFreeSlots(void)
171  {
172  const auto cs = CriticalGuard{};
173  return m_u16Free;
174  }
175 
176  bool IsFull(void) { return (GetFreeSlots() == 0); }
177  bool IsEmpty(void) { return (GetFreeSlots() == m_u16Count); }
178 
179 private:
187  void* GetHeadPointer(void)
188  {
189  auto uAddr = reinterpret_cast<K_ADDR>(m_pvBuffer);
190  uAddr += static_cast<K_ADDR>(m_u16ElementSize) * static_cast<K_ADDR>(m_u16Head);
191  return reinterpret_cast<void*>(uAddr);
192  }
193 
201  void* GetTailPointer(void)
202  {
203  auto uAddr = reinterpret_cast<K_ADDR>(m_pvBuffer);
204  uAddr += static_cast<K_ADDR>(m_u16ElementSize) * static_cast<K_ADDR>(m_u16Tail);
205  return reinterpret_cast<void*>(uAddr);
206  }
207 
216  void CopyData(const void* src_, void* dst_, uint16_t len_)
217  {
218  auto* u8Src = reinterpret_cast<const uint8_t*>(src_);
219  auto* u8Dst = reinterpret_cast<uint8_t*>(dst_);
220  while (len_--) { *u8Dst++ = *u8Src++; }
221  }
222 
227  void MoveTailForward(void)
228  {
229  m_u16Tail++;
230  if (m_u16Tail == m_u16Count) {
231  m_u16Tail = 0;
232  }
233  }
234 
239  void MoveHeadForward(void)
240  {
241  m_u16Head++;
242  if (m_u16Head == m_u16Count) {
243  m_u16Head = 0;
244  }
245  }
246 
251  void MoveTailBackward(void)
252  {
253  if (m_u16Tail == 0) {
255  }
256  m_u16Tail--;
257  }
258 
263  void MoveHeadBackward(void)
264  {
265  if (m_u16Head == 0) {
267  }
268  m_u16Head--;
269  }
270 
280  bool Send_i(const void* pvData_, bool bTail_, uint32_t u32TimeoutMS_);
281 
291  bool Receive_i(void* pvData_, bool bTail_, uint32_t u32WaitTimeMS_);
292 
293  uint16_t m_u16Head;
294  uint16_t m_u16Tail;
295 
296  uint16_t m_u16Count;
297  volatile uint16_t m_u16Free;
298 
299  uint16_t m_u16ElementSize;
300  const void* m_pvBuffer;
301 
304 };
305 } // namespace Mark3
uint16_t m_u16Count
Count of items in the mailbox.
Definition: mailbox.h:296
Semaphore Blocking Object class declarations.
void * GetHeadPointer(void)
GetHeadPointer Return a pointer to the current head of the mailbox&#39;s internal circular buffer...
Definition: mailbox.h:187
bool IsEmpty(void)
Definition: mailbox.h:177
Basic data type primatives used throughout the OS.
void * GetTailPointer(void)
GetTailPointer Return a pointer to the current tail of the mailbox&#39;s internal circular buffer...
Definition: mailbox.h:201
void MoveHeadForward(void)
MoveHeadForward Move the head index forward one element.
Definition: mailbox.h:239
Thread porting interface.
void Init(void *pvBuffer_, uint16_t u16BufferSize_, uint16_t u16ElementSize_)
Init Initialize the mailbox object prior to its use. This must be called before any calls can be made...
Definition: mailbox.cpp:34
uint16_t GetFreeSlots(void)
Definition: mailbox.h:170
#define K_ADDR
Size of an address (pointer size)
Definition: portcfg.h:63
void CopyData(const void *src_, void *dst_, uint16_t len_)
CopyData Perform a direct byte-copy from a source to a destination object.
Definition: mailbox.h:216
the Semaphore class provides Binary & Counting semaphore objects, based on BlockingObject base class...
Definition: ksemaphore.h:36
void MoveHeadBackward(void)
MoveHeadBackward Move the head index backward one element.
Definition: mailbox.h:263
void Receive(void *pvData_)
Receive Read one envelope from the head of the mailbox. If the mailbox is currently empty...
Definition: mailbox.cpp:83
bool Receive_i(void *pvData_, bool bTail_, uint32_t u32WaitTimeMS_)
Receive_i Internal method which implements all Read() methods in the class.
Definition: mailbox.cpp:197
Semaphore m_clSendSem
Binary semaphore for send-blocked threads.
Definition: mailbox.h:303
Definition: atomic.cpp:23
Mark3 Kernel Configuration This file is used to configure the kernel for your specific application in...
bool SendTail(void *pvData_)
SendTail Send an envelope to the mailbox. This safely copies the data contents of the datastructure t...
Definition: mailbox.cpp:118
void MoveTailForward(void)
MoveTailForward Move the tail index forward one element.
Definition: mailbox.h:227
uint16_t m_u16Tail
Current tail index.
Definition: mailbox.h:294
volatile uint16_t m_u16Free
Current number of free slots in the mailbox.
Definition: mailbox.h:297
Semaphore m_clRecvSem
Counting semaphore used to synchronize threads on the object.
Definition: mailbox.h:302
uint16_t m_u16Head
Current head index.
Definition: mailbox.h:293
void MoveTailBackward(void)
MoveTailBackward Move the tail index backward one element.
Definition: mailbox.h:251
The CriticalGuard class. This class provides an implemention of RAII for critical sections...
Definition: criticalguard.h:38
bool IsFull(void)
Definition: mailbox.h:176
The Mailbox class. This class implements an IPC mechnism based on sending/receiving envelopes contain...
Definition: mailbox.h:35
bool Send_i(const void *pvData_, bool bTail_, uint32_t u32TimeoutMS_)
Send_i Internal method which implements all Send() methods in the class.
Definition: mailbox.cpp:139
const void * m_pvBuffer
Pointer to the data-buffer managed by this mailbox.
Definition: mailbox.h:300
bool Send(void *pvData_)
Send Send an envelope to the mailbox. This safely copies the data contents of the datastructure to th...
Definition: mailbox.cpp:111
uint16_t m_u16ElementSize
Size of the objects tracked in this mailbox.
Definition: mailbox.h:299
void ReceiveTail(void *pvData_)
ReceiveTail Read one envelope from the tail of the mailbox. If the mailbox is currently empty...
Definition: mailbox.cpp:97