diff --git a/include/haproxy/hlua.h b/include/haproxy/hlua.h index 18fad9ff2..122e962d0 100644 --- a/include/haproxy/hlua.h +++ b/include/haproxy/hlua.h @@ -50,6 +50,7 @@ #define HLUA_INIT(__hlua) do { (__hlua)->T = 0; } while(0) /* Lua HAProxy integration functions. */ +void hlua_yield_asap(lua_State *L); const char *hlua_traceback(lua_State *L, const char* sep); void hlua_ctx_destroy(struct hlua *lua); void hlua_init(); diff --git a/src/hlua.c b/src/hlua.c index b15e416f9..767406a43 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -1979,6 +1979,20 @@ void hlua_hook(lua_State *L, lua_Debug *ar) lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_get_nb_instruction(hlua)); } +/* tries to yield as soon as possible if the context permits it */ +void hlua_yield_asap(lua_State *L) +{ + struct hlua *hlua; + + /* Get hlua struct, or NULL if we execute from main lua state */ + hlua = hlua_gethlua(L); + if (!hlua) + return; + + /* enforce hook for current hlua ctx on the next Lua instruction */ + lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); +} + /* This function start or resumes the Lua stack execution. If the flag * "yield_allowed" if no set and the LUA stack execution returns a yield * The function return an error.