aboutsummaryrefslogtreecommitdiff
path: root/source/env.c
blob: fbfc30284375f4323d79f011fff2213856fa4143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
}