diff options
Diffstat (limited to 'source/env.c')
| -rw-r--r-- | source/env.c | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/source/env.c b/source/env.c index fbfc302..b5d59b8 100644 --- a/source/env.c +++ b/source/env.c @@ -40,17 +40,21 @@ kpl_exit_code kpl_env_init(kpl_env *env) { 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]; +extern inline void kpl_env_priority_increment(kpl_thread *thread); + +extern inline void kpl_env_priority_add(kpl_thread *thread, ssize_t amount); + +extern inline void kpl_env_priority_decrement(kpl_thread *thread); + +static void kpl_env_thread_queue_task(kpl_thread *thread, kpl_object_task *task) { kpl_object_queue_enqueue(&thread->task_queue, task); - thread->priority++; - env->total_priority++; + kpl_env_priority_increment(thread); 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); + return kpl_env_thread_queue_task(&env->threads[task->thread_id], task); 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++) { @@ -60,7 +64,7 @@ void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode thread_id = next_thread_id; } } - kpl_env_thread_queue_task(env, task, thread_id); + kpl_env_thread_queue_task(&env->threads[thread_id], task); } static void kpl_env_thread_init(kpl_thread *thread) { @@ -75,7 +79,7 @@ static void kpl_env_thread_init(kpl_thread *thread) { 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))) + while ((tmp_task = kpl_io_uring_get_completed(thread))) continue; kpl_io_uring_free(&thread->io_uring); kpl_gc_free(&thread->gc, thread); @@ -83,6 +87,13 @@ static void kpl_env_thread_free(kpl_thread *thread) { sem_destroy(&thread->sem); } +#define KPL_ENV_FLAGS_STOP_THREAD_MASK (KPL_ENV_FLAG_STOP | KPL_ENV_FLAG_HAS_ERROR) + +static void kpl_env_wake_all_threads(kpl_env *env) { + for (int32_t thread_id = 0; thread_id < env->thread_count; thread_id++) + sem_post(&env->threads[thread_id].sem); +} + void *kpl_env_run_loop(void *arg) { kpl_thread *thread = arg; cpu_set_t cpus; @@ -91,9 +102,29 @@ void *kpl_env_run_loop(void *arg) { 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; + for (;;) { + // TODO RUN GC + kpl_object_task *task = kpl_object_queue_dequeue(&thread->task_queue); + if (!task) + task = kpl_io_uring_get_completed(thread); + if (task) { + kpl_object_error *error = task->task_fn(task, thread); + if (!error) + continue; + kpl_object_queue_enqueue(&thread->error_queue, error); + thread->env->flags |= KPL_ENV_FLAG_HAS_ERROR; + kpl_env_wake_all_threads(thread->env); + break; + } + kpl_env_priority_decrement(thread); + if (!thread->env->total_priority) { + kpl_env_wake_all_threads(thread->env); + break; + } + sem_wait(&thread->sem); + if (!thread->env->total_priority || thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) + break; + kpl_env_priority_increment(thread); } // TODO REPORT ERRORS kpl_env_thread_free(thread); |
