From 07af98f3a5a54a8736109ed031d3177508943299 Mon Sep 17 00:00:00 2001 From: nodist Date: Wed, 23 Sep 2026 12:32:31 -0400 Subject: add task queues --- source/queue.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 source/queue.c (limited to 'source/queue.c') diff --git a/source/queue.c b/source/queue.c new file mode 100644 index 0000000..519aa1f --- /dev/null +++ b/source/queue.c @@ -0,0 +1,42 @@ + +#include "kpl.h" + +void kpl_task_queue_init(kpl_task_queue *task_queue) { + __atomic_store_n(&task_queue->dummy.next, NULL, __ATOMIC_RELAXED); + __atomic_store_n(&task_queue->head, &task_queue->dummy, __ATOMIC_RELAXED); + task_queue->tail = (kpl_task*) &task_queue->dummy; +} + +void kpl_task_queue_add(kpl_task_queue *task_queue, kpl_task *task) { + __atomic_store_n(&task->next, NULL, __ATOMIC_RELAXED); + kpl_task *head = __atomic_exchange_n(&task_queue->head, task, __ATOMIC_SEQ_CST); + __atomic_store_n(&head->next, task, __ATOMIC_RELEASE); +} + +kpl_task *kpl_task_queue_next(kpl_task_queue *task_queue) { + kpl_task *task = NULL; + for (;;) { + kpl_task *tail = task_queue->tail, *next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); + if (tail == (kpl_task*) &task_queue->dummy) { + if (!next) + break; + tail = task_queue->tail = next; + next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); + } + if (!next) { + kpl_task *head = __atomic_load_n(&task_queue->head, __ATOMIC_ACQUIRE); + if (tail != head) + continue; + kpl_task_queue_add(task_queue, (kpl_task*) &task_queue->dummy); + next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); + } + if (next) { + task_queue->tail = next; + task = tail; + break; + } + } + if (task) + __atomic_store_n(&task->next, NULL, __ATOMIC_RELAXED); + return task; +} -- cgit v1.2.3