]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua-thread: Add the "thread" core variable
authorThierry Fournier <thierry.fournier@ozon.io>
Sat, 28 Nov 2020 14:49:44 +0000 (15:49 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 2 Dec 2020 20:53:16 +0000 (21:53 +0100)
The goal is to allow execution of one main lua state per thread.

This commit introduces this variable in the core. Lua state initialized
by thread will have access to this variable, which reports the executing
thread. 0 indicates the shared thread. Programs which must be executed
only once can check for core.thread <= 1.

doc/lua-api/index.rst
src/hlua.c

index adeec112f18f4553fbb71783b93e9d8c7eccc613..b6b9809e823b5073a0aa63020858efe3030cba59 100644 (file)
@@ -201,6 +201,16 @@ Core class
   :see: :js:attr:`core.proxies`
   :see: :js:attr:`core.backends`
 
+.. js:attribute:: core.thread
+
+  **context**: task, action, sample-fetch, converter, applet
+
+  This variable contains the executing thread number starting at 1. 0 is a
+  special case for the common lua context. So, if thread is 0, Lua scope is
+  shared by all threads, otherwise the scope is dedicated to a single thread.
+  A program which needs to execute some parts exactly once regardless of the
+  number of threads can check that core.thread is 0 or 1.
+
 .. js:function:: core.log(loglevel, msg)
 
   **context**: body, init, task, action, sample-fetch, converter
index 34e7403cf89f796e65ae73562704d4dea09b92e9..6153ab76971d3cf13b964e201c779e71c586c544 100644 (file)
@@ -8397,11 +8397,11 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
        return ptr;
 }
 
-/* Ithis function can fail with an abort() due to an Lua critical error.
+/* This function can fail with an abort() due to a Lua critical error.
  * We are in the initialisation process of HAProxy, this abort() is
  * tolerated.
  */
-lua_State *hlua_init_state(void)
+lua_State *hlua_init_state(int thread_num)
 {
        int i;
        int idx;
@@ -8468,6 +8468,9 @@ lua_State *hlua_init_state(void)
        /* This table entry is the object "core" base. */
        lua_newtable(L);
 
+       /* set the thread id */
+       hlua_class_const_int(L, "thread", thread_num);
+
        /* Push the loglevel constants. */
        for (i = 0; i < NB_LOG_LEVELS; i++)
                hlua_class_const_int(L, log_levels[i], i);
@@ -8993,7 +8996,7 @@ lua_State *hlua_init_state(void)
 }
 
 void hlua_init(void) {
-       gL.T = hlua_init_state();
+       gL.T = hlua_init_state(0);
 }
 
 static void hlua_deinit()