aboutsummaryrefslogtreecommitdiff
path: root/include/kpl/env.h
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 /include/kpl/env.h
parentfcc5294b919856ff04fef859760d48d0da136396 (diff)
add basic gc test
Diffstat (limited to 'include/kpl/env.h')
-rw-r--r--include/kpl/env.h50
1 files changed, 31 insertions, 19 deletions
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