support for disk without partition table
[oweals/mountd.git] / timer.c
1 #include <signal.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "include/log.h"
6 #include "include/list.h"
7 #include "include/timer.h"
8 #include "include/ucix.h"
9
10 /* when using this file, alarm() is used */
11
12 struct list_head timers;
13
14 struct timer {
15         struct list_head list;
16         int count;
17         int timeout;
18         timercb_t timercb;
19 };
20
21 void timer_add(timercb_t timercb, int timeout)
22 {
23         struct timer *timer;
24         timer = malloc(sizeof(struct timer));
25         if(!timer)
26         {
27                 log_printf("unable to get timer buffer\n");
28                 return;
29         }
30         timer->count = 0;
31         timer->timeout = timeout;
32         timer->timercb = timercb;
33         INIT_LIST_HEAD(&timer->list);
34         list_add(&timer->list, &timers);
35 }
36
37 static void timer_proc(int signo)
38 {
39         struct list_head *p;
40         list_for_each(p, &timers)
41         {
42                 struct timer *q = container_of(p, struct timer, list);
43                 q->count++;
44                 if(!(q->count%q->timeout))
45                 {
46                         q->timercb();
47                 }
48         }
49         alarm(1);
50 }
51
52 void timer_init(void)
53 {
54         struct sigaction s;
55         INIT_LIST_HEAD(&timers);
56         s.sa_handler = timer_proc;
57         s.sa_flags = 0;
58         sigaction(SIGALRM, &s, NULL);
59         alarm(1);
60 }