aboutsummaryrefslogtreecommitdiff
path: root/source/queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/queue.c')
-rw-r--r--source/queue.c91
1 files changed, 0 insertions, 91 deletions
diff --git a/source/queue.c b/source/queue.c
deleted file mode 100644
index 350c4e2..0000000
--- a/source/queue.c
+++ /dev/null
@@ -1,91 +0,0 @@
-
-#include "kpl.h"
-
-extern inline void kpl_task_queue_init(kpl_task_queue *task_queue);
-
-extern inline void kpl_task_queue_add(kpl_task_queue *task_queue, kpl_task *task);
-
-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;
-}
-
-bool kpl_ring_queue_init(kpl_ring_queue *ring_queue, int8_t size_pow2) {
- if (size_pow2 < KPL_RING_QUEUE_MIN_POW2 || size_pow2 > KPL_RING_QUEUE_MAX_POW2)
- return false;
- size_t size = 1L << size_pow2;
- ring_queue->mmap_size = KPL_PAGE_SIZE_ROUND(sizeof(kpl_ring_queue_item) * size);
- ring_queue->items = mmap(NULL, ring_queue->mmap_size,
- PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- ring_queue->items_mask = size - 1;
- __atomic_store_n(&ring_queue->head_index, 0, __ATOMIC_RELAXED);
- __atomic_store_n(&ring_queue->tail_index, 0, __ATOMIC_RELAXED);
- for (size_t index = 0; index < size; index++)
- __atomic_store_n(&ring_queue->items[index].index, index, __ATOMIC_RELAXED);
- return true;
-}
-
-extern inline void kpl_ring_queue_free(kpl_ring_queue *ring_queue);
-
-bool kpl_ring_queue_add(kpl_ring_queue *ring_queue, void *data) {
- for (;;) {
- ssize_t tail_index = __atomic_load_n(&ring_queue->tail_index, __ATOMIC_RELAXED);
- kpl_ring_queue_item *item = &ring_queue->items[tail_index & ring_queue->items_mask];
- ssize_t item_index = __atomic_load_n(&item->index, __ATOMIC_ACQUIRE), diff = item_index - tail_index;
- if (diff > 0)
- continue;
- if (diff < 0)
- break;
- if (!__atomic_compare_exchange_n(&ring_queue->tail_index, &tail_index, tail_index + 1, true,
- __ATOMIC_RELAXED, __ATOMIC_RELAXED))
- continue;
- item->data = data;
- __atomic_store_n(&item->index, tail_index + 1, __ATOMIC_RELEASE);
- return true;
- }
- return false;
-}
-
-void *kpl_ring_queue_next(kpl_ring_queue *ring_queue) {
- for (;;) {
- ssize_t head_index = __atomic_load_n(&ring_queue->head_index, __ATOMIC_RELAXED);
- kpl_ring_queue_item *item = &ring_queue->items[head_index & ring_queue->items_mask];
- ssize_t item_index = __atomic_load_n(&item->index, __ATOMIC_ACQUIRE),
- diff = item_index - (head_index + 1);
- if (diff > 0)
- continue;
- if (diff < 0)
- break;
- if (!__atomic_compare_exchange_n(&ring_queue->head_index, &head_index, head_index + 1, true,
- __ATOMIC_RELAXED, __ATOMIC_RELAXED))
- continue;
- void *data = item->data;
- item->data = NULL;
- __atomic_store_n(&item->index, head_index + ring_queue->items_mask + 1, __ATOMIC_RELEASE);
- return data;
- }
- return NULL;
-}