timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* History:
  28. * --------
  29. * 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  30. * 2003-03-29 cleaning pkg_mallocs introduced (jiri)
  31. */
  32. #include "timer.h"
  33. #include "dprint.h"
  34. #include "error.h"
  35. #include "config.h"
  36. #include "mem/mem.h"
  37. #ifdef SHM_MEM
  38. #include "mem/shm_mem.h"
  39. #endif
  40. #include <stdlib.h>
  41. struct sr_timer* timer_list=0;
  42. static int* jiffies=0;
  43. static int timer_id=0;
  44. /* ret 0 on success, <0 on error*/
  45. int init_timer()
  46. {
  47. #ifdef SHM_MEM
  48. jiffies=shm_malloc(sizeof(int));
  49. #else
  50. /* in this case get_ticks won't work! */
  51. LOG(L_INFO, "WARNING: no shared memory support compiled in"
  52. " get_ticks won't work\n");
  53. jiffies=pkg_malloc(sizeof(int));
  54. #endif
  55. if (jiffies==0){
  56. LOG(L_CRIT, "ERROR: init_timer: could not init jiffies\n");
  57. return E_OUT_OF_MEM;
  58. }
  59. *jiffies=0;
  60. return 0;
  61. }
  62. void destroy_timer()
  63. {
  64. struct sr_timer* t, *foo;
  65. if (jiffies){
  66. #ifdef SHM_MEM
  67. shm_free(jiffies); jiffies=0;
  68. #else
  69. pkg_free(jiffies); jiffies=0;
  70. #endif
  71. }
  72. t=timer_list;
  73. while(t) {
  74. foo=t->next;
  75. pkg_free(t);
  76. t=foo;
  77. }
  78. }
  79. /*register a periodic timer;
  80. * ret: <0 on error
  81. * Hint: if you need it in a module, register it from mod_init or it
  82. * won't work otherwise*/
  83. int register_timer(timer_function f, void* param, unsigned int interval)
  84. {
  85. struct sr_timer* t;
  86. t=pkg_malloc(sizeof(struct sr_timer));
  87. if (t==0){
  88. LOG(L_ERR, "ERROR: register_timer: out of memory\n");
  89. goto error;
  90. }
  91. t->id=timer_id++;
  92. t->timer_f=f;
  93. t->t_param=param;
  94. t->interval=interval;
  95. t->expires=*jiffies+interval;
  96. /* insert it into the list*/
  97. t->next=timer_list;
  98. timer_list=t;
  99. return t->id;
  100. error:
  101. return E_OUT_OF_MEM;
  102. }
  103. void timer_ticker()
  104. {
  105. struct sr_timer* t;
  106. unsigned int prev_jiffies;
  107. prev_jiffies=*jiffies;
  108. *jiffies+=TIMER_TICK;
  109. /* test for overflow (if tick= 1s =>overflow in 136 years)*/
  110. if (*jiffies<prev_jiffies){
  111. /*force expire & update every timer, a little buggy but it
  112. * happens once in 136 years :) */
  113. for(t=timer_list;t;t=t->next){
  114. t->expires=*jiffies+t->interval;
  115. t->timer_f(*jiffies, t->t_param);
  116. }
  117. return;
  118. }
  119. for (t=timer_list;t; t=t->next){
  120. if (*jiffies>=t->expires){
  121. t->expires=*jiffies+t->interval;
  122. t->timer_f(*jiffies, t->t_param);
  123. }
  124. }
  125. }
  126. unsigned int get_ticks()
  127. {
  128. if (jiffies==0){
  129. LOG(L_CRIT, "BUG: get_ticks: jiffies not intialized\n");
  130. return 0;
  131. }
  132. #ifndef SHM_MEM
  133. LOG(L_CRIT, "WARNING: get_ticks: no shared memory support compiled in"
  134. ", returning 0 (probably wrong)");
  135. return 0;
  136. #endif
  137. return *jiffies;
  138. }