diff options
Diffstat (limited to 'include/kpl')
| -rw-r--r-- | include/kpl/allocate.h | 38 | ||||
| -rw-r--r-- | include/kpl/class.h | 10 | ||||
| -rw-r--r-- | include/kpl/env.h | 50 | ||||
| -rw-r--r-- | include/kpl/gc.h | 73 | ||||
| -rw-r--r-- | include/kpl/object.h | 29 | ||||
| -rw-r--r-- | include/kpl/storage.h | 7 | ||||
| -rw-r--r-- | include/kpl/type.h | 79 | ||||
| -rw-r--r-- | include/kpl/value.h | 18 |
8 files changed, 219 insertions, 85 deletions
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; |
