lua-users home
lua-l archive

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


> You can yield from a C hook (lua_sethook), but not from a Lua
> hook (debug.sethook) which sits on top of a C frame. That's the
> same behavior as plain Lua.

Can lua_sethook be called on a coroutine?  Will it interrupt only the coroutine
or also the main "thread/coroutine"? 

Here is an example:

Should lua_sethook call "hook" on l thread or on both l and j threads?

void hook(lua_State* l, lua_Debug* ar) {
    //printf("hi\n");
    lua_yield(l, 0);
}

int main(int argc, char** argv) {
    lua_State* superstate, *l, *j;
    int lstatus, jstatus;

    superstate = luaL_newstate();
    luaL_openlibs(superstate);

    l = lua_newthread(superstate);
    j = lua_newthread(superstate);

    lua_sethook(l, hook, LUA_MASKLINE, 0);

    luaL_loadstring(l, "while true do print('a') end");
    luaL_loadstring(j, "while true do print('b') end");

    lstatus = jstatus = LUA_YIELD;
    while (1) {
        if (lstatus == LUA_YIELD) {
            lstatus = lua_resume(l, 0);
        }

        if (jstatus == LUA_YIELD) {
            jstatus = lua_resume(j, 0);
        }
    }
}