blob: bcc4bb92a5f2fc1fee5b5065d8a1289fb24f460e [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 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
20#ifndef __PJ_TIMER_H__
21#define __PJ_TIMER_H__
22
23/**
24 * @file timer.h
25 * @brief Timer Heap
26 */
27
28#include <pj/types.h>
29
30PJ_BEGIN_DECL
31
32/**
33 * @defgroup PJ_TIMER Timer Heap Management.
34 * @ingroup PJ_MISC
35 * @brief
36 * The timer scheduling implementation here is based on ACE library's
37 * ACE_Timer_Heap, with only little modification to suit our library's style
38 * (I even left most of the comments in the original source).
39 *
40 * To quote the original quote in ACE_Timer_Heap_T class:
41 *
42 * This implementation uses a heap-based callout queue of
43 * absolute times. Therefore, in the average and worst case,
44 * scheduling, canceling, and expiring timers is O(log N) (where
45 * N is the total number of timers). In addition, we can also
46 * preallocate as many \a ACE_Timer_Nodes as there are slots in
47 * the heap. This allows us to completely remove the need for
48 * dynamic memory allocation, which is important for real-time
49 * systems.
50 * @{
51 *
52 * \section pj_timer_examples_sec Examples
53 *
54 * For some examples on how to use the timer heap, please see the link below.
55 *
56 * - \ref page_pjlib_timer_test
57 */
58
59
60/**
61 * The type for internal timer ID.
62 */
63typedef int pj_timer_id_t;
64
65/**
66 * Forward declaration for pj_timer_entry.
67 */
68struct pj_timer_entry;
69
70/**
71 * The type of callback function to be called by timer scheduler when a timer
72 * has expired.
73 *
74 * @param timer_heap The timer heap.
75 * @param entry Timer entry which timer's has expired.
76 */
77typedef void pj_timer_heap_callback(pj_timer_heap_t *timer_heap,
78 struct pj_timer_entry *entry);
79
80
81/**
82 * This structure represents an entry to the timer.
83 */
84struct pj_timer_entry
85{
86 /**
87 * User data to be associated with this entry.
88 * Applications normally will put the instance of object that
89 * owns the timer entry in this field.
90 */
91 void *user_data;
92
93 /**
94 * Arbitrary ID assigned by the user/owner of this entry.
95 * Applications can use this ID to distinguish multiple
96 * timer entries that share the same callback and user_data.
97 */
98 int id;
99
100 /**
101 * Callback to be called when the timer expires.
102 */
103 pj_timer_heap_callback *cb;
104
105 /**
106 * Internal unique timer ID, which is assigned by the timer heap.
107 * Application should not touch this ID.
108 */
109 pj_timer_id_t _timer_id;
110
111 /**
112 * The future time when the timer expires, which the value is updated
113 * by timer heap when the timer is scheduled.
114 */
115 pj_time_val _timer_value;
116};
117
118
119/**
120 * Calculate memory size required to create a timer heap.
121 *
122 * @param count Number of timer entries to be supported.
123 * @return Memory size requirement in bytes.
124 */
125PJ_DECL(pj_size_t) pj_timer_heap_mem_size(pj_size_t count);
126
127/**
128 * Create a timer heap.
129 *
130 * @param pool The pool where allocations in the timer heap will be
131 * allocated. The timer heap will dynamicly allocate
132 * more storate from the pool if the number of timer
133 * entries registered is more than the size originally
134 * requested when calling this function.
135 * @param count The maximum number of timer entries to be supported
136 * initially. If the application registers more entries
137 * during runtime, then the timer heap will resize.
138 * @param ht Pointer to receive the created timer heap.
139 *
140 * @return PJ_SUCCESS, or the appropriate error code.
141 */
142PJ_DECL(pj_status_t) pj_timer_heap_create( pj_pool_t *pool,
143 pj_size_t count,
144 pj_timer_heap_t **ht);
145
146/**
147 * Destroy the timer heap.
148 *
149 * @param ht The timer heap.
150 */
151PJ_DECL(void) pj_timer_heap_destroy( pj_timer_heap_t *ht );
152
153
154/**
155 * Set lock object to be used by the timer heap. By default, the timer heap
156 * uses dummy synchronization.
157 *
158 * @param ht The timer heap.
159 * @param lock The lock object to be used for synchronization.
160 * @param auto_del If nonzero, the lock object will be destroyed when
161 * the timer heap is destroyed.
162 */
163PJ_DECL(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht,
164 pj_lock_t *lock,
165 pj_bool_t auto_del );
166
167/**
168 * Set maximum number of timed out entries to process in a single poll.
169 *
170 * @param ht The timer heap.
171 * @param count Number of entries.
172 *
173 * @return The old number.
174 */
175PJ_DECL(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht,
176 unsigned count );
177
178/**
179 * Initialize a timer entry. Application should call this function at least
180 * once before scheduling the entry to the timer heap, to properly initialize
181 * the timer entry.
182 *
183 * @param entry The timer entry to be initialized.
184 * @param id Arbitrary ID assigned by the user/owner of this entry.
185 * Applications can use this ID to distinguish multiple
186 * timer entries that share the same callback and user_data.
187 * @param user_data User data to be associated with this entry.
188 * Applications normally will put the instance of object that
189 * owns the timer entry in this field.
190 * @param cb Callback function to be called when the timer elapses.
191 *
192 * @return The timer entry itself.
193 */
194PJ_DECL(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry,
195 int id,
196 void *user_data,
197 pj_timer_heap_callback *cb );
198
199/**
200 * Schedule a timer entry which will expire AFTER the specified delay.
201 *
202 * @param ht The timer heap.
203 * @param entry The entry to be registered.
204 * @param delay The interval to expire.
205 * @return PJ_SUCCESS, or the appropriate error code.
206 */
207PJ_DECL(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht,
208 pj_timer_entry *entry,
209 const pj_time_val *delay);
210
211/**
212 * Cancel a previously registered timer.
213 *
214 * @param ht The timer heap.
215 * @param entry The entry to be cancelled.
216 * @return The number of timer cancelled, which should be one if the
217 * entry has really been registered, or zero if no timer was
218 * cancelled.
219 */
220PJ_DECL(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
221 pj_timer_entry *entry);
222
223/**
224 * Get the number of timer entries.
225 *
226 * @param ht The timer heap.
227 * @return The number of timer entries.
228 */
229PJ_DECL(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht );
230
231/**
232 * Get the earliest time registered in the timer heap. The timer heap
233 * MUST have at least one timer being scheduled (application should use
234 * #pj_timer_heap_count() before calling this function).
235 *
236 * @param ht The timer heap.
237 * @param timeval The time deadline of the earliest timer entry.
238 *
239 * @return PJ_SUCCESS, or PJ_ENOTFOUND if no entry is scheduled.
240 */
241PJ_DECL(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t *ht,
242 pj_time_val *timeval);
243
244/**
245 * Poll the timer heap, check for expired timers and call the callback for
246 * each of the expired timers.
247 *
248 * @param ht The timer heap.
249 * @param next_delay If this parameter is not NULL, it will be filled up with
250 * the time delay until the next timer elapsed, or -1 in
251 * the sec part if no entry exist.
252 *
253 * @return The number of timers expired.
254 */
255PJ_DECL(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht,
256 pj_time_val *next_delay);
257
258/**
259 * @}
260 */
261
262PJ_END_DECL
263
264#endif /* __PJ_TIMER_H__ */
265