queue: k_queue_get: Fix NULL return
k_queue_get shall never return NULL when timeout is K_FOREVER which can
happen when a higher priority thread cancel/take an item before the
waiting thread.
Fixes issue #4358
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
diff --git a/kernel/queue.c b/kernel/queue.c
index dcf7132..dd30639 100644
--- a/kernel/queue.c
+++ b/kernel/queue.c
@@ -205,21 +205,25 @@
k_poll_event_init(&event, K_POLL_TYPE_FIFO_DATA_AVAILABLE,
K_POLL_MODE_NOTIFY_ONLY, queue);
- event.state = K_POLL_STATE_NOT_READY;
+ do {
+ event.state = K_POLL_STATE_NOT_READY;
- err = k_poll(&event, 1, timeout);
- if (err) {
- return NULL;
- }
+ err = k_poll(&event, 1, timeout);
+ if (err) {
+ return NULL;
+ }
- __ASSERT_NO_MSG(event.state == K_POLL_STATE_FIFO_DATA_AVAILABLE);
+ __ASSERT_NO_MSG(event.state ==
+ K_POLL_STATE_FIFO_DATA_AVAILABLE);
- /* sys_slist_* aren't threadsafe, so must be always protected by
- * irq_lock.
- */
- key = irq_lock();
- val = sys_slist_get(&queue->data_q);
- irq_unlock(key);
+ /* sys_slist_* aren't threadsafe, so must be always protected by
+ * irq_lock.
+ */
+ key = irq_lock();
+ val = sys_slist_get(&queue->data_q);
+ irq_unlock(key);
+ } while (!val && timeout == K_FOREVER);
+
return val;
}
#endif /* CONFIG_POLL */