mruby-fiber: fiber_switch() to use nesting VM when it's called from C API or...

mruby-fiber: fiber_switch() to use nesting VM when it's called from C API or mrb_funcall(); close #3056
parent 4683b89b
......@@ -87,7 +87,7 @@ typedef struct {
enum mrb_fiber_state {
MRB_FIBER_CREATED = 0,
MRB_FIBER_RUNNING,
MRB_FIBER_RESUMING,
MRB_FIBER_RESUMED,
MRB_FIBER_SUSPENDED,
MRB_FIBER_TRANSFERRED,
MRB_FIBER_TERMINATED,
......@@ -108,6 +108,7 @@ struct mrb_context {
int esize;
enum mrb_fiber_state status;
mrb_bool vmexec;
struct RFiber *fib;
};
......@@ -934,6 +935,7 @@ MRB_API mrb_value mrb_top_self(mrb_state *);
MRB_API mrb_value mrb_run(mrb_state*, struct RProc*, mrb_value);
MRB_API mrb_value mrb_toplevel_run(mrb_state*, struct RProc*);
MRB_API mrb_value mrb_context_run(mrb_state*, struct RProc*, mrb_value, unsigned int);
MRB_API mrb_value mrb_vm_exec(mrb_state*, struct RProc*, mrb_code*);
MRB_API void mrb_p(mrb_state*, mrb_value);
MRB_API mrb_int mrb_obj_id(mrb_value obj);
......
......@@ -7,6 +7,7 @@
#define FIBER_STACK_INIT_SIZE 64
#define FIBER_CI_INIT_SIZE 8
#define CI_ACC_RESUMED -3
/*
* call-seq:
......@@ -155,10 +156,9 @@ fiber_result(mrb_state *mrb, const mrb_value *a, mrb_int len)
/* mark return from context modifying method */
#define MARK_CONTEXT_MODIFY(c) (c)->ci->target_class = NULL
static mrb_value
fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mrb_bool resume)
static void
fiber_check_cfunc(mrb_state *mrb, struct mrb_context *c)
{
struct mrb_context *c = fiber_check(mrb, self);
mrb_callinfo *ci;
for (ci = c->ci; ci >= c->cibase; ci--) {
......@@ -166,16 +166,26 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
mrb_raise(mrb, E_FIBER_ERROR, "can't cross C function boundary");
}
}
}
static mrb_value
fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mrb_bool resume, mrb_bool vmexec)
{
struct mrb_context *c = fiber_check(mrb, self);
struct mrb_context *old_c = mrb->c;
mrb_value value;
fiber_check_cfunc(mrb, c);
if (resume && c->status == MRB_FIBER_TRANSFERRED) {
mrb_raise(mrb, E_FIBER_ERROR, "resuming transferred fiber");
}
if (c->status == MRB_FIBER_RUNNING || c->status == MRB_FIBER_RESUMING) {
mrb_raise(mrb, E_FIBER_ERROR, "double resume");
if (c->status == MRB_FIBER_RUNNING || c->status == MRB_FIBER_RESUMED) {
mrb_raise(mrb, E_FIBER_ERROR, "double resume (fib)");
}
if (c->status == MRB_FIBER_TERMINATED) {
mrb_raise(mrb, E_FIBER_ERROR, "resuming dead fiber");
}
mrb->c->status = resume ? MRB_FIBER_RESUMING : MRB_FIBER_TRANSFERRED;
mrb->c->status = resume ? MRB_FIBER_RESUMED : MRB_FIBER_TRANSFERRED;
c->prev = resume ? mrb->c : (c->prev ? c->prev : mrb->root_c);
if (c->status == MRB_FIBER_CREATED) {
mrb_value *b = c->stack+1;
......@@ -185,22 +195,24 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
*b++ = *a++;
}
c->cibase->argc = len;
if (c->prev->fib)
mrb_field_write_barrier(mrb, (struct RBasic*)c->fib, (struct RBasic*)c->prev->fib);
mrb_write_barrier(mrb, (struct RBasic*)c->fib);
c->status = MRB_FIBER_RUNNING;
mrb->c = c;
MARK_CONTEXT_MODIFY(c);
return c->ci->proc->env->stack[0];
value = c->stack[0] = c->ci->proc->env->stack[0];
}
else {
value = fiber_result(mrb, a, len);
}
MARK_CONTEXT_MODIFY(c);
if (c->prev->fib)
mrb_field_write_barrier(mrb, (struct RBasic*)c->fib, (struct RBasic*)c->prev->fib);
mrb_write_barrier(mrb, (struct RBasic*)c->fib);
c->status = MRB_FIBER_RUNNING;
mrb->c = c;
return fiber_result(mrb, a, len);
if (vmexec) {
c->vmexec = TRUE;
value = mrb_vm_exec(mrb, c->ci[-1].proc, c->ci->pc);
mrb->c = old_c;
}
else {
MARK_CONTEXT_MODIFY(c);
}
return value;
}
/*
......@@ -223,16 +235,20 @@ fiber_resume(mrb_state *mrb, mrb_value self)
{
mrb_value *a;
mrb_int len;
mrb_bool vmexec = FALSE;
mrb_get_args(mrb, "*", &a, &len);
return fiber_switch(mrb, self, len, a, TRUE);
if (mrb->c->ci->acc < 0) {
vmexec = TRUE;
}
return fiber_switch(mrb, self, len, a, TRUE, vmexec);
}
/* resume thread with given arguments */
MRB_API mrb_value
mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int len, const mrb_value *a)
{
return fiber_switch(mrb, fib, len, a, TRUE);
return fiber_switch(mrb, fib, len, a, TRUE, TRUE);
}
/*
......@@ -280,6 +296,7 @@ fiber_transfer(mrb_state *mrb, mrb_value self)
mrb_value* a;
mrb_int len;
fiber_check_cfunc(mrb, mrb->c);
mrb_get_args(mrb, "*", &a, &len);
if (c == mrb->root_c) {
......@@ -295,7 +312,7 @@ fiber_transfer(mrb_state *mrb, mrb_value self)
return fiber_result(mrb, a, len);
}
return fiber_switch(mrb, self, len, a, FALSE);
return fiber_switch(mrb, self, len, a, FALSE, FALSE);
}
/* yield values to the caller fiber */
......@@ -304,13 +321,7 @@ MRB_API mrb_value
mrb_fiber_yield(mrb_state *mrb, mrb_int len, const mrb_value *a)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci;
for (ci = c->ci; ci >= c->cibase; ci--) {
if (ci->acc < 0) {
mrb_raise(mrb, E_FIBER_ERROR, "can't cross C function boundary");
}
}
if (!c->prev) {
mrb_raise(mrb, E_FIBER_ERROR, "can't yield from root fiber");
}
......@@ -319,8 +330,12 @@ mrb_fiber_yield(mrb_state *mrb, mrb_int len, const mrb_value *a)
c->status = MRB_FIBER_SUSPENDED;
mrb->c = c->prev;
c->prev = NULL;
MARK_CONTEXT_MODIFY(mrb->c);
if (c->vmexec) {
c->vmexec = FALSE;
mrb->c->ci->acc = CI_ACC_RESUMED;
}
mrb_write_barrier(mrb, (struct RBasic*)c->fib);
MARK_CONTEXT_MODIFY(mrb->c);
return fiber_result(mrb, a, len);
}
......
......@@ -209,6 +209,7 @@ top_env(mrb_state *mrb, struct RProc *proc)
#define CI_ACC_SKIP -1
#define CI_ACC_DIRECT -2
#define CI_ACC_RESUMED -3
static mrb_callinfo*
cipush(mrb_state *mrb)
......@@ -735,10 +736,22 @@ void mrb_method_missing(mrb_state *mrb, mrb_sym name, mrb_value self, mrb_value
MRB_API mrb_value
mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep)
{
mrb_irep *irep = proc->body.irep;
if (!mrb->c->stack) {
stack_init(mrb);
}
stack_extend(mrb, irep->nregs, stack_keep);
mrb->c->stack[0] = self;
return mrb_vm_exec(mrb, proc, irep->iseq);
}
MRB_API mrb_value
mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
{
/* mrb_assert(mrb_proc_cfunc_p(proc)) */
mrb_irep *irep = proc->body.irep;
mrb_code *pc = irep->iseq;
mrb_value *pool = irep->pool;
mrb_sym *syms = irep->syms;
mrb_value *regs = NULL;
......@@ -782,14 +795,9 @@ RETRY_TRY_BLOCK:
goto L_RAISE;
}
mrb->jmp = &c_jmp;
if (!mrb->c->stack) {
stack_init(mrb);
}
stack_extend(mrb, irep->nregs, stack_keep);
mrb->c->ci->proc = proc;
mrb->c->ci->nregs = irep->nregs;
regs = mrb->c->stack;
regs[0] = self;
INIT_DISPATCH {
CASE(OP_NOP) {
......@@ -1131,19 +1139,24 @@ RETRY_TRY_BLOCK:
ci->nregs = n + 2;
}
result = m->body.func(mrb, recv);
mrb->c->stack[0] = result;
mrb_gc_arena_restore(mrb, ai);
if (mrb->exc) goto L_RAISE;
/* pop stackpos */
ci = mrb->c->ci;
if (!ci->target_class) { /* return from context modifying method (resume/yield) */
if (!MRB_PROC_CFUNC_P(ci[-1].proc)) {
if (ci->acc == CI_ACC_RESUMED) {
mrb->jmp = prev_jmp;
return result;
}
else {
mrb_assert(!MRB_PROC_CFUNC_P(ci[-1].proc));
proc = ci[-1].proc;
irep = proc->body.irep;
pool = irep->pool;
syms = irep->syms;
}
}
mrb->c->stack[0] = result;
regs = mrb->c->stack = ci->stackent;
pc = ci->pc;
cipop(mrb);
......@@ -1608,14 +1621,19 @@ RETRY_TRY_BLOCK:
while (eidx > mrb->c->ci[-1].eidx) {
ecall(mrb, --eidx);
}
if (mrb->c->vmexec && !mrb->c->ci->target_class) {
mrb->c->vmexec = FALSE;
mrb->jmp = prev_jmp;
return v;
}
cipop(mrb);
acc = ci->acc;
pc = ci->pc;
regs = mrb->c->stack = ci->stackent;
if (acc == CI_ACC_SKIP) {
mrb->jmp = prev_jmp;
return v;
}
pc = ci->pc;
DEBUG(printf("from :%s\n", mrb_sym2name(mrb, ci->mid)));
proc = mrb->c->ci->proc;
irep = proc->body.irep;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment