sched_ticks_counter = 0;
}
-int
+struct lx_timer*
timer_run_second(uint32_t second, void (*callback)(void*), void* payload, uint8_t flags)
{
return timer_run(second * timer_ctx->running_frequency, callback, payload, flags);
}
-int
+struct lx_timer*
timer_run_ms(uint32_t millisecond, void (*callback)(void*), void* payload, uint8_t flags)
{
return timer_run(timer_ctx->running_frequency / 1000 * millisecond, callback, payload, flags);
}
-int
+struct lx_timer*
timer_run(ticks_t ticks, void (*callback)(void*), void* payload, uint8_t flags)
{
struct lx_timer* timer = (struct lx_timer*)lxmalloc(sizeof(struct lx_timer));
- if (!timer) return 0;
+ if (!timer) return NULL;
timer->callback = callback;
timer->counter = ticks;
llist_append(timer_ctx->active_timers, &timer->link);
- return 1;
+ return timer;
}
static void
llist_for_each(pos, n, &timer_list_head->link, link)
{
- if (--pos->counter) {
+ if (!pos->counter) {
+ llist_delete(&pos->link);
+ lxfree(pos);
+ continue;
+ }
+
+ if (--(pos->counter)) {
continue;
}
pos->callback ? pos->callback(pos->payload) : 1;
- if (pos->flags & TIMER_MODE_PERIODIC) {
+ if ((pos->flags & TIMER_MODE_PERIODIC)) {
pos->counter = pos->deadline;
- } else {
- llist_delete(&pos->link);
- lxfree(pos);
}
}