MINOR: hlua: simplify lua locking

The check on lua state==0 to know whether locking is required or not can
be performed in a locking wrapper to simplify things a bit and prevent
implementation errors.

Locking from hlua context should now be performed via hlua_lock(L) and
unlocking via hlua_unlock(L)
This commit is contained in:
Aurelien DARRAGON 2023-03-21 13:22:33 +01:00 committed by Christopher Faulet
parent fde199dddc
commit e36f803b71

View File

@ -152,7 +152,7 @@ lua_State *hlua_init_state(int thread_id);
/* This function takes the Lua global lock. Keep this function's visibility /* This function takes the Lua global lock. Keep this function's visibility
* global so that it can appear in stack dumps and performance profiles! * global so that it can appear in stack dumps and performance profiles!
*/ */
void lua_take_global_lock() static inline void lua_take_global_lock()
{ {
HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
} }
@ -162,16 +162,26 @@ static inline void lua_drop_global_lock()
HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
} }
/* lua lock helpers: only lock when required */
static inline void hlua_lock(struct hlua *hlua)
{
if (hlua->state_id == 0)
lua_take_global_lock();
}
static inline void hlua_unlock(struct hlua *hlua)
{
if (hlua->state_id == 0)
lua_drop_global_lock();
}
#define SET_SAFE_LJMP_L(__L, __HLUA) \ #define SET_SAFE_LJMP_L(__L, __HLUA) \
({ \ ({ \
int ret; \ int ret; \
if ((__HLUA)->state_id == 0) \ hlua_lock(__HLUA); \
lua_take_global_lock(); \
if (setjmp(safe_ljmp_env) != 0) { \ if (setjmp(safe_ljmp_env) != 0) { \
lua_atpanic(__L, hlua_panic_safe); \ lua_atpanic(__L, hlua_panic_safe); \
ret = 0; \ ret = 0; \
if ((__HLUA)->state_id == 0) \ hlua_unlock(__HLUA); \
lua_drop_global_lock(); \
} else { \ } else { \
lua_atpanic(__L, hlua_panic_ljmp); \ lua_atpanic(__L, hlua_panic_ljmp); \
ret = 1; \ ret = 1; \
@ -185,8 +195,7 @@ static inline void lua_drop_global_lock()
#define RESET_SAFE_LJMP_L(__L, __HLUA) \ #define RESET_SAFE_LJMP_L(__L, __HLUA) \
do { \ do { \
lua_atpanic(__L, hlua_panic_safe); \ lua_atpanic(__L, hlua_panic_safe); \
if ((__HLUA)->state_id == 0) \ hlua_unlock(__HLUA); \
lua_drop_global_lock(); \
} while(0) } while(0)
#define SET_SAFE_LJMP(__HLUA) \ #define SET_SAFE_LJMP(__HLUA) \
@ -1523,8 +1532,7 @@ static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
/* Lock the whole Lua execution. This lock must be before the /* Lock the whole Lua execution. This lock must be before the
* label "resume_execution". * label "resume_execution".
*/ */
if (lua->state_id == 0) hlua_lock(lua);
lua_take_global_lock();
resume_execution: resume_execution:
@ -1671,8 +1679,7 @@ resume_execution:
} }
/* This is the main exit point, remove the Lua lock. */ /* This is the main exit point, remove the Lua lock. */
if (lua->state_id == 0) hlua_unlock(lua);
lua_drop_global_lock();
return ret; return ret;
} }