Queue Objects

The FIFO (First In, First Out) queue implementation facilitates data exchange between tasks. Messages are appended to the end of the queue and retrieved from the beginning. The message size and the maximum number of messages that can be stored are determined during the creation of the queue object.

Queue Creation and Deletion

The osCreateQueue function is used to create a queue object. A queue may be created prior to system startup, from within an interrupt handler, or by a task. During creation, an optional name may be assigned to the queue, enabling other tasks to open it using the osOpenQueue function. Both creation and opening functions return a system-assigned handle used to identify the object. All subsequent queue operations require this handle as a reference. When a queue is no longer needed, it should be closed with osCloseHandle. The queue object is deleted only after all tasks holding an open handle have closed it. For further information, refer to the System Objects Management section.

If queue objects are not utilized, the OS_USE_QUEUE constant can be set to 0 to reduce the total code size.

Using a Queue Object

The maximum message size and the total number of messages are defined during queue creation. Upon creation, a memory buffer is allocated; its size is the product of the maximum message size and the maximum number of messages.

To insert a message into the queue, the osQueuePost function should be called. The osQueuePend function retrieves and subsequently removes the first message from the queue. To read a message without removing it, use the osQueuePeek function. Read and write operations can also be performed via standard system functions such as osRead and osWrite. To remove all messages currently in the queue, call the osClearQueue function.

Synchronization for queue access is implemented in three ways: via a mutex, an auto-reset event, or by disabling interrupts during the data copy operation. The latter is intended only for small data transfers. The synchronization method is selected during creation using the OS_IPC_PROTECT_MUTEX, OS_IPC_PROTECT_EVENT, or OS_IPC_PROTECT_INT_CTRL mode flags. Synchronization via mutex or auto-reset event is available only if the corresponding OS_QUEUE_PROTECT_MUTEX or OS_QUEUE_PROTECT_EVENT constants are set to 1.

If a read operation is attempted on an empty queue, or a write operation on a full queue, the function returns an error. Alternatively, a task may wait for the operation to complete. In this case, the task is resumed once the data is successfully transferred or a specified timeout expires. This behavior is enabled by specifying the OS_IPC_WAIT_IF_EMPTY and OS_IPC_WAIT_IF_FULL mode flags during creation. These features require the OS_QUEUE_ALLOW_WAIT_IF_EMPTY and OS_QUEUE_ALLOW_WAIT_IF_FULL constants to be set to 1.

When blocking operations are enabled, data can be transferred directly between tasks without being stored in the buffer. To enable this optimization, use the OS_IPC_DIRECT_READ_WRITE mode flag; this feature is only available if OS_QUEUE_ALLOW_DIRECT_RW is set to 1.

SpaceShadow documentation