File queue.h

Coroutine aware queue implementation.

Typedefs

typedef struct queue Queue

Enums

enum res_code_queue

Queue specific result codes.

Values:

enumerator RES_QUEUE_EMPTY

Attempted to get item from an empty queue.

enumerator RES_QUEUE_FULL

Attempted to put an item into a full queue.

Functions

Queue *queue_create_static(Queue *queue, size_t num_items, size_t item_size, uint8_t *item_buffer)

Initialises a statically defined queue.

Parameters:
  • queue – Queue to initialise.

  • num_items – Number of items this queue will manage.

  • item_size – Size of an individual item, in bytes.

  • item_buffer – Buffer containing enough space for the items.

Returns:

Pointer to the queue, or NULL if an error has occurred.

Queue *queue_create(size_t num_items, size_t item_size)

Creates a queue with a fixed number of elements.

Parameters:
  • num_items – Number of items this queue will manage.

  • item_size – Size of an individual item, in bytes.

Returns:

Pointer to the queue, or NULL if an error has occurred.

void queue_free(Queue *queue)

Frees a previously created queue.

Warning

Freeing a statically created queue is undefined.

Parameters:

queue – Queue to Free.

size_t queue_item_count(Queue const *queue)

Gets the number of items in a queue.

Parameters:

queue

Returns:

Number of items in the queue.

bool queue_is_full(Queue const *queue)

Check if the queue is full.

Parameters:

queue – Queue to check.

Returns:

True if queue is full, false otherwise.

bool queue_is_empty(Queue const *queue)

Gets the number of items in a queue.

Parameters:

queue – Queue to check.

Returns:

True if queue is empty, false otherwise.

Result queue_put(Queue *queue, void const *item, PlatformTick timeout)

Puts an item into the queue from a coroutine.

Items are copied into the queue. It is up to the caller to allocate the correct variable size.

This operation blocks the calling coroutine until the item can be queued.

Parameters:
  • queue – Queue to put the item into.

  • item – Item to put into the queue.

  • timeout – Maximum time to wait before giving up.

Return values:
  • RES_OK – on success.

  • RES_TIMEOUT – if the maximum time was awaited.

Result queue_put_no_wait(Queue *queue, void const *item)

Puts an item without waiting.

Parameters:
  • queue – Queue to put the item into.

  • item – Item to put into the queue.

Return values:
  • RES_OK – Item has been queued.

  • RES_QUEUE_FULL – Queue is full, no item has been inserted.

  • RES_NOTIFY_FAILED – if the operation failed to notify the scheduler.

Result queue_put_from_isr(Queue *queue, void const *item)

Puts an item from an ISR.

Parameters:
  • queue – Queue to put the item into.

  • item – Item to put into the queue.

Return values:
  • RES_OK – Item has been queued.

  • RES_QUEUE_FULL – Queue is full, no item has been inserted.

  • RES_NOTIFY_FAILED – if the operation failed to notify the scheduler.

Result queue_get(Queue *queue, void *item, PlatformTick timeout)

Gets an item from the queue from a coroutine.

Items are copied out of the queue. It is up to the caller to allocate the correct variable size.

Parameters:
  • queue – Queue to get the item from.

  • item – Item to get from the queue.

  • timeout – Maximum time to wait before giving up.

Return values:
  • RES_OK – on success.

  • RES_TIMEOUT – if the maximum time was awaited.

Result queue_get_no_wait(Queue *queue, void *item)

Gets an item without waiting.

Parameters:
  • queue – Queue to get the item from.

  • item – Item to get from the queue, only valid if result was RES_OK.

Return values:
  • RES_OK – An item has been taken.

  • RES_QUEUE_EMPTY – Queue has no items to get.

  • RES_NOTIFY_FAILED – if the operation failed to notify the scheduler.

Result queue_get_from_isr(Queue *queue, void *item)

Gets an item from an ISR.

Parameters:
  • queue – Queue to get the item from.

  • item – Item to get from the queue, only valid if result was RES_OK.

Return values:
  • RES_OK – An item has been taken.

  • RES_QUEUE_EMPTY – Queue has no items to get.

  • RES_NOTIFY_FAILED – if the operation failed to notify the scheduler.

struct queue

Public Members

size_t volatile count
size_t volatile read_idx
size_t volatile write_idx
uint8_t *item_buffer
size_t item_size
size_t max_items