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