]>
git.proxmox.com Git - ceph.git/blob - ceph/src/lua/src/lcorolib.c
2 ** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $
4 ** See Copyright Notice in lua.h
21 static lua_State
*getco (lua_State
*L
) {
22 lua_State
*co
= lua_tothread(L
, 1);
23 luaL_argcheck(L
, co
, 1, "thread expected");
28 static int auxresume (lua_State
*L
, lua_State
*co
, int narg
) {
30 if (!lua_checkstack(co
, narg
)) {
31 lua_pushliteral(L
, "too many arguments to resume");
32 return -1; /* error flag */
34 if (lua_status(co
) == LUA_OK
&& lua_gettop(co
) == 0) {
35 lua_pushliteral(L
, "cannot resume dead coroutine");
36 return -1; /* error flag */
38 lua_xmove(L
, co
, narg
);
39 status
= lua_resume(co
, L
, narg
);
40 if (status
== LUA_OK
|| status
== LUA_YIELD
) {
41 int nres
= lua_gettop(co
);
42 if (!lua_checkstack(L
, nres
+ 1)) {
43 lua_pop(co
, nres
); /* remove results anyway */
44 lua_pushliteral(L
, "too many results to resume");
45 return -1; /* error flag */
47 lua_xmove(co
, L
, nres
); /* move yielded values */
51 lua_xmove(co
, L
, 1); /* move error message */
52 return -1; /* error flag */
57 static int luaB_coresume (lua_State
*L
) {
58 lua_State
*co
= getco(L
);
60 r
= auxresume(L
, co
, lua_gettop(L
) - 1);
62 lua_pushboolean(L
, 0);
64 return 2; /* return false + error message */
67 lua_pushboolean(L
, 1);
68 lua_insert(L
, -(r
+ 1));
69 return r
+ 1; /* return true + 'resume' returns */
74 static int luaB_auxwrap (lua_State
*L
) {
75 lua_State
*co
= lua_tothread(L
, lua_upvalueindex(1));
76 int r
= auxresume(L
, co
, lua_gettop(L
));
78 if (lua_isstring(L
, -1)) { /* error object is a string? */
79 luaL_where(L
, 1); /* add extra info */
83 return lua_error(L
); /* propagate error */
89 static int luaB_cocreate (lua_State
*L
) {
91 luaL_checktype(L
, 1, LUA_TFUNCTION
);
92 NL
= lua_newthread(L
);
93 lua_pushvalue(L
, 1); /* move function to top */
94 lua_xmove(L
, NL
, 1); /* move function from L to NL */
99 static int luaB_cowrap (lua_State
*L
) {
101 lua_pushcclosure(L
, luaB_auxwrap
, 1);
106 static int luaB_yield (lua_State
*L
) {
107 return lua_yield(L
, lua_gettop(L
));
111 static int luaB_costatus (lua_State
*L
) {
112 lua_State
*co
= getco(L
);
113 if (L
== co
) lua_pushliteral(L
, "running");
115 switch (lua_status(co
)) {
117 lua_pushliteral(L
, "suspended");
121 if (lua_getstack(co
, 0, &ar
) > 0) /* does it have frames? */
122 lua_pushliteral(L
, "normal"); /* it is running */
123 else if (lua_gettop(co
) == 0)
124 lua_pushliteral(L
, "dead");
126 lua_pushliteral(L
, "suspended"); /* initial state */
129 default: /* some error occurred */
130 lua_pushliteral(L
, "dead");
138 static int luaB_yieldable (lua_State
*L
) {
139 lua_pushboolean(L
, lua_isyieldable(L
));
144 static int luaB_corunning (lua_State
*L
) {
145 int ismain
= lua_pushthread(L
);
146 lua_pushboolean(L
, ismain
);
151 static const luaL_Reg co_funcs
[] = {
152 {"create", luaB_cocreate
},
153 {"resume", luaB_coresume
},
154 {"running", luaB_corunning
},
155 {"status", luaB_costatus
},
156 {"wrap", luaB_cowrap
},
157 {"yield", luaB_yield
},
158 {"isyieldable", luaB_yieldable
},
164 LUAMOD_API
int luaopen_coroutine (lua_State
*L
) {
165 luaL_newlib(L
, co_funcs
);