timer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. struct timer_ln; /* forward decl */
  64. /* new
  65. * params:
  66. * - handle pointer to the corresponding struct timer_ln
  67. * return: 0 if the timer is one shot, new expire interval if not, -1
  68. * if periodic
  69. * e.g.: - a periodic timer would return: (ticks_t)(-1) or
  70. * ((struct timer_ln*)handle)->initial_timeout
  71. * - a timer which wants to expire again in x ms would return:
  72. * (x * TICKS_HZ + 999)/1000
  73. */
  74. typedef ticks_t (timer_handler_f)(ticks_t t, struct timer_ln* tl,
  75. void* data);
  76. /* timer flags */
  77. #define F_TIMER_FAST 1
  78. #define F_TIMER_ON_SLOW_LIST 0x100
  79. #define F_TIMER_ACTIVE 0x200 /* timer is running or has run and expired
  80. (one shot) */
  81. #ifdef TIMER_DEBUG
  82. #define F_TIMER_DELETED 0x400
  83. #endif
  84. struct timer_ln{ /* timer_link already used in tm */
  85. struct timer_ln* next;
  86. struct timer_ln* prev;
  87. ticks_t expire;
  88. ticks_t initial_timeout;
  89. void* data;
  90. timer_handler_f* f;
  91. volatile unsigned short flags;
  92. #ifdef USE_SLOW_TIMER
  93. volatile slow_idx_t slow_idx;
  94. #else
  95. unsigned short reserved;
  96. #endif
  97. #ifdef TIMER_DEBUG
  98. unsigned int expires_no; /* timer handler calls */
  99. const char* add_file;
  100. const char* add_func;
  101. unsigned add_line;
  102. unsigned add_calls;
  103. const char* del_file;
  104. const char* del_func;
  105. unsigned del_line;
  106. unsigned int del_calls;
  107. unsigned int init; /* how many times was init/re-init */
  108. #endif
  109. };
  110. void timer_main(); /* timer main loop, never exists */
  111. int init_timer();
  112. int arm_timer();
  113. void destroy_timer();
  114. #ifdef USE_SLOW_TIMER
  115. int arm_slow_timer();
  116. void slow_timer_main();
  117. #endif
  118. struct timer_ln* timer_alloc();
  119. void timer_free(struct timer_ln* t);
  120. #ifdef TIMER_DEBUG
  121. /* use for a deleted/expired timer that you want to add again */
  122. #define timer_reinit(tl) \
  123. do{ \
  124. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  125. F_TIMER_ACTIVE));\
  126. (tl)->init++; \
  127. }while(0)
  128. #else
  129. /* use for a deleted/expired timer that you want to add again */
  130. #define timer_reinit(tl) \
  131. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  132. F_TIMER_ACTIVE))
  133. #endif
  134. #define timer_init(tl, fun, param, flgs) \
  135. do{ \
  136. memset((tl), 0, sizeof(struct timer_ln)); \
  137. (tl)->f=(fun); \
  138. (tl)->data=(param); \
  139. (tl)->flags=(flgs); \
  140. timer_reinit(tl); \
  141. }while(0)
  142. #ifdef TIMER_DEBUG
  143. int timer_add_safe(struct timer_ln *tl, ticks_t delta,
  144. const char*, const char*, unsigned);
  145. int timer_del_safe(struct timer_ln *tl,
  146. const char*, const char*, unsigned);
  147. #define timer_add(tl, d) \
  148. timer_add_safe((tl), (d), __FILE__, __FUNCTION__, __LINE__)
  149. #define timer_del(tl) \
  150. timer_del_safe((tl), __FILE__, __FUNCTION__, __LINE__)
  151. #else
  152. int timer_add_safe(struct timer_ln *tl, ticks_t delta);
  153. int timer_del_safe(struct timer_ln *tl);
  154. #define timer_add timer_add_safe
  155. #define timer_del timer_del_safe
  156. #endif
  157. void timer_allow_del();
  158. /* old timer compatibility functions & structure */
  159. struct sr_timer{
  160. struct timer_ln tl;
  161. int id;
  162. timer_function* timer_f;
  163. void* t_param;
  164. };
  165. /*register a periodic timer;
  166. * ret: <0 on error*/
  167. int register_timer(timer_function f, void* param, unsigned int interval);
  168. ticks_t get_ticks();
  169. ticks_t get_ticks_raw();
  170. #endif