timer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifndef __PJ_TIMER_H__
  17. #define __PJ_TIMER_H__
  18. /**
  19. * @file timer.h
  20. * @brief Timer Heap
  21. */
  22. #include <pj/types.h>
  23. #include <pj/lock.h>
  24. #if PJ_TIMER_USE_LINKED_LIST
  25. # include <pj/list.h>
  26. #endif
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJ_TIMER Timer Heap Management.
  30. * @ingroup PJ_MISC
  31. * @brief
  32. * The timer scheduling implementation here is based on ACE library's
  33. * ACE_Timer_Heap, with only little modification to suit our library's style
  34. * (I even left most of the comments in the original source).
  35. *
  36. * To quote the original quote in ACE_Timer_Heap_T class:
  37. *
  38. * This implementation uses a heap-based callout queue of
  39. * absolute times. Therefore, in the average and worst case,
  40. * scheduling, canceling, and expiring timers is O(log N) (where
  41. * N is the total number of timers). In addition, we can also
  42. * preallocate as many \a ACE_Timer_Nodes as there are slots in
  43. * the heap. This allows us to completely remove the need for
  44. * dynamic memory allocation, which is important for real-time
  45. * systems.
  46. *
  47. * You can find the fine ACE library at:
  48. * http://www.cs.wustl.edu/~schmidt/ACE.html
  49. *
  50. * ACE is Copyright (C)1993-2006 Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
  51. *
  52. * @{
  53. *
  54. * \section pj_timer_examples_sec Examples
  55. *
  56. * For some examples on how to use the timer heap, please see the link below.
  57. *
  58. * - Timer test: \src{pjlib/src/pjlib-test/timer.c}
  59. */
  60. /**
  61. * The type for internal timer ID.
  62. */
  63. typedef int pj_timer_id_t;
  64. /**
  65. * Forward declaration for pj_timer_entry.
  66. */
  67. struct pj_timer_entry;
  68. /**
  69. * The type of callback function to be called by timer scheduler when a timer
  70. * has expired.
  71. *
  72. * @param timer_heap The timer heap.
  73. * @param entry Timer entry which timer's has expired.
  74. */
  75. typedef void pj_timer_heap_callback(pj_timer_heap_t *timer_heap,
  76. struct pj_timer_entry *entry);
  77. /**
  78. * This structure represents an entry to the timer.
  79. */
  80. typedef struct pj_timer_entry
  81. {
  82. #if !PJ_TIMER_USE_COPY && PJ_TIMER_USE_LINKED_LIST
  83. /**
  84. * Standard list members.
  85. */
  86. PJ_DECL_LIST_MEMBER(struct pj_timer_entry);
  87. #endif
  88. /**
  89. * User data to be associated with this entry.
  90. * Applications normally will put the instance of object that
  91. * owns the timer entry in this field.
  92. */
  93. void *user_data;
  94. /**
  95. * Arbitrary ID assigned by the user/owner of this entry.
  96. * Applications can use this ID to distinguish multiple
  97. * timer entries that share the same callback and user_data.
  98. */
  99. int id;
  100. /**
  101. * Callback to be called when the timer expires.
  102. */
  103. pj_timer_heap_callback *cb;
  104. /**
  105. * Internal unique timer ID, which is assigned by the timer heap.
  106. * Positive values indicate that the timer entry is running,
  107. * while -1 means that it's not. Any other value may indicate that it
  108. * hasn't been properly initialised or is in a bad state.
  109. * Application should not touch this ID.
  110. */
  111. pj_timer_id_t _timer_id;
  112. #if !PJ_TIMER_USE_COPY
  113. /**
  114. * The future time when the timer expires, which the value is updated
  115. * by timer heap when the timer is scheduled.
  116. */
  117. pj_time_val _timer_value;
  118. /**
  119. * Internal: the group lock used by this entry, set when
  120. * pj_timer_heap_schedule_w_lock() is used.
  121. */
  122. pj_grp_lock_t *_grp_lock;
  123. #if PJ_TIMER_DEBUG
  124. const char *src_file;
  125. int src_line;
  126. #endif
  127. #endif
  128. } pj_timer_entry;
  129. /**
  130. * Calculate memory size required to create a timer heap.
  131. *
  132. * @param count Number of timer entries to be supported.
  133. * @return Memory size requirement in bytes.
  134. */
  135. PJ_DECL(pj_size_t) pj_timer_heap_mem_size(pj_size_t count);
  136. /**
  137. * Create a timer heap.
  138. *
  139. * @param pool The pool where allocations in the timer heap will be
  140. * allocated. The timer heap will dynamicly allocate
  141. * more storate from the pool if the number of timer
  142. * entries registered is more than the size originally
  143. * requested when calling this function.
  144. * @param count The maximum number of timer entries to be supported
  145. * initially. If the application registers more entries
  146. * during runtime, then the timer heap will resize.
  147. * @param ht Pointer to receive the created timer heap.
  148. *
  149. * @return PJ_SUCCESS, or the appropriate error code.
  150. */
  151. PJ_DECL(pj_status_t) pj_timer_heap_create( pj_pool_t *pool,
  152. pj_size_t count,
  153. pj_timer_heap_t **ht);
  154. /**
  155. * Destroy the timer heap.
  156. *
  157. * @param ht The timer heap.
  158. */
  159. PJ_DECL(void) pj_timer_heap_destroy( pj_timer_heap_t *ht );
  160. /**
  161. * Set lock object to be used by the timer heap. By default, the timer heap
  162. * uses dummy synchronization.
  163. *
  164. * @param ht The timer heap.
  165. * @param lock The lock object to be used for synchronization.
  166. * @param auto_del If nonzero, the lock object will be destroyed when
  167. * the timer heap is destroyed.
  168. */
  169. PJ_DECL(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht,
  170. pj_lock_t *lock,
  171. pj_bool_t auto_del );
  172. /**
  173. * Set maximum number of timed out entries to process in a single poll.
  174. *
  175. * @param ht The timer heap.
  176. * @param count Number of entries.
  177. *
  178. * @return The old number.
  179. */
  180. PJ_DECL(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht,
  181. unsigned count );
  182. /**
  183. * Initialize a timer entry. Application should call this function at least
  184. * once before scheduling the entry to the timer heap, to properly initialize
  185. * the timer entry.
  186. *
  187. * @param entry The timer entry to be initialized.
  188. * @param id Arbitrary ID assigned by the user/owner of this entry.
  189. * Applications can use this ID to distinguish multiple
  190. * timer entries that share the same callback and user_data.
  191. * @param user_data User data to be associated with this entry.
  192. * Applications normally will put the instance of object that
  193. * owns the timer entry in this field.
  194. * @param cb Callback function to be called when the timer elapses.
  195. *
  196. * @return The timer entry itself.
  197. */
  198. PJ_DECL(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry,
  199. int id,
  200. void *user_data,
  201. pj_timer_heap_callback *cb );
  202. /**
  203. * Queries whether a timer entry is currently running.
  204. *
  205. * @param entry The timer entry to query.
  206. *
  207. * @return PJ_TRUE if the timer is running. PJ_FALSE if not.
  208. */
  209. PJ_DECL(pj_bool_t) pj_timer_entry_running( pj_timer_entry *entry );
  210. /**
  211. * Schedule a timer entry which will expire AFTER the specified delay.
  212. *
  213. * @param ht The timer heap.
  214. * @param entry The entry to be registered.
  215. * @param delay The interval to expire.
  216. * @return PJ_SUCCESS, or the appropriate error code.
  217. */
  218. #if PJ_TIMER_DEBUG
  219. # define pj_timer_heap_schedule(ht,e,d) \
  220. pj_timer_heap_schedule_dbg(ht,e,d,__FILE__,__LINE__)
  221. PJ_DECL(pj_status_t) pj_timer_heap_schedule_dbg( pj_timer_heap_t *ht,
  222. pj_timer_entry *entry,
  223. const pj_time_val *delay,
  224. const char *src_file,
  225. int src_line);
  226. #else
  227. PJ_DECL(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht,
  228. pj_timer_entry *entry,
  229. const pj_time_val *delay);
  230. #endif /* PJ_TIMER_DEBUG */
  231. /**
  232. * Schedule a timer entry which will expire AFTER the specified delay, and
  233. * increment the reference counter of the group lock while the timer entry
  234. * is active. The group lock reference counter will automatically be released
  235. * after the timer callback is called or when the timer is cancelled.
  236. *
  237. * @param ht The timer heap.
  238. * @param entry The entry to be registered.
  239. * @param delay The interval to expire.
  240. * @param id_val The value to be set to the "id" field of the timer entry
  241. * once the timer is scheduled.
  242. * @param grp_lock The group lock.
  243. *
  244. * @return PJ_SUCCESS, or the appropriate error code.
  245. */
  246. #if PJ_TIMER_DEBUG
  247. # define pj_timer_heap_schedule_w_grp_lock(ht,e,d,id,g) \
  248. pj_timer_heap_schedule_w_grp_lock_dbg(ht,e,d,id,g,__FILE__,__LINE__)
  249. PJ_DECL(pj_status_t) pj_timer_heap_schedule_w_grp_lock_dbg(
  250. pj_timer_heap_t *ht,
  251. pj_timer_entry *entry,
  252. const pj_time_val *delay,
  253. int id_val,
  254. pj_grp_lock_t *grp_lock,
  255. const char *src_file,
  256. int src_line);
  257. #else
  258. PJ_DECL(pj_status_t) pj_timer_heap_schedule_w_grp_lock(
  259. pj_timer_heap_t *ht,
  260. pj_timer_entry *entry,
  261. const pj_time_val *delay,
  262. int id_val,
  263. pj_grp_lock_t *grp_lock);
  264. #endif /* PJ_TIMER_DEBUG */
  265. /**
  266. * Cancel a previously registered timer. This will also decrement the
  267. * reference counter of the group lock associated with the timer entry,
  268. * if the entry was scheduled with one.
  269. *
  270. * @param ht The timer heap.
  271. * @param entry The entry to be cancelled.
  272. * @return The number of timer cancelled, which should be one if the
  273. * entry has really been registered, or zero if no timer was
  274. * cancelled.
  275. */
  276. PJ_DECL(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
  277. pj_timer_entry *entry);
  278. /**
  279. * Cancel only if the previously registered timer is active. This will
  280. * also decrement the reference counter of the group lock associated
  281. * with the timer entry, if the entry was scheduled with one. In any
  282. * case, set the "id" to the specified value.
  283. *
  284. * @param ht The timer heap.
  285. * @param entry The entry to be cancelled.
  286. * @param id_val Value to be set to "id"
  287. *
  288. * @return The number of timer cancelled, which should be one if the
  289. * entry has really been registered, or zero if no timer was
  290. * cancelled.
  291. */
  292. PJ_DECL(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht,
  293. pj_timer_entry *entry,
  294. int id_val);
  295. /**
  296. * Get the number of timer entries.
  297. *
  298. * @param ht The timer heap.
  299. * @return The number of timer entries.
  300. */
  301. PJ_DECL(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht );
  302. /**
  303. * Get the earliest time registered in the timer heap. The timer heap
  304. * MUST have at least one timer being scheduled (application should use
  305. * #pj_timer_heap_count() before calling this function).
  306. *
  307. * @param ht The timer heap.
  308. * @param timeval The time deadline of the earliest timer entry.
  309. *
  310. * @return PJ_SUCCESS, or PJ_ENOTFOUND if no entry is scheduled.
  311. */
  312. PJ_DECL(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t *ht,
  313. pj_time_val *timeval);
  314. /**
  315. * Poll the timer heap, check for expired timers and call the callback for
  316. * each of the expired timers.
  317. *
  318. * Note: polling the timer heap is not necessary in Symbian. Please see
  319. * @ref PJ_SYMBIAN_OS for more info.
  320. *
  321. * @param ht The timer heap.
  322. * @param next_delay If this parameter is not NULL, it will be filled up with
  323. * the time delay until the next timer elapsed, or
  324. * PJ_MAXINT32 in the sec part if no entry exist.
  325. *
  326. * @return The number of timers expired.
  327. */
  328. PJ_DECL(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht,
  329. pj_time_val *next_delay);
  330. #if PJ_TIMER_DEBUG
  331. /**
  332. * Dump timer heap entries.
  333. *
  334. * @param ht The timer heap.
  335. */
  336. PJ_DECL(void) pj_timer_heap_dump(pj_timer_heap_t *ht);
  337. #endif
  338. /**
  339. * @}
  340. */
  341. PJ_END_DECL
  342. #endif /* __PJ_TIMER_H__ */