Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 1 | /* $Id$ |
Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 2 | */
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 3 | #ifndef __PJPP_TIMER_H__
|
| 4 | #define __PJPP_TIMER_H__
|
| 5 |
|
| 6 | #include <pj/timer.h>
|
| 7 | #include <pj++/types.hpp>
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 8 | #include <pj/assert.h>
|
| 9 | #include <pj++/lock.hpp>
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 10 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 11 | class Pj_Timer_Heap;
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 12 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 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
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 21 | {
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 22 | friend class Pj_Timer_Heap;
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 23 |
|
| 24 | public:
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 25 | //
|
| 26 | // Default constructor.
|
| 27 | //
|
| 28 | Pj_Timer_Entry()
|
| 29 | {
|
| 30 | entry_.user_data = this;
|
| 31 | entry_.cb = &timer_heap_callback;
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 32 | }
|
| 33 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 34 | //
|
| 35 | // Destructor, do nothing.
|
| 36 | //
|
| 37 | ~Pj_Timer_Entry()
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 38 | {
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 39 | }
|
| 40 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 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)
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 50 | {
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 51 | Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data;
|
| 52 | entry->on_timeout(e->id);
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 53 | }
|
| 54 |
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 55 | };
|
| 56 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 57 | //////////////////////////////////////////////////////////////////////////////
|
| 58 | // Timer heap.
|
| 59 | //
|
| 60 | class Pj_Timer_Heap : public Pj_Object
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 61 | {
|
| 62 | public:
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 63 | //
|
| 64 | // Default constructor.
|
| 65 | //
|
| 66 | Pj_Timer_Heap()
|
| 67 | : ht_(NULL)
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 68 | {
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 69 | }
|
| 70 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 71 | //
|
| 72 | // Construct timer heap.
|
| 73 | //
|
| 74 | Pj_Timer_Heap(Pj_Pool *pool, pj_size_t initial_count)
|
| 75 | : ht_(NULL)
|
| 76 | {
|
| 77 | create(pool, initial_count);
|
| 78 | }
|
| 79 |
|
| 80 | //
|
| 81 | // Destructor.
|
| 82 | //
|
| 83 | ~Pj_Timer_Heap()
|
| 84 | {
|
| 85 | destroy();
|
| 86 | }
|
| 87 |
|
| 88 | //
|
| 89 | // Create
|
| 90 | //
|
| 91 | pj_status_t create(Pj_Pool *pool, pj_size_t initial_count)
|
| 92 | {
|
| 93 | destroy();
|
| 94 | return pj_timer_heap_create(pool->pool_(), initial_count, &ht_);
|
| 95 | }
|
| 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 | //
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 111 | pj_timer_heap_t *get_timer_heap()
|
| 112 | {
|
| 113 | return ht_;
|
| 114 | }
|
| 115 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 116 | //
|
| 117 | // Set the lock object.
|
| 118 | //
|
| 119 | void set_lock( Pj_Lock *lock, bool auto_delete )
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 120 | {
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 121 | pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete);
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 122 | }
|
| 123 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 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)
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 128 | {
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 129 | return pj_timer_heap_set_max_timed_out_per_poll(ht_, count);
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 130 | }
|
| 131 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 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 | //
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 153 | pj_size_t count()
|
| 154 | {
|
| 155 | return pj_timer_heap_count(ht_);
|
| 156 | }
|
| 157 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 158 | //
|
| 159 | // Get the earliest time.
|
| 160 | // Return false if no timer is found.
|
| 161 | //
|
| 162 | bool earliest_time(Pj_Time_Val *t)
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 163 | {
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 164 | return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS;
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 165 | }
|
| 166 |
|
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame^] | 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)
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 172 | {
|
| 173 | return pj_timer_heap_poll(ht_, next_delay);
|
| 174 | }
|
| 175 |
|
| 176 | private:
|
| 177 | pj_timer_heap_t *ht_;
|
| 178 | };
|
| 179 |
|
| 180 | #endif /* __PJPP_TIMER_H__ */
|