diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-11-01 10:07:03 -0500 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-11-01 10:07:03 -0500 |
| commit | 9a61168aec3373be702bdbf252043ce3605f2b5b (patch) | |
| tree | 49078373733f1eb4294569cede513aa7f82b94f0 /source/env.c | |
| parent | fcc5294b919856ff04fef859760d48d0da136396 (diff) | |
add basic gc test
Diffstat (limited to 'source/env.c')
| -rw-r--r-- | source/env.c | 119 |
1 files changed, 119 insertions, 0 deletions
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 +} |
