Skip to content

Commit dbf743a

Browse files
committed
kernel: queue, fifo: Add peek_head/peek_tail accessors
As explained in the docstrings, a usecase behind these operations is when other container objects are put in a fifo. The typical processing iteration make take just some data from a container at the head of fifo, with the container still being kept at the fifo, unless it becomes empty, and only then it's removed. Similarly with adding more data - first step may be to try to add more data to a container at the tail of fifo, and only if it's full, add another container to a fifo. The specific usecase these operations are added for is network subsystem processing, where net_buf's and net_pkt's are added to fifo. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent ae9b7f7 commit dbf743a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

include/kernel.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,34 @@ static inline int k_queue_is_empty(struct k_queue *queue)
14581458
return (int)sys_slist_is_empty(&queue->data_q);
14591459
}
14601460

1461+
/**
1462+
* @brief Peek element at the head of queue.
1463+
*
1464+
* Return element from the head of queue without removing it.
1465+
*
1466+
* @param queue Address of the queue.
1467+
*
1468+
* @return Head element, or NULL if queue is empty.
1469+
*/
1470+
static inline void *k_queue_peek_head(struct k_queue *queue)
1471+
{
1472+
return sys_slist_peek_head(&queue->data_q);
1473+
}
1474+
1475+
/**
1476+
* @brief Peek element at the tail of queue.
1477+
*
1478+
* Return element from the tail of queue without removing it.
1479+
*
1480+
* @param queue Address of the queue.
1481+
*
1482+
* @return Tail element, or NULL if queue is empty.
1483+
*/
1484+
static inline void *k_queue_peek_tail(struct k_queue *queue)
1485+
{
1486+
return sys_slist_peek_tail(&queue->data_q);
1487+
}
1488+
14611489
/**
14621490
* @brief Statically define and initialize a queue.
14631491
*
@@ -1615,6 +1643,36 @@ struct k_fifo {
16151643
#define k_fifo_is_empty(fifo) \
16161644
k_queue_is_empty((struct k_queue *) fifo)
16171645

1646+
/**
1647+
* @brief Peek element at the head of fifo.
1648+
*
1649+
* Return element from the head of fifo without removing it. A usecase
1650+
* for this is if elements of the fifo are themselves containers. Then
1651+
* on each iteration of processing, a head container will be peeked,
1652+
* and some data processed out of it, and only if the container is empty,
1653+
* it will be completely remove from the fifo.
1654+
*
1655+
* @param fifo Address of the fifo.
1656+
*
1657+
* @return Head element, or NULL if the fifo is empty.
1658+
*/
1659+
#define k_fifo_peek_head(fifo) \
1660+
k_queue_peek_head((struct k_queue *) fifo)
1661+
1662+
/**
1663+
* @brief Peek element at the tail of fifo.
1664+
*
1665+
* Return element from the tail of fifo (without removing it). A usecase
1666+
* for this is if elements of the fifo are themselves containers. Then
1667+
* it may be useful to add more data to the last container in fifo.
1668+
*
1669+
* @param fifo Address of the fifo.
1670+
*
1671+
* @return Tail element, or NULL if fifo is empty.
1672+
*/
1673+
#define k_fifo_peek_tail(fifo) \
1674+
k_queue_peek_tail((struct k_queue *) fifo)
1675+
16181676
/**
16191677
* @brief Statically define and initialize a fifo.
16201678
*

0 commit comments

Comments
 (0)