blob: b1070421baf5fcb1d3b1f424574850f9f4c1bec8 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijono4766ffe2005-11-01 17:56:59 +00002 */
Benny Prijono0a749f12005-10-31 21:02:30 +00003#ifndef __PJPP_TIMER_H__
4#define __PJPP_TIMER_H__
5
6#include <pj/timer.h>
7#include <pj++/types.hpp>
Benny Prijono85d3f452005-11-09 15:37:19 +00008#include <pj/assert.h>
9#include <pj++/lock.hpp>
Benny Prijono0a749f12005-10-31 21:02:30 +000010
Benny Prijono85d3f452005-11-09 15:37:19 +000011class Pj_Timer_Heap;
Benny Prijono0a749f12005-10-31 21:02:30 +000012
Benny Prijono85d3f452005-11-09 15:37:19 +000013//////////////////////////////////////////////////////////////////////////////
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//
20class Pj_Timer_Entry : public Pj_Object
Benny Prijono0a749f12005-10-31 21:02:30 +000021{
Benny Prijono85d3f452005-11-09 15:37:19 +000022 friend class Pj_Timer_Heap;
Benny Prijono0a749f12005-10-31 21:02:30 +000023
24public:
Benny Prijono85d3f452005-11-09 15:37:19 +000025 //
26 // Default constructor.
27 //
28 Pj_Timer_Entry()
29 {
30 entry_.user_data = this;
31 entry_.cb = &timer_heap_callback;
Benny Prijono0a749f12005-10-31 21:02:30 +000032 }
33
Benny Prijono85d3f452005-11-09 15:37:19 +000034 //
35 // Destructor, do nothing.
36 //
37 ~Pj_Timer_Entry()
Benny Prijono0a749f12005-10-31 21:02:30 +000038 {
Benny Prijono0a749f12005-10-31 21:02:30 +000039 }
40
Benny Prijono85d3f452005-11-09 15:37:19 +000041 //
42 // Override this to get the timeout notification.
43 //
44 virtual void on_timeout(int id) = 0;
45
46private:
47 pj_timer_entry entry_;
48
49 static void timer_heap_callback(pj_timer_heap_t *th, pj_timer_entry *e)
Benny Prijono0a749f12005-10-31 21:02:30 +000050 {
Benny Prijono85d3f452005-11-09 15:37:19 +000051 Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data;
52 entry->on_timeout(e->id);
Benny Prijono0a749f12005-10-31 21:02:30 +000053 }
54
Benny Prijono0a749f12005-10-31 21:02:30 +000055};
56
Benny Prijono85d3f452005-11-09 15:37:19 +000057//////////////////////////////////////////////////////////////////////////////
58// Timer heap.
59//
60class Pj_Timer_Heap : public Pj_Object
Benny Prijono0a749f12005-10-31 21:02:30 +000061{
62public:
Benny Prijono85d3f452005-11-09 15:37:19 +000063 //
64 // Default constructor.
65 //
66 Pj_Timer_Heap()
67 : ht_(NULL)
Benny Prijono0a749f12005-10-31 21:02:30 +000068 {
Benny Prijono0a749f12005-10-31 21:02:30 +000069 }
70
Benny Prijono85d3f452005-11-09 15:37:19 +000071 //
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 Prijono0a749f12005-10-31 21:02:30 +0000111 pj_timer_heap_t *get_timer_heap()
112 {
113 return ht_;
114 }
115
Benny Prijono85d3f452005-11-09 15:37:19 +0000116 //
117 // Set the lock object.
118 //
119 void set_lock( Pj_Lock *lock, bool auto_delete )
Benny Prijono0a749f12005-10-31 21:02:30 +0000120 {
Benny Prijono85d3f452005-11-09 15:37:19 +0000121 pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete);
Benny Prijono0a749f12005-10-31 21:02:30 +0000122 }
123
Benny Prijono85d3f452005-11-09 15:37:19 +0000124 //
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 Prijono0a749f12005-10-31 21:02:30 +0000128 {
Benny Prijono85d3f452005-11-09 15:37:19 +0000129 return pj_timer_heap_set_max_timed_out_per_poll(ht_, count);
Benny Prijono0a749f12005-10-31 21:02:30 +0000130 }
131
Benny Prijono85d3f452005-11-09 15:37:19 +0000132 //
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 Prijono0a749f12005-10-31 21:02:30 +0000153 pj_size_t count()
154 {
155 return pj_timer_heap_count(ht_);
156 }
157
Benny Prijono85d3f452005-11-09 15:37:19 +0000158 //
159 // Get the earliest time.
160 // Return false if no timer is found.
161 //
162 bool earliest_time(Pj_Time_Val *t)
Benny Prijono0a749f12005-10-31 21:02:30 +0000163 {
Benny Prijono85d3f452005-11-09 15:37:19 +0000164 return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS;
Benny Prijono0a749f12005-10-31 21:02:30 +0000165 }
166
Benny Prijono85d3f452005-11-09 15:37:19 +0000167 //
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 Prijono0a749f12005-10-31 21:02:30 +0000172 {
173 return pj_timer_heap_poll(ht_, next_delay);
174 }
175
176private:
177 pj_timer_heap_t *ht_;
178};
179
180#endif /* __PJPP_TIMER_H__ */