nfqws2: timer_info, timer_enum

This commit is contained in:
bol-van
2026-04-28 10:21:46 +03:00
parent 5aefa8610b
commit 01a4ee63d4
5 changed files with 81 additions and 4 deletions
+4
View File
@@ -284,3 +284,7 @@ v0.9.4.3
0.9.5
* nfqws2: timers
0.9.5.1
* nfqws2: timer_info, timer_enum
+23 -1
View File
@@ -907,13 +907,27 @@ function test_ifaddrs(opts)
end
end
function timer_info_print(tinfo)
print(" timer_info.name="..tinfo.name)
print(" timer_info.func="..tinfo.func)
print(" timer_info.period="..tinfo.period)
print(" timer_info.oneshot="..tostring(tinfo.oneshot))
print(" timer_info.fires="..tinfo.fires)
end
function timer_info_print_by_name(name)
local tinfo = timer_info(name)
timer_info_print(tinfo)
end
function timer1(name, data)
print("timer "..name.." fired. data="..tostring(data))
timer_info_print_by_name(name)
end
function timer2(name, data)
data.n = data.n+1
print("timer "..name.." fired. data.n="..tostring(data.n))
if data.n>=3 then
timer_info_print_by_name(name)
if data.n>=4 then
timer_del(name)
end
end
@@ -921,6 +935,14 @@ function test_timer(opts)
timer_set("t1","timer1",500,true,"sample_data");
local tbl = {n=0}
timer_set("t2","timer2",700,false,tbl);
print("* timers\n")
local timers=timer_enum()
for i,timer in ipairs(timers) do
print("TIMER "..i.." :")
timer_info_print(timer)
end
print()
end
+50 -1
View File
@@ -3847,6 +3847,53 @@ static int luacall_timer_del(lua_State *L)
lua_pushboolean(L, !!timer);
LUA_STACK_GUARD_RETURN(L,1)
}
static int lua_push_timer_info(lua_State *L, const timer_pool *timer)
{
lua_newtable(L);
if (timer->str) lua_pushf_str(L, "name", timer->str);
if (timer->func) lua_pushf_str(L, "func", timer->func);
lua_pushf_lint(L, "period", timer->period);
lua_pushf_bool(L, "oneshot", timer->oneshot);
lua_pushf_lint(L, "fires", timer->fires);
}
static void lua_pushi_timer_info(lua_State *L, lua_Integer idx, const timer_pool *timer)
{
lua_pushinteger(L, idx);
lua_push_timer_info(L, timer);
lua_rawset(L,-3);
}
static int luacall_timer_info(lua_State *L)
{
// timer_del(name)
lua_check_argc(L,"timer_info",1);
LUA_STACK_GUARD_ENTER(L)
const char *name = luaL_checkstring(L,1);
const timer_pool *timer = TimerPoolSearch(params.timers, name);
if (timer)
lua_push_timer_info(L, timer);
else
lua_pushnil(L);
LUA_STACK_GUARD_RETURN(L,1)
}
static int luacall_timer_enum(lua_State *L)
{
// timer_enum()
lua_check_argc(L,"timer_enum",0);
LUA_STACK_GUARD_ENTER(L)
lua_Integer n=1;
timer_pool *timer, *tmp, *p;
lua_newtable(L);
HASH_ITER(hh, params.timers, timer, tmp) lua_pushi_timer_info(L, n++, timer);
LUA_STACK_GUARD_RETURN(L,1)
}
// ----------------------------------------
@@ -4505,7 +4552,9 @@ static void lua_init_functions(void)
// timers
{"timer_set",luacall_timer_set},
{"timer_del",luacall_timer_del}
{"timer_del",luacall_timer_del},
{"timer_info",luacall_timer_info},
{"timer_enum",luacall_timer_enum}
};
for(int i=0;i<(sizeof(lfunc)/sizeof(*lfunc));i++)
lua_register(params.L,lfunc[i].name,lfunc[i].f);
+3 -1
View File
@@ -52,6 +52,7 @@ struct timer_pool *TimerPoolAdd(timer_pool **pp, const char *str, const char *fu
elem->lua_ref = LUA_NOREF;
elem->bt_prev = boottime_ms();
elem->n = ++timer_n;
elem->fires = 0;
return elem;
}
@@ -66,7 +67,8 @@ static bool TimerPoolRunTimer(timer_pool *p)
}
lua_pushstring(params.L, p->str);
lua_rawgeti(params.L, LUA_REGISTRYINDEX, p->lua_ref);
DLOG("\ntimer: '%s' function '%s' period %llu oneshot %u\n",p->str,p->func,p->period,p->oneshot);
p->fires++;
DLOG("\ntimer: '%s' function '%s' period %llu oneshot %u fires=%u\n",p->str,p->func,p->period,p->oneshot,p->fires);
int status = lua_pcall(params.L, 2, 0, 0);
if (status)
{
+1 -1
View File
@@ -12,7 +12,7 @@ typedef struct timer_pool {
bool oneshot;
int lua_ref;
uint64_t bt_prev;
unsigned int n;
unsigned int n, fires;
UT_hash_handle hh; /* makes this structure hashable */
} timer_pool;