1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* $Id: timer.c,v 1.1.1.1 2005/06/13 16:47:29 bogdan_iancu Exp $
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
* 2003-03-29 cleaning pkg_mallocs introduced (jiri)
*/
#include "timer.h"
#include "dprint.h"
#include "error.h"
#include "config.h"
#include "mem/mem.h"
#ifdef SHM_MEM
#include "mem/shm_mem.h"
#endif
#include <stdlib.h>
struct sr_timer* timer_list=0;
static int* jiffies=0;
static int timer_id=0;
/* ret 0 on success, <0 on error*/
int init_timer()
{
#ifdef SHM_MEM
jiffies=shm_malloc(sizeof(int));
#else
/* in this case get_ticks won't work! */
LOG(L_INFO, "WARNING: no shared memory support compiled in"
" get_ticks won't work\n");
jiffies=pkg_malloc(sizeof(int));
#endif
if (jiffies==0){
LOG(L_CRIT, "ERROR: init_timer: could not init jiffies\n");
return E_OUT_OF_MEM;
}
*jiffies=0;
return 0;
}
void destroy_timer()
{
struct sr_timer* t, *foo;
if (jiffies){
#ifdef SHM_MEM
shm_free(jiffies); jiffies=0;
#else
pkg_free(jiffies); jiffies=0;
#endif
}
t=timer_list;
while(t) {
foo=t->next;
pkg_free(t);
t=foo;
}
}
/*register a periodic timer;
* ret: <0 on error
* Hint: if you need it in a module, register it from mod_init or it
* won't work otherwise*/
int register_timer(timer_function f, void* param, unsigned int interval)
{
struct sr_timer* t;
t=pkg_malloc(sizeof(struct sr_timer));
if (t==0){
LOG(L_ERR, "ERROR: register_timer: out of memory\n");
goto error;
}
t->id=timer_id++;
t->timer_f=f;
t->t_param=param;
t->interval=interval;
t->expires=*jiffies+interval;
/* insert it into the list*/
t->next=timer_list;
timer_list=t;
return t->id;
error:
return E_OUT_OF_MEM;
}
void timer_ticker()
{
struct sr_timer* t;
unsigned int prev_jiffies;
prev_jiffies=*jiffies;
*jiffies+=TIMER_TICK;
/* test for overflow (if tick= 1s =>overflow in 136 years)*/
if (*jiffies<prev_jiffies){
/*force expire & update every timer, a little buggy but it
* happens once in 136 years :) */
for(t=timer_list;t;t=t->next){
t->expires=*jiffies+t->interval;
t->timer_f(*jiffies, t->t_param);
}
return;
}
for (t=timer_list;t; t=t->next){
if (*jiffies>=t->expires){
t->expires=*jiffies+t->interval;
t->timer_f(*jiffies, t->t_param);
}
}
}
unsigned int get_ticks()
{
if (jiffies==0){
LOG(L_CRIT, "BUG: get_ticks: jiffies not initialized\n");
return 0;
}
#ifndef SHM_MEM
LOG(L_CRIT, "WARNING: get_ticks: no shared memory support compiled in"
", returning 0 (probably wrong)");
return 0;
#endif
return *jiffies;
}
|