Debian Codegraph @

Xpjproject/pjlib/include/pj/timer.h

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #ifndef __PJ_TIMER_H__
18 #define __PJ_TIMER_H__
19
20 /**
21 * @file timer.h
22 * @brief Timer Heap
23 */
24
25 #include <pj/types.h>
26 #include <pj/lock.h>
27
28 #if PJ_TIMER_USE_LINKED_LIST
29 # include <pj/list.h>
30 #endif
31
32 PJ_BEGIN_DECL
33
34 /**
35 * @defgroup PJ_TIMER Timer Heap Management.
36 * @ingroup PJ_MISC
37 * @brief
38 * The timer scheduling implementation here is based on ACE library's
39 * ACE_Timer_Heap, with only little modification to suit our library's style
40 * (I even left most of the comments in the original source).
41 *
42 * To quote the original quote in ACE_Timer_Heap_T class:
43 *
44 * This implementation uses a heap-based callout queue of
45 * absolute times. Therefore, in the average and worst case,
46 * scheduling, canceling, and expiring timers is O(log N) (where
47 * N is the total number of timers). In addition, we can also
48 * preallocate as many \a ACE_Timer_Nodes as there are slots in
49 * the heap. This allows us to completely remove the need for
50 * dynamic memory allocation, which is important for real-time
51 * systems.
52 *
53 * You can find the fine ACE library at:
54 * http://www.cs.wustl.edu/~schmidt/ACE.html
55 *
56 * ACE is Copyright (C)1993-2006 Douglas C. Schmidt <[email protected]>
57 *
58 * @{
59 *
60 * \section pj_timer_examples_sec Examples
61 *
62 * For some examples on how to use the timer heap, please see the link below.
63 *
64 * - Timer test: \src{pjlib/src/pjlib-test/timer.c}
65 */
66
67
68 /**
69 * The type for internal timer ID.
70 */
71 typedef int pj_timer_id_t;
72
73 /**
74 * Forward declaration for pj_timer_entry.
75 */
76 struct pj_timer_entry;
77
78 /**
79 * The type of callback function to be called by timer scheduler when a timer
80 * has expired.
81 *
82 * @param timer_heap The timer heap.
83 * @param entry Timer entry which timer's has expired.
84 */
85 typedef void pj_timer_heap_callback(pj_timer_heap_t *timer_heap,
86 struct pj_timer_entry *entry);
87
88
89 /**
90 * This structure represents an entry to the timer.
91 */
92 typedef struct pj_timer_entry
93 {
94 #if !PJ_TIMER_USE_COPY && PJ_TIMER_USE_LINKED_LIST
95 /**
96 * Standard list members.
97 */
98 PJ_DECL_LIST_MEMBER(struct pj_timer_entry);
99 #endif
100
101 /**
102 * User data to be associated with this entry.
103 * Applications normally will put the instance of object that
104 * owns the timer entry in this field.
105 */
106 void *user_data;
107
108 /**
109 * Arbitrary ID assigned by the user/owner of this entry.
110 * Applications can use this ID to distinguish multiple
111 * timer entries that share the same callback and user_data.
112 */
113 int id;
114
115 /**
116 * Callback to be called when the timer expires.
117 */
118 pj_timer_heap_callback *cb;
119
120 /**
121 * Internal unique timer ID, which is assigned by the timer heap.
122 * Positive values indicate that the timer entry is running,
123 * while -1 means that it's not. Any other value may indicate that it
124 * hasn't been properly initialised or is in a bad state.
125 * Application should not touch this ID.
126 */
127 pj_timer_id_t _timer_id;
128
129 #if !PJ_TIMER_USE_COPY
130 /**
131 * The future time when the timer expires, which the value is updated
132 * by timer heap when the timer is scheduled.
133 */
134 pj_time_val _timer_value;
135
136 /**
137 * Internal: the group lock used by this entry, set when
138 * pj_timer_heap_schedule_w_lock() is used.
139 */
140 pj_grp_lock_t *_grp_lock;
141
142 #if PJ_TIMER_DEBUG
143 const char *src_file;
144 int src_line;
145 #endif
146
147 #endif
148 } pj_timer_entry;
149
150
151 /**
152 * Calculate memory size required to create a timer heap.
153 *
154 * @param count Number of timer entries to be supported.
155 * @return Memory size requirement in bytes.
156 */
157 PJ_DECL(pj_size_t) pj_timer_heap_mem_size(pj_size_t count);
158
159 /**
160 * Create a timer heap.
161 *
162 * @param pool The pool where allocations in the timer heap will be
163 * allocated. The timer heap will dynamicly allocate
164 * more storate from the pool if the number of timer
165 * entries registered is more than the size originally
166 * requested when calling this function.
167 * @param count The maximum number of timer entries to be supported
168 * initially. If the application registers more entries
169 * during runtime, then the timer heap will resize.
170 * @param ht Pointer to receive the created timer heap.
171 *
172 * @return PJ_SUCCESS, or the appropriate error code.
173 */
174 PJ_DECL(pj_status_t) pj_timer_heap_create( pj_pool_t *pool,
175 pj_size_t count,
176 pj_timer_heap_t **ht);
177
178 /**
179 * Destroy the timer heap.
180 *
181 * @param ht The timer heap.
182 */
183 PJ_DECL(void) pj_timer_heap_destroy( pj_timer_heap_t *ht );
184
185
186 /**
187 * Set lock object to be used by the timer heap. By default, the timer heap
188 * uses dummy synchronization.
189 *
190 * @param ht The timer heap.
191 * @param lock The lock object to be used for synchronization.
192 * @param auto_del If nonzero, the lock object will be destroyed when
193 * the timer heap is destroyed.
194 */
195 PJ_DECL(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht,
196 pj_lock_t *lock,
197 pj_bool_t auto_del );
198
199 /**
200 * Set maximum number of timed out entries to process in a single poll.
201 *
202 * @param ht The timer heap.
203 * @param count Number of entries.
204 *
205 * @return The old number.
206 */
207 PJ_DECL(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht,
208 unsigned count );
209
210 /**
211 * Initialize a timer entry. Application should call this function at least
212 * once before scheduling the entry to the timer heap, to properly initialize
213 * the timer entry.
214 *
215 * @param entry The timer entry to be initialized.
216 * @param id Arbitrary ID assigned by the user/owner of this entry.
217 * Applications can use this ID to distinguish multiple
218 * timer entries that share the same callback and user_data.
219 * @param user_data User data to be associated with this entry.
220 * Applications normally will put the instance of object that
221 * owns the timer entry in this field.
222 * @param cb Callback function to be called when the timer elapses.
223 *
224 * @return The timer entry itself.
225 */
226 PJ_DECL(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry,
227 int id,
228 void *user_data,
229 pj_timer_heap_callback *cb );
230
231 /**
232 * Queries whether a timer entry is currently running.
233 *
234 * @param entry The timer entry to query.
235 *
236 * @return PJ_TRUE if the timer is running. PJ_FALSE if not.
237 */
238 PJ_DECL(pj_bool_t) pj_timer_entry_running( pj_timer_entry *entry );
239
240 /**
241 * Schedule a timer entry which will expire AFTER the specified delay.
242 *
243 * @param ht The timer heap.
244 * @param entry The entry to be registered.
245 * @param delay The interval to expire.
246 * @return PJ_SUCCESS, or the appropriate error code.
247 */
248 #if PJ_TIMER_DEBUG
249 # define pj_timer_heap_schedule(ht,e,d) \
250 pj_timer_heap_schedule_dbg(ht,e,d,__FILE__,__LINE__)
251
252 PJ_DECL(pj_status_t) pj_timer_heap_schedule_dbg( pj_timer_heap_t *ht,
253 pj_timer_entry *entry,
254 const pj_time_val *delay,
255 const char *src_file,
256 int src_line);
257 #else
258 PJ_DECL(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht,
259 pj_timer_entry *entry,
260 const pj_time_val *delay);
261 #endif /* PJ_TIMER_DEBUG */
262
263 /**
264 * Schedule a timer entry which will expire AFTER the specified delay, and
265 * increment the reference counter of the group lock while the timer entry
266 * is active. The group lock reference counter will automatically be released
267 * after the timer callback is called or when the timer is cancelled.
268 *
269 * @param ht The timer heap.
270 * @param entry The entry to be registered.
271 * @param delay The interval to expire.
272 * @param id_val The value to be set to the "id" field of the timer entry
273 * once the timer is scheduled.
274 * @param grp_lock The group lock.
275 *
276 * @return PJ_SUCCESS, or the appropriate error code.
277 */
278 #if PJ_TIMER_DEBUG
279 # define pj_timer_heap_schedule_w_grp_lock(ht,e,d,id,g) \
280 pj_timer_heap_schedule_w_grp_lock_dbg(ht,e,d,id,g,__FILE__,__LINE__)
281
282 PJ_DECL(pj_status_t) pj_timer_heap_schedule_w_grp_lock_dbg(
283 pj_timer_heap_t *ht,
284 pj_timer_entry *entry,
285 const pj_time_val *delay,
286 int id_val,
287 pj_grp_lock_t *grp_lock,
288 const char *src_file,
289 int src_line);
290 #else
291 PJ_DECL(pj_status_t) pj_timer_heap_schedule_w_grp_lock(
292 pj_timer_heap_t *ht,
293 pj_timer_entry *entry,
294 const pj_time_val *delay,
295 int id_val,
296 pj_grp_lock_t *grp_lock);
297 #endif /* PJ_TIMER_DEBUG */
298
299
300 /**
301 * Cancel a previously registered timer. This will also decrement the
302 * reference counter of the group lock associated with the timer entry,
303 * if the entry was scheduled with one.
304 *
305 * @param ht The timer heap.
306 * @param entry The entry to be cancelled.
307 * @return The number of timer cancelled, which should be one if the
308 * entry has really been registered, or zero if no timer was
309 * cancelled.
310 */
311 PJ_DECL(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
312 pj_timer_entry *entry);
313
314 /**
315 * Cancel only if the previously registered timer is active. This will
316 * also decrement the reference counter of the group lock associated
317 * with the timer entry, if the entry was scheduled with one. In any
318 * case, set the "id" to the specified value.
319 *
320 * @param ht The timer heap.
321 * @param entry The entry to be cancelled.
322 * @param id_val Value to be set to "id"
323 *
324 * @return The number of timer cancelled, which should be one if the
325 * entry has really been registered, or zero if no timer was
326 * cancelled.
327 */
328 PJ_DECL(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht,
329 pj_timer_entry *entry,
330 int id_val);
331
332 /**
333 * Get the number of timer entries.
334 *
335 * @param ht The timer heap.
336 * @return The number of timer entries.
337 */
338 PJ_DECL(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht );
339
340 /**
341 * Get the earliest time registered in the timer heap. The timer heap
342 * MUST have at least one timer being scheduled (application should use
343 * #pj_timer_heap_count() before calling this function).
344 *
345 * @param ht The timer heap.
346 * @param timeval The time deadline of the earliest timer entry.
347 *
348 * @return PJ_SUCCESS, or PJ_ENOTFOUND if no entry is scheduled.
349 */
350 PJ_DECL(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t *ht,
351 pj_time_val *timeval);
352
353 /**
354 * Poll the timer heap, check for expired timers and call the callback for
355 * each of the expired timers.
356 *
357 * Note: polling the timer heap is not necessary in Symbian. Please see
358 * @ref PJ_SYMBIAN_OS for more info.
359 *
360 * @param ht The timer heap.
361 * @param next_delay If this parameter is not NULL, it will be filled up with
362 * the time delay until the next timer elapsed, or
363 * PJ_MAXINT32 in the sec part if no entry exist.
364 *
365 * @return The number of timers expired.
366 */
367 PJ_DECL(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht,
368 pj_time_val *next_delay);
369
370 #if PJ_TIMER_DEBUG
371 /**
372 * Dump timer heap entries.
373 *
374 * @param ht The timer heap.
375 */
376 PJ_DECL(void) pj_timer_heap_dump(pj_timer_heap_t *ht);
377 #endif
378
379 /**
380 * @}
381 */
382
383 PJ_END_DECL
384
385 #endif /* __PJ_TIMER_H__ */
386