janitor work on 'lua_resume' and related code

This commit is contained in:
Roberto Ierusalimschy 2014-06-11 13:01:55 -03:00
parent 5784331768
commit d94bb6c273
1 changed files with 32 additions and 28 deletions

60
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.119 2014/06/10 18:51:21 roberto Exp roberto $ ** $Id: ldo.c,v 2.120 2014/06/10 19:18:50 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -33,6 +33,8 @@
#define errorstatus(s) ((s) > LUA_YIELD)
/* /*
** {====================================================== ** {======================================================
@ -419,8 +421,10 @@ void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
static void finishCcall (lua_State *L, int status) { static void finishCcall (lua_State *L, int status) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
int n; int n;
lua_assert(ci->u.c.k != NULL); /* must have a continuation */ /* must have a continuation and must be able to call it */
lua_assert(L->nny == 0); lua_assert(ci->u.c.k != NULL && L->nny == 0);
/* error status can only happen in a protected call */
lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
L->errfunc = ci->u.c.old_errfunc; L->errfunc = ci->u.c.old_errfunc;
@ -443,16 +447,15 @@ static void finishCcall (lua_State *L, int status) {
** previously interrupted coroutine until the stack is empty (or another ** previously interrupted coroutine until the stack is empty (or another
** interruption long-jumps out of the loop). If the coroutine is ** interruption long-jumps out of the loop). If the coroutine is
** recovering from an error, 'ud' points to the error status, which must ** recovering from an error, 'ud' points to the error status, which must
** be passed to the first (only the first) continuation (otherwise the ** be passed to the first continuation function (otherwise the default
** default status is LUA_YIELD). ** status is LUA_YIELD).
*/ */
static void unroll (lua_State *L, void *ud) { static void unroll (lua_State *L, void *ud) {
int status = (ud) ? *(int *)ud : LUA_YIELD; if (ud != NULL) /* error status? */
finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
while (L->ci != &L->base_ci) { /* something in the stack */ while (L->ci != &L->base_ci) { /* something in the stack */
if (!isLua(L->ci)) { /* C function? */ if (!isLua(L->ci)) /* C function? */
finishCcall(L, status); /* complete its execution */ finishCcall(L, LUA_YIELD); /* complete its execution */
status = LUA_YIELD; /* back to default status */
}
else { /* Lua function */ else { /* Lua function */
luaV_finishOp(L); /* finish interrupted instruction */ luaV_finishOp(L); /* finish interrupted instruction */
luaV_execute(L); /* execute down to higher C 'boundary' */ luaV_execute(L); /* execute down to higher C 'boundary' */
@ -511,7 +514,11 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
/* /*
** do the work for 'lua_resume' in protected mode ** Do the work for 'lua_resume' in protected mode. Most of the work
** depends on the status of the coroutine: initial state, suspended
** inside a hook, or regulary suspended (optionally with a continuation
** function), plus erroneous cases: non-suspended coroutine or dead
** coroutine.
*/ */
static void resume (lua_State *L, void *ud) { static void resume (lua_State *L, void *ud) {
int nCcalls = L->nCcalls; int nCcalls = L->nCcalls;
@ -529,12 +536,12 @@ static void resume (lua_State *L, void *ud) {
else if (L->status != LUA_YIELD) else if (L->status != LUA_YIELD)
resume_error(L, "cannot resume dead coroutine", firstArg); resume_error(L, "cannot resume dead coroutine", firstArg);
else { /* resuming from previous yield */ else { /* resuming from previous yield */
L->status = LUA_OK; L->status = LUA_OK; /* mark that it is running (again) */
ci->func = restorestack(L, ci->extra); ci->func = restorestack(L, ci->extra);
if (isLua(ci)) /* yielded inside a hook? */ if (isLua(ci)) /* yielded inside a hook? */
luaV_execute(L); /* just continue running Lua code */ luaV_execute(L); /* just continue running Lua code */
else { /* 'common' yield */ else { /* 'common' yield */
if (ci->u.c.k != NULL) { /* does it have a continuation? */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */
int n; int n;
lua_unlock(L); lua_unlock(L);
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
@ -544,7 +551,7 @@ static void resume (lua_State *L, void *ud) {
} }
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
} }
unroll(L, NULL); unroll(L, NULL); /* run continuation */
} }
lua_assert(nCcalls == L->nCcalls); lua_assert(nCcalls == L->nCcalls);
} }
@ -552,7 +559,7 @@ static void resume (lua_State *L, void *ud) {
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
int status; int status;
int oldnny = L->nny; /* save 'nny' */ int oldnny = L->nny; /* save "number of non-yieldable" calls */
lua_lock(L); lua_lock(L);
luai_userstateresume(L, nargs); luai_userstateresume(L, nargs);
L->nCcalls = (from) ? from->nCcalls + 1 : 1; L->nCcalls = (from) ? from->nCcalls + 1 : 1;
@ -561,20 +568,17 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
status = luaD_rawrunprotected(L, resume, L->top - nargs); status = luaD_rawrunprotected(L, resume, L->top - nargs);
if (status == -1) /* error calling 'lua_resume'? */ if (status == -1) /* error calling 'lua_resume'? */
status = LUA_ERRRUN; status = LUA_ERRRUN;
else { /* yield or error running coroutine */ else { /* continue running after recoverable errors */
while (status != LUA_OK && status != LUA_YIELD) { /* error? */ while (errorstatus(status) && recover(L, status)) {
if (recover(L, status)) { /* recover point? */ /* unroll continuation */
/* unroll continuation */ status = luaD_rawrunprotected(L, unroll, &status);
status = luaD_rawrunprotected(L, unroll, &status);
}
else { /* unrecoverable error */
L->status = cast_byte(status); /* mark thread as `dead' */
seterrorobj(L, status, L->top);
L->ci->top = L->top;
break; /* stop running it */
}
} }
lua_assert(status == L->status); if (errorstatus(status)) { /* unrecoverable error? */
L->status = cast_byte(status); /* mark thread as `dead' */
seterrorobj(L, status, L->top); /* push error message */
L->ci->top = L->top;
}
else lua_assert(status == L->status); /* normal end or yield */
} }
L->nny = oldnny; /* restore 'nny' */ L->nny = oldnny; /* restore 'nny' */
L->nCcalls--; L->nCcalls--;