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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#include "kpl.h"
static uint32_t kpl_env_run_mode(kpl_env *env) {
return env->flags & (KPL_ENV_FLAG_RUN_MODULE | KPL_ENV_FLAG_RUN_EVAL |
KPL_ENV_FLAG_RUN_REPL | KPL_ENV_FLAG_RUN_FUNCTION);
}
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 = kpl_env_run_mode(env);
if (run_mode != KPL_ENV_FLAG_RUN_MODULE && run_mode != KPL_ENV_FLAG_RUN_EVAL &&
run_mode != KPL_ENV_FLAG_RUN_REPL && run_mode != KPL_ENV_FLAG_RUN_FUNCTION)
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
return KPL_EXIT_CODE_SUCCESS;
}
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);
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->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++) {
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->threads[thread_id], task);
}
static void kpl_env_thread_init_eval(kpl_thread *thread) {
kpl_object_string *string = kpl_object_string_from_c_string(thread, thread->env->kpl_c_string_entry);
(void) string;
// TODO
}
static void kpl_env_thread_init_function(kpl_thread *thread) {
kpl_object_task *object_task = kpl_object_task_create(thread, 0, 0);
object_task->task_fn = thread->env->kpl_fn_entry;
kpl_env_queue_task(thread->env, object_task, KPL_ENV_QUEUE_MODE_SYNC);
}
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);
if (thread->thread_id != KPL_THREAD_MAIN_ID)
return;
switch (kpl_env_run_mode(thread->env)) {
case KPL_ENV_FLAG_RUN_MODULE:
// TODO
break;
case KPL_ENV_FLAG_RUN_EVAL:
kpl_env_thread_init_eval(thread);
break;
case KPL_ENV_FLAG_RUN_REPL:
// TODO
break;
case KPL_ENV_FLAG_RUN_FUNCTION:
kpl_env_thread_init_function(thread);
break;
}
}
static kpl_object_task *kpl_thread_dequeue_task(kpl_thread *thread) {
kpl_object_task *object_task = kpl_object_queue_dequeue(&thread->task_queue);
if (object_task)
kpl_env_priority_decrement(thread);
return object_task;
}
static void kpl_env_thread_free(kpl_thread *thread) {
kpl_object_task *tmp_task;
while ((tmp_task = kpl_io_uring_get_completed(thread)))
continue;
kpl_io_uring_free(&thread->io_uring);
kpl_gc_free(thread);
kpl_memory_allocator_free(&thread->memory_allocator);
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;
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);
for (;;) {
if (thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) {
kpl_env_wake_all_threads(thread->env);
break;
}
// TODO RUN GC
kpl_object_task *object_task = kpl_thread_dequeue_task(thread);
if (!object_task)
object_task = kpl_io_uring_get_completed(thread);
if (object_task) {
object_task->task_fn(thread, object_task);
kpl_object_task_destroy(thread, object_task);
continue;
}
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);
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;
// TODO
}
void kpl_env_free(kpl_env *env) {
free(env->threads);
// TODO NAMESPACE AND MODULE TREES
// TODO MODULE TREE MUTEX
}
|