diff --git a/include/nanokernel.h b/include/nanokernel.h index f125bf15111..a1d272f7979 100644 --- a/include/nanokernel.h +++ b/include/nanokernel.h @@ -399,6 +399,24 @@ extern void *nano_fifo_get(struct nano_fifo *fifo); */ extern void *nano_fifo_get_wait(struct nano_fifo *fifo); +/** + * + * @brief Get the head element of a fifo, poll/pend with timeout if empty + * + * This is a convenience wrapper for the execution context-specific APIs. This + * is helpful whenever the exact execution context is not known, but should be + * avoided when the context is known up-front (to avoid unnecessary overhead). + * + * @warning It's only valid to call this API from a fiber or a task. + * + * @param fifo FIFO on which to interact. + * @param timeout Timeout measured in ticks + * + * @return Pointer to head element in the list + */ +extern void *nano_fifo_get_wait_timeout(struct nano_fifo *fifo, + int32_t timeout); + /* * methods for ISRs */ diff --git a/kernel/nanokernel/nano_fifo.c b/kernel/nanokernel/nano_fifo.c index 9fad5a1622e..abc6c899761 100644 --- a/kernel/nanokernel/nano_fifo.c +++ b/kernel/nanokernel/nano_fifo.c @@ -150,8 +150,11 @@ void nano_task_fifo_put(struct nano_fifo *fifo, void *data) void nano_fifo_put(struct nano_fifo *fifo, void *data) { static void (*func[3])(struct nano_fifo *fifo, void *data) = { - nano_isr_fifo_put, nano_fiber_fifo_put, nano_task_fifo_put + nano_isr_fifo_put, + nano_fiber_fifo_put, + nano_task_fifo_put }; + func[sys_execution_context_type_get()](fifo, data); } @@ -259,8 +262,11 @@ void *nano_task_fifo_get_wait(struct nano_fifo *fifo) void *nano_fifo_get_wait(struct nano_fifo *fifo) { static void *(*func[3])(struct nano_fifo *fifo) = { - NULL, nano_fiber_fifo_get_wait, nano_task_fifo_get_wait + NULL, + nano_fiber_fifo_get_wait, + nano_task_fifo_get_wait }; + return func[sys_execution_context_type_get()](fifo); } @@ -340,4 +346,15 @@ void *nano_task_fifo_get_wait_timeout(struct nano_fifo *fifo, irq_unlock(key); return NULL; } + +void *nano_fifo_get_wait_timeout(struct nano_fifo *fifo, int32_t timeout) +{ + static void *(*func[3])(struct nano_fifo *, int32_t) = { + NULL, + nano_fiber_fifo_get_wait_timeout, + nano_task_fifo_get_wait_timeout + }; + + return func[sys_execution_context_type_get()](fifo, timeout); +} #endif /* CONFIG_NANO_TIMEOUTS */