timer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 ser, a free SIP server.
  10. *
  11. * ser 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. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /* History:
  31. * --------
  32. * 2005-07-27 complete re-design/re-implemnetation (andrei)
  33. */
  34. #ifndef timer_h
  35. #define timer_h
  36. #define USE_SLOW_TIMER /* use another process to run the timer handlers
  37. marked "slow" */
  38. /*#define TIMER_DEBUG -- compile with -DTIMER_DEBUG*/
  39. #include "clist.h"
  40. #include "dprint.h"
  41. #include "timer_ticks.h"
  42. #ifdef USE_SLOW_TIMER
  43. #include <sys/types.h>
  44. typedef unsigned short slow_idx_t; /* type fot the slow index */
  45. extern pid_t slow_timer_pid;
  46. #endif
  47. /* deprecated, old, kept for compatibility */
  48. typedef void (timer_function)(unsigned int ticks, void* param);
  49. /* deprecated, old, kept for compatibility
  50. get_ticks()*TIMER_TICK used to be the time in s
  51. for new code, use get_ticks_raw() and one of the macros defined in
  52. time_ticks.h (.e.g TICKS_TO_S(tick) to convert to s or ms )*/
  53. #define TIMER_TICK 1 /* 1 s, kept for compatibility */
  54. struct timer_ln; /* forward decl */
  55. /* new
  56. * params:
  57. * - handle pointer to the corresponding struct timer_ln
  58. * return: 0 if the timer is one shot, new expire interval if not, -1
  59. * if periodic
  60. * e.g.: - a periodic timer would return: (ticks_t)(-1) or
  61. * ((struct timer_ln*)handle)->initial_timeout
  62. * - a timer which wants to expire again in x ms would return:
  63. * (x * TICKS_HZ + 999)/1000
  64. */
  65. typedef ticks_t (timer_handler_f)(ticks_t t, struct timer_ln* tl,
  66. void* data);
  67. /* timer flags */
  68. #define F_TIMER_FAST 1
  69. #define F_TIMER_ON_SLOW_LIST 0x100
  70. #define F_TIMER_ACTIVE 0x200 /* timer is running or has run and expired
  71. (one shot) */
  72. #ifdef TIMER_DEBUG
  73. #define F_TIMER_DELETED 0x400
  74. #endif
  75. struct timer_ln{ /* timer_link already used in tm */
  76. struct timer_ln* next;
  77. struct timer_ln* prev;
  78. ticks_t expire;
  79. ticks_t initial_timeout;
  80. void* data;
  81. timer_handler_f* f;
  82. volatile unsigned short flags;
  83. #ifdef USE_SLOW_TIMER
  84. volatile slow_idx_t slow_idx;
  85. #else
  86. unsigned short reserved;
  87. #endif
  88. #ifdef TIMER_DEBUG
  89. unsigned int expires_no; /* timer handler calls */
  90. const char* add_file;
  91. const char* add_func;
  92. unsigned add_line;
  93. unsigned add_calls;
  94. const char* del_file;
  95. const char* del_func;
  96. unsigned del_line;
  97. unsigned int del_calls;
  98. unsigned int init; /* how many times was init/re-init */
  99. #endif
  100. };
  101. void timer_main(); /* timer main loop, never exists */
  102. int init_timer();
  103. int arm_timer();
  104. void destroy_timer();
  105. #ifdef USE_SLOW_TIMER
  106. int arm_slow_timer();
  107. void slow_timer_main();
  108. #endif
  109. struct timer_ln* timer_alloc();
  110. void timer_free(struct timer_ln* t);
  111. #ifdef TIMER_DEBUG
  112. /* use for a deleted/expired timer that you want to add again */
  113. #define timer_reinit(tl) \
  114. do{ \
  115. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  116. F_TIMER_ACTIVE));\
  117. (tl)->init++; \
  118. }while(0)
  119. #else
  120. /* use for a deleted/expired timer that you want to add again */
  121. #define timer_reinit(tl) \
  122. (tl)->flags&=~((unsigned short)(F_TIMER_ON_SLOW_LIST | \
  123. F_TIMER_ACTIVE))
  124. #endif
  125. #define timer_init(tl, fun, param, flgs) \
  126. do{ \
  127. memset((tl), 0, sizeof(struct timer_ln)); \
  128. (tl)->f=(fun); \
  129. (tl)->data=(param); \
  130. (tl)->flags=(flgs); \
  131. timer_reinit(tl); \
  132. }while(0)
  133. #ifdef TIMER_DEBUG
  134. int timer_add_safe(struct timer_ln *tl, ticks_t delta,
  135. const char*, const char*, unsigned);
  136. int timer_del_safe(struct timer_ln *tl,
  137. const char*, const char*, unsigned);
  138. #define timer_add(tl, d) \
  139. timer_add_safe((tl), (d), __FILE__, __FUNCTION__, __LINE__)
  140. #define timer_del(tl) \
  141. timer_del_safe((tl), __FILE__, __FUNCTION__, __LINE__)
  142. #else
  143. int timer_add_safe(struct timer_ln *tl, ticks_t delta);
  144. int timer_del_safe(struct timer_ln *tl);
  145. #define timer_add timer_add_safe
  146. #define timer_del timer_del_safe
  147. #endif
  148. void timer_allow_del();
  149. /* old timer compatibility functions & structure */
  150. struct sr_timer{
  151. struct timer_ln tl;
  152. int id;
  153. timer_function* timer_f;
  154. void* t_param;
  155. };
  156. /*register a periodic timer;
  157. * ret: <0 on error*/
  158. int register_timer(timer_function f, void* param, unsigned int interval);
  159. ticks_t get_ticks();
  160. ticks_t get_ticks_raw();
  161. #endif