summaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-lua.c33
-rw-r--r--ctl-lib/ctl-timer.c14
-rw-r--r--ctl-lib/ctl-timer.h1
3 files changed, 40 insertions, 8 deletions
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index 02487b5..ba29c9b 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -40,11 +40,15 @@
static lua_State* luaState;
CtlPluginT *ctlPlugins = NULL;
-#ifndef CTX_MAGIC
+#if CTX_MAGIC_VALUE
+static int CTX_MAGIC = CTX_MAGIC_VALUE;
+#else
static int CTX_MAGIC;
#endif
-#ifndef TIMER_MAGIC
+#if TIMER_MAGIC_VALUE
+static int TIMER_MAGIC = TIMER_MAGIC_VALUE;
+#else
static int TIMER_MAGIC;
#endif
@@ -599,6 +603,23 @@ static int LuaAfbEventSubscribe(lua_State* luaState) {
return 0;
}
+static int LuaLockWait(lua_State* luaState) {
+ luaL_checktype(luaState, LUA_FIST_ARG, LUA_TLIGHTUSERDATA);
+ luaL_checktype(luaState, LUA_FIST_ARG + 1, LUA_TNUMBER);
+
+ CtlSourceT *source = (CtlSourceT *)LuaSourcePop(luaState, LUA_FIST_ARG);
+ if (!source) {
+ lua_pushliteral(luaState, "LuaLockWait-Fail Invalid request handle");
+ lua_error(luaState);
+ return 1;
+ }
+ uint64_t utimeout = (int)lua_tointeger(luaState, LUA_FIST_ARG + 1);
+
+ uint64_t remain_timeout = LockWait(source->api, utimeout);
+ lua_pushinteger(luaState, remain_timeout);
+ return 1; //return nb of pushed elements
+}
+
static int LuaAfbEventMake(lua_State* luaState) {
int count = lua_gettop(luaState);
LuaAfbEvent *afbevt = calloc(1, sizeof (LuaAfbEvent));
@@ -1247,7 +1268,7 @@ static const luaL_Reg afbFunction[] = {
{"getuid", LuaAfbGetUid},
{"status", LuaAfbGetStatus},
{"context", LuaClientCtx},
-
+ {"lockwait", LuaLockWait},
{NULL, NULL} /* sentinel */
};
@@ -1328,13 +1349,9 @@ int LuaConfigLoad(AFB_ApiT apiHandle) {
free(lua_str);
// initialise static magic for context
-#ifndef CTX_MAGIC
+#ifndef CTX_MAGIC_VALUE
CTX_MAGIC = CtlConfigMagicNew();
#endif
-#ifndef TIMER_MAGIC
- TIMER_MAGIC = CtlConfigMagicNew();
-#endif
-
return 0;
}
diff --git a/ctl-lib/ctl-timer.c b/ctl-lib/ctl-timer.c
index 2139c75..c36d2b1 100644
--- a/ctl-lib/ctl-timer.c
+++ b/ctl-lib/ctl-timer.c
@@ -19,6 +19,7 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
+#include <systemd/sd-event.h>
#include "ctl-config.h"
#include "ctl-timer.h"
@@ -88,3 +89,16 @@ int TimerEvtInit (AFB_ApiT apiHandle) {
return 0;
}
+uint64_t LockWait(AFB_ApiT apiHandle, uint64_t utimeout) {
+ uint64_t current_usec, pre_usec;
+
+ struct sd_event *event = AFB_GetEventLoop(apiHandle);
+
+ sd_event_now(event, CLOCK_MONOTONIC, &pre_usec);
+ sd_event_run(event, utimeout);
+ sd_event_now(event, CLOCK_MONOTONIC, &current_usec);
+
+ uint64_t diff = current_usec - pre_usec;
+ utimeout = utimeout < diff ? 0 : utimeout - diff;
+ return utimeout;
+}
diff --git a/ctl-lib/ctl-timer.h b/ctl-lib/ctl-timer.h
index 389d518..71f87af 100644
--- a/ctl-lib/ctl-timer.h
+++ b/ctl-lib/ctl-timer.h
@@ -46,6 +46,7 @@ int TimerEvtInit (AFB_ApiT apiHandle);
void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context);
void TimerEvtStop(TimerHandleT *timerHandle);
+uint64_t LockWait(AFB_ApiT apiHandle, uint64_t utimeout);
#ifdef __cplusplus
}
#endif