aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornodist <kevin.comas.git@gmail.com>2026-11-01 10:07:03 -0500
committernodist <kevin.comas.git@gmail.com>2026-11-01 10:07:03 -0500
commit9a61168aec3373be702bdbf252043ce3605f2b5b (patch)
tree49078373733f1eb4294569cede513aa7f82b94f0
parentfcc5294b919856ff04fef859760d48d0da136396 (diff)
add basic gc test
-rw-r--r--include/kpl.h2
-rw-r--r--include/kpl/allocate.h38
-rw-r--r--include/kpl/class.h10
-rw-r--r--include/kpl/env.h50
-rw-r--r--include/kpl/gc.h73
-rw-r--r--include/kpl/object.h29
-rw-r--r--include/kpl/storage.h7
-rw-r--r--include/kpl/type.h79
-rw-r--r--include/kpl/value.h18
-rw-r--r--source/allocate.c6
-rw-r--r--source/env.c119
-rw-r--r--source/gc.c69
-rw-r--r--source/identifier.c4
-rw-r--r--source/io.c4
-rw-r--r--source/kpl.c5
-rw-r--r--source/type.c84
-rw-r--r--test/gc.c34
17 files changed, 535 insertions, 96 deletions
diff --git a/include/kpl.h b/include/kpl.h
index 8623488..fdf8bbb 100644
--- a/include/kpl.h
+++ b/include/kpl.h
@@ -58,5 +58,5 @@ typedef struct _kpl_env kpl_env;
#include "kpl/ast.h"
#include "kpl/memory.h"
#include "kpl/io.h"
+#include "kpl/gc.h"
#include "kpl/env.h"
-#include "kpl/allocate.h"
diff --git a/include/kpl/allocate.h b/include/kpl/allocate.h
deleted file mode 100644
index 3da4a10..0000000
--- a/include/kpl/allocate.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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;
- thread->gc_cycle_change++;
- 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_thread *thread = &env->threads[obj->thread_id];
- kpl_memory_allocator_return(&thread->memory_allocator, object);
- thread->gc_cycle_change--;
-}
diff --git a/include/kpl/class.h b/include/kpl/class.h
index 5957a98..0842db2 100644
--- a/include/kpl/class.h
+++ b/include/kpl/class.h
@@ -1,11 +1,17 @@
/*
- * The kpl class a 64bit value with an table of functions
+ * The kpl class a 64bit value with a table of functions
*/
#pragma once
+/*
+ * How to traverse the object and mark kpl_object_gc
+ */
+typedef void kpl_interface_gc_mark_object_fn(void *object);
+
typedef struct {
-// TODO KPL INTERFACE
+ // TODO
+ kpl_interface_gc_mark_object_fn *gc_mark_object_fn;
} kpl_interface;
typedef struct {
diff --git a/include/kpl/env.h b/include/kpl/env.h
index 5ea3b33..0cab4bc 100644
--- a/include/kpl/env.h
+++ b/include/kpl/env.h
@@ -13,15 +13,15 @@ typedef enum : int32_t {
typedef struct _kpl_thread {
uint16_t thread_id; // self id
+ kpl_env *env; // access env
kpl_memory_allocator memory_allocator;
- _Atomic ssize_t gc_cycle_change; // number of objects added(+)/removed(-) to gc_head after last sweep
- kpl_object *gc_head, *gc_mark_head; // sweep if not marked or not in io pending
+ kpl_gc gc;
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
+ _Atomic ssize_t priority; // io+running the total number of tasks in threads queue, lower is higher priority
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
+ sem_t sem; // wait when task queue and io queue is empty
} kpl_thread;
typedef enum : int32_t {
@@ -30,9 +30,11 @@ typedef enum : int32_t {
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_FLAG_STOP = 1 << 0, // any thread can set, all threads check
+ KPL_ENV_FLAG_HAS_ERROR = 1 << 1,
+ KPL_ENV_FLAG_RUN_MODULE = 1 << 2,
+ KPL_ENV_FLAG_RUN_EVAL = 1 << 3,
+ KPL_ENV_FLAG_RUN_REPL = 1 << 4
} kpl_env_flags;
/*
@@ -45,7 +47,7 @@ typedef enum : uint32_t {
* kpl_env_free &env
*/
typedef struct _kpl_env {
- uint32_t flags; // see kpl_env_flags for values
+ _Atomic uint32_t flags; // see kpl_env_flags for values, any thread can modify or check needs atomic
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
@@ -57,18 +59,9 @@ typedef struct _kpl_env {
} kpl_env; // kpl state, no globals, must be zero initialized
/*
- * Must match the type of c main return
- * if code != KPL_EXIT_CODE_SUCCESS then return code
- */
-typedef enum : int32_t {
- KPL_EXIT_CODE_SUCCESS = 0,
- KPL_EXIT_CODE_ERROR = 1,
- KPL_EXIT_CODE_HELP = 2
-} kpl_exit_code;
-
-/*
* Set up initial values before initialization
* For defaults and/or directly setting without arguments, pass -1 to argc then fields can be updated directly
+ * If not passing args, one of the RUN flags must be set
*/
kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]);
@@ -78,9 +71,21 @@ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]);
*/
kpl_exit_code kpl_env_init(kpl_env *env);
+typedef enum : uint8_t {
+ KPL_ENV_QUEUE_MODE_ASYNC = 0,
+ KPL_ENV_QUEUE_MODE_SYNC = 1
+} kpl_env_queue_mode;
+
+/*
+ * queue_mode ASYNC queues thread with lowest priority
+ * queue_mode SYNC queues thread on threads[task->thread_id]
+ */
+void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode);
+
/*
* Run once, not re-enetrant
* TODO RUN TASK AND IO ORDERING
+ * Cleans Up each thread async
*/
kpl_exit_code kpl_env_run(kpl_env *env);
@@ -88,7 +93,14 @@ kpl_exit_code kpl_env_run(kpl_env *env);
* Stops KPL, can be called on any task in KPL
* Does not return just signals to all threads to stop, the return status will be from kpl_env_run
*/
-void kpl_env_stop(kpl_env *env);
+inline void kpl_env_stop(kpl_env *env) {
+ env->flags |= KPL_ENV_FLAG_STOP;
+}
+
+/*
+ * Run garbage collector
+ */
+void kpl_env_gc(kpl_env *env);
/*
* Clean up
diff --git a/include/kpl/gc.h b/include/kpl/gc.h
new file mode 100644
index 0000000..074ba49
--- /dev/null
+++ b/include/kpl/gc.h
@@ -0,0 +1,73 @@
+/*
+ * KPL Garbage Collector
+ */
+
+#pragma once
+
+/*
+ * Per thread gc, additions and removals are done by the owner thread
+ * Marking can be done by any thread
+ */
+typedef struct {
+ _Atomic size_t gc_cycle_head_additions; // updated by owner thread, checked by any thread
+ kpl_object_gc *gc_head, *gc_mark_head; // only can be accessed by owner thread
+} kpl_gc;
+
+/*
+ * Init
+ */
+inline void kpl_gc_init(kpl_gc *gc) {
+ __atomic_store_n(&gc->gc_cycle_head_additions, 0, __ATOMIC_RELAXED);
+ gc->gc_head = gc->gc_mark_head = NULL;
+}
+
+/*
+ * Create a new gc node and a new kpl_object with object_size
+ * Add the node to the gc_head
+ * Returns the new kpl_gc_object
+ * Only owner thread calls this
+ */
+kpl_object_gc *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_size);
+
+/*
+ * Remove node from gc_head
+ * Free gc node and the object it wraps
+ * Only owner thread call this during sweep or free
+ */
+void kpl_gc_delete(kpl_thread *thread, kpl_object_gc *object_gc);
+
+/*
+ * Mark a gc object
+ * Tells gc not to delete object
+ */
+inline void kpl_gc_mark(kpl_object_gc *object_gc) {
+ __atomic_test_and_set(&object_gc->gc_mark, __ATOMIC_SEQ_CST);
+}
+
+/*
+ * Move gc_head to gc_mark head
+ * Need task to traverse task and error queue to mark objects all call kpl_interface_gc_mark_object_fn
+ */
+inline void kpl_gc_prepare_sweep(kpl_gc *gc) {
+ gc->gc_mark_head = gc->gc_head;
+ gc->gc_head = NULL;
+ gc->gc_cycle_head_additions = 0;
+}
+
+/*
+ * Run sweep on unmarked objects on gc_mark_head
+ * Clear mark on marked objects and return them to gc_head
+ */
+void kpl_gc_sweep(kpl_thread *thread);
+
+/*
+ * Run delete on all objects on gc_head
+ */
+inline void kpl_gc_free(kpl_gc *gc, kpl_thread *thread) {
+ kpl_object_gc *head = gc->gc_head;
+ while (head) {
+ kpl_object_gc *tmp = head;
+ head = head->gc_next;
+ kpl_gc_delete(thread, tmp);
+ }
+}
diff --git a/include/kpl/object.h b/include/kpl/object.h
index b7b1a33..b157719 100644
--- a/include/kpl/object.h
+++ b/include/kpl/object.h
@@ -4,7 +4,7 @@
#pragma once
-typedef enum : uint8_t {
+typedef enum : uint16_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_GC_PERSIST = 1 << 1 // Object is never swept by GC until program end
@@ -16,10 +16,7 @@ typedef enum : uint8_t {
#define KPL_OBJECT_HEADER(STRUCT_NAME) \
struct STRUCT_NAME *_Atomic queue_next; \
int32_t object_size; \
- uint16_t thread_id; \
- uint8_t object_flags; \
- _Atomic bool gc_mark; \
- struct STRUCT_NAME *gc_prev, *gc_next
+ uint16_t thread_id, object_flags;
typedef struct _kpl_object {
KPL_OBJECT_HEADER(_kpl_object);
@@ -60,6 +57,18 @@ inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *objec
void *kpl_object_queue_dequeue(kpl_object_queue *object_queue);
/*
+ * All runtime objects are wrapped in the object_gc
+ * Only owner thread can use gc_prev and gc_next
+ * Any thread can mark
+ */
+typedef struct _kpl_object_gc {
+ KPL_OBJECT_HEADER(_kpl_object_gc);
+ struct _kpl_object_gc *gc_prev, *gc_next;
+ _Atomic bool gc_mark __attribute((aligned(8)));
+ kpl_class class;
+} kpl_object_gc;
+
+/*
* Class data is read only, cannot modify or race conditions can occur
* The error stack can be modified across threads
*/
@@ -74,14 +83,16 @@ typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_env *env);
/*
* Coroutine state object
+ * Can have up to 32 arguments/locals
+ * The variables_gc_mask is a bitmask, the set bit index represents the variable at the index is a kpl_object_gc
*/
typedef struct _kpl_object_task {
KPL_OBJECT_HEADER(_kpl_object_task);
- _Atomic bool join_ready __attribute__((aligned(4))); // shares cache line with variables_size
- int32_t variables_size, io_res; // variables_size is set once then only read
- uint32_t io_flags; // io_res and io_flags are set by io_uring complete
+ _Atomic bool join_ready __attribute__((aligned(4))); // shares cache line with variables_gc_mask
+ uint32_t variables_gc_mask, io_flags; // set once then only read
+ int32_t io_res; // io_res and io_flags are set by io_uring complete
kpl_object_task *join;
kpl_task_fn *task_fn;
kpl_result result;
- kpl_class variables[]; // a task has no distinction between an argument or a local, both live here
+ kpl_value64 variables[]; // a task has no distinction between an argument or a local, both live here
} kpl_object_task;
diff --git a/include/kpl/storage.h b/include/kpl/storage.h
index d78f80b..8758624 100644
--- a/include/kpl/storage.h
+++ b/include/kpl/storage.h
@@ -11,13 +11,17 @@
typedef struct _kpl_storgae_object {
KPL_TREE_HEADER(_kpl_storgae_object);
kpl_type_base *type;
- kpl_class class;
+ union {
+ kpl_class class;
+ kpl_function function;
+ };
kpl_identifier identifier;
} kpl_stoage_object;
// c_string_ptr used for tree search comparisons
#define KPL_STORAGE_HEADER(STRUCT_NAME) \
KPL_TREE_HEADER(STRUCT_NAME); \
+ bool import_ready; \
kpl_tree object_tree; \
const char *c_string_ptr;
@@ -37,6 +41,7 @@ typedef kpl_stoage_namespace kpl_stoage_search;
/*
* Modules make a copy of absolute file path
* The c_string_ptr is pointed to this file path copy string
+ * This tree can only be accessed through a mutex
*/
typedef struct _kpl_storage_module {
KPL_TREE_HEADER(_kpl_storage_module);
diff --git a/include/kpl/type.h b/include/kpl/type.h
index 76c64c1..7b93e6f 100644
--- a/include/kpl/type.h
+++ b/include/kpl/type.h
@@ -6,7 +6,7 @@
#pragma once
typedef enum : uint8_t {
- KPL_TYPE_NAME_UNKOWN,
+ KPL_TYPE_NAME_INVALID,
// VALUES
KPL_TYPE_NAME_BOOLEAN,
KPL_TYPE_NAME_INTEGER,
@@ -42,24 +42,32 @@ typedef enum : uint8_t {
KPL_TYPE_NAME_NAMESPACE,
// DEFINITIONS
KPL_TYPE_NAME_ALIAS,
- KPL_TYPE_NAME_UNIQUE
+ KPL_TYPE_NAME_UNIQUE,
+ // ENUM COUNT
+ KPL_TYPE_NAME_ENUM_COUNT
} kpl_type_name;
+const char *kpl_type_name_c_string(kpl_type_name type_name);
+
/*
* The c struct for this type
*/
typedef enum : uint8_t {
+ KPL_TYPE_KIND_INVALID,
KPL_TYPE_KIND_BASE, // has no additional type information
- KPL_TYPE_KIND_INNER, // type wraps one or two types
+ KPL_TYPE_KIND_NATIVE, // keeps interface pointer
KPL_TYPE_KIND_ARRAY, // type wraps an unknown number of types
- KPL_TYPE_KIND_TREE_ITEM_TYPE,
- KPL_TYPE_KIND_TREE_ITEM_IDENTIFIER,
- KPL_TYPE_KIND_TREE // tree of type/type or identifier/type
+ KPL_TYPE_KIND_TREE, // tree of type/type or identifier/type
+ KPL_TYPE_KIND_COROUTINE // coroutine type, tree + array
} kpl_type_kind;
+kpl_type_kind kpl_type_name_get_kind(kpl_type_name type_name);
+
typedef enum : uint16_t {
- KPL_TYPE_FLAG_EMPTY = 0,
- KPL_TYPE_FLAG_REF = 1 << 0
+ KPL_TYPE_FLAG_EMPTY = 0,
+ KPL_TYPE_FLAG_NULLABLE = 1 << 0,
+ KPL_TYPE_FLAG_TREE_TYPE = 1 << 1,
+ KPL_TYPE_FLAG_TREE_IDENTIFIER = 1 << 2
} kpl_type_flags;
// type_kind is for struct polymorphism
@@ -77,20 +85,61 @@ typedef struct _kpl_type_base {
} kpl_type_base;
/*
- * Type wraps one or two types
+ * Keeps interface pointer for its type comparison
*/
-typedef struct _kpl_type_inner {
- KPL_TYPE_HEADER(_kpl_type_inner);
- kpl_type_base *inner[2];
-} kpl_type_inner;
+typedef struct _kpl_type_native {
+ KPL_TYPE_HEADER(_kpl_type_native);
+ kpl_interface *interface;
+} kpl_type_native;
/*
* Type wraps an unknown number of types
+ * Array does not grow or shrink
+ * Minimum array size is 1
*/
typedef struct _kpl_type_array {
KPL_TYPE_HEADER(_kpl_type_array);
- uint16_t array_size, array_length;
+ uint16_t array_size;
kpl_type_base *array[];
} kpl_type_array;
-// TODO TREE
+/*
+ * Tree based on matching types
+ */
+typedef struct _kpl_type_tree_item_type {
+ KPL_TREE_HEADER(_kpl_type_tree_item_type);
+ uint16_t tag; // unique id
+ kpl_type_base *type;
+} kpl_type_tree_item_type;
+
+/*
+ * Tree based on matching identifiers
+ */
+typedef struct _kpl_type_tree_item_identifier {
+ KPL_TREE_HEADER(_kpl_type_tree_item_identifier);
+ uint16_t tag; // unique id for the item
+ kpl_type_base *type;
+ kpl_identifier identifier;
+} kpl_type_tree_item_identifier;
+
+/*
+ * Tree of types
+ * Tag counter provides a unique tag to each new inserted item
+ */
+typedef struct _kpl_type_tree {
+ KPL_TYPE_HEADER(_kpl_type_tree);
+ uint16_t tag_counter;
+ kpl_tree tree;
+} kpl_type_tree;
+
+/*
+ * Coroutine Type
+ * Arguments size is known at creation and does not grow
+ * Arguments size can be zero
+ */
+typedef struct _kpl_type_coroutine {
+ KPL_TYPE_HEADER(_kpl_type_coroutine);
+ uint16_t arguments_size, tag_counter; // unique id generator
+ kpl_tree result_tree;
+ kpl_type_base *arguments[];
+} kpl_type_coroutine;
diff --git a/include/kpl/value.h b/include/kpl/value.h
index 1e05c00..e0bf0c7 100644
--- a/include/kpl/value.h
+++ b/include/kpl/value.h
@@ -1,5 +1,5 @@
/*
- * KPL values based on size
+ * KPL value definitions
*/
#pragma once
@@ -49,3 +49,19 @@ typedef struct {
kpl_value64 value;
int64_t tag;
} kpl_result;
+
+typedef struct {
+ void *async, *sync;
+} kpl_function;
+
+/*
+ * Must match the type of c main return
+ * if code != KPL_EXIT_CODE_SUCCESS then return code
+ */
+typedef enum : int32_t {
+ KPL_EXIT_CODE_SUCCESS = 0,
+ KPL_EXIT_CODE_ERROR = 1,
+ KPL_EXIT_CODE_HELP = 2,
+ KPL_EXIT_CODE_IO_ERROR = 3,
+ KPL_EXIT_CODE_THREAD_ERROR = 4
+} kpl_exit_code;
diff --git a/source/allocate.c b/source/allocate.c
deleted file mode 100644
index e242562..0000000
--- a/source/allocate.c
+++ /dev/null
@@ -1,6 +0,0 @@
-
-#include "kpl.h"
-
-extern inline void *kpl_object_allocate(kpl_thread *thread, int32_t size);
-
-extern inline void kpl_object_return(kpl_env *env, void *object);
diff --git a/source/env.c b/source/env.c
index 050f95b..fbfc302 100644
--- a/source/env.c
+++ b/source/env.c
@@ -1,3 +1,122 @@
#include "kpl.h"
+kpl_exit_code kpl_env_init(kpl_env *env) {
+ if (!env->thread_count || env->thread_count < -1)
+ return KPL_EXIT_CODE_ERROR;
+ if (env->io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN ||
+ env->io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX)
+ return KPL_EXIT_CODE_ERROR;
+ uint32_t run_mode = env->flags & (KPL_ENV_FLAG_RUN_MODULE | KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL);
+ if (run_mode != KPL_ENV_FLAG_RUN_MODULE && run_mode != KPL_ENV_FLAG_RUN_EVAL &&
+ run_mode != KPL_ENV_FLAG_RUN_REPL)
+ return KPL_EXIT_CODE_ERROR;
+ if (env->thread_count == -1) {
+ cpu_set_t cpus;
+ CPU_ZERO(&cpus);
+ if (sched_getaffinity(0, sizeof(cpu_set_t), &cpus) != 0)
+ return KPL_EXIT_CODE_ERROR;
+ env->thread_count = CPU_COUNT(&cpus);
+ }
+ __atomic_store_n(&env->total_priority, env->thread_count, __ATOMIC_RELAXED);
+ env->threads = calloc(env->thread_count, sizeof(kpl_thread));
+ for (uint16_t thread_id = 0; thread_id < env->thread_count; thread_id++) {
+ env->threads[thread_id].thread_id = thread_id;
+ env->threads[thread_id].env = env;
+ }
+ // TODO NAMESPACE AND MODULE TREES
+ // TODO MODULE TREE MUTEX
+ switch (run_mode) {
+ case KPL_ENV_FLAG_RUN_MODULE:
+ // TODO
+ break;
+ case KPL_ENV_FLAG_RUN_EVAL:
+ // TODO
+ break;
+ case KPL_ENV_FLAG_RUN_REPL:
+ // TODO
+ break;
+ }
+ return KPL_EXIT_CODE_SUCCESS;
+}
+
+static void kpl_env_thread_queue_task(kpl_env *env, kpl_object_task *task, uint16_t thread_id) {
+ kpl_thread *thread = &env->threads[thread_id];
+ kpl_object_queue_enqueue(&thread->task_queue, task);
+ thread->priority++;
+ env->total_priority++;
+ sem_post(&thread->sem);
+}
+
+void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode) {
+ if (queue_mode == KPL_ENV_QUEUE_MODE_SYNC)
+ return kpl_env_thread_queue_task(env, task, task->thread_id);
+ uint16_t thread_id = 0;
+ ssize_t priority = __atomic_load_n(&env->threads[thread_id].priority, __ATOMIC_ACQUIRE);
+ for (int16_t next_thread_id = 1; next_thread_id < env->thread_count; next_thread_id++) {
+ ssize_t next_priority = __atomic_load_n(&env->threads[next_thread_id].priority, __ATOMIC_ACQUIRE);
+ if (next_priority < priority) {
+ priority = next_priority;
+ thread_id = next_thread_id;
+ }
+ }
+ kpl_env_thread_queue_task(env, task, thread_id);
+}
+
+static void kpl_env_thread_init(kpl_thread *thread) {
+ kpl_memory_allocator_init(&thread->memory_allocator);
+ kpl_gc_init(&thread->gc);
+ kpl_io_uring_init(&thread->io_uring, thread->env->io_uring_queue_size_pow2);
+ __atomic_store_n(&thread->priority, 0, __ATOMIC_RELAXED);
+ kpl_object_queue_init(&thread->task_queue);
+ kpl_object_queue_init(&thread->error_queue);
+ sem_init(&thread->sem, 0, 0);
+}
+
+static void kpl_env_thread_free(kpl_thread *thread) {
+ kpl_object_task *tmp_task;
+ while ((tmp_task = kpl_io_uring_get_completed(&thread->io_uring)))
+ continue;
+ kpl_io_uring_free(&thread->io_uring);
+ kpl_gc_free(&thread->gc, thread);
+ kpl_memory_allocator_free(&thread->memory_allocator);
+ sem_destroy(&thread->sem);
+}
+
+void *kpl_env_run_loop(void *arg) {
+ kpl_thread *thread = arg;
+ cpu_set_t cpus;
+ CPU_ZERO(&cpus);
+ CPU_SET(thread->thread_id, &cpus);
+ if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpus))
+ exit(KPL_EXIT_CODE_THREAD_ERROR);
+ kpl_env_thread_init(thread);
+ while (!(thread->env->flags & KPL_ENV_FLAG_HAS_ERROR)) {
+ // TODO RUN
+ break;
+ }
+ // TODO REPORT ERRORS
+ kpl_env_thread_free(thread);
+ return NULL;
+}
+
+kpl_exit_code kpl_env_run(kpl_env *env) {
+ for (int16_t thread_id = 1; thread_id < env->thread_count; thread_id++)
+ pthread_create(&env->threads[thread_id].pthread, NULL, kpl_env_run_loop, &env->threads[thread_id]);
+ kpl_env_run_loop(&env->threads[KPL_THREAD_MAIN_ID]);
+ for (int16_t thread_id = 1; thread_id < env->thread_count; thread_id++)
+ pthread_join(env->threads[thread_id].pthread, NULL);
+ return env->flags & KPL_ENV_FLAG_HAS_ERROR ? KPL_EXIT_CODE_ERROR : KPL_EXIT_CODE_SUCCESS;
+}
+
+extern inline void kpl_env_stop(kpl_env *env);
+
+void kpl_env_gc(kpl_env *env) {
+ (void) env;
+}
+
+void kpl_env_free(kpl_env *env) {
+ free(env->threads);
+ // TODO NAMESPACE AND MODULE TREES
+ // TODO MODULE TREE MUTEX
+}
diff --git a/source/gc.c b/source/gc.c
new file mode 100644
index 0000000..6db368c
--- /dev/null
+++ b/source/gc.c
@@ -0,0 +1,69 @@
+
+#include "kpl.h"
+
+extern inline void kpl_gc_init(kpl_gc *gc);
+
+kpl_object_gc *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_size) {
+ kpl_object *obj = kpl_memory_allocator_allocate(&thread->memory_allocator, object_size, thread->thread_id);
+ if (!obj)
+ return NULL;
+ obj->object_flags = KPL_OBJECT_FLAGS_EMPTY;
+ kpl_object_gc *obj_gc = kpl_memory_allocator_allocate(&thread->memory_allocator, sizeof(kpl_object_gc),
+ thread->thread_id);
+ if (!obj_gc) {
+ kpl_memory_allocator_return(&thread->memory_allocator, obj);
+ return NULL;
+ }
+ obj_gc->gc_prev = obj_gc->gc_next = NULL;
+ __atomic_clear(&obj_gc->gc_mark, __ATOMIC_RELAXED);
+ obj_gc->class.interface = interface;
+ obj_gc->class.value.ptr = obj;
+ obj_gc->gc_next = thread->gc.gc_head;
+ if (thread->gc.gc_head)
+ thread->gc.gc_head->gc_prev = obj_gc;
+ thread->gc.gc_head = obj_gc;
+ thread->gc.gc_cycle_head_additions++;
+ return obj_gc;
+}
+
+void kpl_gc_delete(kpl_thread *thread, kpl_object_gc *object_gc) {
+ if (object_gc == thread->gc.gc_mark_head) {
+ thread->gc.gc_mark_head = thread->gc.gc_mark_head->gc_next;
+ if (thread->gc.gc_mark_head)
+ thread->gc.gc_mark_head->gc_prev = NULL;
+ }
+ kpl_memory_allocator_return(&thread->memory_allocator, object_gc->class.value.ptr);
+ if (object_gc->gc_prev)
+ object_gc->gc_prev->gc_next = object_gc->gc_next ? object_gc->gc_next : NULL;
+ if (object_gc->gc_next)
+ object_gc->gc_next->gc_prev = object_gc->gc_prev ? object_gc->gc_prev : NULL;
+ kpl_memory_allocator_return(&thread->memory_allocator, object_gc);
+}
+
+extern inline void kpl_gc_mark(kpl_object_gc *object_gc);
+
+extern inline void kpl_gc_prepare_sweep(kpl_gc *gc);
+
+void kpl_gc_sweep(kpl_thread *thread) {
+ if (!thread->gc.gc_mark_head)
+ return;
+ kpl_object_gc *mark_head = thread->gc.gc_mark_head, *mark_tail = mark_head;
+ while (mark_head) {
+ kpl_object_gc *tmp = mark_head;
+ mark_head = mark_head->gc_next;
+ if (tmp->gc_mark) {
+ __atomic_clear(&tmp->gc_mark, __ATOMIC_RELAXED);
+ mark_tail = tmp;
+ } else
+ kpl_gc_delete(thread, tmp);
+ }
+ if (!thread->gc.gc_mark_head)
+ return;
+ mark_tail->gc_next = thread->gc.gc_head;
+ if (thread->gc.gc_head)
+ thread->gc.gc_head->gc_prev = mark_tail;
+ thread->gc.gc_head = thread->gc.gc_mark_head;
+ thread->gc.gc_mark_head = NULL;
+}
+
+extern inline void kpl_gc_free(kpl_gc *gc, kpl_thread *thread);
diff --git a/source/identifier.c b/source/identifier.c
index a65d8d4..28401a2 100644
--- a/source/identifier.c
+++ b/source/identifier.c
@@ -74,8 +74,10 @@ void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIF
int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right) {
for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) {
uint64_t left_part = identifier_left->parts[part_index], right_part = identifier_right->parts[part_index];
+ if (left_part == right_part)
+ continue;
if (!left_part && !right_part)
- return 0;
+ break;
left_part &= KPL_IDENTIFIER_PART_LENGTH_MASK;
right_part &= KPL_IDENTIFIER_PART_LENGTH_MASK;
while (left_part && right_part) {
diff --git a/source/io.c b/source/io.c
index 8f3db55..6e83d8e 100644
--- a/source/io.c
+++ b/source/io.c
@@ -43,7 +43,7 @@ kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) {
uint32_t cring_head = __atomic_load_n(io_uring->cring_head, __ATOMIC_ACQUIRE);
if (cring_head == *io_uring->cring_tail) {
if (kpl_io_uring_enter(io_uring->ring_fd, 0, 1, IORING_ENTER_GETEVENTS) < 0)
- return NULL;
+ exit(KPL_EXIT_CODE_IO_ERROR);
}
struct io_uring_cqe *cqe = &io_uring->cqes[cring_head & *io_uring->cring_mask];
kpl_object_task *task = (kpl_object_task*) cqe->user_data;
@@ -52,7 +52,7 @@ kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) {
cring_head++;
__atomic_store_n(io_uring->cring_head, cring_head, __ATOMIC_RELEASE);
io_uring->submitted_to_kernel--;
- task->object_flags &= ~KPL_OBJECT_FLAGS_IO_PENDING;
+ task->object_flags ^= KPL_OBJECT_FLAGS_IO_PENDING;
return task;
}
diff --git a/source/kpl.c b/source/kpl.c
index d970eb3..0c50b2e 100644
--- a/source/kpl.c
+++ b/source/kpl.c
@@ -10,7 +10,10 @@ int32_t main(int32_t argc, char *argv[]) {
kpl_env env = {};
if ((exit_code = kpl_env_config(&env, argc, argv)) != KPL_EXIT_CODE_SUCCESS)
return exit_code;
- // TODO
+ if ((exit_code = kpl_env_init(&env)) != KPL_EXIT_CODE_SUCCESS)
+ return exit_code;
+ exit_code = kpl_env_run(&env);
+ kpl_env_free(&env);
return exit_code;
}
#endif
diff --git a/source/type.c b/source/type.c
new file mode 100644
index 0000000..684f8fc
--- /dev/null
+++ b/source/type.c
@@ -0,0 +1,84 @@
+
+#include "kpl.h"
+
+static const char *type_name_c_string[] = {
+ "INVALID",
+ "BOOLEAN",
+ "INTEGER",
+ "U8",
+ "U16",
+ "U32",
+ "U64",
+ "I8",
+ "I16",
+ "I32",
+ "I64",
+ "FLOAT",
+ "F32",
+ "F64",
+ "CHAR",
+ "STRING",
+ "ARRAY",
+ "TUPLE",
+ "TABLE",
+ "MAP",
+ "SET",
+ "UNION",
+ "ANY",
+ "NATIVE",
+ "ERROR",
+ "LAMBDA",
+ "GENERATOR",
+ "REGEXP",
+ "MODULE",
+ "NAMESPACE",
+ "ALIAS",
+ "UNIQUE",
+ "ENUM_COUNT"
+};
+
+const char *kpl_type_name_c_string(kpl_type_name type_name) {
+ if (type_name <= KPL_TYPE_NAME_INVALID || type_name >= KPL_TYPE_NAME_ENUM_COUNT)
+ return "INVALID TYPE NAME";
+ return type_name_c_string[type_name];
+}
+
+static const kpl_type_kind type_name_kind[] = {
+ [KPL_TYPE_NAME_INVALID] = KPL_TYPE_KIND_INVALID,
+ [KPL_TYPE_NAME_BOOLEAN] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_INTEGER] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_U8] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_U16] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_U32] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_U64] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_I8] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_I16] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_I32] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_I64] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_FLOAT] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_F32] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_F64] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_CHAR] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_STRING] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_ARRAY] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_TUPLE] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_TABLE] = KPL_TYPE_KIND_TREE,
+ [KPL_TYPE_NAME_MAP] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_SET] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_UNION] = KPL_TYPE_KIND_TREE,
+ [KPL_TYPE_NAME_ANY] = KPL_TYPE_KIND_TREE,
+ [KPL_TYPE_NAME_NATIVE] = KPL_TYPE_KIND_NATIVE,
+ [KPL_TYPE_NAME_ERROR] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_LAMBDA] = KPL_TYPE_KIND_COROUTINE,
+ [KPL_TYPE_NAME_GENERATOR] = KPL_TYPE_KIND_COROUTINE,
+ [KPL_TYPE_NAME_REGEXP] = KPL_TYPE_KIND_COROUTINE,
+ [KPL_TYPE_NAME_MODULE] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_NAMESPACE] = KPL_TYPE_KIND_BASE,
+ [KPL_TYPE_NAME_ALIAS] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_UNIQUE] = KPL_TYPE_KIND_ARRAY,
+ [KPL_TYPE_NAME_ENUM_COUNT] = KPL_TYPE_KIND_INVALID
+};
+
+kpl_type_kind kpl_type_name_get_kind(kpl_type_name type_name) {
+ return type_name_kind[type_name];
+}
diff --git a/test/gc.c b/test/gc.c
new file mode 100644
index 0000000..7796322
--- /dev/null
+++ b/test/gc.c
@@ -0,0 +1,34 @@
+
+#include "kpl_test.h"
+
+#define TEST_OBJECT_COUNT 10
+
+KPL_TEST(basic_mark_and_sweep) {
+ kpl_thread thread = {};
+ kpl_memory_allocator_init(&thread.memory_allocator);
+ kpl_gc_init(&thread.gc);
+ kpl_object_gc *objs[TEST_OBJECT_COUNT] = {};
+ for (int32_t obj_index = 1; obj_index <= TEST_OBJECT_COUNT; obj_index++) {
+ objs[obj_index - 1] = kpl_gc_new(&thread, NULL, sizeof(kpl_object_error));
+ if (obj_index % 2 == 0)
+ kpl_gc_mark(objs[obj_index - 1]);
+ }
+ int32_t counter = 0;
+ kpl_object_gc *gc_head = thread.gc.gc_head;
+ while (gc_head) {
+ counter++;
+ gc_head = gc_head->gc_next;
+ }
+ KPL_ASSERT(counter == TEST_OBJECT_COUNT, "should have TEST_OBJECT_COUNT Objects in gc list");
+ kpl_gc_prepare_sweep(&thread.gc);
+ kpl_gc_sweep(&thread);
+ gc_head = thread.gc.gc_head;
+ counter = 0;
+ while (gc_head) {
+ counter++;
+ gc_head = gc_head->gc_next;
+ }
+ KPL_ASSERT(counter == TEST_OBJECT_COUNT / 2, "should have removed half of the objects");
+ kpl_gc_free(&thread.gc, &thread);
+ kpl_memory_allocator_free(&thread.memory_allocator);
+}