diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/kpl.h | 6 | ||||
| -rw-r--r-- | include/kpl/allocate.h | 35 | ||||
| -rw-r--r-- | include/kpl/class.h | 24 | ||||
| -rw-r--r-- | include/kpl/env.h | 43 | ||||
| -rw-r--r-- | include/kpl/io.h | 4 | ||||
| -rw-r--r-- | include/kpl/memory.h | 2 | ||||
| -rw-r--r-- | include/kpl/object.h | 38 | ||||
| -rw-r--r-- | include/kpl/tree.h | 1 | ||||
| -rw-r--r-- | include/kpl/value.h | 51 | ||||
| -rw-r--r-- | include/kpl_test.h | 2 |
10 files changed, 157 insertions, 49 deletions
diff --git a/include/kpl.h b/include/kpl.h index 7c5e209..838478b 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -6,6 +6,10 @@ #define KPL_VERSION "0.0.0" +#define KPL_FILE_EXTENSION ".kpl" + +#define KPL_WEBSITE "https://kpl.dev" + // System includes #define _GNU_SOURCE #include <stdlib.h> @@ -43,9 +47,11 @@ typedef struct _kpl_env kpl_env; // KPL includes #include "kpl/color.h" +#include "kpl/value.h" #include "kpl/class.h" #include "kpl/object.h" #include "kpl/tree.h" #include "kpl/memory.h" #include "kpl/io.h" #include "kpl/env.h" +#include "kpl/allocate.h" diff --git a/include/kpl/allocate.h b/include/kpl/allocate.h new file mode 100644 index 0000000..ee24d72 --- /dev/null +++ b/include/kpl/allocate.h @@ -0,0 +1,35 @@ +/* + * KPL object allocators helpers + */ + +#pragma once + +/* + * All allocations must extend the KPL_OBJECT_HEADER + * All objects are allocated through this + */ +inline void *kpl_object_allocate(kpl_thread *thread, int32_t size) { + kpl_object *obj = kpl_memory_allocator_allocate(&thread->memory_allocator, size, thread->thread_id); + if (!obj) + return NULL; + obj->object_flags = KPL_OBJECT_FLAGS_EMPTY; + __atomic_clear(&obj->gc_mark, __ATOMIC_RELAXED); + obj->gc_prev = obj->gc_next = NULL; + obj->gc_next = thread->gc_head; + if (thread->gc_head) + thread->gc_head->gc_prev = obj; + thread->gc_head = obj; + return obj; +} + +/* + * All objects are returned by this + */ +inline void kpl_object_return(kpl_env *env, void *object) { + kpl_object *obj = object; + if (obj->gc_prev) + obj->gc_prev->gc_next = obj->gc_next; + if (obj->gc_next) + obj->gc_next->gc_prev = obj->gc_prev; + kpl_memory_allocator_return(&env->threads[obj->thread_id].memory_allocator, object); +} diff --git a/include/kpl/class.h b/include/kpl/class.h index f7f9678..5957a98 100644 --- a/include/kpl/class.h +++ b/include/kpl/class.h @@ -4,33 +4,11 @@ #pragma once -typedef uint8_t kpl_utf8[4]; - -typedef union { - uint8_t u8; - int8_t i8; - uint16_t u16; - int16_t i16; - uint32_t u32; - int32_t i32; - uint64_t u64; - int64_t i64; - kpl_utf8 utf8; - float f32; - double f64; - void *ptr; -} kpl_value; - -typedef struct { - kpl_value value; - int64_t tag; -} kpl_result; - typedef struct { // TODO KPL INTERFACE } kpl_interface; typedef struct { - kpl_value value; + kpl_value64 value; kpl_interface *interface; } kpl_class; diff --git a/include/kpl/env.h b/include/kpl/env.h index 7806969..1e3d3d8 100644 --- a/include/kpl/env.h +++ b/include/kpl/env.h @@ -6,27 +6,33 @@ typedef enum : int32_t { KPL_THREAD_DETERIMINE_COUNT = -1, - KPL_THREAD_MAIN_ID = 0 + KPL_THREAD_MAIN_ID = 0, + KPL_THEAD_THREAD_COUNT_MIN = 1, + KPL_THEAD_THREAD_COUNT_MAX = UINT16_MAX } kpl_thread_enum; typedef struct _kpl_thread { - int32_t thread_id; // self id + uint16_t thread_id; // self id kpl_memory_allocator memory_allocator; + kpl_object *gc_head, *gc_mark_head; // sweep if not marked or not in io pending kpl_io_uring io_uring; // TODO jit allocator _Atomic ssize_t priority; // running + the total number of tasks in threads queue, lower is higher priority - kpl_object_queue task_queue; + kpl_object_queue task_queue, error_queue; // traverse for gc marking pthread_t pthread; // not used for main thread sem_t sem; // wait when no task queue and io queue is empty } kpl_thread; -/* - * All allocations must extend the KPL_OBJECT_HEADER - * All objects are allocated through this - */ -inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size) { - return kpl_memory_allocator_allocate(&thread->memory_allocator, size, thread->thread_id); -} +typedef enum : int32_t { + KPL_ENV_CONFIG_NO_ARGS = -1 +} kpl_env_enum; + +typedef enum : uint32_t { + KPL_ENV_FLAG_EMPTY = 0, + KPL_ENV_FLAG_RUN_MODULE = 1 << 0, + KPL_ENV_FLAG_RUN_EVAL = 1 << 1, + KPL_ENV_FLAG_RUN_REPL = 1 << 2 +} kpl_env_flags; /* * Life cycle: @@ -39,22 +45,17 @@ inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size) { * kpl_env_free &env */ typedef struct _kpl_env { + uint32_t flags; // see kpl_env_flags for values int32_t io_uring_queue_size_pow2; // the power of two to calculate the entries in the io_uring queue int32_t thread_count; // -1 updates this value to max possible, 0 invalid, >= 1 for specific number + const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace _Atomic ssize_t total_priority; // the sum of the threads priority fields, application stops on zero kpl_thread *threads; // 0 is main thread, >= 1 are spawned pthreads - kpl_tree *namespace_tree, *module_tree; // *namespace_tree lookups are thread safe, *module_tree needs mutex - pthread_mutex_t module_tree_mutex; + // TODO namespace tree + // TODO module tree } kpl_env; // kpl state, no globals, must be zero initialized /* - * All objects are returned by this - */ -inline void kpl_env_object_return(kpl_env *env, void *object) { - kpl_memory_allocator_return(&env->threads[((kpl_object*) object)->thread_id].memory_allocator, object); -} - -/* * Must match the type of c main return * if code != KPL_EXIT_CODE_SUCCESS then return code */ @@ -66,12 +67,12 @@ typedef enum : int32_t { /* * Set up initial values before initialization - * For defaults and/or directly setting without arguments, pass 0 to argc then fields can be updated directly + * For defaults and/or directly setting without arguments, pass -1 to argc then fields can be updated directly */ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]); /* - * Initialize + * Initialize, read flags to create initial entry task * env kpl_env_config must be called before this */ kpl_exit_code kpl_env_init(kpl_env *env); diff --git a/include/kpl/io.h b/include/kpl/io.h index f353da2..f5904f4 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -63,6 +63,10 @@ int32_t kpl_io_uring_submit(kpl_io_uring *io_uring); struct io_uring_sqe *kpl_io_uring_op(kpl_io_uring *io_uring, kpl_object_task *task, int32_t op, int32_t fd, const void *addr, uint32_t len, uint64_t offset); +/* + * Add operations to io_uring, can add more then one before submit + */ + inline bool kpl_io_uring_openat(kpl_io_uring *io_uring, kpl_object_task *task, int32_t dir_fd, const char *path, int32_t flags, mode_t mode) { struct io_uring_sqe *sqe = kpl_io_uring_op(io_uring, task, IORING_OP_OPENAT, dir_fd, path, mode, 0); diff --git a/include/kpl/memory.h b/include/kpl/memory.h index b61d861..b69867a 100644 --- a/include/kpl/memory.h +++ b/include/kpl/memory.h @@ -54,7 +54,7 @@ void kpl_memory_allocator_free(kpl_memory_allocator *memory_allocator); * Only call this though kpl_thread_object_allocate * All allocations must extend the KPL_OBJECT_HEADER */ -void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, int32_t thread_id); +void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id); /* * Only call this though kpl_env_object_return diff --git a/include/kpl/object.h b/include/kpl/object.h index 1f3ec04..1053348 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -4,11 +4,21 @@ #pragma once +typedef enum : uint8_t { + KPL_OBJECT_FLAGS_EMPTY = 0, + KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0 // When io pending tasks will not be reachable by GC +} kpl_object_flags; + // for atomic linked list queue // size in bytes, index on the kpl_threads array +// Flags should only be set by owner thread #define KPL_OBJECT_HEADER(STRUCT_NAME) \ struct STRUCT_NAME *_Atomic queue_next; \ - int32_t object_size, thread_id + int32_t object_size; \ + uint16_t thread_id; \ + uint8_t object_flags; \ + _Atomic bool gc_mark; \ + struct STRUCT_NAME *gc_prev, *gc_next typedef struct _kpl_object { KPL_OBJECT_HEADER(_kpl_object); @@ -48,7 +58,18 @@ inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *objec */ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue); -typedef void kpl_task_fn(kpl_object_task *task, kpl_env *env); +/* + * Class data is read only, cannot modify or race conditions can occur + * The error stack can be modified across threads + */ +typedef struct _kpl_object_error { + KPL_OBJECT_HEADER(_kpl_object_error); + kpl_object_queue error_stack; + kpl_class error_file, error_data; // Can be a reference to C code or KPL code + int32_t error_line; +} kpl_object_error; + +typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_env *env); /* * Coroutine state object @@ -63,3 +84,16 @@ typedef struct _kpl_object_task { kpl_result result; kpl_class variables[]; // a task has no distinction between an argument or a local, both live here } kpl_object_task; + +/* + * KPL Object Mutex is a queue of tasks on an object + * Adding: Increment counter, then add task to task queue + * Removing: Remove from task queue, run task, then decrement counter + * Requires spin check when removing task due to space between counter adding and task adding + * If the counter value before was zero take control of the mutex + * The thread that controls the mutex runs each task until counter is zero + */ +typedef struct { + _Atomic int32_t lock_counter; + kpl_object_queue task_queue; +} kpl_object_mutex; diff --git a/include/kpl/tree.h b/include/kpl/tree.h index f3503d2..de9492d 100644 --- a/include/kpl/tree.h +++ b/include/kpl/tree.h @@ -6,6 +6,7 @@ // tree_duplicates is a linked stack #define KPL_TREE_HEADER(STRUCT_NAME) \ + KPL_OBJECT_HEADER(STRUCT_NAME); \ struct STRUCT_NAME *tree_left, *tree_right, *tree_duplicates; \ int32_t tree_height diff --git a/include/kpl/value.h b/include/kpl/value.h new file mode 100644 index 0000000..1e05c00 --- /dev/null +++ b/include/kpl/value.h @@ -0,0 +1,51 @@ +/* + * KPL values based on size + */ + +#pragma once + +#define _KPL_VALUE8 \ + uint8_t u8; \ + int8_t i8 + +typedef union { + _KPL_VALUE8; +} kpl_value8; + +#define _KPL_VALUE16 \ + uint16_t u16; \ + int16_t i16 + +typedef union { + _KPL_VALUE8; + _KPL_VALUE16; +} kpl_value16; + +typedef uint8_t kpl_utf8[4]; + +#define _KPL_VALUE32 \ + kpl_utf8 utf8; \ + uint32_t u32; \ + int32_t i32; \ + float f32 + +typedef union { + _KPL_VALUE8; + _KPL_VALUE16; + _KPL_VALUE32; +} kpl_value32; + +typedef union { + _KPL_VALUE8; + _KPL_VALUE16; + _KPL_VALUE32; + uint64_t u64; + int64_t i64; + double f64; + void *ptr; +} kpl_value64; + +typedef struct { + kpl_value64 value; + int64_t tag; +} kpl_result; diff --git a/include/kpl_test.h b/include/kpl_test.h index 6a423eb..fbad075 100644 --- a/include/kpl_test.h +++ b/include/kpl_test.h @@ -40,8 +40,6 @@ void _kpl_test_add(kpl_test *_test); // do not call outside KPL_TEST macro #define KPL_TEST(NAME) _KPL_TEST_FN_DEFINITION(NAME); \ static void __attribute__((constructor)) kpl_test_constructor_ ## NAME(void) { \ static kpl_test _test = { \ - .tree_left = NULL, .tree_right = NULL, .tree_duplicates = NULL, \ - .tree_height = KPL_TREE_TREE_HEIGHT_INIT, \ .error_line_number = KPL_TEST_ERROR_LINE_NUMBER_INIT, \ .list_next = NULL, \ .source_file = __FILE__, .name = #NAME, .error_message = NULL, \ |
