- ipv6 support (-DUSE_IPV6)
[sip-router] / timer.c
1 /*
2  * $Id$
3  */
4
5 #include "timer.h"
6 #include "dprint.h"
7 #include "error.h"
8 #include "config.h"
9 #include "mem/mem.h"
10 #ifdef SHM_MEM
11 #include "mem/shm_mem.h"
12 #endif
13
14 #include <stdlib.h>
15
16
17 struct sr_timer* timer_list=0;
18
19 static int* jiffies=0;
20 static int timer_id=0;
21
22
23
24 /* ret 0 on success, <0 on error*/
25 int init_timer()
26 {
27 #ifdef SHM_MEM
28         jiffies=shm_malloc(sizeof(int));
29 #else
30         /* in this case get_ticks won't work! */
31         LOG(L_INFO, "WARNING: no shared memory support compiled in"
32                                 " get_ticks won't work\n");
33         jiffies=malloc(sizeof(int));
34 #endif
35         if (jiffies==0){
36                 LOG(L_CRIT, "ERROR: init_timer: could not init jiffies\n");
37                 return E_OUT_OF_MEM;
38         }
39         *jiffies=0;
40         return 0;
41 }
42
43
44
45         
46 /*register a periodic timer;
47  * ret: <0 on error*/
48 int register_timer(timer_function f, void* param, unsigned int interval)
49 {
50         struct sr_timer* t;
51
52         t=malloc(sizeof(struct sr_timer));
53         if (t==0){
54                 LOG(L_ERR, "ERROR: register_timer: out of memory\n");
55                 goto error;
56         }
57         t->id=timer_id++;
58         t->timer_f=f;
59         t->t_param=param;
60         t->interval=interval;
61         t->expires=*jiffies+interval;
62         /* insert it into the list*/
63         t->next=timer_list;
64         timer_list=t;
65         return t->id;
66
67 error:
68         return E_OUT_OF_MEM;
69 }
70
71
72
73 void timer_ticker()
74 {
75         struct sr_timer* t;
76         unsigned int prev_jiffies;
77         
78         prev_jiffies=*jiffies;
79         *jiffies+=TIMER_TICK;
80         /* test for overflow (if tick= 1s =>overflow in 136 years)*/
81         if (*jiffies<prev_jiffies){ 
82                 /*force expire & update every timer, a little buggy but it 
83                  * happens once in 136 years :) */
84                 for(t=timer_list;t;t=t->next){
85                         t->expires=*jiffies+t->interval;
86                         t->timer_f(*jiffies, t->t_param);
87                 }
88                 return;
89         }
90         
91         for (t=timer_list;t; t=t->next){
92                 if (*jiffies>=t->expires){
93                         t->expires=*jiffies+t->interval;
94                         t->timer_f(*jiffies, t->t_param);
95                 }
96         }
97 }
98
99
100
101 unsigned int get_ticks()
102 {
103         if (jiffies==0){
104                 LOG(L_CRIT, "BUG: get_ticks: jiffies not intialized\n");
105                 return 0;
106         }
107 #ifndef SHM_MEM
108         LOG(L_CRIT, "WARNING: get_ticks: no shared memory support compiled in"
109                         ", returning 0 (probably wrong)");
110         return 0;
111 #endif
112         return *jiffies;
113 }