From 2e908a7be31558cc98a9f0e97556e49ee5748c16 Mon Sep 17 00:00:00 2001 From: nodist Date: Mon, 28 Sep 2026 16:15:39 -0400 Subject: inline some queue fns --- include/kpl.h | 1 + include/kpl/code.h | 4 ++-- include/kpl/io.h | 4 ++++ include/kpl/macro.h | 2 ++ include/kpl/process.h | 15 +-------------- include/kpl/queue.h | 20 ++++++++++++++------ include/kpl/system.h | 8 ++++++++ include/kpl/type.h | 19 +++++++++++++++++++ source/queue.c | 20 ++++++-------------- test/queue.c | 2 +- 10 files changed, 58 insertions(+), 37 deletions(-) diff --git a/include/kpl.h b/include/kpl.h index 4f1a74b..833c18b 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -32,6 +32,7 @@ int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char typedef struct _kpl_env { kpl_config config; + _Atomic ssize_t total_priority; kpl_io io; kpl_process process; kpl_code code; diff --git a/include/kpl/code.h b/include/kpl/code.h index fb44f86..1902c42 100644 --- a/include/kpl/code.h +++ b/include/kpl/code.h @@ -7,8 +7,8 @@ typedef enum : int8_t { KPL_CONFIG_CODE_QUEUE_POW2_MIN = 4, - KPL_CONFIG_CODE_QUEUE_POW2_DEFAULT = 6, - KPL_CONFIG_CODE_QUEUE_POW2_MAX = 8, + KPL_CONFIG_CODE_QUEUE_POW2_DEFAULT = 7, + KPL_CONFIG_CODE_QUEUE_POW2_MAX = 10, } kpl_config_code_queue_pow; typedef struct { diff --git a/include/kpl/io.h b/include/kpl/io.h index 514f2d4..a1b4b3f 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -19,3 +19,7 @@ typedef struct { kpl_config_io *config_io; // TODO } kpl_io; + +bool kpl_io_init(kpl_io *io); + +void kpl_io_free(kpl_io *io); diff --git a/include/kpl/macro.h b/include/kpl/macro.h index cdcaa1f..e0b59e5 100644 --- a/include/kpl/macro.h +++ b/include/kpl/macro.h @@ -11,3 +11,5 @@ #define KPL_PRINT_TODO(MESSAGE) _KPL_PRINT_(LIGHT_YELLOW, MESSAGE) #define KPL_PRINT_ERROR(MESSAGE) _KPL_PRINT_(LIGHT_RED, MESSAGE) + +#define KPL_PAGE_SIZE_ROUND(SIZE) ((SIZE + (getpagesize() - 1)) & ~(getpagesize() - 1)) diff --git a/include/kpl/process.h b/include/kpl/process.h index 7d13daf..528365a 100644 --- a/include/kpl/process.h +++ b/include/kpl/process.h @@ -22,23 +22,11 @@ typedef struct { int8_t count, ring_pool_entries_pow2[KPL_CONFIG_THREAD_RING_POOL_COUNT]; } kpl_config_thread; -typedef struct _kpl_env kpl_env; - -typedef void kpl_task_fn(kpl_task *task, kpl_env *env); - -typedef struct _kpl_task { - struct _kpl_task *_Atomic next, *join; - // TODO - kpl_result result; - int32_t thread_id; - _Atomic bool join_ready; -} kpl_task; - typedef struct { int32_t thread_id; + _Atomic ssize_t priority; kpl_env *env; kpl_task_queue task_queue; - _Atomic ssize_t priority; // TODO sem_t sem; pthread_t pthread; @@ -46,7 +34,6 @@ typedef struct { typedef struct { kpl_config_thread *config_thread; - _Atomic ssize_t total_priority; kpl_thread *threads; kpl_ring_queue memory_pool[KPL_CONFIG_THREAD_RING_POOL_COUNT]; } kpl_process; diff --git a/include/kpl/queue.h b/include/kpl/queue.h index 977a32d..0107434 100644 --- a/include/kpl/queue.h +++ b/include/kpl/queue.h @@ -5,8 +5,6 @@ #include "kpl/macro.h" -typedef struct _kpl_task kpl_task; - typedef struct { kpl_task *_Atomic head, *tail; struct { @@ -14,9 +12,17 @@ typedef struct { } dummy; } kpl_task_queue; -void kpl_task_queue_init(kpl_task_queue *task_queue); +inline 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); +inline 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); @@ -31,14 +37,16 @@ typedef struct { } kpl_ring_queue_item; typedef struct { - size_t items_mask; + size_t mmap_size, items_mask; _Atomic ssize_t head_index, tail_index; kpl_ring_queue_item *items; } kpl_ring_queue; bool kpl_ring_queue_init(kpl_ring_queue *ring_queue, int8_t size_pow2); -void kpl_ring_queue_free(kpl_ring_queue *ring_queue); +inline void kpl_ring_queue_free(kpl_ring_queue *ring_queue) { + munmap(ring_queue->items, ring_queue->mmap_size); +} bool kpl_ring_queue_add(kpl_ring_queue *ring_queue, void *data); diff --git a/include/kpl/system.h b/include/kpl/system.h index 3f897ab..2d1ec48 100644 --- a/include/kpl/system.h +++ b/include/kpl/system.h @@ -11,3 +11,11 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/include/kpl/type.h b/include/kpl/type.h index b068080..5003cf0 100644 --- a/include/kpl/type.h +++ b/include/kpl/type.h @@ -32,3 +32,22 @@ typedef struct { kpl_value value; int64_t tag; } kpl_result; + +typedef struct _kpl_task kpl_task; + +typedef struct _kpl_env kpl_env; + +typedef void kpl_task_fn(kpl_task *task, kpl_env *env); + +typedef struct _kpl_task { + struct _kpl_task *_Atomic next, *join; + kpl_task_fn *task_fn; + int32_t io_res; + uint32_t io_flags; + int32_t thread_id; + _Atomic bool canceled __attribute__((aligned(4))), join_ready __attribute__((aligned(8))); + kpl_result result; + // TODO TASK ARGUMENTS +} kpl_task; + + diff --git a/source/queue.c b/source/queue.c index 6f36ce8..350c4e2 100644 --- a/source/queue.c +++ b/source/queue.c @@ -1,17 +1,9 @@ #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; -} +extern inline void kpl_task_queue_init(kpl_task_queue *task_queue); -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); -} +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; @@ -45,7 +37,9 @@ 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->items = calloc(1, sizeof(kpl_ring_queue_item) * size); + 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); @@ -54,9 +48,7 @@ bool kpl_ring_queue_init(kpl_ring_queue *ring_queue, int8_t size_pow2) { return true; } -void kpl_ring_queue_free(kpl_ring_queue *ring_queue) { - free(ring_queue->items); -} +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 (;;) { diff --git a/test/queue.c b/test/queue.c index b011725..0f63322 100644 --- a/test/queue.c +++ b/test/queue.c @@ -84,7 +84,7 @@ static void *ring_queue_thread_fn(void *arg) { KPL_TEST(ring_queue_threads) { for (int32_t skip_round = 0; skip_round < RING_QUEUE_THREAD_COUNT; skip_round++) ring_queue_test_slots[skip_round * RING_QUEUE_SLOTS_PER_THREAD] = true; - kpl_ring_queue_init(&ring_queue, 12); + KPL_ASSERT(kpl_ring_queue_init(&ring_queue, 12), "Ring queue should init"); for (int32_t id = 0; id < RING_QUEUE_THREAD_COUNT; id++) pthread_create(&ring_queue_threads[id], NULL, ring_queue_thread_fn, (void*) (intptr_t) id); for (int32_t id = 0; id < RING_QUEUE_THREAD_COUNT; id++) -- cgit v1.2.3