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