blob: 3f1402abae07f5ba1779d73100c6b6b1d0285e07 [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/**
113 * Default flag for timer heap, indicates that synchronization will be
114 * used.
115 */
116#define PJ_TIMER_HEAP_SYNCHRONIZE (0)
117
118/**
119 * Flag to indicate that thread synchronization is NOT needed for the
120 * timer heap.
121 */
122#define PJ_TIMER_HEAP_NO_SYNCHRONIZE (1)
123
124/**
125 * Calculate memory size required to create a timer heap.
126 *
127 * @param count Number of timer entries to be supported.
128 * @return Memory size requirement in bytes.
129 */
130PJ_DECL(pj_size_t) pj_timer_heap_mem_size(pj_size_t count);
131
132/**
133 * Create a timer heap.
134 *
135 * @param pool The pool where allocations in the timer heap will be
136 * allocated. The timer heap will dynamicly allocate
137 * more storate from the pool if the number of timer
138 * entries registered is more than the size originally
139 * requested when calling this function.
140 * @param count The maximum number of timer entries to be supported
141 * initially. If the application registers more entries
142 * during runtime, then the timer heap will resize.
143 * @param flag Creation flag, currently only PJ_TIMER_HEAP_NO_SYNCHRONIZE
144 * is recognized..
145 * @param ht Pointer to receive the created timer heap.
146 *
147 * @return PJ_SUCCESS, or the appropriate error code.
148 */
149PJ_DECL(pj_status_t) pj_timer_heap_create( pj_pool_t *pool,
150 pj_size_t count,
151 unsigned flag,
152 pj_timer_heap_t **ht);
153
154/**
155 * Initialize a timer entry. Application should call this function at least
156 * once before scheduling the entry to the timer heap, to properly initialize
157 * the timer entry.
158 *
159 * @param entry The timer entry to be initialized.
160 * @param id Arbitrary ID assigned by the user/owner of this entry.
161 * Applications can use this ID to distinguish multiple
162 * timer entries that share the same callback and user_data.
163 * @param user_data User data to be associated with this entry.
164 * Applications normally will put the instance of object that
165 * owns the timer entry in this field.
166 * @param cb Callback function to be called when the timer elapses.
167 *
168 * @return The timer entry itself.
169 */
170PJ_DECL(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry,
171 int id,
172 void *user_data,
173 pj_timer_heap_callback *cb );
174
175/**
176 * Schedule a timer entry which will expire AFTER the specified delay.
177 *
178 * @param ht The timer heap.
179 * @param entry The entry to be registered.
180 * @param delay The interval to expire.
181 * @return PJ_SUCCESS, or the appropriate error code.
182 */
183PJ_DECL(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht,
184 pj_timer_entry *entry,
185 const pj_time_val *delay);
186
187/**
188 * Cancel a previously registered timer.
189 *
190 * @param ht The timer heap.
191 * @param entry The entry to be cancelled.
192 * @return The number of timer cancelled, which should be one if the
193 * entry has really been registered, or zero if no timer was
194 * cancelled.
195 */
196PJ_DECL(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
197 pj_timer_entry *entry);
198
199/**
200 * Get the number of timer entries.
201 *
202 * @param ht The timer heap.
203 * @return The number of timer entries.
204 */
205PJ_DECL(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht );
206
207/**
208 * Get the earliest time registered in the timer heap. The timer heap
209 * MUST have at least one timer being scheduled (application should use
210 * #pj_timer_heap_count() before calling this function).
211 *
212 * @param ht The timer heap.
213 * @param timeval The time deadline of the earliest timer entry.
214 *
215 * @return PJ_SUCCESS, or PJ_ENOTFOUND if no entry is scheduled.
216 */
217PJ_DECL(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t *ht,
218 pj_time_val *timeval);
219
220/**
221 * Poll the timer heap, check for expired timers and call the callback for
222 * each of the expired timers.
223 *
224 * @param ht The timer heap.
225 * @param next_delay If this parameter is not NULL, it will be filled up with
226 * the time delay until the next timer elapsed, or -1 in
227 * the sec part if no entry exist.
228 * @return The number of timers expired.
229 */
230PJ_DECL(int) pj_timer_heap_poll( pj_timer_heap_t *ht, pj_time_val *next_delay);
231
232/**
233 * @}
234 */
235
236PJ_END_DECL
237
238#endif /* __PJ_TIMER_H__ */
239