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