lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Cloud,

I think there's no easy and efficient way to solve your problem in lua
5.1. And lua_getinfo() is pretty slow for you scenario. My suggestion
is that you can patch the lua core, add a few functions for your own
use.

In lua 5.2 it's not a problem, for everything is now yieldable.

Hu

On Mon, Aug 16, 2010 at 5:27 PM, 云风 <cloudwu@gmail.com> wrote:
> I want to execute some lua code and yield back when timeout .  So I
> use a debug hook like this :
>
> static void
> _hook_count(lua_State *L, lua_Debug *ar)
> {
>        lua_yield(L,0);
> }
>
> /*
>  1 function main
>  2 integer timeout
>  */
> static int
> _execute(lua_State *L)
> {
>  lua_sethook(L, _hook_count , LUA_MASKCOUNT , luaL_checkinteger(L,2));
>  lua_State *sL = lua_newthread(L);
>  lua_pushvalue(L,1);
>  lua_xmove(L, sL, 1);
>  lua_resume(sL , 0);
>  lua_sethook(L, NULL, 0, 0);
>
>  return 0;
> }
>
> But lua_yield in _hook_count is not safe. It may across
> metamethod/C-call boundary .
> So I want to detect it first . If it is in a metamethod/C-call, It
> wait for returning .
>
> static void _hook_count(lua_State *L, lua_Debug *ar);
>
> static void
> _hook_ret(lua_State *L, lua_Debug *ar)
> {
>        bool in_c = _checkstack(L,ar);
>        if (! in_c) {
>                lua_sethook(L, NULL , 0 , 0);
>                lua_yield(L,0);
>        }
> }
>
> static void
> _hook_count(lua_State *L, lua_Debug *ar)
> {
>        bool in_c = _checkstack(L,ar);
>        if (in_c) {
>                lua_sethook(L, _hook_ret , LUA_MASKRET , 0);
>        }
>        else {
>                lua_yield(L,0);
>        }
> }
>
> How can I implement  "_checkstack" ? I read the ldo.c :
>
> if (L->nCcalls > L->baseCcalls)
>    luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
>
> but I can't access nCcalls or baseCcalls field in L .
>
> Can I get this information by lua_getinfo ? or is there another way ?
>
> --
> http://blog.codingnow.com
>