1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

More changes to threading model.

This commit is contained in:
Calvin Rose 2017-07-01 11:17:29 -04:00
parent c380f8a2ca
commit 957a513fd6
2 changed files with 41 additions and 18 deletions

View File

@ -40,6 +40,7 @@ int gst_continue(Gst *vm) {
#define gst_assert(vm, cond, e) do {if (!(cond)){gst_error((vm), (e));}} while (0) #define gst_assert(vm, cond, e) do {if (!(cond)){gst_error((vm), (e));}} while (0)
/* Intialize local state */ /* Intialize local state */
vm->thread->status = GST_THREAD_ALIVE;
stack = gst_thread_stack(vm->thread); stack = gst_thread_stack(vm->thread);
pc = gst_frame_pc(stack); pc = gst_frame_pc(stack);
@ -356,14 +357,20 @@ int gst_continue(Gst *vm) {
case GST_OP_TRN: /* Transfer */ case GST_OP_TRN: /* Transfer */
temp = stack[pc[2]]; /* The thread */ temp = stack[pc[2]]; /* The thread */
v1 = stack[pc[3]]; /* The value to pass in */ v1 = stack[pc[3]]; /* The value to pass in */
if (temp.type != GST_THREAD) if (temp.type != GST_THREAD && temp.type != GST_NIL)
gst_error(vm, "expected thread"); gst_error(vm, "expected thread");
if (temp.data.thread->status == GST_THREAD_DEAD || if (temp.type == GST_THREAD) {
temp.data.thread->status == GST_THREAD_ERROR) if (temp.data.thread->status == GST_THREAD_DEAD ||
gst_error(vm, "cannot enter dead thread"); temp.data.thread->status == GST_THREAD_ERROR)
gst_error(vm, "cannot enter dead thread");
}
gst_frame_ret(stack) = pc[1]; gst_frame_ret(stack) = pc[1];
vm->thread->status = GST_THREAD_PENDING; vm->thread->status = GST_THREAD_PENDING;
gst_frame_pc(stack) = pc + 4; gst_frame_pc(stack) = pc + 4;
if (temp.type == GST_NIL) {
vm->ret = v1;
return GST_RETURN_OK;
}
temp.data.thread->status = GST_THREAD_ALIVE; temp.data.thread->status = GST_THREAD_ALIVE;
vm->thread = temp.data.thread; vm->thread = temp.data.thread;
stack = gst_thread_stack(temp.data.thread); stack = gst_thread_stack(temp.data.thread);
@ -423,22 +430,35 @@ int gst_continue(Gst *vm) {
/* Run the vm with a given function. This function is /* Run the vm with a given function. This function is
* called to start the vm. */ * called to start the vm. */
int gst_run(Gst *vm, GstValue callee) { int gst_run(Gst *vm, GstValue callee) {
if (vm->thread == NULL) { int result;
vm->thread = gst_thread(vm, callee, 64); if (vm->thread &&
} else { (vm->thread->status == GST_THREAD_DEAD ||
vm->thread->status == GST_THREAD_ALIVE)) {
/* Reuse old thread */ /* Reuse old thread */
gst_thread_reset(vm, vm->thread, callee); gst_thread_reset(vm, vm->thread, callee);
} else {
/* Create new thread */
vm->thread = gst_thread(vm, callee, 64);
if (vm->thread == NULL)
return GST_RETURN_CRASH;
} }
if (vm->thread == NULL)
return GST_RETURN_CRASH;
if (callee.type == GST_CFUNCTION) { if (callee.type == GST_CFUNCTION) {
vm->ret.type = GST_NIL; vm->ret.type = GST_NIL;
return callee.data.cfunction(vm); result = callee.data.cfunction(vm);
} else if (callee.type == GST_FUNCTION) { } else if (callee.type == GST_FUNCTION) {
return gst_continue(vm); result = gst_continue(vm);
} else { } else {
return GST_RETURN_CRASH; return GST_RETURN_CRASH;
} }
/* Handle yields */
while (result == GST_RETURN_OK && vm->thread->status == GST_THREAD_PENDING) {
/* Send back in the value yielded - TODO - do something useful with this */
GstValue *stack = gst_thread_stack(vm->thread);
stack[gst_frame_ret(stack)] = vm->ret;
/* Resume */
result = gst_continue(vm);
}
return result;
} }
/* Get an argument from the stack */ /* Get an argument from the stack */

View File

@ -1,14 +1,17 @@
(print "Running basic tests...") (print "Running basic tests...")
(var numTestsPassed 0) (var num-tests-passed 0)
(def assert (fn [x e] (var num-tests-run 0)
(def assert (fn [x e]
(varset! num-tests-run (+ 1 num-tests-run))
(if x (if x
(do (do
(print " \e[32m✔\e[0m" e) (print " \e[32m✔\e[0m" e)
(varset! numTestsPassed (+ 1 numTestsPassed)) x) (varset! num-tests-passed (+ 1 num-tests-passed))
x)
(do (do
(print e) (print " \e[31m✘\e[0m" e)
(exit! (+ numTestsPassed 1)))))) x))))
(assert (= 10 (+ 1 2 3 4)) "addition") (assert (= 10 (+ 1 2 3 4)) "addition")
(assert (= -8 (- 1 2 3 4)) "subtraction") (assert (= -8 (- 1 2 3 4)) "subtraction")
@ -77,7 +80,7 @@
(assert (= athread-result "hello, world!") "thread error result") (assert (= athread-result "hello, world!") "thread error result")
(assert (= (status athread) "error") "thread error status") (assert (= (status athread) "error") "thread error status")
"All tests passed" "report"
(print "All" numTestsPassed "tests passed") (print num-tests-passed "of" num-tests-run "tests passed")
(exit! 0) (exit! 0)