RTOS Wrapper
API reference
Loading...
Searching...
No Matches
OsQueue.h
Go to the documentation of this file.
1
21#ifndef OS_QUEUE_H_
22#define OS_QUEUE_H_
23
24#include <cstdint>
25#include <cstddef>
26#include <climits>
27#include <cstring>
28#include <type_traits>
29#include "OsAbstraction.h"
30#include "OsUtility.h"
31
40template <typename MessageType, size_t kCapacity>
41class OsQueue {
42 static_assert(std::is_trivially_copyable_v<MessageType>,
43 "OsQueue<T, N> requires a trivially copyable T");
44 static_assert(kCapacity > 0, "OsQueue<T, N> needs a capacity of at least 1");
45
46public:
50 explicit OsQueue(const char* queueName) noexcept
51 : name_(queueName)
52 {
54 storage_, static_cast<OS_Ulong>(kCapacity));
55 }
56
57 OsQueue(const OsQueue&) = delete;
58 OsQueue& operator=(const OsQueue&) = delete;
59
60 ~OsQueue() noexcept
61 {
62 if (created_) {
64 }
65 }
66
68 [[nodiscard]] bool IsValid() const noexcept { return created_; }
69
74 bool Send(const MessageType& message, uint32_t timeout_ms = UINT32_MAX) noexcept
75 {
76 if (!created_) return false;
77 uint32_t item[kItemWords] = {};
78 std::memcpy(item, &message, sizeof(MessageType));
79 return os_queue_send_ex(queue_, item, ToTicks(timeout_ms));
80 }
81
86 bool Receive(MessageType& out, uint32_t timeout_ms = UINT32_MAX) noexcept
87 {
88 if (!created_) return false;
89 uint32_t item[kItemWords] = {};
90 if (!os_queue_receive_ex(queue_, item, ToTicks(timeout_ms))) return false;
91 std::memcpy(&out, item, sizeof(MessageType));
92 return true;
93 }
94
98 [[nodiscard]] static constexpr size_t Capacity() noexcept { return kCapacity; }
99
100private:
102 static constexpr size_t kItemWords = (sizeof(MessageType) + sizeof(uint32_t) - 1U) / sizeof(uint32_t);
104 static constexpr size_t kStoreBytes =
105 (OS_QUEUE_STORE_BYTES(kItemWords, kCapacity) > 0U) ? OS_QUEUE_STORE_BYTES(kItemWords, kCapacity) : 1U;
106
107 static OS_Ulong ToTicks(uint32_t timeout_ms) noexcept
108 {
109 return (timeout_ms == UINT32_MAX)
110 ? static_cast<OS_Ulong>(OS_WAIT_FOREVER)
111 : static_cast<OS_Ulong>(os_convert_msec_to_delay_ticks(timeout_ms));
112 }
113
114 const char* name_;
115 bool created_{false};
117 alignas(std::max_align_t) uint8_t storage_[kStoreBytes];
118};
119
120#endif /* OS_QUEUE_H_ */
121
Generic RTOS abstraction — public header.
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
constexpr uint32_t os_convert_msec_to_delay_ticks(uint32_t milliseconds)
Converts milliseconds to delay ticks, rounding down but never to zero.
Definition OsUtility.h:74
bool os_queue_create_ex(OS_Queue &queue, const char *queueName, OS_Uint messageSizeInWords, void *queueStorage, OS_Ulong queueSize, bool suppressVerbose=true) noexcept
Creates a queue with the specified parameters.
Definition OsUtility.cpp:513
bool os_queue_delete_ex(OS_Queue &queue, bool suppressVerbose=true) noexcept
Deletes the specified queue and logs any error.
Definition OsUtility.cpp:540
bool os_queue_receive_ex(OS_Queue &queue, void *message, OS_Ulong wait_option=OS_WAIT_FOREVER, bool suppressVerbose=true) noexcept
Receive a message from the queue.
Definition OsUtility.cpp:580
bool os_queue_send_ex(OS_Queue &queue, void *message, OS_Ulong wait_option=OS_WAIT_FOREVER, bool suppressVerbose=true) noexcept
Send a message to the queue.
Definition OsUtility.cpp:564
Fixed-capacity, eager-init MPMC queue of trivially-copyable items.
Definition OsQueue.h:41
static OS_Ulong ToTicks(uint32_t timeout_ms) noexcept
Definition OsQueue.h:107
~OsQueue() noexcept
Definition OsQueue.h:60
bool created_
Definition OsQueue.h:115
OsQueue(const char *queueName) noexcept
Construct and create the underlying RTOS queue eagerly.
Definition OsQueue.h:50
static constexpr size_t Capacity() noexcept
Compile-time capacity in elements.
Definition OsQueue.h:98
OsQueue & operator=(const OsQueue &)=delete
bool Receive(MessageType &out, uint32_t timeout_ms=UINT32_MAX) noexcept
Receive a message; blocks up to timeout_ms.
Definition OsQueue.h:86
static constexpr size_t kItemWords
Item size in 32-bit words, as the kernel queue stores it.
Definition OsQueue.h:102
bool Send(const MessageType &message, uint32_t timeout_ms=UINT32_MAX) noexcept
Send a message; blocks up to timeout_ms.
Definition OsQueue.h:74
uint8_t storage_[kStoreBytes]
Definition OsQueue.h:117
OS_Queue queue_
Definition OsQueue.h:116
OsQueue(const OsQueue &)=delete
const char * name_
Definition OsQueue.h:114
bool IsValid() const noexcept
True if the underlying RTOS queue was created successfully.
Definition OsQueue.h:68
static constexpr size_t kStoreBytes
Backing storage the backend asks for (at least one byte, so the array is never empty).
Definition OsQueue.h:104
@ OS_WAIT_FOREVER
Definition os_cmsis.h:48
unsigned int OS_Uint
Definition os_cmsis.h:36
osMessageQueueId_t OS_Queue
Definition os_cmsis.h:30
#define OS_QUEUE_STORE_BYTES(item_words, length)
Definition os_cmsis.h:284
unsigned long OS_Ulong
Definition os_cmsis.h:35