From 6b99ed2192c8c24f00d2d14c20e2136e4b89741e Mon Sep 17 00:00:00 2001
From: Clément Bénier <clement.benier@iot.bzh>
Date: Tue, 3 Jul 2018 18:34:11 +0200
Subject: asynchronism for test: LockWait added
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

added LockWait to wait for incoming events, as soon as an event is
received, the wait is unlocked
You can indicate the timeout

Change-Id: If29ca754618edb5b9fdc32c1c40b47016c6fc88a
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
---
 ctl-lib/ctl-lua.c   | 33 +++++++++++++++++++++++++--------
 ctl-lib/ctl-timer.c | 14 ++++++++++++++
 ctl-lib/ctl-timer.h |  1 +
 3 files changed, 40 insertions(+), 8 deletions(-)

(limited to 'ctl-lib')

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
-- 
cgit