class LuaCoroutine {
// ...
public:
void pause_execution();
private:
static void terminate_hook(lua_State*,lua_Debug*);
// ...
};
void LuaCoroutine::pause_execution() {
if (!done) lua_sethook(tState,terminate_hook,LUA_MASKCOUNT,1); // force it to run next time
}
void LuaCoroutine::terminate_hook(lua_State* tState,lua_Debug* dbg) {
lua_sethook(tState,nullptr,NULL,NULL); // remove hook
lua_yield(tState,0);
}
After this, pause_execution could be used with a watcher thread, that looks over the thread running Lua. If it hasn't yielded within x number of seconds, pause_execution() is called and the thread has to yield.
This could be done with:
After this, pause_execution could be used with a watcher thread, that looks over the thread running Lua. If it hasn't yielded within
x
number of seconds,pause_execution()
is called and the thread has to yield.