MINOR: lua: use bitfield and macro in place of integer and enum
In the future, the lua execution must return scheduling informations.
We want more than one flag, so I convert an integer used with an
enum into an interer used as bitfield.
diff --git a/include/proto/hlua.h b/include/proto/hlua.h
index b9530aa..1428673 100644
--- a/include/proto/hlua.h
+++ b/include/proto/hlua.h
@@ -7,6 +7,11 @@
#include <types/hlua.h>
+/* The following macros are used to set flags. */
+#define HLUA_SET_RUN(__hlua) do {(__hlua)->flags |= HLUA_RUN;} while(0)
+#define HLUA_CLR_RUN(__hlua) do {(__hlua)->flags &= ~HLUA_RUN;} while(0)
+#define HLUA_IS_RUNNING(__hlua) ((__hlua)->flags & HLUA_RUN)
+
#define HLUA_INIT(__hlua) do { (__hlua)->T = 0; } while(0)
/* Lua HAProxy integration functions. */
@@ -16,6 +21,8 @@
#else /* USE_LUA */
+#define HLUA_IS_RUNNING(__hlua) 0
+
#define HLUA_INIT(__hlua)
/* Empty function for compilation without Lua. */
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 2257d5c..5920e03 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -16,10 +16,7 @@
struct session;
-enum hlua_state {
- HLUA_STOP = 0,
- HLUA_RUN,
-};
+#define HLUA_RUN 0x00000001
enum hlua_exec {
HLUA_E_OK = 0,
@@ -37,7 +34,7 @@
int Mref; /* The reference of the memory context in coroutine case.
-1 if the memory context is not used. */
int nargs; /* The number of arguments in the stack at the start of execution. */
- enum hlua_state state; /* The current execution state. */
+ unsigned int flags; /* The current execution flags. */
struct task *task; /* The task associated with the lua stack execution.
We must wake this task to continue the task execution */
struct list com; /* The list head of the signals attached to this task. */
diff --git a/src/hlua.c b/src/hlua.c
index 83c31a7..724d5a1 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -20,6 +20,7 @@
#include <proto/arg.h>
#include <proto/channel.h>
#include <proto/hdr_idx.h>
+#include <proto/hlua.h>
#include <proto/obj_type.h>
#include <proto/pattern.h>
#include <proto/payload.h>
@@ -507,7 +508,7 @@
int hlua_ctx_init(struct hlua *lua, struct task *task)
{
lua->Mref = LUA_REFNIL;
- lua->state = HLUA_STOP;
+ lua->flags = 0;
LIST_INIT(&lua->com);
lua->T = lua_newthread(gL.T);
if (!lua->T) {
@@ -610,7 +611,7 @@
int ret;
const char *msg;
- lua->state = HLUA_RUN;
+ HLUA_SET_RUN(lua);
/* Call the function. */
ret = lua_resume(lua->T, gL.T, lua->nargs);
@@ -692,17 +693,17 @@
case HLUA_E_ERRMSG:
hlua_com_purge(lua);
hlua_ctx_renew(lua, 1);
- lua->state = HLUA_STOP;
+ HLUA_CLR_RUN(lua);
break;
case HLUA_E_ERR:
- lua->state = HLUA_STOP;
+ HLUA_CLR_RUN(lua);
hlua_com_purge(lua);
hlua_ctx_renew(lua, 0);
break;
case HLUA_E_OK:
- lua->state = HLUA_STOP;
+ HLUA_CLR_RUN(lua);
hlua_com_purge(lua);
break;
}
@@ -1649,7 +1650,7 @@
socket->s->hlua.Tref = LUA_REFNIL;
socket->s->hlua.Mref = LUA_REFNIL;
socket->s->hlua.nargs = 0;
- socket->s->hlua.state = HLUA_STOP;
+ socket->s->hlua.flags = 0;
LIST_INIT(&socket->s->hlua.com);
/* session initialisation. */
@@ -2731,7 +2732,7 @@
}
/* If it is the first run, initialize the data for the call. */
- if (session->hlua.state == HLUA_STOP) {
+ if (!HLUA_IS_RUNNING(&session->hlua)) {
/* Check stack available size. */
if (!lua_checkstack(session->hlua.T, 1)) {
send_log(session->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
@@ -2768,7 +2769,7 @@
}
/* Set the currently running flag. */
- session->hlua.state = HLUA_RUN;
+ HLUA_SET_RUN(&session->hlua);
}
/* Execute the function. */
@@ -2830,7 +2831,7 @@
}
/* If it is the first run, initialize the data for the call. */
- if (s->hlua.state == HLUA_STOP) {
+ if (!HLUA_IS_RUNNING(&s->hlua)) {
/* Check stack available size. */
if (!lua_checkstack(s->hlua.T, 2)) {
send_log(px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
@@ -2871,7 +2872,7 @@
}
/* Set the currently running flag. */
- s->hlua.state = HLUA_RUN;
+ HLUA_SET_RUN(&s->hlua);
}
/* Execute the function. */
@@ -3109,7 +3110,7 @@
}
/* If it is the first run, initialize the data for the call. */
- if (s->hlua.state == HLUA_STOP) {
+ if (!HLUA_IS_RUNNING(&s->hlua)) {
/* Check stack available size. */
if (!lua_checkstack(s->hlua.T, 1)) {
send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
@@ -3143,7 +3144,7 @@
}
/* Set the currently running flag. */
- s->hlua.state = HLUA_RUN;
+ HLUA_SET_RUN(&s->hlua);
}
/* Execute the function. */
@@ -3409,7 +3410,7 @@
/* Init main lua stack. */
gL.Mref = LUA_REFNIL;
- gL.state = HLUA_STOP;
+ gL.flags = 0;
LIST_INIT(&gL.com);
gL.T = luaL_newstate();
hlua_sethlua(&gL);