Compare commits

...

4 Commits

Author SHA1 Message Date
Mid
319779a16c secondary_thread test 2025-09-06 15:06:23 +03:00
Mid
2bf96c0f76 Yield because maybe good??? 2025-09-06 15:06:14 +03:00
Mid
fa67393723 threads.run 2025-09-06 15:06:03 +03:00
Mid
c2cd319d0c Bug fix 2025-09-06 14:58:10 +03:00
3 changed files with 45 additions and 0 deletions

33
main.c
View File

@ -71,13 +71,43 @@ static size_t threads_parallel(LVM *lvm, void *ud, size_t argn, set_LValueU *hea
lvm_reset_regs(&set);
lvm_call(lvm, func, 0, heap, &set);
// This thread must still respond to the GC
while(atomic_load(&ctx.finished) != no - 1) {
lvm->safepoint_func(lvm, heap, regset);
thrd_yield();
}
return 0;
}
struct ThreadsRunCtx {
LVM *L;
LFunc *func;
};
static int threads_run_worker(void *arg) {
struct ThreadsRunCtx *ctx = arg;
LRegSet regs = {};
lvm_reset_regs(&regs);
lvm_run(ctx->L, ctx->func, 0, &regs);
free(ctx);
return 0;
}
static size_t threads_run(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, LRegSet *regset) {
LFunc *func = (LFunc*) (regset->regs[0].u & ~LTAG_MASK);
struct ThreadsRunCtx *ctx = calloc(1, sizeof(*ctx));
ctx->L = lvm;
ctx->func = func;
thrd_t thrd;
thrd_create(&thrd, threads_run_worker, ctx);
return 0;
}
static char* read_full_file(const char *fn) {
FILE *f = fn ? fopen(fn, "rb") : stdin;
fseek(f, 0, SEEK_END);
@ -112,6 +142,9 @@ int main(int argc, char **argv) {
ltable_set(threads,
lvalue_from_string(lstring_newz("parallel")),
lvalue_from_func(lvm_func_from_native(threads_parallel, NULL)));
ltable_set(threads,
lvalue_from_string(lstring_newz("run")),
lvalue_from_func(lvm_func_from_native(threads_run, NULL)));
}
ltable_set(env,
lvalue_from_string(lstring_newz("threads")),

View File

@ -1072,6 +1072,7 @@ bool parse_stat(Parser *P) {
Scope *new_scope = calloc(1, sizeof(*new_scope));
new_scope->parent = P->scope;
P->scope = new_scope;
expect(P, TOK_DO);
parse_chunk(P);

11
secondary_thread.lua Normal file
View File

@ -0,0 +1,11 @@
done = false
threads.run(function()
for i = 1, 100000 do
print(i)
end
done = true
end)
while done == false do
end