WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
371 stars 28 forks source link

Feature Request:Allow threads to be yielded forcefully and externally. #109

Closed RadiantUwU closed 1 year ago

RadiantUwU commented 1 year ago

This could be done with:

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.

Trey2k commented 1 year ago

Added in #110