timer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * timer related functions (public interface)
  6. *
  7. * Copyright (C) 2001-2003 FhG Fokus
  8. *
  9. * This file is part of SIP-router, a free SIP server.
  10. *
  11. * SIP-router is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * SIP-router is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /* History:
  26. * --------
  27. * 2005-07-27 complete re-design/re-implementation (andrei)
  28. */
  29. /**
  30. * @file
  31. * @brief SIP-router core :: timer related functions (public interface)
  32. * @ingroup core
  33. *
  34. * Module: \ref core
  35. *
  36. * - \ref TimerDoc
  37. */
  38. /**
  39. * @page TimerDoc SIP-router's timer documentation
  40. * @verbinclude timers.txt
  41. *
  42. */
  43. #ifndef timer_h
  44. #define timer_h
  45. #define USE_SLOW_TIMER /* use another process to run the timer handlers
  46. marked "slow" */
  47. /*#define TIMER_DEBUG -- compile with -DTIMER_DEBUG*/
  48. #include "clist.h"
  49. #include "dprint.h"
  50. #include "timer_ticks.h"
  51. #ifdef USE_SLOW_TIMER
  52. #include <sys/types.h>
  53. typedef unsigned short slow_idx_t; /* type fot the slow index */
  54. extern pid_t slow_timer_pid;
  55. #endif
  56. /* deprecated, old, kept for compatibility */
  57. typedef void (timer_function)(unsigned int ticks, void* param);
  58. /* deprecated, old, kept for compatibility
  59. get_ticks()*TIMER_TICK used to be the time in s
  60. for new code, use get_ticks_raw() and one of the macros defined in
  61. timer_ticks.h (.e.g TICKS_TO_S(tick) to convert to s or ms )*/
  62. #define TIMER_TICK 1 /* 1 s, kept for compatibility */
  63. /*function prototype to execute on mili-second based basic timers */
  64. typedef void (utimer_function)(unsigned int uticks, void* param);
  65. struct timer_ln; /* forward decl */
  66. /* new
  67. * params:
  68. * - handle pointer to the corresponding struct timer_ln
  69. * return: 0 if the timer is one shot, new expire interval if not, -1
  70. * if periodic
  71. * e.g.: - a periodic timer would return: (ticks_t)(-1) or
  72. * ((struct timer_ln*)handle)->initial_timeout
  73. * - a timer which wants to expire again in x ms would return:
  74. * (x * TICKS_HZ + 999)/1000
  75. */
  76. typedef ticks_t (timer_handler_f)(ticks_t t, struct timer_ln* tl,
  77. void* data);
  78. /* timer flags */
  79. #define F_TIMER_FAST 1
  80. #define F_TIMER_ON_SLOW_LIST 0x100
  81. #define F_TIMER_ACTIVE 0x200 /* timer is running or has run and expired
  82. (one shot) */
  83. #ifdef TIMER_DEBUG
  84. #define F_TIMER_DELETED 0x400
  85. #endif
  86. struct timer_ln{ /* timer_link already used in tm */
  87. struct timer_ln* next;
  88. struct timer_ln* prev;
  89. ticks_t expire;
  90. ticks_t initial_timeout;
  91. void* data;
  92. timer_handler_f* f;
  93. volatile unsigned short flags;
  94. #ifdef USE_SLOW_TIMER
  95. volatile slow_idx_t slow_idx;
  96. #else
  97. unsigned short reserved;
  98. #endif
  99. #ifdef TIMER_DEBUG
  100. unsigned int expires_no; /* timer handler calls */
  101. const char* add_file;
  102. const char* add_func;
  103. unsigned add_line;
  104. unsigned add_calls;
  105. const char* del_file;
  106. const char* del_func;
  107. unsigned del_line;
  108. unsigned int del_calls;
  109. unsigned int init; /* how many times was init/re-init */
  110. #endif
  111. };
  112. void timer_main(void); /* timer main loop, never exists */
  113. int init_timer(void);
  114. int arm_timer(void);
  115. void destroy_timer(void);
  116. #ifdef USE_SLOW_TIMER
  117. int arm_slow_timer(void);
  118. void slow_timer_main(void);
  119. #endif
  120. struct timer_ln* timer_alloc(void);
  121. void timer_free(struct timer_ln* t);
  122. #ifdef TIMER_DEBUG
  123. /* use for a deleted/expired timer that you want to add again */
  124. #define timer_reinit(tl) \
  125. do{ \
  126. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  127. F_TIMER_ACTIVE));\
  128. (tl)->init++; \
  129. }while(0)
  130. #else
  131. /* use for a deleted/expired timer that you want to add again */
  132. #define timer_reinit(tl) \
  133. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  134. F_TIMER_ACTIVE))
  135. #endif
  136. #define timer_init(tl, fun, param, flgs) \
  137. do{ \
  138. memset((tl), 0, sizeof(struct timer_ln)); \
  139. (tl)->f=(fun); \
  140. (tl)->data=(param); \
  141. (tl)->flags=(flgs); \
  142. timer_reinit(tl); \
  143. }while(0)
  144. #ifdef TIMER_DEBUG
  145. int timer_add_safe(struct timer_ln *tl, ticks_t delta,
  146. const char*, const char*, unsigned);
  147. int timer_del_safe(struct timer_ln *tl,
  148. const char*, const char*, unsigned);
  149. #define timer_add(tl, d) \
  150. timer_add_safe((tl), (d), __FILE__, __FUNCTION__, __LINE__)
  151. #define timer_del(tl) \
  152. timer_del_safe((tl), __FILE__, __FUNCTION__, __LINE__)
  153. #else
  154. int timer_add_safe(struct timer_ln *tl, ticks_t delta);
  155. int timer_del_safe(struct timer_ln *tl);
  156. #define timer_add timer_add_safe
  157. #define timer_del timer_del_safe
  158. #endif
  159. void timer_allow_del(void);
  160. /* old timer compatibility functions & structure */
  161. struct sr_timer{
  162. struct timer_ln tl;
  163. int id;
  164. timer_function* timer_f;
  165. void* t_param;
  166. };
  167. /*register a periodic timer;
  168. * ret: <0 on error*/
  169. int register_timer(timer_function f, void* param, unsigned int interval);
  170. ticks_t get_ticks(void);
  171. ticks_t get_ticks_raw(void);
  172. #endif