@web-font-path: "roboto-debian.css";
v2.2.0 (RP2040)
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _PICO_UTIL_QUEUE_H
#define _PICO_UTIL_QUEUE_H
#include "pico.h"
#include "hardware/sync.h"
// PICO_CONFIG: PICO_QUEUE_MAX_LEVEL, Maintain a field for the highest level that has been reached by a queue, type=bool, default=0, advanced=true, group=queue
#ifndef PICO_QUEUE_MAX_LEVEL
#define PICO_QUEUE_MAX_LEVEL 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "pico/lock_core.h"
typedef struct {
lock_core_t core;
uint8_t *data;
uint16_t wptr;
uint16_t rptr;
uint16_t element_size;
uint16_t element_count;
#if PICO_QUEUE_MAX_LEVEL
uint16_t max_level;
#endif
} queue_t;
void queue_init_with_spinlock(queue_t *q, uint element_size, uint element_count, uint spinlock_num);
static inline void queue_init(queue_t *q, uint element_size, uint element_count) {
queue_init_with_spinlock(q, element_size, element_count, next_striped_spin_lock_num());
}
void queue_free(queue_t *q);
static inline uint queue_get_level_unsafe(queue_t *q) {
int32_t rc = (int32_t)q->wptr - (int32_t)q->rptr;
if (rc < 0) {
rc += q->element_count + 1;
}
return (uint)rc;
}
static inline uint queue_get_level(queue_t *q) {
uint32_t save = spin_lock_blocking(q->core.spin_lock);
uint level = queue_get_level_unsafe(q);
spin_unlock(q->core.spin_lock, save);
return level;
}
#if PICO_QUEUE_MAX_LEVEL
static inline uint queue_get_max_level(queue_t *q) {
return q->max_level;
}
#endif
#if PICO_QUEUE_MAX_LEVEL
static inline void queue_reset_max_level(queue_t *q) {
uint32_t save = spin_lock_blocking(q->core.spin_lock);
q->max_level = queue_get_level_unsafe(q);
spin_unlock(q->core.spin_lock, save);
}
#endif
static inline bool queue_is_empty(queue_t *q) {
return queue_get_level(q) == 0;
}
static inline bool queue_is_full(queue_t *q) {
return queue_get_level(q) == q->element_count;
}
// nonblocking queue access functions:
bool queue_try_add(queue_t *q, const void *data);
bool queue_try_remove(queue_t *q, void *data);
bool queue_try_peek(queue_t *q, void *data);
// blocking queue access functions:
void queue_add_blocking(queue_t *q, const void *data);
void queue_remove_blocking(queue_t *q, void *data);
void queue_peek_blocking(queue_t *q, void *data);
#ifdef __cplusplus
}
#endif
#endif
spin_lock_blocking
static __force_inline uint32_t spin_lock_blocking(spin_lock_t *lock)
Acquire a spin lock safely.
Definition spin_lock.h:301
spin_unlock
static __force_inline void spin_unlock(spin_lock_t *lock, uint32_t saved_irq)
Release a spin lock safely.
Definition spin_lock.h:332
next_striped_spin_lock_num
uint next_striped_spin_lock_num(void)
Return a spin lock number from the striped range.
Definition sync.c:18
queue_init
static void queue_init(queue_t *q, uint element_size, uint element_count)
Initialise a queue, allocating a (possibly shared) spinlock.
Definition queue.h:61
queue_peek_blocking
void queue_peek_blocking(queue_t *q, void *data)
Blocking peek at next value to be removed from queue.
Definition queue.c:121
queue_get_level_unsafe
static uint queue_get_level_unsafe(queue_t *q)
Unsafe check of level of the specified queue.
Definition queue.h:83
queue_try_remove
bool queue_try_remove(queue_t *q, void *data)
Non-blocking removal of entry from the queue if non empty.
Definition queue.c:105
queue_try_add
bool queue_try_add(queue_t *q, const void *data)
Non-blocking add value queue if not full.
Definition queue.c:101
queue_get_level
static uint queue_get_level(queue_t *q)
Check of level of the specified queue.
Definition queue.h:97
queue_init_with_spinlock
void queue_init_with_spinlock(queue_t *q, uint element_size, uint element_count, uint spinlock_num)
Initialise a queue with a specific spinlock for concurrency protection.
Definition queue.c:11
queue_is_empty
static bool queue_is_empty(queue_t *q)
Check if queue is empty.
Definition queue.h:138
queue_remove_blocking
void queue_remove_blocking(queue_t *q, void *data)
Blocking remove entry from queue.
Definition queue.c:117
queue_free
void queue_free(queue_t *q)
Destroy the specified queue.
Definition queue.c:20
queue_try_peek
bool queue_try_peek(queue_t *q, void *data)
Non-blocking peek at the next item to be removed from the queue.
Definition queue.c:109
queue_add_blocking
void queue_add_blocking(queue_t *q, const void *data)
Blocking add of value to queue.
Definition queue.c:113
queue_is_full
static bool queue_is_full(queue_t *q)
Check if queue is full.
Definition queue.h:150
lock_core.h
pico.h
sync.h
queue_t
Definition queue.h:32