Skip to content

Commit 474e6a7

Browse files
committed
cron: tidying and more chatty debugging
1 parent 4bcb4bd commit 474e6a7

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

app/modules/cron.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
#include "rtc/rtctime.h"
1313
#include "mem.h"
1414

15+
static const char *CRON_ENTRY_METATABLE = "cron.entry";
16+
17+
#if 0
18+
# define cron_dbg(...) printf(__VA_ARGS__)
19+
#else
20+
# define cron_dbg(...)
21+
#endif
22+
1523
struct cronent_desc {
1624
uint64_t min; // Minutes repeat - bits 0-59
1725
uint32_t hour; // Hours repeat - bits 0-23
@@ -51,15 +59,14 @@ static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min
5159
}
5260
} else {
5361
uint32_t val;
54-
while (1) {
62+
do {
5563
val = strtol(str, end, 10);
5664
if (val < min || val > max) {
5765
return luaL_error(L, "invalid spec (val %d out of range %d..%d)", val, min, max);
5866
}
5967
res |= (uint64_t)1 << (val - min);
60-
if (**end != ',') break;
6168
str = *end + 1;
62-
}
69+
} while (**end == ',');
6370
}
6471
return res;
6572
}
@@ -101,7 +108,7 @@ static int lcron_create(lua_State *L) {
101108
// Allocate userdata onto the stack
102109
cronent_ud_t *ud = lua_newuserdata(L, sizeof(cronent_ud_t));
103110
// Set metatable
104-
luaL_getmetatable(L, "cron.entry");
111+
luaL_getmetatable(L, CRON_ENTRY_METATABLE);
105112
lua_setmetatable(L, -2);
106113
// Set callback
107114
lua_pushvalue(L, 2);
@@ -134,7 +141,7 @@ static size_t lcron_findindex(lua_State *L, cronent_ud_t *ud) {
134141
}
135142

136143
static int lcron_schedule(lua_State *L) {
137-
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
144+
cronent_ud_t *ud = luaL_checkudata(L, 1, CRON_ENTRY_METATABLE);
138145
char *strdesc = (char*)luaL_optstring(L, 2, NULL);
139146

140147
if (strdesc != NULL) {
@@ -145,14 +152,16 @@ static int lcron_schedule(lua_State *L) {
145152

146153
size_t i = lcron_findindex(L, ud);
147154

155+
cron_dbg("cron: schedule %p at index %d\n", ud, i);
156+
148157
lua_pushvalue(L, 1); // copy ud to top of stack
149158
lua_rawseti(L, -2, i); // install into table
150159

151160
return 0;
152161
}
153162

154163
static int lcron_handler(lua_State *L) {
155-
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
164+
cronent_ud_t *ud = luaL_checkudata(L, 1, CRON_ENTRY_METATABLE);
156165
luaL_checktype(L, 2, LUA_TFUNCTION);
157166
lua_pushvalue(L, 2);
158167
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
@@ -161,9 +170,11 @@ static int lcron_handler(lua_State *L) {
161170
}
162171

163172
static int lcron_unschedule(lua_State *L) {
164-
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
173+
cronent_ud_t *ud = luaL_checkudata(L, 1, CRON_ENTRY_METATABLE);
165174
size_t i = lcron_findindex(L, ud);
166175

176+
cron_dbg("cron: unschedule %p at index %d\n", ud, i);
177+
167178
lua_pushnil(L);
168179
lua_rawseti(L, -2, i);
169180

@@ -172,7 +183,7 @@ static int lcron_unschedule(lua_State *L) {
172183

173184
// scheduled entries are pinned, so we cannot arrive at the __gc metamethod
174185
static int lcron_delete(lua_State *L) {
175-
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
186+
cronent_ud_t *ud = luaL_checkudata(L, 1, CRON_ENTRY_METATABLE);
176187
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
177188
return 0;
178189
}
@@ -182,6 +193,8 @@ static int lcron_reset(lua_State *L) {
182193
luaL_unref(L, LUA_REGISTRYINDEX, cronent_table_ref);
183194
cronent_table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
184195

196+
cron_dbg("cron: cronent_table_ref is %d\n", cronent_table_ref);
197+
185198
return 0;
186199
}
187200

@@ -198,10 +211,14 @@ static void cron_handle_time(uint8_t mon, uint8_t dom, uint8_t dow, uint8_t hour
198211
size_t count = lua_objlen(L, -1);
199212

200213
for (size_t i = 1; i <= count; i++) {
214+
cron_dbg("cron: handle_time index %d (of %d; top %d)\n", i, count, lua_gettop(L));
215+
201216
lua_rawgeti(L, -1, i);
202217
cronent_ud_t *ent = lua_touserdata(L, -1);
203218
lua_pop(L, 1);
204219

220+
cron_dbg(" ... is %p\n", ent);
221+
205222
if ((ent->desc.mon & desc.mon ) == 0) continue;
206223
if ((ent->desc.dom & desc.dom ) == 0) continue;
207224
if ((ent->desc.dow & desc.dow ) == 0) continue;
@@ -261,7 +278,7 @@ int luaopen_cron( lua_State *L ) {
261278
//cron_handle_tmr determines when to execute a scheduled cron job
262279
//My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer.
263280
os_timer_arm(&cron_timer, 1000, 0);
264-
luaL_rometatable(L, "cron.entry", LROT_TABLEREF(cronent));
281+
luaL_rometatable(L, CRON_ENTRY_METATABLE, LROT_TABLEREF(cronent));
265282

266283
cronent_table_ref = LUA_NOREF;
267284
lcron_reset(L);

0 commit comments

Comments
 (0)