123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <signal.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "include/log.h"
- #include "include/list.h"
- #include "include/timer.h"
- #include "include/ucix.h"
- /* when using this file, alarm() is used */
- static struct list_head timers;
- struct timer {
- struct list_head list;
- int count;
- int timeout;
- timercb_t timercb;
- };
- void timer_add(timercb_t timercb, int timeout)
- {
- struct timer *timer;
- timer = malloc(sizeof(struct timer));
- if(!timer)
- {
- log_printf("unable to get timer buffer\n");
- return;
- }
- timer->count = 0;
- timer->timeout = timeout;
- timer->timercb = timercb;
- INIT_LIST_HEAD(&timer->list);
- list_add(&timer->list, &timers);
- }
- static void timer_proc(int signo)
- {
- struct list_head *p;
- list_for_each(p, &timers)
- {
- struct timer *q = container_of(p, struct timer, list);
- q->count++;
- if(!(q->count%q->timeout))
- {
- q->timercb();
- }
- }
- alarm(1);
- }
- void timer_init(void)
- {
- struct sigaction s;
- INIT_LIST_HEAD(&timers);
- s.sa_handler = timer_proc;
- s.sa_flags = 0;
- sigaction(SIGALRM, &s, NULL);
- alarm(1);
- }
|