Bug fixes

This commit is contained in:
Mid 2025-09-06 13:59:14 +03:00
parent 5809a89eae
commit eb5a23761d
4 changed files with 41 additions and 14 deletions

44
main.c
View File

@ -36,21 +36,44 @@ static size_t table_insert(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, L
return 0; return 0;
} }
// This function is intended for small-medium runtimes, since the caller thread's heap is not touchable during this call. struct ThreadsParallelCtx {
LVM *L;
LFunc *func;
_Atomic uint32_t finished;
};
static int threads_parallel_worker(void *arg) {
struct ThreadsParallelCtx *ctx = arg;
LRegSet regset = {};
lvm_reset_regs(&regset);
lvm_run(ctx->L, ctx->func, 0, &regset);
atomic_fetch_add(&ctx->finished, 1);
return 0;
}
static size_t threads_parallel(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, LRegSet *regset) { static size_t threads_parallel(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, LRegSet *regset) {
size_t no = lvalue_to_int32(regset->regs[0]); size_t no = lvalue_to_int32(regset->regs[0]);
LFunc *func = (LFunc*) (regset->regs[1].u & ~LTAG_MASK); LFunc *func = (LFunc*) (regset->regs[1].u & ~LTAG_MASK);
atomic_fetch_sub(&lvm->active_thread_count, 1); if(no == 0) {
return 0;
#pragma omp parallel for
for(size_t i = 0; i < no; i++) {
LRegSet regset = {};
lvm_reset_regs(&regset);
lvm_run(lvm, func, 0, &regset);
} }
atomic_fetch_add(&lvm->active_thread_count, 1); struct ThreadsParallelCtx ctx = {.L = lvm, .func = func, .finished = 0};
thrd_t thrds[no];
for(size_t i = 1; i < no; i++) {
thrd_create(&thrds[i], threads_parallel_worker, &ctx);
}
LRegSet set = {.parent = regset};
lvm_reset_regs(&set);
lvm_call(lvm, func, 0, heap, &set);
while(atomic_load(&ctx.finished) != no - 1) {
lvm->safepoint_func(lvm, heap, regset);
}
return 0; return 0;
} }
@ -122,6 +145,9 @@ int main(int argc, char **argv) {
LVM lvm = {}; LVM lvm = {};
lvm_init(&lvm); lvm_init(&lvm);
lvm.unit_count = 1;
lvm.units = calloc(1, sizeof(*lvm.units));
lvm.units[0] = unit;
LRegSet regset = {.parent = NULL}; LRegSet regset = {.parent = NULL};
lvm_reset_regs(&regset); lvm_reset_regs(&regset);

View File

@ -1195,7 +1195,7 @@ LUnit *lparse(size_t sz, Token *tokens, LTable *environment) {
P.unit_functions.data[0].lua_instrs = P.current_chunk.instrs.data; P.unit_functions.data[0].lua_instrs = P.current_chunk.instrs.data;
unit->abyss = P.abyss; unit->abyss = P.abyss;
unit->func_count = 1; unit->func_count = P.unit_functions.size;
unit->funcs = P.unit_functions.data; unit->funcs = P.unit_functions.data;
for(Expr *e = last_desc; e;) { for(Expr *e = last_desc; e;) {

7
vm.c
View File

@ -63,9 +63,10 @@ do_getglobal:;
regset->regs[inst->a] = ltable_get(func->env, lvalue_from_string(str)); regset->regs[inst->a] = ltable_get(func->env, lvalue_from_string(str));
set_LValueU_insert(heap, lvalue_from_string(str).u); free(str);
lvm_gc_alert(L, &privates, sizeof(*str) + len); //set_LValueU_insert(heap, lvalue_from_string(str).u);
//lvm_gc_alert(L, &privates, sizeof(*str) + len);
} }
DISPATCH(); DISPATCH();
@ -421,7 +422,7 @@ static void gc_mark(LValue v) {
} }
static void gc_mark_units(LVM *L) { static void gc_mark_units(LVM *L) {
for(size_t u = 0; u < L->unit_count; u++) { for(size_t u = 0; u < L->unit_count; u++) {
LUnit *unit = &L->units[u]; LUnit *unit = L->units[u];
for(size_t f = 0; f < unit->func_count; f++) { for(size_t f = 0; f < unit->func_count; f++) {
LFunc *func = &unit->funcs[f]; LFunc *func = &unit->funcs[f];
gc_mark(lvalue_from_table(func->env)); gc_mark(lvalue_from_table(func->env));

2
vm.h
View File

@ -113,7 +113,7 @@ typedef struct LThreadPrivates {
#define L_THREADS_MAX 32 #define L_THREADS_MAX 32
typedef struct LVM { typedef struct LVM {
size_t unit_count; size_t unit_count;
LUnit *units; LUnit **units;
// The following is all used for GC // The following is all used for GC
_Atomic bool gcInProgress; _Atomic bool gcInProgress;