FreeRTOS: POSIX
Return to main page ↑
FreeRTOS_POSIX_mqueue.c File Reference

Implementation of message queue functions in mqueue.h. More...

#include <string.h>
#include "FreeRTOS_POSIX.h"
#include "FreeRTOS_POSIX/errno.h"
#include "FreeRTOS_POSIX/fcntl.h"
#include "FreeRTOS_POSIX/mqueue.h"
#include "FreeRTOS_POSIX/utils.h"

Data Structures

struct  QueueElement_t
 Element of the FreeRTOS queues that store mq data. More...
 
struct  QueueListElement_t
 Data structure of an mq. More...
 

Functions

static int prvCalculateTickTimeout (long lMessageQueueFlags, const struct timespec *const pxAbsoluteTimeout, TickType_t *pxTimeoutTicks)
 Convert an absolute timespec into a tick timeout, taking into account queue flags. More...
 
static BaseType_t prvCreateNewMessageQueue (QueueListElement_t **ppxMessageQueue, const struct mq_attr *const pxAttr, const char *const pcName, size_t xNameLength)
 Add a new queue to the queue list. More...
 
static void prvDeleteMessageQueue (const QueueListElement_t *const pxMessageQueue)
 Free all the resources used by a message queue. More...
 
static BaseType_t prvFindQueueInList (QueueListElement_t **const ppxQueueListElement, const char *const pcName, mqd_t xMessageQueueDescriptor)
 Attempt to find the queue identified by pcName or xMqId in the queue list. More...
 
static void prvInitializeQueueList (void)
 Initialize the queue list. More...
 
static BaseType_t prvValidateQueueName (const char *const pcName, size_t *pxNameLength)
 Checks that pcName is a valid name for a message queue. More...
 
int mq_close (mqd_t mqdes)
 Close a message queue. More...
 
int mq_getattr (mqd_t mqdes, struct mq_attr *mqstat)
 Get message queue attributes. More...
 
mqd_t mq_open (const char *name, int oflag, mode_t mode, struct mq_attr *attr)
 Open a message queue. More...
 
ssize_t mq_receive (mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio)
 Receive a message from a message queue. More...
 
int mq_send (mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
 Send a message to a message queue. More...
 
ssize_t mq_timedreceive (mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio, const struct timespec *abstime)
 Receive a message from a message queue with timeout. More...
 
int mq_timedsend (mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned int msg_prio, const struct timespec *abstime)
 
int mq_unlink (const char *name)
 Remove a message queue. More...
 

Variables

static StaticSemaphore_t xQueueListMutex = { { 0 }, .u = { 0 } }
 Guards access to the list of message queues.
 
static Link_t xQueueListHead = { 0 }
 Head of the linked list of queues.
 

Detailed Description

Implementation of message queue functions in mqueue.h.

Function Documentation

◆ prvCalculateTickTimeout()

static int prvCalculateTickTimeout ( long  lMessageQueueFlags,
const struct timespec *const  pxAbsoluteTimeout,
TickType_t *  pxTimeoutTicks 
)
static

Convert an absolute timespec into a tick timeout, taking into account queue flags.

Parameters
[in]lMessageQueueFlagsMessage queue flags to consider.
[in]pxAbsoluteTimeoutThe absolute timespec to convert.
[out]pxTimeoutTicksOutput parameter of the timeout in ticks.
Returns
0 if successful; EINVAL if pxAbsoluteTimeout is invalid, or ETIMEDOUT if pxAbsoluteTimeout is in the past.

◆ prvCreateNewMessageQueue()

static BaseType_t prvCreateNewMessageQueue ( QueueListElement_t **  ppxMessageQueue,
const struct mq_attr *const  pxAttr,
const char *const  pcName,
size_t  xNameLength 
)
static

Add a new queue to the queue list.

Parameters
[out]ppxMessageQueuePointer to new queue.
[in]pxAttrmq_attr of the new queue.
[in]pcNameName of new queue.
[in]xNameLengthLength of pcName.
Returns
pdTRUE if the queue is found; pdFALSE otherwise.

◆ prvDeleteMessageQueue()

static void prvDeleteMessageQueue ( const QueueListElement_t *const  pxMessageQueue)
static

Free all the resources used by a message queue.

Parameters
[out]pxMessageQueuePointer to queue to free.
Returns
nothing

◆ prvFindQueueInList()

static BaseType_t prvFindQueueInList ( QueueListElement_t **const  ppxQueueListElement,
const char *const  pcName,
mqd_t  xMessageQueueDescriptor 
)
static

Attempt to find the queue identified by pcName or xMqId in the queue list.

Matches queues by pcName first; if pcName is NULL, matches by xMqId.

Parameters
[out]ppxQueueListElementOutput parameter set when queue is found.
[in]pcNameA queue name to match.
[in]xMessageQueueDescriptorA queue descriptor to match.
Returns
pdTRUE if the queue is found; pdFALSE otherwise.

◆ prvInitializeQueueList()

static void prvInitializeQueueList ( void  )
static

Initialize the queue list.

Performs initialization of the queue list mutex and queue list head.

Returns
nothing

◆ prvValidateQueueName()

static BaseType_t prvValidateQueueName ( const char *const  pcName,
size_t *  pxNameLength 
)
static

Checks that pcName is a valid name for a message queue.

Also outputs the length of pcName.

Parameters
[in]pcNameThe name to check.
[out]pxNameLengthOutput parameter for name length.
Returns
pdTRUE if the name is valid; pdFALSE otherwise.

◆ mq_close()

int mq_close ( mqd_t  mqdes)

Close a message queue.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_close.html

Return values
0- Upon successful completion
-1- A error occurred. errno is also set.
Side Effects Possible errno values

EBADF - The mqdes argument is not a valid message queue descriptor.

◆ mq_getattr()

int mq_getattr ( mqd_t  mqdes,
struct mq_attr mqstat 
)

Get message queue attributes.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_getattr.html

Return values
0- Upon successful completion
-1- A error occurred. errno is also set.
Side Effects Possible errno values

DBADF - The mqdes argument is not a valid message queue descriptor.

◆ mq_open()

mqd_t mq_open ( const char *  name,
int  oflag,
mode_t  mode,
struct mq_attr attr 
)

Open a message queue.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html

Note
Supported name pattern: leading <slash> character in name is always required; the maximum length (excluding null-terminator) of the name argument can be NAME_MAX. The default value of NAME_MAX in FreeRTOS_POSIX_portable_default.h is 64, which can be overwritten by user.
mode argument is not supported.
Supported oflags: O_RDWR, O_CREAT, O_EXCL, and O_NONBLOCK.
Return values
Messagequeue descriptor – Upon successful completion
(mqd_t)- 1 – An error occurred. errno is also set.
Side Effects Possible errno values

EINVAL - name argument is invalid (not following name pattern), OR if O_CREAT is specified in oflag with attr argument not NULL and either mq_maxmsg or mq_msgsize is equal to or less than zero, OR either O_CREAT or O_EXCL is not set and a queue with the same name is unlinked but pending to be removed.
EEXIST - O_CREAT and O_EXCL are set and the named message queue already exists.
ENOSPC - There is insufficient space for the creation of the new message queue.
ENOENT - O_CREAT is not set and the named message queue does not exist.

◆ mq_receive()

ssize_t mq_receive ( mqd_t  mqdes,
char *  msg_ptr,
size_t  msg_len,
unsigned int *  msg_prio 
)

Receive a message from a message queue.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_receive.html

Note
msg_prio argument is not supported. Messages are not checked for corruption.
Return values
Thelength of the selected message in bytes - Upon successful completion. The message is removed from the queue
-1- An error occurred. errno is also set.
Side Effects Possible errno values

EBADF - The mqdes argument is not a valid message queue descriptor open for reading.
EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue.
ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, but no message arrived on the queue before the specified timeout expired.
EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty.

◆ mq_send()

int mq_send ( mqd_t  mqdes,
const char *  msg_ptr,
size_t  msg_len,
unsigned  msg_prio 
)

Send a message to a message queue.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_send.html

Note
msg_prio argument is not supported.
Return values
0- Upon successful completion.
-1- An error occurred. errno is also set.
Side Effects Possible errno values

EBADF - The mqdes argument is not a valid message queue descriptor open for writing.
EMSGSIZE - The specified message length, msg_len, exceeds the message size attribute of the message queue, OR insufficient memory for the message to be sent.
ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, but the timeout expired before the message could be added to the queue.
EAGAIN - The O_NONBLOCK flag is set in the message queue description associated with mqdes, and the specified message queue is full.

◆ mq_timedreceive()

ssize_t mq_timedreceive ( mqd_t  mqdes,
char *  msg_ptr,
size_t  msg_len,
unsigned *  msg_prio,
const struct timespec abstime 
)

Receive a message from a message queue with timeout.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_timedreceive.html

Note
msg_prio argument is not supported. Messages are not checked for corruption.
Return values
Thelength of the selected message in bytes - Upon successful completion. The message is removed from the queue
-1- An error occurred. errno is also set.
Side Effects Possible errno values

EBADF - The mqdes argument is not a valid message queue descriptor open for reading.
EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue.
EINVAL - The process or thread would have blocked, and the abstime parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million.
ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, but no message arrived on the queue before the specified timeout expired.
EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty.

◆ mq_unlink()

int mq_unlink ( const char *  name)

Remove a message queue.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html

Return values
0- Upon successful completion.
-1- An error occurred. errno is also set.
Side Effects Possible errno values

EINVAL - name argument is invalid. Refer to requirements on name argument in mq_open().
ENOENT - The named message queue does not exist.