123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /*
- * $Id$
- *
- * Copyright (C) 2001-2003 Fhg Fokus
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser 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
- */
- #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=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;
- }
-
- /*register a periodic timer;
- * ret: <0 on error*/
- int register_timer(timer_function f, void* param, unsigned int interval)
- {
- struct sr_timer* t;
- t=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 intialized\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;
- }
|