fifo: Make use of k_queue as implementation

This makes k_fifo functions rely on k_queue and port k_poll to use
k_queue directly.

Once all users of k_fifo migrate to k_queue this should no longer be
needed.

Change-Id: Icf16d580f88d11b2cb89e1abd23ae314f43dbd20
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2017-02-21 15:27:20 +02:00 committed by Anas Nashif
commit e5ed88f328
9 changed files with 48 additions and 231 deletions

View file

@ -1283,19 +1283,12 @@ static inline int k_queue_is_empty(struct k_queue *queue)
*/ */
struct k_fifo { struct k_fifo {
_wait_q_t wait_q; struct k_queue _queue;
sys_slist_t data_q;
_POLL_EVENT;
_OBJECT_TRACING_NEXT_PTR(k_fifo);
}; };
#define K_FIFO_INITIALIZER(obj) \ #define K_FIFO_INITIALIZER(obj) \
{ \ { \
.wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ ._queue = K_QUEUE_INITIALIZER(obj._queue) \
.data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
_POLL_EVENT_OBJ_INIT \
_OBJECT_TRACING_INIT \
} }
/** /**
@ -1317,7 +1310,8 @@ struct k_fifo {
* *
* @return N/A * @return N/A
*/ */
extern void k_fifo_init(struct k_fifo *fifo); #define k_fifo_init(fifo) \
k_queue_init((struct k_queue *) fifo)
/** /**
* @brief Add an element to a fifo. * @brief Add an element to a fifo.
@ -1333,7 +1327,8 @@ extern void k_fifo_init(struct k_fifo *fifo);
* *
* @return N/A * @return N/A
*/ */
extern void k_fifo_put(struct k_fifo *fifo, void *data); #define k_fifo_put(fifo, data) \
k_queue_append((struct k_queue *) fifo, data)
/** /**
* @brief Atomically add a list of elements to a fifo. * @brief Atomically add a list of elements to a fifo.
@ -1351,7 +1346,8 @@ extern void k_fifo_put(struct k_fifo *fifo, void *data);
* *
* @return N/A * @return N/A
*/ */
extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); #define k_fifo_put_list(fifo, head, tail) \
k_queue_append_list((struct k_queue *) fifo, head, tail)
/** /**
* @brief Atomically add a list of elements to a fifo. * @brief Atomically add a list of elements to a fifo.
@ -1368,7 +1364,8 @@ extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
* *
* @return N/A * @return N/A
*/ */
extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); #define k_fifo_put_slist(fifo, list) \
k_queue_merge_slist((struct k_queue *) fifo, list)
/** /**
* @brief Get an element from a fifo. * @brief Get an element from a fifo.
@ -1385,7 +1382,8 @@ extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
* @return Address of the data item if successful; NULL if returned * @return Address of the data item if successful; NULL if returned
* without waiting, or waiting period timed out. * without waiting, or waiting period timed out.
*/ */
extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); #define k_fifo_get(fifo, timeout) \
k_queue_get((struct k_queue *) fifo, timeout)
/** /**
* @brief Query a fifo to see if it has data available. * @brief Query a fifo to see if it has data available.
@ -1400,10 +1398,8 @@ extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
* @return Non-zero if the fifo is empty. * @return Non-zero if the fifo is empty.
* @return 0 if data is available. * @return 0 if data is available.
*/ */
static inline int k_fifo_is_empty(struct k_fifo *fifo) #define k_fifo_is_empty(fifo) \
{ k_queue_is_empty((struct k_queue *) fifo)
return (int)sys_slist_is_empty(&fifo->data_q);
}
/** /**
* @brief Statically define and initialize a fifo. * @brief Statically define and initialize a fifo.
@ -1416,7 +1412,7 @@ static inline int k_fifo_is_empty(struct k_fifo *fifo)
*/ */
#define K_FIFO_DEFINE(name) \ #define K_FIFO_DEFINE(name) \
struct k_fifo name \ struct k_fifo name \
__in_section(_k_fifo, static, name) = \ __in_section(_k_queue, static, name) = \
K_FIFO_INITIALIZER(name) K_FIFO_INITIALIZER(name)
/** /**
@ -3430,6 +3426,7 @@ struct k_poll_event {
struct k_poll_signal *signal; struct k_poll_signal *signal;
struct k_sem *sem; struct k_sem *sem;
struct k_fifo *fifo; struct k_fifo *fifo;
struct k_queue *queue;
}; };
}; };

View file

@ -72,13 +72,6 @@
_k_queue_list_end = .; _k_queue_list_end = .;
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
SECTION_DATA_PROLOGUE(_k_fifo_area, (OPTIONAL),)
{
_k_fifo_list_start = .;
KEEP(*(SORT_BY_NAME("._k_fifo.static.*")))
_k_fifo_list_end = .;
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
SECTION_DATA_PROLOGUE(_k_lifo_area, (OPTIONAL),) SECTION_DATA_PROLOGUE(_k_lifo_area, (OPTIONAL),)
{ {
_k_lifo_list_start = .; _k_lifo_list_start = .;

View file

@ -20,7 +20,6 @@ lib-y += $(strip \
mutex.o \ mutex.o \
queue.o \ queue.o \
lifo.o \ lifo.o \
fifo.o \
stack.o \ stack.o \
mem_slab.o \ mem_slab.o \
mem_pool.o \ mem_pool.o \

View file

@ -1,174 +0,0 @@
/*
* Copyright (c) 2010-2016 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* @brief dynamic-size FIFO queue object.
*/
#include <kernel.h>
#include <kernel_structs.h>
#include <debug/object_tracing_common.h>
#include <toolchain.h>
#include <sections.h>
#include <wait_q.h>
#include <ksched.h>
#include <misc/slist.h>
#include <init.h>
extern struct k_fifo _k_fifo_list_start[];
extern struct k_fifo _k_fifo_list_end[];
struct k_fifo *_trace_list_k_fifo;
#ifdef CONFIG_OBJECT_TRACING
/*
* Complete initialization of statically defined fifos.
*/
static int init_fifo_module(struct device *dev)
{
ARG_UNUSED(dev);
struct k_fifo *fifo;
for (fifo = _k_fifo_list_start; fifo < _k_fifo_list_end; fifo++) {
SYS_TRACING_OBJ_INIT(k_fifo, fifo);
}
return 0;
}
SYS_INIT(init_fifo_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
#endif /* CONFIG_OBJECT_TRACING */
void k_fifo_init(struct k_fifo *fifo)
{
sys_slist_init(&fifo->data_q);
sys_dlist_init(&fifo->wait_q);
_INIT_OBJ_POLL_EVENT(fifo);
SYS_TRACING_OBJ_INIT(k_fifo, fifo);
}
static void prepare_thread_to_run(struct k_thread *thread, void *data)
{
_abort_thread_timeout(thread);
_ready_thread(thread);
_set_thread_return_value_with_data(thread, 0, data);
}
/* returns 1 if a reschedule must take place, 0 otherwise */
static inline int handle_poll_event(struct k_fifo *fifo)
{
#ifdef CONFIG_POLL
uint32_t state = K_POLL_STATE_FIFO_DATA_AVAILABLE;
return fifo->poll_event ?
_handle_obj_poll_event(&fifo->poll_event, state) : 0;
#else
return 0;
#endif
}
void k_fifo_put(struct k_fifo *fifo, void *data)
{
struct k_thread *first_pending_thread;
unsigned int key;
key = irq_lock();
first_pending_thread = _unpend_first_thread(&fifo->wait_q);
if (first_pending_thread) {
prepare_thread_to_run(first_pending_thread, data);
if (!_is_in_isr() && _must_switch_threads()) {
(void)_Swap(key);
return;
}
} else {
sys_slist_append(&fifo->data_q, data);
if (handle_poll_event(fifo)) {
(void)_Swap(key);
return;
}
}
irq_unlock(key);
}
void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail)
{
__ASSERT(head && tail, "invalid head or tail");
struct k_thread *first_thread, *thread;
unsigned int key;
key = irq_lock();
first_thread = _peek_first_pending_thread(&fifo->wait_q);
while (head && ((thread = _unpend_first_thread(&fifo->wait_q)))) {
prepare_thread_to_run(thread, head);
head = *(void **)head;
}
if (head) {
sys_slist_append_list(&fifo->data_q, head, tail);
}
if (first_thread) {
if (!_is_in_isr() && _must_switch_threads()) {
(void)_Swap(key);
return;
}
} else {
if (handle_poll_event(fifo)) {
(void)_Swap(key);
return;
}
}
irq_unlock(key);
}
void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list)
{
__ASSERT(!sys_slist_is_empty(list), "list must not be empty");
/*
* note: this works as long as:
* - the slist implementation keeps the next pointer as the first
* field of the node object type
* - list->tail->next = NULL.
*/
return k_fifo_put_list(fifo, list->head, list->tail);
}
void *k_fifo_get(struct k_fifo *fifo, int32_t timeout)
{
unsigned int key;
void *data;
key = irq_lock();
if (likely(!sys_slist_is_empty(&fifo->data_q))) {
data = sys_slist_get_not_empty(&fifo->data_q);
irq_unlock(key);
return data;
}
if (timeout == K_NO_WAIT) {
irq_unlock(key);
return NULL;
}
_pend_current_thread(&fifo->wait_q, timeout);
return _Swap(key) ? NULL : _current->base.swap_data;
}

View file

@ -67,8 +67,8 @@ static inline int is_condition_met(struct k_poll_event *event, uint32_t *state)
return 1; return 1;
} }
break; break;
case K_POLL_TYPE_FIFO_DATA_AVAILABLE: case K_POLL_TYPE_DATA_AVAILABLE:
if (!k_fifo_is_empty(event->fifo)) { if (!k_queue_is_empty(event->queue)) {
*state = K_POLL_STATE_FIFO_DATA_AVAILABLE; *state = K_POLL_STATE_FIFO_DATA_AVAILABLE;
return 1; return 1;
} }
@ -100,15 +100,15 @@ static inline int register_event(struct k_poll_event *event)
} }
event->sem->poll_event = event; event->sem->poll_event = event;
break; break;
case K_POLL_TYPE_FIFO_DATA_AVAILABLE: case K_POLL_TYPE_DATA_AVAILABLE:
__ASSERT(event->fifo, "invalid fifo\n"); __ASSERT(event->queue, "invalid queue\n");
if (event->fifo->poll_event) { if (event->queue->poll_event) {
return -EADDRINUSE; return -EADDRINUSE;
} }
event->fifo->poll_event = event; event->queue->poll_event = event;
break; break;
case K_POLL_TYPE_SIGNAL: case K_POLL_TYPE_SIGNAL:
__ASSERT(event->fifo, "invalid poll signal\n"); __ASSERT(event->queue, "invalid poll signal\n");
if (event->signal->poll_event) { if (event->signal->poll_event) {
return -EADDRINUSE; return -EADDRINUSE;
} }
@ -135,9 +135,9 @@ static inline void clear_event_registration(struct k_poll_event *event)
__ASSERT(event->sem, "invalid semaphore\n"); __ASSERT(event->sem, "invalid semaphore\n");
event->sem->poll_event = NULL; event->sem->poll_event = NULL;
break; break;
case K_POLL_TYPE_FIFO_DATA_AVAILABLE: case K_POLL_TYPE_DATA_AVAILABLE:
__ASSERT(event->fifo, "invalid fifo\n"); __ASSERT(event->queue, "invalid queue\n");
event->fifo->poll_event = NULL; event->queue->poll_event = NULL;
break; break;
case K_POLL_TYPE_SIGNAL: case K_POLL_TYPE_SIGNAL:
__ASSERT(event->signal, "invalid poll signal\n"); __ASSERT(event->signal, "invalid poll signal\n");

View file

@ -17,20 +17,20 @@ void irq_unlock(unsigned int key)
#include <net/buf.c> #include <net/buf.c>
void k_fifo_init(struct k_fifo *fifo) {} void k_queue_init(struct k_queue *fifo) {}
void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail) {} void k_queue_append_list(struct k_queue *fifo, void *head, void *tail) {}
int k_is_in_isr(void) int k_is_in_isr(void)
{ {
return 0; return 0;
} }
void *k_fifo_get(struct k_fifo *fifo, int32_t timeout) void *k_queue_get(struct k_queue *fifo, int32_t timeout)
{ {
return NULL; return NULL;
} }
void k_fifo_put(struct k_fifo *fifo, void *data) void k_queue_append(struct k_queue *fifo, void *data)
{ {
} }

View file

@ -47,12 +47,13 @@ static pfunc func_array[] = {
(pfunc)k_sem_reset, (pfunc)k_sem_reset,
(pfunc)k_sem_count_get, (pfunc)k_sem_count_get,
/* FIFOs */ /* queues */
(pfunc)k_fifo_init, (pfunc)k_queue_init,
(pfunc)k_fifo_put, (pfunc)k_queue_append,
(pfunc)k_fifo_put_list, (pfunc)k_queue_prepend,
(pfunc)k_fifo_put_slist, (pfunc)k_queue_append_list,
(pfunc)k_fifo_get, (pfunc)k_queue_merge_slist,
(pfunc)k_queue_get,
/* mem slabs */ /* mem slabs */
(pfunc)k_mem_slab_init, (pfunc)k_mem_slab_init,

View file

@ -67,12 +67,13 @@ static pfunc func_array[] = {
(pfunc)k_stack_push, (pfunc)k_stack_push,
(pfunc)k_stack_pop, (pfunc)k_stack_pop,
/* FIFOs */ /* queues */
(pfunc)k_fifo_init, (pfunc)k_queue_init,
(pfunc)k_fifo_put, (pfunc)k_queue_append,
(pfunc)k_fifo_put_list, (pfunc)k_queue_prepend,
(pfunc)k_fifo_put_slist, (pfunc)k_queue_append_list,
(pfunc)k_fifo_get, (pfunc)k_queue_merge_slist,
(pfunc)k_queue_get,
#endif #endif
}; };

View file

@ -17,20 +17,20 @@ void irq_unlock(unsigned int key)
#include <net/buf.c> #include <net/buf.c>
void k_fifo_init(struct k_fifo *fifo) {} void k_queue_init(struct k_queue *queue) {}
void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail) {} void k_queue_append_list(struct k_queue *queue, void *head, void *tail) {}
int k_is_in_isr(void) int k_is_in_isr(void)
{ {
return 0; return 0;
} }
void *k_fifo_get(struct k_fifo *fifo, int32_t timeout) void *k_queue_get(struct k_queue *queue, int32_t timeout)
{ {
return NULL; return NULL;
} }
void k_fifo_put(struct k_fifo *fifo, void *data) void k_queue_append(struct k_queue *queue, void *data)
{ {
} }