timer.c 3.0 KB

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