timer.hpp 4.2 KB

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