4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of ser, a free SIP server.
8 * ser is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
13 * For a license to use the ser software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
18 * ser is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
30 * 2003-03-29 cleaning pkg_mallocs introduced (jiri)
40 #include "mem/shm_mem.h"
46 struct sr_timer* timer_list=0;
48 static int* jiffies=0;
49 static int timer_id=0;
53 /* ret 0 on success, <0 on error*/
57 jiffies=shm_malloc(sizeof(int));
59 /* in this case get_ticks won't work! */
60 LOG(L_INFO, "WARNING: no shared memory support compiled in"
61 " get_ticks won't work\n");
62 jiffies=pkg_malloc(sizeof(int));
65 LOG(L_CRIT, "ERROR: init_timer: could not init jiffies\n");
76 struct sr_timer* t, *foo;
80 shm_free(jiffies); jiffies=0;
82 pkg_free(jiffies); jiffies=0;
96 /*register a periodic timer;
98 * Hint: if you need it in a module, register it from mod_init or it
99 * won't work otherwise*/
100 int register_timer(timer_function f, void* param, unsigned int interval)
104 t=pkg_malloc(sizeof(struct sr_timer));
106 LOG(L_ERR, "ERROR: register_timer: out of memory\n");
112 t->interval=interval;
113 t->expires=*jiffies+interval;
114 /* insert it into the list*/
128 unsigned int prev_jiffies;
130 prev_jiffies=*jiffies;
131 *jiffies+=TIMER_TICK;
132 /* test for overflow (if tick= 1s =>overflow in 136 years)*/
133 if (*jiffies<prev_jiffies){
134 /*force expire & update every timer, a little buggy but it
135 * happens once in 136 years :) */
136 for(t=timer_list;t;t=t->next){
137 t->expires=*jiffies+t->interval;
138 t->timer_f(*jiffies, t->t_param);
143 for (t=timer_list;t; t=t->next){
144 if (*jiffies>=t->expires){
145 t->expires=*jiffies+t->interval;
146 t->timer_f(*jiffies, t->t_param);
153 unsigned int get_ticks()
156 LOG(L_CRIT, "BUG: get_ticks: jiffies not initialized\n");
160 LOG(L_CRIT, "WARNING: get_ticks: no shared memory support compiled in"
161 ", returning 0 (probably wrong)");