CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
1#pragma once
2#include "FreeRTOS.h"
3#include <queue.h>
4
5/**
6 * \file
7 * \brief FreeRTOS queue compatibility APIs.
8 *
9 * This file defines the compatibility wrappers for FreeRTOS's queue APIs.
10 * This is intended for porting code from FreeRTOS and should not be used in
11 * new CHERIoT software. The functions in this file wrap the APIs exposed in
12 * queue.h. These are implemented in the `message_queue_library` library and,
13 * if used across a trust boundary, the `message_queue` compartment, which must
14 * be linked to the final firmware image.
15 */
16
17#define errQUEUE_EMPTY pdFALSE
18#define errQUEUE_FULL pdFALSE
19
20/**
21 * Queue handle. This is used to reference queues in the API functions.
22 */
24
25/**
26 * Receive a message on a queue. The message is received into `buffer`, which
27 * must be large enough to accommodate a message of the size passed to
28 * `xQueueCreate`. This blocks for up to `waitTicks` ticks if the queue is
29 * empty.
30 *
31 * Returns `pdPASS` if a message was received, or `errQUEUE_EMPTY` if the queue
32 * is empty and no message arrived (or was not collected by another
33 * higher-priority thread) for the duration of the call.
34 */
35static inline BaseType_t
36xQueueReceive(QueueHandle_t queueHandle, void *buffer, TickType_t waitTicks)
37{
38 struct Timeout timeout = {0, waitTicks};
39 int rv = queue_receive(&timeout, queueHandle, buffer);
40
41 if (rv == 0)
42 return pdPASS;
43 return errQUEUE_EMPTY;
44}
45
46/**
47 * Send a message to the queue. Blocks for up to `waitTicks` ticks if the
48 * queue is full. The message is copied from `buffer` which must be large
49 * enough to accommodate a message of the size passed to `xQueueCreate`.
50 *
51 * returns `pdPASS` if the message was sent, or `errQUEUE_FULL` if the queue
52 * remained full for the duration of the call.
53 */
54static inline BaseType_t xQueueSendToBack(QueueHandle_t queueHandle,
55 const void *buffer,
56 TickType_t waitTicks)
57{
58 struct Timeout timeout = {0, waitTicks};
59 int rv = queue_send(&timeout, queueHandle, buffer);
60
61 if (rv == 0)
62 return pdPASS;
63 return errQUEUE_FULL;
64}
65
66/**
67 * Send a message to the queue from an ISR. We do not allow running code from
68 * ISRs and so this behaves like a non-blocking `xQueueSendToBack`.
69 *
70 * The `pxHigherPriorityTaskWoken` parameter is used to return whether a yield
71 * is necessary. A yield is never necessary in this implementation and so this
72 * is unconditionally given a value of `pdFALSE`.
73 */
74static inline BaseType_t
76 const void *buffer,
77 BaseType_t *pxHigherPriorityTaskWoken)
78{
79 *pxHigherPriorityTaskWoken = pdFALSE;
80 return xQueueSendToBack(queueHandle, buffer, 0);
81}
82
83#ifndef CHERIOT_NO_AMBIENT_MALLOC
84/**
85 * Create a queue that can store `uxQueueLength` messages of size `uxItemSize`.
86 * Returns NULL if queue creation failed, false otherwise.
87 */
88static inline QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength,
89 UBaseType_t uxItemSize)
90{
91 QueueHandle_t ret = NULL;
92 struct Timeout timeout = {0, UnlimitedTimeout};
93 int rc = queue_create(
94 &timeout, MALLOC_CAPABILITY, &ret, uxItemSize, uxQueueLength);
95 return ret;
96}
97#endif
98
99/**
100 * Delete a queue. This frees the memory associated with the queue.
101 */
102static inline void vQueueDelete(QueueHandle_t xQueue)
103{
104 queue_destroy(MALLOC_CAPABILITY, xQueue);
105}
106
107/**
108 * Return the number of messages waiting in a queue.
109 *
110 * Note: This is inherently racy and should not be used for anything other than
111 * debugging.
112 */
114{
115 size_t ret;
116 int rv = queue_items_remaining(xQueue, &ret);
117
118 assert(rv == 0);
119
120 return ret;
121}
FreeRTOS queue compatibility APIs.
static void vQueueDelete(QueueHandle_t xQueue)
Delete a queue.
Definition queue.h:102
static BaseType_t xQueueReceive(QueueHandle_t queueHandle, void *buffer, TickType_t waitTicks)
Receive a message on a queue.
Definition queue.h:36
struct MessageQueue * QueueHandle_t
Queue handle.
Definition queue.h:23
static BaseType_t xQueueSendToBack(QueueHandle_t queueHandle, const void *buffer, TickType_t waitTicks)
Send a message to the queue.
Definition queue.h:54
static UBaseType_t uxQueueMessagesWaiting(const QueueHandle_t xQueue)
Return the number of messages waiting in a queue.
Definition queue.h:113
static QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize)
Create a queue that can store uxQueueLength messages of size uxItemSize.
Definition queue.h:88
static BaseType_t xQueueSendToBackFromISR(QueueHandle_t queueHandle, const void *buffer, BaseType_t *pxHigherPriorityTaskWoken)
Send a message to the queue from an ISR.
Definition queue.h:75
FreeRTOS porting layer.
#define pdPASS
FreeRTOS definition indicating success.
Definition FreeRTOS.h:61
int32_t BaseType_t
FreeRTOS default signed integer type.
Definition FreeRTOS.h:179
Ticks TickType_t
FreeRTOS type for durations expressed in ticks.
Definition FreeRTOS.h:175
#define pdFALSE
FreeRTOS definition of false.
Definition FreeRTOS.h:48
uint32_t UBaseType_t
FreeRTOS default unsigned integer type.
Definition FreeRTOS.h:183
int __cheri_libcall queue_send(TimeoutArgument timeout, struct MessageQueue *handle, const void *src)
Send a message to the queue specified by handle.
int __cheri_libcall queue_destroy(AllocatorCapability heapCapability, struct MessageQueue *handle)
Destroys a queue.
int __cheri_libcall queue_create(TimeoutArgument timeout, AllocatorCapability heapCapability, struct MessageQueue **outQueue, size_t elementSize, size_t elementCount)
Allocates space for a queue using heapCapability and stores a handle to it via outQueue.
int __cheri_libcall queue_items_remaining(struct MessageQueue *handle, size_t *items)
Returns the number of items in the queue specified by handle via items.
int __cheri_libcall queue_receive(TimeoutArgument timeout, struct MessageQueue *handle, void *dst)
Receive a message over a queue specified by handle.
Structure representing a queue.
Definition queue.h:44
Structure representing a timeout.
Definition timeout.h:47
static const Ticks UnlimitedTimeout
Value indicating an unbounded timeout for use with Timeout.
Definition timeout.h:22