1#ifndef OS_WRAPPER_MESSAGE_QUEUE_H_INCLUDED
2#define OS_WRAPPER_MESSAGE_QUEUE_H_INCLUDED
31 const std::size_t alignedMQSize =
32 (
sizeof(
MessageQueue) + (
sizeof(
double) - 1U)) & ~(
sizeof(double) - 1U);
33 const std::size_t rbBufSize = maxSize + 1U;
34 const std::size_t poolBufSize = alignedMQSize + (
sizeof(T) * rbBufSize);
49 reinterpret_cast<T*
>(
static_cast<unsigned char*
>(p) + alignedMQSize), rbBufSize);
50 if (!m->createOSObjects()) {
139#ifndef CPPELIB_NO_EXCEPTIONS
143#ifndef CPPELIB_NO_EXCEPTIONS
220#ifndef CPPELIB_NO_EXCEPTIONS
224#ifndef CPPELIB_NO_EXCEPTIONS
243 return m_rb.getSize();
253 return m_rb.getMaxSize();
261 std::size_t m_bufSize;
264 std::size_t next_idx(std::size_t idx)
const
266 if (idx + 1U < m_bufSize) {
269 return idx + 1U - m_bufSize;
272 void construct(T* p,
const T& val) {
new(p) T(val); }
273 void destroy(T* p) { p->~T(); }
275 RingBuf(
const RingBuf&);
276 RingBuf& operator=(
const RingBuf&);
278 RingBuf(T* buf, std::size_t bufSize) : m_begin(0U), m_end(0U), m_bufSize(bufSize), m_buf(buf) {}
282 std::size_t getSize()
const
284 if (m_begin <= m_end) {
285 return m_end - m_begin;
287 return m_bufSize - m_begin + m_end;
290 std::size_t getMaxSize()
const
292 return m_bufSize - 1U;
297 return m_begin == m_end;
302 return getSize() == getMaxSize();
305 void push(
const T& data)
307 construct(&m_buf[m_end], data);
308 m_end = next_idx(m_end);
314 *data = m_buf[m_begin];
316 destroy(&m_buf[m_begin]);
317 m_begin = next_idx(m_begin);
323 FixedMemoryPool* m_pool;
332 MessageQueue(FixedMemoryPool* pool, T* rbBuffer, std::size_t rbBufSize)
333 : m_rb(rbBuffer, rbBufSize), m_pool(pool), m_mtxRB(0), m_mtxSend(0), m_mtxRecv(0), m_event(0)
345 bool createOSObjects()
352 if (m_mtxSend == 0) {
356 if (m_mtxRecv == 0) {
368 LockGuard lock(m_mtxRB);
369 const bool is_empty = m_rb.isEmpty();
371 m_event->reset(EV_NOT_EMPTY);
378 LockGuard lock(m_mtxRB);
379 const bool is_full = m_rb.isFull();
381 m_event->reset(EV_NOT_FULL);
386 void push(
const T& msg)
388 LockGuard lock(m_mtxRB);
390 m_event->set(EV_NOT_EMPTY);
395 LockGuard lock(m_mtxRB);
397 m_event->set(EV_NOT_FULL);
400 MessageQueue(
const MessageQueue&);
401 MessageQueue& operator=(
const MessageQueue&);
Class used when CHECK_ASSERT() macro fails.
Definition Assertion.h:74
Container::BitPattern< unsigned int > Pattern
Type for bit pattern of EventFlag.
Definition EventFlag.h:40
static EventFlag * create(bool autoReset)
Create an EventFlag object.
Definition EventFlag.cpp:14
@ OR
Definition EventFlag.h:33
static void destroy(EventFlag *e)
Destroy an EventFlag object.
Definition EventFlag.cpp:20
virtual Error timedWait(Pattern bitPattern, Mode waitMode, Pattern *releasedPattern, Timeout tmout)=0
Block the current thread until the condition is satisfied but only within the limited time.
Abstract class that has functions of common RTOS's fixed-size memory pool.
Definition FixedMemoryPool.h:23
static FixedMemoryPool * create(std::size_t blockSize, std::size_t memoryPoolSize, void *memoryPoolAddress=0)
Create a FixedMemoryPool object.
Definition FixedMemoryPool.cpp:14
virtual void * allocate()=0
Allocate a block from this FixedMemoryPool.
static std::size_t getRequiredMemorySize(std::size_t blockSize, std::size_t numBlocks)
Get the required total memory size for allocation of (blockSize * numBlocks)
Definition FixedMemoryPool.cpp:27
static void destroy(FixedMemoryPool *p)
Destroy a FixedMemoryPool object.
Definition FixedMemoryPool.cpp:20
virtual void deallocate(void *p)=0
Release the allocated block.
RAII wrapper of Mutex.
Definition Mutex.h:99
static const AdoptLock ADOPT_LOCK
Used as argument of LockGuard's constructor.
Definition Mutex.h:102
Class template of message queue.
Definition MessageQueue.h:22
std::size_t getSize() const
Get the queue size.
Definition MessageQueue.h:240
Error send(const T &msg)
Send the message.
Definition MessageQueue.h:86
Error trySend(const T &msg)
Send the message without blocking.
Definition MessageQueue.h:103
Error timedReceive(T *msg, Timeout tmout)
Receive the message within the limited time.
Definition MessageQueue.h:204
static MessageQueue * create(std::size_t maxSize)
Create a MessageQueue object.
Definition MessageQueue.h:29
std::size_t getMaxSize() const
Get the max queue size.
Definition MessageQueue.h:250
Error timedSend(const T &msg, Timeout tmout)
Send the message within the limited time.
Definition MessageQueue.h:123
Error receive(T *msg)
Receive the message.
Definition MessageQueue.h:167
static void destroy(MessageQueue *m)
Destroy a MessageQueue object.
Definition MessageQueue.h:63
Error tryReceive(T *msg)
Receive the message without blocking.
Definition MessageQueue.h:184
static Mutex * create()
Create a Mutex object.
Definition Mutex.cpp:14
virtual Error timedLock(Timeout tmout)=0
Block the current thread until locks this mutex but only within the limited time.
static void destroy(Mutex *m)
Destroy a Mutex object.
Definition Mutex.cpp:26
Value object for the timeout.
Definition Timeout.h:11
static const Timeout FOREVER
Constant value object for waiting forever until condition satisfied.
Definition Timeout.h:42
static const Timeout POLLING
Constant value object for non-blocking.
Definition Timeout.h:37
OSWrapper provides abstract C++ interface of common RTOS: thread, mutex, event flag,...
Definition EventFlag.cpp:5
Error
Kinds of errors of the OS objects.
Definition OSWrapperError.h:9
@ OK
Definition OSWrapperError.h:10
@ OtherError
Definition OSWrapperError.h:15