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)

(cherry picked from commit e36f803b71126507fcb6d5d69d507ceebea652c0)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 483e3529144a627202843e0ce8821074f3a2ca77)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit b38b0677e63893d2d7d003689f26e4340f28c8be)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit c144a34204b89f3c3cc9e07dab80d8f5d6c29af7)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/hlua.c b/src/hlua.c
index 434e8b3..707419f 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -146,7 +146,7 @@
 /* This function takes the Lua global lock. Keep this function's visibility
  * 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);
 }
@@ -156,16 +156,26 @@
 	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) \
 	({ \
 		int ret; \
-		if ((__HLUA)->state_id == 0) \
-			lua_take_global_lock(); \
+		hlua_lock(__HLUA); \
 		if (setjmp(safe_ljmp_env) != 0) { \
 			lua_atpanic(__L, hlua_panic_safe); \
 			ret = 0; \
-			if ((__HLUA)->state_id == 0) \
-				lua_drop_global_lock(); \
+			hlua_unlock(__HLUA); \
 		} else { \
 			lua_atpanic(__L, hlua_panic_ljmp); \
 			ret = 1; \
@@ -179,8 +189,7 @@
 #define RESET_SAFE_LJMP_L(__L, __HLUA) \
 	do { \
 		lua_atpanic(__L, hlua_panic_safe); \
-		if ((__HLUA)->state_id == 0) \
-			lua_drop_global_lock(); \
+		hlua_unlock(__HLUA); \
 	} while(0)
 
 #define SET_SAFE_LJMP(__HLUA) \
@@ -1337,8 +1346,7 @@
 	/* Lock the whole Lua execution. This lock must be before the
 	 * label "resume_execution".
 	 */
-	if (lua->state_id == 0)
-		lua_take_global_lock();
+	hlua_lock(lua);
 
 resume_execution:
 
@@ -1485,8 +1493,7 @@
 	}
 
 	/* This is the main exit point, remove the Lua lock. */
-	if (lua->state_id == 0)
-		lua_drop_global_lock();
+	hlua_unlock(lua);
 
 	return ret;
 }