23 luaL_argexpected(L, co, 1,
"thread");
34 if (l_unlikely(!lua_checkstack(co, narg))) {
35 lua_pushliteral(L,
"too many arguments to resume");
38 lua_xmove(L, co, narg);
39 status = lua_resume(co, L, narg, &nres);
40 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
41 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
43 lua_pushliteral(L,
"too many results to resume");
46 lua_xmove(co, L, nres);
59 r = auxresume(L, co, lua_gettop(L) - 1);
60 if (l_unlikely(r < 0)) {
61 lua_pushboolean(L, 0);
66 lua_pushboolean(L, 1);
67 lua_insert(L, -(r + 1));
74 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
75 int r = auxresume(L, co, lua_gettop(L));
76 if (l_unlikely(r < 0)) {
77 int stat = lua_status(co);
78 if (stat != LUA_OK && stat != LUA_YIELD) {
79 stat = lua_closethread(co, L);
80 lua_assert(stat != LUA_OK);
83 if (stat != LUA_ERRMEM &&
84 lua_type(L, -1) == LUA_TSTRING) {
97 luaL_checktype(L, 1, LUA_TFUNCTION);
98 NL = lua_newthread(L);
107 lua_pushcclosure(L, luaB_auxwrap, 1);
113 return lua_yield(L, lua_gettop(L));
123static const char *
const statname[] =
124 {
"running",
"dead",
"suspended",
"normal"};
128 if (L == co)
return COS_RUN;
130 switch (lua_status(co)) {
135 if (lua_getstack(co, 0, &ar))
137 else if (lua_gettop(co) == 0)
151 lua_pushstring(L, statname[auxstatus(L, co)]);
156static int luaB_yieldable (
lua_State *L) {
157 lua_State *co = lua_isnone(L, 1) ? L : getco(L);
158 lua_pushboolean(L, lua_isyieldable(co));
163static int luaB_corunning (
lua_State *L) {
164 int ismain = lua_pushthread(L);
165 lua_pushboolean(L, ismain);
172 int status = auxstatus(L, co);
174 case COS_DEAD:
case COS_YIELD: {
175 status = lua_closethread(co, L);
176 if (status == LUA_OK) {
177 lua_pushboolean(L, 1);
181 lua_pushboolean(L, 0);
187 return luaL_error(L,
"cannot close a %s coroutine", statname[status]);
193 {
"create", luaB_cocreate},
194 {
"resume", luaB_coresume},
195 {
"running", luaB_corunning},
196 {
"status", luaB_costatus},
197 {
"wrap", luaB_cowrap},
198 {
"yield", luaB_yield},
199 {
"isyieldable", luaB_yieldable},
200 {
"close", luaB_close},
206LUAMOD_API
int luaopen_coroutine (
lua_State *L) {
207 luaL_newlib(L, co_funcs);