Use int64_t for timers and mg_millis()

This commit is contained in:
Sergey Lyubka
2021-12-21 21:50:18 +00:00
parent fb0a9bc7e3
commit ae6767b1d2
15 changed files with 123 additions and 118 deletions
+6 -7
View File
@@ -6,7 +6,7 @@
struct mg_timer *g_timers;
void mg_timer_init(struct mg_timer *t, unsigned long ms, unsigned flags,
void mg_timer_init(struct mg_timer *t, int64_t ms, unsigned flags,
void (*fn)(void *), void *arg) {
struct mg_timer tmp = {ms, 0UL, flags, fn, arg, g_timers};
*t = tmp;
@@ -20,11 +20,11 @@ void mg_timer_free(struct mg_timer *t) {
if (*head) *head = t->next;
}
void mg_timer_poll(unsigned long now_ms) {
void mg_timer_poll(int64_t now_ms) {
// If time goes back (wrapped around), reset timers
struct mg_timer *t, *tmp;
static unsigned long oldnow; // Timestamp in a previous invocation
if (oldnow > now_ms) { // If it is wrapped, reset timers
static int64_t oldnow; // Timestamp in a previous invocation
if (oldnow > now_ms) { // If it is wrapped, reset timers
for (t = g_timers; t != NULL; t = t->next) t->expire = 0;
}
oldnow = now_ms;
@@ -36,9 +36,8 @@ void mg_timer_poll(unsigned long now_ms) {
t->fn(t->arg);
// Try to tick timers with the given period as accurate as possible,
// even if this polling function is called with some random period.
t->expire = now_ms - t->expire > (unsigned long) t->period_ms
? now_ms + t->period_ms
: t->expire + t->period_ms;
t->expire = now_ms - t->expire > t->period_ms ? now_ms + t->period_ms
: t->expire + t->period_ms;
if (!(t->flags & MG_TIMER_REPEAT)) mg_timer_free(t);
}
}