Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-10-15 01:17:36 +0300
committerFelix Fietkau <nbd@nbd.name>2023-11-02 19:49:55 +0300
commit82fa6480de7a85d0ced0701ab7c8825e31b90770 (patch)
tree580124998f50d5745f26d71e777ee58a1bc5a87e /lua
parent75a3b870cace1171faf57bd55e5a9a2f1564f757 (diff)
uloop: add support for interval timers
So far, the only way to implement periodic interval timers was to use one-shot uloop_timeout timers which are rearmed within their completion callback immediately on expiration. While simple, this approach is not very precise and interval lengths will slowly drift over time, due to callback execution overhead, scheduling granularity etc. In order to make uloop provide stable and precise interval timer capabilities, this commit introduces a new `uloop_interval` structure along with the new related `uloop_interval_set()`, `uloop_interval_cancel()` and `uloop_interval_remaining()` api functions. Periodic timers are implemented using the timerfd facility an Linux and kqueue EVFILT_TIMER events on macOS/BSD. The Lua binding has been updated to include support for the new timer type as well. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lua')
-rw-r--r--lua/uloop.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/lua/uloop.c b/lua/uloop.c
index 7e9ed10..45c9bc7 100644
--- a/lua/uloop.c
+++ b/lua/uloop.c
@@ -41,6 +41,11 @@ struct lua_uloop_process {
int r;
};
+struct lua_uloop_interval {
+ struct uloop_interval i;
+ int r;
+};
+
static lua_State *state;
static void *
@@ -382,6 +387,112 @@ static int ul_process(lua_State *L)
return 1;
}
+static void ul_interval_cb(struct uloop_interval *i)
+{
+ struct lua_uloop_interval *intv = container_of(i, struct lua_uloop_interval, i);
+
+ lua_getglobal(state, "__uloop_cb");
+ lua_rawgeti(state, -1, intv->r);
+ lua_remove(state, -2);
+
+ lua_call(state, 0, 0);
+}
+
+static int ul_interval_set(lua_State *L)
+{
+ struct lua_uloop_interval *intv;
+ double set;
+
+ if (!lua_isnumber(L, -1)) {
+ lua_pushstring(L, "invalid arg list");
+ lua_error(L);
+
+ return 0;
+ }
+
+ set = lua_tointeger(L, -1);
+ intv = lua_touserdata(L, 1);
+ uloop_interval_set(&intv->i, set);
+
+ return 1;
+}
+
+static int ul_interval_expirations(lua_State *L)
+{
+ struct lua_uloop_interval *intv;
+
+ intv = lua_touserdata(L, 1);
+ lua_pushinteger(L, intv->i.expirations);
+ return 1;
+}
+
+static int ul_interval_remaining(lua_State *L)
+{
+ struct lua_uloop_interval *intv;
+
+ intv = lua_touserdata(L, 1);
+ lua_pushnumber(L, uloop_interval_remaining(&intv->i));
+ return 1;
+}
+
+static int ul_interval_free(lua_State *L)
+{
+ struct lua_uloop_interval *intv = lua_touserdata(L, 1);
+
+ uloop_interval_cancel(&intv->i);
+
+ /* obj.__index.__gc = nil , make sure executing only once*/
+ lua_getfield(L, -1, "__index");
+ lua_pushstring(L, "__gc");
+ lua_pushnil(L);
+ lua_settable(L, -3);
+
+ lua_getglobal(state, "__uloop_cb");
+ luaL_unref(state, -1, intv->r);
+
+ return 1;
+}
+
+static const luaL_Reg interval_m[] = {
+ { "set", ul_interval_set },
+ { "expirations", ul_interval_expirations },
+ { "remaining", ul_interval_remaining },
+ { "cancel", ul_interval_free },
+ { NULL, NULL }
+};
+
+static int ul_interval(lua_State *L)
+{
+ struct lua_uloop_interval *intv;
+ unsigned int set = 0;
+ int ref;
+
+ if (lua_isnumber(L, -1)) {
+ set = lua_tointeger(L, -1);
+ lua_pop(L, 1);
+ }
+
+ if (!lua_isfunction(L, -1)) {
+ lua_pushstring(L, "invalid arg list");
+ lua_error(L);
+
+ return 0;
+ }
+
+ lua_getglobal(L, "__uloop_cb");
+ lua_pushvalue(L, -2);
+ ref = luaL_ref(L, -2);
+
+ intv = ul_create_userdata(L, sizeof(*intv), interval_m, ul_interval_free);
+ intv->r = ref;
+ intv->i.cb = ul_interval_cb;
+
+ if (set)
+ uloop_interval_set(&intv->i, set);
+
+ return 1;
+}
+
static int ul_init(lua_State *L)
{
uloop_init();
@@ -410,6 +521,7 @@ static luaL_reg uloop_func[] = {
{"timer", ul_timer},
{"process", ul_process},
{"fd_add", ul_ufd_add},
+ {"interval", ul_interval},
{"cancel", ul_end},
{NULL, NULL},
};