You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
1.8 KiB
94 lines
1.8 KiB
5 years ago
|
#include <stdlib.h>
|
||
|
#include <stdint.h>
|
||
|
#include "timer_list.h"
|
||
|
#include "tick.h"
|
||
|
|
||
|
struct timer_list {
|
||
|
struct timer_list *next;
|
||
|
unsigned int timeout;
|
||
|
uint64_t millis;
|
||
|
void (*callback)(struct timer_list *, void *user);
|
||
|
void *user;
|
||
|
};
|
||
|
|
||
|
static struct timer_list *head = NULL, **tail = &head;
|
||
|
|
||
|
struct timer_list *timer_list_create(void (*f)(struct timer_list *, void *user), void *user)
|
||
|
{
|
||
|
struct timer_list *it;
|
||
|
|
||
|
it = malloc(sizeof *it);
|
||
|
if (it) {
|
||
|
it->next = NULL;
|
||
|
it->millis = 0;
|
||
|
it->timeout = 0;
|
||
|
it->callback = f;
|
||
|
it->user = user;
|
||
|
}
|
||
|
return (it);
|
||
|
}
|
||
|
|
||
|
struct timer_list *timer_list_match(int (*match)(void *user, void *arg), void *arg, void **r)
|
||
|
{
|
||
|
struct timer_list *it = head;
|
||
|
|
||
|
while (it) {
|
||
|
if (match(it->user, arg)) {
|
||
|
if (r) {
|
||
|
*r = it->user;
|
||
|
}
|
||
|
return it;
|
||
|
}
|
||
|
it = it->next;
|
||
|
}
|
||
|
return (NULL);
|
||
|
}
|
||
|
|
||
|
void timer_list_activate(struct timer_list *it, unsigned int ms)
|
||
|
{
|
||
|
it->next = NULL;
|
||
|
it->timeout = ms;
|
||
|
it->millis = millis();
|
||
|
|
||
|
*tail = it;
|
||
|
tail = &it->next;
|
||
|
}
|
||
|
|
||
|
void timer_list_deactive(struct timer_list *it)
|
||
|
{
|
||
|
struct timer_list **next;
|
||
|
|
||
|
next = &head;
|
||
|
while ((*next) != NULL) {
|
||
|
if ((*next) == it) {
|
||
|
*next = it->next;
|
||
|
if (head == NULL) {
|
||
|
tail = &head;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
next = &(*next)->next;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void timer_list_destroy(struct timer_list *it)
|
||
|
{
|
||
|
free(it);
|
||
|
}
|
||
|
|
||
|
void timer_list_dispatch()
|
||
|
{
|
||
|
struct timer_list *it;
|
||
|
uint64_t c = millis();
|
||
|
|
||
|
it = head;
|
||
|
while (it) {
|
||
|
if ((it->millis + it->timeout) <= c) {
|
||
|
it->callback(it, it->user);
|
||
|
it->millis = c;
|
||
|
}
|
||
|
it = it->next;
|
||
|
}
|
||
|
}
|
||
|
|