M hlog/common/config.c +2 -2
@@ 136,7 136,7 @@ int load_config(int data_dir_fd, struct
return PTR_ERR(lua);
}
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
switch (lua_type(L, -1)) {
case LUA_TTABLE:
@@ 159,7 159,7 @@ int load_config(int data_dir_fd, struct
ret = 0;
err_lua:
- xlua_put_and_close(lua, L);
+ xlua_close_locked(lua, L);
if (ret)
fprintf(stderr, "Error: failed to load config: %s\n",
M hlog/contest/script.c +12 -12
@@ 333,7 333,7 @@ struct xlua_state *script_load(struct xf
return lua;
}
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
if (lua_type(L, -1) != LUA_TTABLE) {
fprintf(stderr, "Error: contest script init failed: "
@@ 385,12 385,12 @@ struct xlua_state *script_load(struct xf
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return lua;
err:
- xlua_put_and_close(lua, L);
+ xlua_close_locked(lua, L);
return ERR_PTR(ret);
}
@@ 456,13 456,13 @@ int script_event_export(struct xlua_stat
int type;
int ret;
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
type = lua_getfield(L, LUA_REGISTRYINDEX, EVENT_EXPORT);
if (type == LUA_TNIL) {
lua_pop(L, 1);
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
snprintf(error, errlen, "Contest doesn't define an export "
"function");
return -ENOTSUP;
@@ 474,7 474,7 @@ int script_event_export(struct xlua_stat
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return ret;
}
@@ 492,13 492,13 @@ int script_event_field_change(struct xlu
int type;
int ret;
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
type = lua_getfield(L, LUA_REGISTRYINDEX, EVENT_FIELD_CHANGED);
if (type == LUA_TNIL) {
lua_pop(L, 1);
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return 0;
}
@@ 545,7 545,7 @@ int script_event_field_change(struct xlu
out:
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return ret;
}
@@ 557,13 557,13 @@ static int event_qso(struct xlua_state *
int type;
int ret;
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
type = lua_getfield(L, LUA_REGISTRYINDEX, event);
if (type == LUA_TNIL) {
lua_pop(L, 1);
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return 0;
}
@@ 571,7 571,7 @@ static int event_qso(struct xlua_state *
ASSERT_LUA_STACK(L, 0);
- xlua_put_state(lua, L);
+ xlua_unlock(lua, L);
return ret;
}
M hlog/hlog.c +2 -2
@@ 1993,7 1993,7 @@ static void print_stats(void)
return;
}
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
/* iterate over all the QSOs */
for (qso = index_iter_history_start(&ctx, true), failed = false;
@@ 2015,7 2015,7 @@ static void print_stats(void)
if (ret)
printf("Error: %s\n", error);
- xlua_put_and_close(lua, L);
+ xlua_close_locked(lua, L);
}
static void state_free(void)
M run-script/run-script.c +2 -2
@@ 48,13 48,13 @@ static void process(const char *scriptna
return;
}
- L = xlua_get_state(lua);
+ L = xlua_lock(lua);
ret = xlua_call(L, "main", 0, 0, error, sizeof(error));
if (ret)
printf("Error: failed to execute lua: %s\n", error);
- xlua_put_and_close(lua, L);
+ xlua_close_locked(lua, L);
}
static void usage(const char *prog)
M xlua/include/hlog-lua/xlua.h +4 -3
@@ 59,8 59,8 @@ extern struct xlua_state *xlua_init_scri
size_t errlen);
extern void xlua_close(struct xlua_state *lua);
-extern lua_State *xlua_get_state(struct xlua_state *lua);
-extern void xlua_put_state(struct xlua_state *lua, lua_State *L);
+extern lua_State *xlua_lock(struct xlua_state *lua);
+extern void xlua_unlock(struct xlua_state *lua, lua_State *L);
/* return raw script data */
extern int xlua_script_find(const char *name, const uint8_t **data_r,
@@ 101,8 101,9 @@ static inline struct xlua_state *xlua_in
return xlua_init_at(AT_FDCWD, script_fname, nresults, error, errlen);
}
-static inline void xlua_put_and_close(struct xlua_state *lua, lua_State *L)
+static inline void xlua_close_locked(struct xlua_state *lua, lua_State *L)
{
+ xlua_unlock(lua, L);
xlua_close(lua);
}
M xlua/xlua-impl.h +1 -0
@@ 30,6 30,7 @@
struct xlua_state {
lua_State *L;
+ struct lock lock;
};
extern int xlua_load_script_internal(lua_State *L, const char *scriptname,
M xlua/xlua.c +19 -2
@@ 26,6 26,8 @@
#include "xlua-impl.h"
#include "script.h"
+static LOCK_CLASS(xlua_state_lc);
+
int lua2errno(int e)
{
switch (e) {
@@ 240,6 242,8 @@ static struct xlua_state *__xlua_init(co
return ERR_PTR(-ENOMEM);
}
+ MXINIT(&lua->lock, &xlua_state_lc);
+
lua->L = luaL_newstate();
if (!lua->L) {
strcpy_safe(error, "failed to allocate lua state", errlen);
@@ 249,6 253,8 @@ static struct xlua_state *__xlua_init(co
L = lua->L;
+ MXLOCK(&lua->lock);
+
luaL_openlibs(L);
xlua_register(L);
@@ 263,6 269,7 @@ static struct xlua_state *__xlua_init(co
ASSERT_LUA_STACK(L, nresults);
+ MXUNLOCK(&lua->lock);
return lua;
@@ 271,7 278,11 @@ err_free:
lua_close(L);
+ MXUNLOCK(&lua->lock);
+
err:
+ MXDESTROY(&lua->lock);
+
free(lua);
return ERR_PTR(lua2errno(ret));
@@ 326,19 337,25 @@ struct xlua_state *xlua_init_script(cons
void xlua_close(struct xlua_state *lua)
{
+ MXDESTROY(&lua->lock);
+
lua_close(lua->L);
free(lua);
}
-lua_State *xlua_get_state(struct xlua_state *lua)
+lua_State *xlua_lock(struct xlua_state *lua)
{
+ MXLOCK(&lua->lock);
+
return lua->L;
}
-void xlua_put_state(struct xlua_state *lua, lua_State *L)
+void xlua_unlock(struct xlua_state *lua, lua_State *L)
{
ASSERT3P(lua->L, ==, L);
+
+ MXUNLOCK(&lua->lock);
}
int xlua_call(lua_State *L, const char *funcname, int nargs, int nresults,