From: Thierry Fournier Date: Sat, 28 Nov 2020 11:26:24 +0000 (+0100) Subject: MINOR: lua-thread: Split hlua_init() function in two parts X-Git-Tag: v2.4-dev3~117 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1eac28f5fc3b5ed293824d5bea04fd9b2723ac37;p=thirdparty%2Fhaproxy.git MINOR: lua-thread: Split hlua_init() function in two parts The goal is to allow execution of one main lua state per thread. This is a preparative work in order to init more than one stack in the lua-thread objective. --- diff --git a/src/hlua.c b/src/hlua.c index c9c211743b..2c70a69bb3 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -8380,7 +8380,7 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) * We are in the initialisation process of HAProxy, this abort() is * tolerated. */ -void hlua_init(void) +lua_State *hlua_init_state(void) { int i; int idx; @@ -8389,6 +8389,7 @@ void hlua_init(void) char *p; const char *error_msg; void **context; + lua_State *L; #ifdef USE_OPENSSL struct srv_kw *kw; int tmp_error; @@ -8402,10 +8403,10 @@ void hlua_init(void) #endif /* Init main lua stack. */ - gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); + L = lua_newstate(hlua_alloc, &hlua_global_allocator); /* Initialise Lua context to NULL */ - context = lua_getextraspace(gL.T); + context = lua_getextraspace(L); *context = NULL; /* From this point, until the end of the initialisation function, @@ -8414,24 +8415,25 @@ void hlua_init(void) */ /* Set safe environment for the initialisation. */ - if (!SET_SAFE_LJMP(gL.T)) { - if (lua_type(gL.T, -1) == LUA_TSTRING) - error_msg = lua_tostring(gL.T, -1); + if (!SET_SAFE_LJMP(L)) { + if (lua_type(L, -1) == LUA_TSTRING) + error_msg = lua_tostring(L, -1); else error_msg = "critical error"; fprintf(stderr, "Lua init: %s.\n", error_msg); exit(1); } + /* Initialise lua. */ - luaL_openlibs(gL.T); + luaL_openlibs(L); #define HLUA_PREPEND_PATH_TOSTRING1(x) #x #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) #ifdef HLUA_PREPEND_PATH - hlua_prepend_path(gL.T, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); + hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); #endif #ifdef HLUA_PREPEND_CPATH - hlua_prepend_path(gL.T, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); + hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); #endif #undef HLUA_PREPEND_PATH_TOSTRING #undef HLUA_PREPEND_PATH_TOSTRING1 @@ -8443,38 +8445,38 @@ void hlua_init(void) */ /* This table entry is the object "core" base. */ - lua_newtable(gL.T); + lua_newtable(L); /* Push the loglevel constants. */ for (i = 0; i < NB_LOG_LEVELS; i++) - hlua_class_const_int(gL.T, log_levels[i], i); + hlua_class_const_int(L, log_levels[i], i); /* Register special functions. */ - hlua_class_function(gL.T, "register_init", hlua_register_init); - hlua_class_function(gL.T, "register_task", hlua_register_task); - hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); - hlua_class_function(gL.T, "register_converters", hlua_register_converters); - hlua_class_function(gL.T, "register_action", hlua_register_action); - hlua_class_function(gL.T, "register_service", hlua_register_service); - hlua_class_function(gL.T, "register_cli", hlua_register_cli); - hlua_class_function(gL.T, "yield", hlua_yield); - hlua_class_function(gL.T, "set_nice", hlua_set_nice); - hlua_class_function(gL.T, "sleep", hlua_sleep); - hlua_class_function(gL.T, "msleep", hlua_msleep); - hlua_class_function(gL.T, "add_acl", hlua_add_acl); - hlua_class_function(gL.T, "del_acl", hlua_del_acl); - hlua_class_function(gL.T, "set_map", hlua_set_map); - hlua_class_function(gL.T, "del_map", hlua_del_map); - hlua_class_function(gL.T, "tcp", hlua_socket_new); - hlua_class_function(gL.T, "log", hlua_log); - hlua_class_function(gL.T, "Debug", hlua_log_debug); - hlua_class_function(gL.T, "Info", hlua_log_info); - hlua_class_function(gL.T, "Warning", hlua_log_warning); - hlua_class_function(gL.T, "Alert", hlua_log_alert); - hlua_class_function(gL.T, "done", hlua_done); - hlua_fcn_reg_core_fcn(gL.T); - - lua_setglobal(gL.T, "core"); + hlua_class_function(L, "register_init", hlua_register_init); + hlua_class_function(L, "register_task", hlua_register_task); + hlua_class_function(L, "register_fetches", hlua_register_fetches); + hlua_class_function(L, "register_converters", hlua_register_converters); + hlua_class_function(L, "register_action", hlua_register_action); + hlua_class_function(L, "register_service", hlua_register_service); + hlua_class_function(L, "register_cli", hlua_register_cli); + hlua_class_function(L, "yield", hlua_yield); + hlua_class_function(L, "set_nice", hlua_set_nice); + hlua_class_function(L, "sleep", hlua_sleep); + hlua_class_function(L, "msleep", hlua_msleep); + hlua_class_function(L, "add_acl", hlua_add_acl); + hlua_class_function(L, "del_acl", hlua_del_acl); + hlua_class_function(L, "set_map", hlua_set_map); + hlua_class_function(L, "del_map", hlua_del_map); + hlua_class_function(L, "tcp", hlua_socket_new); + hlua_class_function(L, "log", hlua_log); + hlua_class_function(L, "Debug", hlua_log_debug); + hlua_class_function(L, "Info", hlua_log_info); + hlua_class_function(L, "Warning", hlua_log_warning); + hlua_class_function(L, "Alert", hlua_log_alert); + hlua_class_function(L, "done", hlua_done); + hlua_fcn_reg_core_fcn(L); + + lua_setglobal(L, "core"); /* * @@ -8483,21 +8485,21 @@ void hlua_init(void) */ /* This table entry is the object "act" base. */ - lua_newtable(gL.T); + lua_newtable(L); /* push action return constants */ - hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); - hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); - hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); - hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); - hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); - hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); - hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); - hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); + hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); + hlua_class_const_int(L, "STOP", ACT_RET_STOP); + hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); + hlua_class_const_int(L, "ERROR", ACT_RET_ERR); + hlua_class_const_int(L, "DONE", ACT_RET_DONE); + hlua_class_const_int(L, "DENY", ACT_RET_DENY); + hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); + hlua_class_const_int(L, "INVALID", ACT_RET_INV); - hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); + hlua_class_function(L, "wake_time", hlua_set_wake_time); - lua_setglobal(gL.T, "act"); + lua_setglobal(L, "act"); /* * @@ -8506,44 +8508,44 @@ void hlua_init(void) */ /* This table entry is the object "Map" base. */ - lua_newtable(gL.T); + lua_newtable(L); /* register pattern types. */ for (i=0; i