Compare commits
4 Commits
eb5a23761d
...
319779a16c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
319779a16c | ||
![]() |
2bf96c0f76 | ||
![]() |
fa67393723 | ||
![]() |
c2cd319d0c |
33
main.c
33
main.c
@ -71,13 +71,43 @@ static size_t threads_parallel(LVM *lvm, void *ud, size_t argn, set_LValueU *hea
|
|||||||
lvm_reset_regs(&set);
|
lvm_reset_regs(&set);
|
||||||
lvm_call(lvm, func, 0, heap, &set);
|
lvm_call(lvm, func, 0, heap, &set);
|
||||||
|
|
||||||
|
// This thread must still respond to the GC
|
||||||
while(atomic_load(&ctx.finished) != no - 1) {
|
while(atomic_load(&ctx.finished) != no - 1) {
|
||||||
lvm->safepoint_func(lvm, heap, regset);
|
lvm->safepoint_func(lvm, heap, regset);
|
||||||
|
thrd_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ThreadsRunCtx {
|
||||||
|
LVM *L;
|
||||||
|
LFunc *func;
|
||||||
|
};
|
||||||
|
static int threads_run_worker(void *arg) {
|
||||||
|
struct ThreadsRunCtx *ctx = arg;
|
||||||
|
|
||||||
|
LRegSet regs = {};
|
||||||
|
lvm_reset_regs(®s);
|
||||||
|
lvm_run(ctx->L, ctx->func, 0, ®s);
|
||||||
|
|
||||||
|
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) {
|
static char* read_full_file(const char *fn) {
|
||||||
FILE *f = fn ? fopen(fn, "rb") : stdin;
|
FILE *f = fn ? fopen(fn, "rb") : stdin;
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
@ -112,6 +142,9 @@ int main(int argc, char **argv) {
|
|||||||
ltable_set(threads,
|
ltable_set(threads,
|
||||||
lvalue_from_string(lstring_newz("parallel")),
|
lvalue_from_string(lstring_newz("parallel")),
|
||||||
lvalue_from_func(lvm_func_from_native(threads_parallel, NULL)));
|
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,
|
ltable_set(env,
|
||||||
lvalue_from_string(lstring_newz("threads")),
|
lvalue_from_string(lstring_newz("threads")),
|
||||||
|
1
parse.c
1
parse.c
@ -1072,6 +1072,7 @@ bool parse_stat(Parser *P) {
|
|||||||
|
|
||||||
Scope *new_scope = calloc(1, sizeof(*new_scope));
|
Scope *new_scope = calloc(1, sizeof(*new_scope));
|
||||||
new_scope->parent = P->scope;
|
new_scope->parent = P->scope;
|
||||||
|
P->scope = new_scope;
|
||||||
|
|
||||||
expect(P, TOK_DO);
|
expect(P, TOK_DO);
|
||||||
parse_chunk(P);
|
parse_chunk(P);
|
||||||
|
11
secondary_thread.lua
Normal file
11
secondary_thread.lua
Normal 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
|
Loading…
Reference in New Issue
Block a user