lua-users home
lua-l archive

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


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