POSIX compliant message queue, using the default RTOS allocator. More...
#include <cmsis-plus/rtos/os.h>
Classes | |
class | arena |
Storage for a static message queue. More... | |
class | attributes |
Message queue attributes. More... | |
Public Types | |
using | allocator_type = memory::allocator< thread::stack::allocation_element_t > |
Default RTOS allocator. | |
using | index_t = message_queue::size_t |
Type of list index storage. | |
using | msg_size_t = uint16_t |
Type of message size storage. | |
using | priority_t = uint8_t |
Type of message priority storage. | |
using | size_t = uint8_t |
Type of a queue size storage. | |
Public Member Functions | |
template<typename T > | |
constexpr std::size_t | compute_allocated_size_bytes (std::size_t msgs, std::size_t msg_size_bytes) |
Calculator for queue storage requirements. | |
Constructors & Destructor | |
message_queue (std::size_t msgs, std::size_t msg_size_bytes, const attributes &attr=initializer, const allocator_type &allocator=allocator_type()) | |
Construct a message queue object instance. | |
message_queue (const char *name, std::size_t msgs, std::size_t msg_size_bytes, const attributes &attr=initializer, const allocator_type &allocator=allocator_type()) | |
Construct a named message queue object instance. | |
virtual | ~message_queue () |
Destruct the message queue object instance. | |
Operators | |
bool | operator== (const message_queue &rhs) const |
Compare memory queues. | |
Public Member Functions | |
result_t | send (const void *msg, std::size_t nbytes, priority_t mprio=default_priority) |
Send a message to the queue. | |
result_t | try_send (const void *msg, std::size_t nbytes, priority_t mprio=default_priority) |
Try to send a message to the queue. | |
result_t | timed_send (const void *msg, std::size_t nbytes, clock::duration_t timeout, priority_t mprio=default_priority) |
Send a message to the queue with timeout. | |
result_t | receive (void *msg, std::size_t nbytes, priority_t *mprio=nullptr) |
Receive a message from the queue. | |
result_t | try_receive (void *msg, std::size_t nbytes, priority_t *mprio=nullptr) |
Try to receive a message from the queue. | |
result_t | timed_receive (void *msg, std::size_t nbytes, clock::duration_t timeout, priority_t *mprio=nullptr) |
Receive a message from the queue with timeout. | |
std::size_t | capacity (void) const |
Get queue capacity. | |
std::size_t | length (void) const |
Get queue length. | |
std::size_t | msg_size (void) const |
Get message size. | |
bool | empty (void) const |
Check if the queue is empty. | |
bool | full (void) const |
Check if the queue is full. | |
result_t | reset (void) |
Reset the message queue. | |
Public Member Functions | |
const char * | name (void) const |
Get object name. | |
Static Public Member Functions | |
Operators | |
static void * | operator new (std::size_t bytes) |
Allocate space for a new object instance using the RTOS system allocator. | |
static void * | operator new (std::size_t bytes, void *ptr) |
Emplace a new object instance. | |
static void * | operator new[] (std::size_t bytes) |
Allocate space for an array of new object instances using the RTOS system allocator. | |
static void * | operator new[] (std::size_t bytes, void *ptr) |
Emplace an array of new object instances. | |
static void | operator delete (void *ptr, std::size_t bytes) |
Deallocate the dynamically allocated object instance. using the RTOS system allocator. | |
static void | operator delete[] (void *ptr, std::size_t bytes) |
Deallocate the dynamically allocated array of object. instances using the RTOS system allocator. | |
Static Public Attributes | |
static constexpr priority_t | default_priority = 0 |
Default message priority. | |
static const attributes | initializer |
Default message queue initialiser. | |
static constexpr msg_size_t | max_msg_size = 0xFFFF |
Maximum message size. | |
static constexpr priority_t | max_priority = 0xFF |
Maximum message priority. | |
static constexpr message_queue::size_t | max_size = 0xFF |
Maximum queue size. | |
static constexpr index_t | no_index = max_size |
Index value to represent an illegal index. | |
POSIX compliant message queue, using the default RTOS allocator.
POSIX message queues allow threads to exchange data in the form of messages. Messages are transferred to and from a queue using send() and receive(). Each message has an associated priority, and messages are always delivered to the receiving process highest priority first.
The storage for the message queue is allocated dynamically, using the RTOS specific allocator (os::memory::allocator
).
For special cases, the storage can be allocated outside the class and specified via the mq_queue_address
and mq_queue_size_bytes
attributes.
message_queue is a representative instance of the message_queue_allocated template; it is also used by the C API.
mqd_t
from <mqueue.h> (IEEE Std 1003.1, 2013 Edition). Definition at line 67 of file os-mqueue.h.
os::rtos::message_queue::message_queue | ( | std::size_t | msgs, |
std::size_t | msg_size_bytes, | ||
const attributes & | attr = initializer , |
||
const allocator_type & | allocator = allocator_type () |
||
) |
Construct a message queue object instance.
[in] | msgs | The number of messages. |
[in] | msg_size_bytes | The message size, in bytes. |
[in] | attr | Reference to attributes. |
[in] | allocator | Reference to allocator. Default a local temporary instance. |
This constructor shall initialise a message queue object with attributes referenced by attr. If the attributes specified by attr are modified later, the memory pool attributes shall not be affected. Upon successful initialisation, the state of the message queue object shall become initialised.
Only the message queue itself may be used for performing synchronisation. It is not allowed to make copies of message queue objects.
In cases where default message queue attributes are appropriate, the variable message_queue::initializer
can be used to initialise message queue. The effect shall be equivalent to creating a message queue object with the simple constructor.
If the attributes define a storage area (via mq_queue_address
and mq_queue_size_bytes
), that storage is used, otherwise the storage is dynamically allocated using the RTOS specific allocator (rtos::memory::allocator
).
If the attr attributes are modified after the message_queue creation, the message_queue attributes shall not be affected.
Definition at line 383 of file os-mqueue.cpp.
os::rtos::message_queue::message_queue | ( | const char * | name, |
std::size_t | msgs, | ||
std::size_t | msg_size_bytes, | ||
const attributes & | attr = initializer , |
||
const allocator_type & | allocator = allocator_type () |
||
) |
Construct a named message queue object instance.
[in] | name | Pointer to name. |
[in] | msgs | The number of messages. |
[in] | msg_size_bytes | The message size, in bytes. |
[in] | attr | Reference to attributes. |
[in] | allocator | Reference to allocator. Default a local temporary instance. |
This constructor shall initialise a named message queue object with attributes referenced by attr. If the attributes specified by attr are modified later, the memory pool attributes shall not be affected. Upon successful initialisation, the state of the message queue object shall become initialised.
Only the message queue itself may be used for performing synchronisation. It is not allowed to make copies of message queue objects.
In cases where default message queue attributes are appropriate, the variable message_queue::initializer
can be used to initialise message queue. The effect shall be equivalent to creating a message queue object with the simple constructor.
If the attributes define a storage area (via mq_queue_address
and mq_queue_size_bytes
), that storage is used, otherwise the storage is dynamically allocated using the RTOS specific allocator (rtos::memory::allocator
).
If the attr attributes are modified after the message_queue creation, the message_queue attributes shall not be affected.
Definition at line 420 of file os-mqueue.cpp.
|
virtual |
Destruct the message queue object instance.
This destructor shall destroy the message queue object; the object becomes, in effect, uninitialised. An implementation may cause the destructor to set the object to an invalid value.
It shall be safe to destroy an initialised message queue object upon which no threads are currently blocked. Attempting to destroy a message queue object upon which other threads are currently blocked results in undefined behaviour.
If the storage for the message queue was dynamically allocated, it is deallocated using the same allocator.
Definition at line 476 of file os-mqueue.cpp.
|
inline |
Get queue capacity.
Definition at line 1312 of file os-mqueue.h.
|
inlineconstexpr |
Calculator for queue storage requirements.
msgs | Number of messages. |
msg_size_bytes | Size of message. |
Definition at line 250 of file os-mqueue.h.
|
inline |
Check if the queue is empty.
true | The queue has no messages. |
false | The queue has some messages. |
Definition at line 1336 of file os-mqueue.h.
|
inline |
Check if the queue is full.
true | The queue is full. |
false | The queue is not full. |
Definition at line 1348 of file os-mqueue.h.
|
inline |
Get queue length.
Definition at line 1300 of file os-mqueue.h.
|
inline |
Get message size.
Definition at line 1324 of file os-mqueue.h.
|
inlineinherited |
Get object name.
All objects return a non-null string; anonymous objects return "-"
.
Definition at line 759 of file os-decls.h.
|
inlinestaticinherited |
Deallocate the dynamically allocated object instance. using the RTOS system allocator.
ptr | Pointer to object. |
bytes | Number of bytes to deallocate. |
The deallocation function (3.7.4.2) called by a delete-expression to render the value of ptr invalid.
ptr shall be a null pointer or its value shall be a value returned by an earlier call to the (possibly replaced) operator new() which has not been invalidated by an intervening call to operator delete(void*).
If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.
The storage is deallocated using the RTOS system allocator.
Definition at line 122 of file os-inlines.h.
|
inlinestaticinherited |
Deallocate the dynamically allocated array of object. instances using the RTOS system allocator.
ptr | Pointer to array of objects. |
bytes | Number of bytes to deallocate. |
The deallocation function (3.7.4.2) called by the array form of a delete-expression to render the value of ptr invalid.
If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.
The storage is deallocated using the RTOS system allocator.
Definition at line 143 of file os-inlines.h.
|
inlinestaticinherited |
Allocate space for a new object instance using the RTOS system allocator.
bytes | Number of bytes to allocate. |
The allocation function (3.7.4.1) called by a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).
The storage is allocated using the RTOS system allocator.
Definition at line 44 of file os-inlines.h.
|
inlinestaticinherited |
Emplace a new object instance.
bytes | Number of bytes to emplace. |
ptr | Pointer to location to emplace the object. |
The allocation function (3.7.4.1) called by a placement new-expression to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).
The storage is allocated using the RTOS system allocator.
Definition at line 81 of file os-inlines.h.
|
inlinestaticinherited |
Allocate space for an array of new object instances using the RTOS system allocator.
bytes | Number of bytes to allocate. |
The allocation function (3.7.4.1) called by the array form of a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.
The storage is allocated using the RTOS system allocator.
Definition at line 62 of file os-inlines.h.
|
inlinestaticinherited |
Emplace an array of new object instances.
bytes | Number of bytes to emplace. |
ptr | Pointer to location to emplace the object. |
The allocation function (3.7.4.1) called by the array form of a placement new-expression to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.
The storage is allocated using the RTOS system allocator.
Definition at line 98 of file os-inlines.h.
|
inline |
Compare memory queues.
true | The given memory queue is the same as this memory queue. |
false | The memory queues are different. |
Identical message queues should have the same memory address.
Definition at line 1288 of file os-mqueue.h.
result_t os::rtos::message_queue::receive | ( | void * | msg, |
std::size_t | nbytes, | ||
priority_t * | mprio = nullptr |
||
) |
Receive a message from the queue.
[out] | msg | The address where to store the dequeued message. |
[in] | nbytes | The size of the destination buffer. Must be lower than the value used when creating the queue. |
[out] | mprio | The address where to store the message priority. The default is nullptr . |
result::ok | The message was received. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, is greater than the message size attribute of the message queue. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
ENOTRECOVERABLE | The message could not be dequeued (extension to POSIX). |
EBADMSG | The implementation has detected a data corruption problem with the message. |
EINTR | The operation was interrupted. |
The receive()
function shall receive the oldest of the highest priority message(s) from the message queue. If the size of the buffer in bytes, specified by the nbytes argument, is less than the msg_size_bytes attribute of the message queue, the function shall fail and return an error. Otherwise, the selected message shall be removed from the queue and copied to the buffer pointed to by the msg argument.
If the value of nbytes is greater than message_queue::max_size
, the result is implementation-defined.
If the argument mprio is not nullptr, the priority of the selected message shall be stored in the location referenced by mprio.
If the message queue is empty, receive()
shall block until a message is enqueued on the message queue or until receive()
is cancelled/interrupted. If more than one thread is waiting to receive a message when a message arrives at an empty queue and the Priority Scheduling option is supported, then the thread of highest priority that has been waiting the longest shall be selected to receive the message. Otherwise, it is unspecified which waiting thread receives the message.
mq_receive()
with O_NONBLOCK
not set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition).Definition at line 1211 of file os-mqueue.cpp.
result_t os::rtos::message_queue::reset | ( | void | ) |
Reset the message queue.
result::ok | The queue was reset. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
Clear both send and receive counter and return the queue to the initial state.
Definition at line 1534 of file os-mqueue.cpp.
result_t os::rtos::message_queue::send | ( | const void * | msg, |
std::size_t | nbytes, | ||
priority_t | mprio = default_priority |
||
) |
Send a message to the queue.
[in] | msg | The address of the message to enqueue. |
[in] | nbytes | The length of the message. Must be not higher than the value used when creating the queue. |
[in] | mprio | The message priority. The default is 0. |
result::ok | The message was enqueued. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, exceeds the message size attribute of the message queue. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
ENOTRECOVERABLE | The message could not be enqueue (extension to POSIX). |
EINTR | The operation was interrupted. |
The send()
function shall add the message pointed to by the argument msg to the message queue. The nbytes argument specifies the length of the message, in bytes, pointed to by msg. The value of nbytes shall be less than or equal to the msg_size_bytes parameter of the message queue object, or send()
shall fail.
If the specified message queue is not full, send()
shall behave as if the message is inserted into the message queue at the position indicated by the mprio argument. A message with a larger numeric value of mprio shall be inserted before messages with lower values of mprio. A message shall be inserted after other messages in the queue, if any, with equal mprio. The value of mprio shall be less than message_queue::max_priority
.
If the specified message queue is full, send()
shall block until space becomes available to enqueue the message, or until send()
is cancelled/interrupted. If more than one thread is waiting to send when space becomes available in the message queue and the Priority Scheduling option is supported, then the thread of the highest priority that has been waiting the longest shall be unblocked to send its message. Otherwise, it is unspecified which waiting thread is unblocked.
mq_send()
with O_NONBLOCK
not set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition).Definition at line 879 of file os-mqueue.cpp.
result_t os::rtos::message_queue::timed_receive | ( | void * | msg, |
std::size_t | nbytes, | ||
clock::duration_t | timeout, | ||
priority_t * | mprio = nullptr |
||
) |
Receive a message from the queue with timeout.
[out] | msg | The address where to store the dequeued message. |
[in] | nbytes | The size of the destination buffer. Must be lower than the value used when creating the queue. |
[in] | timeout | The timeout duration. |
[out] | mprio | The address where to store the message priority. The default is nullptr . |
result::ok | The message was received. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, is greater than the message size attribute of the message queue. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
ENOTRECOVERABLE | The message could not be dequeued (extension to POSIX). |
EBADMSG | The implementation has detected a data corruption problem with the message. |
EINTR | The operation was interrupted. |
ETIMEDOUT | No message arrived on the queue before the specified timeout expired. |
The timed_receive()
function shall receive the oldest of the highest priority message(s) from the message queue. If the size of the buffer in bytes, specified by the nbytes argument, is less than the msg_size_bytes attribute of the message queue, the function shall fail and return an error. Otherwise, the selected message shall be removed from the queue and copied to the buffer pointed to by the msg argument.
If the value of nbytes is greater than message_queue::max_size
, the result is implementation-defined.
If the argument mprio is not nullptr, the priority of the selected message shall be stored in the location referenced by mprio.
If the message queue is empty, timed_receive()
shall block until a message is enqueued on the message queue or until timed_receive()
is cancelled/interrupted. If more than one thread is waiting to receive a message when a message arrives at an empty queue and the Priority Scheduling option is supported, then the thread of highest priority that has been waiting the longest shall be selected to receive the message. Otherwise, it is unspecified which waiting thread receives the message.
The timed_receive()
function shall receive the oldest of the highest priority messages from the message queue as described for the receive()
function. However, if no message exists on the queue to satisfy the receive, the wait for such a message shall be terminated when the specified timeout expires.
The timeout shall expire after the number of time units (that is when the value of that clock equals or exceeds (now()+duration). The resolution of the timeout shall be the resolution of the clock on which it is based.
Under no circumstance shall the operation fail with a timeout if a message can be removed from the message queue immediately.
The clock used for timeouts can be specified via the clock
attribute. By default, the clock derived from the scheduler timer is used, and the durations are expressed in ticks.
Compatible with POSIX mq_receive()
with O_NONBLOCK set. http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_receive.html#
mq_timedreceive()
with O_NONBLOCK
not set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition). Definition at line 1421 of file os-mqueue.cpp.
result_t os::rtos::message_queue::timed_send | ( | const void * | msg, |
std::size_t | nbytes, | ||
clock::duration_t | timeout, | ||
priority_t | mprio = default_priority |
||
) |
Send a message to the queue with timeout.
[in] | msg | The address of the message to enqueue. |
[in] | nbytes | The length of the message. Must be not higher than the value used when creating the queue. |
[in] | timeout | The timeout duration. |
[in] | mprio | The message priority. The default is 0. |
result::ok | The message was enqueued. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, exceeds the message size attribute of the message queue. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
ETIMEDOUT | The timeout expired before the message could be added to the queue. |
ENOTRECOVERABLE | The message could not be enqueue (extension to POSIX). |
EINTR | The operation was interrupted. |
The timed_send()
function shall add the message pointed to by the argument msg to the message queue. The nbytes argument specifies the length of the message, in bytes, pointed to by msg. The value of nbytes shall be less than or equal to the msg_size_bytes attribute of the message queue object, or timed_send()
shall fail.
If the message queue is not full, timed_send()
shall behave as if the message is inserted into the message queue at the position indicated by the mprio argument. A message with a larger numeric value of mprio shall be inserted before messages with lower values of mprio. A message shall be inserted after other messages in the queue, if any, with equal mprio. The value of mprio shall be less than message_queue::max_priority
.
If the message queue is full, the wait for sufficient room in the queue shall be terminated when the specified timeout expires.
The timeout shall expire after the number of time units (that is when the value of that clock equals or exceeds (now()+timeout). The resolution of the timeout shall be the resolution of the clock on which it is based.
Under no circumstance shall the operation fail with a timeout if there is sufficient room in the queue to add the message immediately.
The clock used for timeouts can be specified via the clock
attribute. By default, the clock derived from the scheduler timer is used, and the durations are expressed in ticks.
mq_timedsend()
with O_NONBLOCK
not set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition). Definition at line 1074 of file os-mqueue.cpp.
result_t os::rtos::message_queue::try_receive | ( | void * | msg, |
std::size_t | nbytes, | ||
priority_t * | mprio = nullptr |
||
) |
Try to receive a message from the queue.
[out] | msg | The address where to store the dequeued message. |
[in] | nbytes | The size of the destination buffer. Must be lower than the value used when creating the queue. |
[out] | mprio | The address where to store the message priority. The default is nullptr . |
result::ok | The message was received. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, is greater than the message size attribute of the message queue. |
ENOTRECOVERABLE | The message could not be dequeued (extension to POSIX). |
EBADMSG | The implementation has detected a data corruption problem with the message. |
EWOULDBLOCK | The specified message queue is empty. |
The try_receive()
function shall try to receive the oldest of the highest priority message(s) from the message queue. If the size of the buffer in bytes, specified by the nbytes argument, is less than the msg_size_bytes attribute of the message queue, the function shall fail and return an error. Otherwise, the selected message shall be removed from the queue and copied to the buffer pointed to by the msg argument.
If the value of nbytes is greater than message_queue::max_size
, the result is implementation-defined.
If the argument mprio is not nullptr, the priority of the selected message shall be stored in the location referenced by mprio.
If the message queue is empty, no message shall be removed from the queue, and try_receive()
shall return an error.
mq_receive()
with O_NONBLOCK
set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition). Definition at line 1323 of file os-mqueue.cpp.
result_t os::rtos::message_queue::try_send | ( | const void * | msg, |
std::size_t | nbytes, | ||
priority_t | mprio = default_priority |
||
) |
Try to send a message to the queue.
[in] | msg | The address of the message to enqueue. |
[in] | nbytes | The length of the message. Must be not higher than the value used when creating the queue. |
[in] | mprio | The message priority. The default is 0. |
result::ok | The message was enqueued. |
EWOULDBLOCK | The specified message queue is full. |
EINVAL | A parameter is invalid or outside of a permitted range. |
EMSGSIZE | The specified message length, nbytes, exceeds the message size attribute of the message queue. |
ENOTRECOVERABLE | The message could not be enqueue (extension to POSIX). |
The try_send()
function shall try to add the message pointed to by the argument msg to the message queue. The nbytes argument specifies the length of the message, in bytes, pointed to by msg. The value of nbytes shall be less than or equal to the msg_size_bytes parameter of the message queue object, or try_send()
shall fail.
If the message queue is not full, try_send()
shall behave as if the message is inserted into the message queue at the position indicated by the mprio argument. A message with a larger numeric value of mprio shall be inserted before messages with lower values of mprio. A message shall be inserted after other messages in the queue, if any, with equal mprio. The value of mprio shall be less than message_queue::max_priority
.
If the message queue is full, the message shall not be queued and try_send()
shall return an error.
mq_send()
with O_NONBLOCK
set, from <mqueue.h> (IEEE Std 1003.1, 2013 Edition). Definition at line 990 of file os-mqueue.cpp.