timer.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id$ */
  2. /*
  3. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef __PJPP_TIMER_HPP__
  21. #define __PJPP_TIMER_HPP__
  22. #include <pj/timer.h>
  23. #include <pj++/types.hpp>
  24. #include <pj/assert.h>
  25. #include <pj++/lock.hpp>
  26. class Pj_Timer_Heap;
  27. //////////////////////////////////////////////////////////////////////////////
  28. // Timer entry.
  29. //
  30. // How to use:
  31. // Derive class from Pj_Timer_Entry and override on_timeout().
  32. // Scheduler timer in Pj_Timer_Heap.
  33. //
  34. class Pj_Timer_Entry : public Pj_Object
  35. {
  36. friend class Pj_Timer_Heap;
  37. public:
  38. //
  39. // Default constructor.
  40. //
  41. Pj_Timer_Entry()
  42. {
  43. entry_.user_data = this;
  44. entry_.cb = &timer_heap_callback;
  45. }
  46. //
  47. // Destructor, do nothing.
  48. //
  49. ~Pj_Timer_Entry()
  50. {
  51. }
  52. //
  53. // Override this to get the timeout notification.
  54. //
  55. virtual void on_timeout(int id) = 0;
  56. private:
  57. pj_timer_entry entry_;
  58. static void timer_heap_callback(pj_timer_heap_t*, pj_timer_entry *e)
  59. {
  60. Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data;
  61. entry->on_timeout(e->id);
  62. }
  63. };
  64. //////////////////////////////////////////////////////////////////////////////
  65. // Timer heap.
  66. //
  67. class Pj_Timer_Heap : public Pj_Object
  68. {
  69. public:
  70. //
  71. // Default constructor.
  72. //
  73. Pj_Timer_Heap()
  74. : ht_(NULL)
  75. {
  76. }
  77. //
  78. // Construct timer heap.
  79. //
  80. Pj_Timer_Heap(Pj_Pool *pool, pj_size_t initial_count)
  81. : ht_(NULL)
  82. {
  83. create(pool, initial_count);
  84. }
  85. //
  86. // Destructor.
  87. //
  88. ~Pj_Timer_Heap()
  89. {
  90. destroy();
  91. }
  92. //
  93. // Create
  94. //
  95. pj_status_t create(Pj_Pool *pool, pj_size_t initial_count)
  96. {
  97. destroy();
  98. return pj_timer_heap_create(pool->pool_(), initial_count, &ht_);
  99. }
  100. //
  101. // Destroy
  102. //
  103. void destroy()
  104. {
  105. if (ht_) {
  106. pj_timer_heap_destroy(ht_);
  107. ht_ = NULL;
  108. }
  109. }
  110. //
  111. // Get pjlib compatible timer heap object.
  112. //
  113. pj_timer_heap_t *get_timer_heap()
  114. {
  115. return ht_;
  116. }
  117. //
  118. // Set the lock object.
  119. //
  120. void set_lock( Pj_Lock *lock, bool auto_delete )
  121. {
  122. pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete);
  123. }
  124. //
  125. // Set maximum number of timed out entries to be processed per poll.
  126. //
  127. unsigned set_max_timed_out_per_poll(unsigned count)
  128. {
  129. return pj_timer_heap_set_max_timed_out_per_poll(ht_, count);
  130. }
  131. //
  132. // Schedule a timer.
  133. //
  134. bool schedule( Pj_Timer_Entry *ent, const Pj_Time_Val &delay,
  135. int id)
  136. {
  137. ent->entry_.id = id;
  138. return pj_timer_heap_schedule(ht_, &ent->entry_, &delay) == 0;
  139. }
  140. //
  141. // Cancel a timer.
  142. //
  143. bool cancel(Pj_Timer_Entry *ent)
  144. {
  145. return pj_timer_heap_cancel(ht_, &ent->entry_) == 1;
  146. }
  147. //
  148. // Get current number of timers
  149. //
  150. pj_size_t count()
  151. {
  152. return pj_timer_heap_count(ht_);
  153. }
  154. //
  155. // Get the earliest time.
  156. // Return false if no timer is found.
  157. //
  158. bool earliest_time(Pj_Time_Val *t)
  159. {
  160. return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS;
  161. }
  162. //
  163. // Poll the timer.
  164. // Return number of timed out entries has been called.
  165. //
  166. unsigned poll(Pj_Time_Val *next_delay = NULL)
  167. {
  168. return pj_timer_heap_poll(ht_, next_delay);
  169. }
  170. private:
  171. pj_timer_heap_t *ht_;
  172. };
  173. #endif /* __PJPP_TIMER_HPP__ */