summaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-09-15 17:39:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-09-15 17:39:42 -0300
commit2419f2bf02a9165471248f09bae57e3fa134e545 (patch)
tree9f7e387740892ee591218b8ea745d35a45aa6e41 /ldo.c
parent0e54d2be365ec77cb455e0d0f3c5c6f9efa6e04c (diff)
downloadlua-2419f2bf02a9165471248f09bae57e3fa134e545.tar.gz
lua-2419f2bf02a9165471248f09bae57e3fa134e545.tar.bz2
lua-2419f2bf02a9165471248f09bae57e3fa134e545.zip
cleaner API for coroutines
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c32
1 files changed, 14 insertions, 18 deletions
diff --git a/ldo.c b/ldo.c
index 19c57153..a016f71a 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 2.8 2004/09/03 15:48:56 roberto Exp roberto $ 2** $Id: ldo.c,v 2.9 2004/09/08 14:23:09 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -358,7 +358,7 @@ static void resume (lua_State *L, void *ud) {
358 StkId firstResult; 358 StkId firstResult;
359 int nargs = *cast(int *, ud); 359 int nargs = *cast(int *, ud);
360 CallInfo *ci = L->ci; 360 CallInfo *ci = L->ci;
361 if (!L->isSuspended) { 361 if (L->status != LUA_YIELD) {
362 lua_assert(ci == L->base_ci && nargs < L->top - L->base); 362 lua_assert(ci == L->base_ci && nargs < L->top - L->base);
363 luaD_precall(L, L->top - (nargs + 1), LUA_MULTRET); /* start coroutine */ 363 luaD_precall(L, L->top - (nargs + 1), LUA_MULTRET); /* start coroutine */
364 } 364 }
@@ -372,10 +372,11 @@ static void resume (lua_State *L, void *ud) {
372 if (nresults >= 0) L->top = L->ci->top; 372 if (nresults >= 0) L->top = L->ci->top;
373 } /* else yielded inside a hook: just continue its execution */ 373 } /* else yielded inside a hook: just continue its execution */
374 } 374 }
375 L->isSuspended = 0; 375 L->status = 0;
376 firstResult = luaV_execute(L, L->ci - L->base_ci); 376 firstResult = luaV_execute(L, L->ci - L->base_ci);
377 if (firstResult != NULL) /* return? */ 377 if (firstResult != NULL) { /* return? */
378 luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */ 378 luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
379 }
379} 380}
380 381
381 382
@@ -393,25 +394,20 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
393 lu_byte old_allowhooks; 394 lu_byte old_allowhooks;
394 lua_lock(L); 395 lua_lock(L);
395 lua_assert(L->errfunc == 0 && L->nCcalls == 0); 396 lua_assert(L->errfunc == 0 && L->nCcalls == 0);
396 if (!L->isSuspended) { 397 if (L->status != LUA_YIELD) {
397 if (L->ci == L->base_ci) { /* no activation record? */ 398 if (L->status != 0)
398 if (nargs >= L->top - L->base) 399 return resume_error(L, "cannot resume dead coroutine");
399 return resume_error(L, "cannot resume dead coroutine"); 400 else if (L->ci != L->base_ci)
400 }
401 else
402 return resume_error(L, "cannot resume non-suspended coroutine"); 401 return resume_error(L, "cannot resume non-suspended coroutine");
403 } 402 }
404 old_allowhooks = L->allowhook; 403 old_allowhooks = L->allowhook;
405 status = luaD_rawrunprotected(L, resume, &nargs); 404 status = luaD_rawrunprotected(L, resume, &nargs);
406 if (status != 0) { /* error? */ 405 if (status != 0) { /* error? */
407 L->ci = L->base_ci; /* go back to initial level */ 406 L->status = status; /* mark thread as `dead' */
408 L->base = L->ci->base; 407 seterrorobj(L, status, L->top);
409 L->nCcalls = 0;
410 luaF_close(L, L->base); /* close eventual pending closures */
411 seterrorobj(L, status, L->base);
412 L->allowhook = old_allowhooks;
413 restore_stack_limit(L);
414 } 408 }
409 else
410 status = L->status;
415 lua_unlock(L); 411 lua_unlock(L);
416 return status; 412 return status;
417} 413}
@@ -431,7 +427,7 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
431 L->top = L->base + nresults; 427 L->top = L->base + nresults;
432 } 428 }
433 } /* else it's an yield inside a hook: nothing to do */ 429 } /* else it's an yield inside a hook: nothing to do */
434 L->isSuspended = 1; 430 L->status = LUA_YIELD;
435 lua_unlock(L); 431 lua_unlock(L);
436 return -1; 432 return -1;
437} 433}