Changeset 36 for pjproject/main/pjlib/include/pj++/timer.hpp
- Timestamp:
- Nov 9, 2005 3:37:19 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/main/pjlib/include/pj++/timer.hpp
r29 r36 1 1 /* $Id$ 2 *3 2 */ 4 3 #ifndef __PJPP_TIMER_H__ … … 7 6 #include <pj/timer.h> 8 7 #include <pj++/types.hpp> 8 #include <pj/assert.h> 9 #include <pj++/lock.hpp> 9 10 10 class P J_Timer_Heap;11 class Pj_Timer_Heap; 11 12 12 class PJ_Timer_Entry : private pj_timer_entry 13 ////////////////////////////////////////////////////////////////////////////// 14 // Timer entry. 15 // 16 // How to use: 17 // Derive class from Pj_Timer_Entry and override on_timeout(). 18 // Scheduler timer in Pj_Timer_Heap. 19 // 20 class Pj_Timer_Entry : public Pj_Object 13 21 { 14 friend class P J_Timer_Heap;22 friend class Pj_Timer_Heap; 15 23 16 24 public: 17 static void timer_heap_callback(pj_timer_heap_t *, pj_timer_entry *);18 19 PJ_Timer_Entry() { cb = &timer_heap_callback; }20 P J_Timer_Entry(int arg_id, void *arg_user_data)21 { 22 cb = &timer_heap_callback; 23 init(arg_id, arg_user_data); 25 // 26 // Default constructor. 27 // 28 Pj_Timer_Entry() 29 { 30 entry_.user_data = this; 31 entry_.cb = &timer_heap_callback; 24 32 } 25 33 26 virtual void on_timeout() = 0; 27 28 void init(int arg_id, void *arg_user_data) 34 // 35 // Destructor, do nothing. 36 // 37 ~Pj_Timer_Entry() 29 38 { 30 id = arg_id;31 user_data = arg_user_data;32 39 } 33 40 34 int get_id() const 41 // 42 // Override this to get the timeout notification. 43 // 44 virtual void on_timeout(int id) = 0; 45 46 private: 47 pj_timer_entry entry_; 48 49 static void timer_heap_callback(pj_timer_heap_t *th, pj_timer_entry *e) 35 50 { 36 return id; 51 Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data; 52 entry->on_timeout(e->id); 37 53 } 38 54 39 void set_id(int arg_id) 55 }; 56 57 ////////////////////////////////////////////////////////////////////////////// 58 // Timer heap. 59 // 60 class Pj_Timer_Heap : public Pj_Object 61 { 62 public: 63 // 64 // Default constructor. 65 // 66 Pj_Timer_Heap() 67 : ht_(NULL) 40 68 { 41 id = arg_id;42 69 } 43 70 44 void set_user_data(void *arg_user_data) 71 // 72 // Construct timer heap. 73 // 74 Pj_Timer_Heap(Pj_Pool *pool, pj_size_t initial_count) 75 : ht_(NULL) 45 76 { 46 user_data = arg_user_data;77 create(pool, initial_count); 47 78 } 48 79 49 void *get_user_data() const 80 // 81 // Destructor. 82 // 83 ~Pj_Timer_Heap() 50 84 { 51 return user_data;85 destroy(); 52 86 } 53 87 54 const PJ_Time_Val &get_timeout() const 88 // 89 // Create 90 // 91 pj_status_t create(Pj_Pool *pool, pj_size_t initial_count) 55 92 { 56 pj_assert(sizeof(PJ_Time_Val) == sizeof(pj_time_val)); 57 return (PJ_Time_Val&)_timer_value; 58 } 59 }; 60 61 class PJ_Timer_Heap 62 { 63 public: 64 PJ_Timer_Heap() {} 65 66 bool create(PJ_Pool *pool, pj_size_t initial_count, 67 unsigned flag = PJ_TIMER_HEAP_SYNCHRONIZE) 68 { 69 ht_ = pj_timer_heap_create(pool->pool_(), initial_count, flag); 70 return ht_ != NULL; 93 destroy(); 94 return pj_timer_heap_create(pool->pool_(), initial_count, &ht_); 71 95 } 72 96 97 // 98 // Destroy 99 // 100 void destroy() 101 { 102 if (ht_) { 103 pj_timer_heap_destroy(ht_); 104 ht_ = NULL; 105 } 106 } 107 108 // 109 // Get pjlib compatible timer heap object. 110 // 73 111 pj_timer_heap_t *get_timer_heap() 74 112 { … … 76 114 } 77 115 78 bool schedule( PJ_Timer_Entry *ent, const PJ_Time_Val &delay) 116 // 117 // Set the lock object. 118 // 119 void set_lock( Pj_Lock *lock, bool auto_delete ) 79 120 { 80 return pj_timer_heap_schedule(ht_, ent, &delay) == 0;121 pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete); 81 122 } 82 123 83 bool cancel(PJ_Timer_Entry *ent) 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) 84 128 { 85 return pj_timer_heap_cancel(ht_, ent) == 1;129 return pj_timer_heap_set_max_timed_out_per_poll(ht_, count); 86 130 } 87 131 132 // 133 // Schedule a timer. 134 // 135 bool schedule( Pj_Timer_Entry *ent, const Pj_Time_Val &delay, 136 int id) 137 { 138 ent->entry_.id = id; 139 return pj_timer_heap_schedule(ht_, &ent->entry_, &delay) == 0; 140 } 141 142 // 143 // Cancel a timer. 144 // 145 bool cancel(Pj_Timer_Entry *ent) 146 { 147 return pj_timer_heap_cancel(ht_, &ent->entry_) == 1; 148 } 149 150 // 151 // Get current number of timers 152 // 88 153 pj_size_t count() 89 154 { … … 91 156 } 92 157 93 void earliest_time(PJ_Time_Val *t) 158 // 159 // Get the earliest time. 160 // Return false if no timer is found. 161 // 162 bool earliest_time(Pj_Time_Val *t) 94 163 { 95 pj_timer_heap_earliest_time(ht_, t);164 return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS; 96 165 } 97 166 98 int poll(PJ_Time_Val *next_delay = NULL) 167 // 168 // Poll the timer. 169 // Return number of timed out entries has been called. 170 // 171 unsigned poll(Pj_Time_Val *next_delay = NULL) 99 172 { 100 173 return pj_timer_heap_poll(ht_, next_delay);
Note: See TracChangeset
for help on using the changeset viewer.