]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: add hlua_yield_asap() helper
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 30 Apr 2025 14:37:56 +0000 (16:37 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Wed, 30 Apr 2025 15:00:27 +0000 (17:00 +0200)
When called, this function will try to enforce a yield (if available) as
soon as possible. Indeed, automatic yield is already enforced every X
Lua instructions. However, there may be some cases where we know after
running heavy operation that we should yield already to avoid taking too
much CPU at once.

This is what this function offers, instead of asking the user to manually
yield using "core.yield()" from Lua itself after using an expensive
Lua method offered by haproxy, we can directly enforce the yield without
the need to do it in the Lua script.

include/haproxy/hlua.h
src/hlua.c

index 18fad9ff2c030a3776a0f3bd59df0329934277fe..122e962d063ed2b9fa66a58797e588fa491a62b2 100644 (file)
@@ -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();
index b15e416f938882c39be69a690111030196900203..767406a43b53e7394e7de465903abb00d54f794c 100644 (file)
@@ -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.