| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 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 | #include <pj/timer.h> |
| 20 | #include <pj/pool.h> |
| 21 | #include <pj/assert.h> |
| 22 | #include <pj/errno.h> |
| 23 | #include <pj/lock.h> |
| 24 | |
| 25 | #include "os_symbian.h" |
| 26 | |
| 27 | |
| 28 | #define DEFAULT_MAX_TIMED_OUT_PER_POLL (64) |
| 29 | |
| 30 | // Maximum number of miliseconds that RTimer.At() supports |
| 31 | #define MAX_RTIMER_INTERVAL 2147 |
| 32 | |
| 33 | /* Absolute maximum number of timer entries */ |
| 34 | #ifndef PJ_SYMBIAN_TIMER_MAX_COUNT |
| 35 | # define PJ_SYMBIAN_TIMER_MAX_COUNT 65535 |
| 36 | #endif |
| 37 | |
| 38 | /* Get the number of free slots in the timer heap */ |
| 39 | #define FREECNT(th) (th->max_size - th->cur_size) |
| 40 | |
| 41 | // Forward declaration |
| 42 | class CPjTimerEntry; |
| 43 | |
| 44 | /** |
| 45 | * The implementation of timer heap. |
| 46 | */ |
| 47 | struct pj_timer_heap_t |
| 48 | { |
| 49 | /** Maximum size of the heap. */ |
| 50 | pj_size_t max_size; |
| 51 | |
| 52 | /** Current size of the heap. */ |
| 53 | pj_size_t cur_size; |
| 54 | |
| 55 | /** Array of timer entries. A scheduled timer will occupy one slot, and |
| 56 | * the slot number will be saved in entry->_timer_id |
| 57 | */ |
| 58 | CPjTimerEntry **entries; |
| 59 | |
| 60 | /** Array of free slot indexes in the "entries" array */ |
| 61 | int *free_slots; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Active object for each timer entry. |
| 66 | */ |
| 67 | class CPjTimerEntry : public CActive |
| 68 | { |
| 69 | public: |
| 70 | pj_timer_entry *entry_; |
| 71 | |
| 72 | static CPjTimerEntry* NewL( pj_timer_heap_t *timer_heap, |
| 73 | pj_timer_entry *entry, |
| 74 | const pj_time_val *delay); |
| 75 | |
| 76 | ~CPjTimerEntry(); |
| 77 | |
| 78 | virtual void RunL(); |
| 79 | virtual void DoCancel(); |
| 80 | |
| 81 | private: |
| 82 | pj_timer_heap_t *timer_heap_; |
| 83 | RTimer rtimer_; |
| 84 | pj_uint32_t interval_left_; |
| 85 | |
| 86 | CPjTimerEntry(pj_timer_heap_t *timer_heap, pj_timer_entry *entry); |
| 87 | void ConstructL(const pj_time_val *delay); |
| 88 | void Schedule(); |
| 89 | }; |
| 90 | |
| 91 | ////////////////////////////////////////////////////////////////////////////// |
| 92 | /* |
| 93 | * Implementation. |
| 94 | */ |
| 95 | |
| 96 | /* Grow timer heap to the specified size */ |
| 97 | static pj_status_t realloc_timer_heap(pj_timer_heap_t *th, pj_size_t new_size) |
| 98 | { |
| 99 | typedef CPjTimerEntry *entry_ptr; |
| 100 | CPjTimerEntry **entries = NULL; |
| 101 | int *free_slots = NULL; |
| 102 | unsigned i, j; |
| 103 | |
| 104 | if (new_size > PJ_SYMBIAN_TIMER_MAX_COUNT) { |
| 105 | /* Just some sanity limit */ |
| 106 | new_size = PJ_SYMBIAN_TIMER_MAX_COUNT; |
| 107 | if (new_size <= th->max_size) { |
| 108 | /* We've grown large enough */ |
| 109 | pj_assert(!"Too many timer heap entries"); |
| 110 | return PJ_ETOOMANY; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* Allocate entries, move entries from the old array if there is one */ |
| 115 | entries = new entry_ptr[new_size]; |
| 116 | if (th->entries) { |
| 117 | pj_memcpy(entries, th->entries, th->max_size * sizeof(th->entries[0])); |
| 118 | } |
| 119 | /* Initialize the remaining new area */ |
| 120 | pj_bzero(&entries[th->max_size], |
| 121 | (new_size - th->max_size) * sizeof(th->entries[0])); |
| 122 | |
| 123 | /* Allocate free slots array */ |
| 124 | free_slots = new int[new_size]; |
| 125 | if (th->free_slots) { |
| 126 | pj_memcpy(free_slots, th->free_slots, |
| 127 | FREECNT(th) * sizeof(th->free_slots[0])); |
| 128 | } |
| 129 | /* Initialize the remaining new area */ |
| 130 | for (i=FREECNT(th), j=th->max_size; j<new_size; ++i, ++j) { |
| 131 | free_slots[i] = j; |
| 132 | } |
| 133 | for ( ; i<new_size; ++i) { |
| 134 | free_slots[i] = -1; |
| 135 | } |
| 136 | |
| 137 | /* Apply */ |
| 138 | delete [] th->entries; |
| 139 | th->entries = entries; |
| 140 | th->max_size = new_size; |
| 141 | delete [] th->free_slots; |
| 142 | th->free_slots = free_slots; |
| 143 | |
| 144 | return PJ_SUCCESS; |
| 145 | } |
| 146 | |
| 147 | /* Allocate and register an entry to timer heap for newly scheduled entry */ |
| 148 | static pj_status_t add_entry(pj_timer_heap_t *th, CPjTimerEntry *entry) |
| 149 | { |
| 150 | pj_status_t status; |
| 151 | int slot; |
| 152 | |
| 153 | /* Check that there's still capacity left in the timer heap */ |
| 154 | if (FREECNT(th) < 1) { |
| 155 | // Grow the timer heap twice the capacity |
| 156 | status = realloc_timer_heap(th, th->max_size * 2); |
| 157 | if (status != PJ_SUCCESS) |
| 158 | return status; |
| 159 | } |
| 160 | |
| 161 | /* Allocate one free slot. Use LIFO */ |
| 162 | slot = th->free_slots[FREECNT(th)-1]; |
| 163 | PJ_ASSERT_RETURN((slot >= 0) && (slot < (int)th->max_size) && |
| 164 | (th->entries[slot]==NULL), PJ_EBUG); |
| 165 | |
| 166 | th->free_slots[FREECNT(th)-1] = -1; |
| 167 | th->entries[slot] = entry; |
| 168 | entry->entry_->_timer_id = slot; |
| 169 | ++th->cur_size; |
| 170 | |
| 171 | return PJ_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /* Free a slot when an entry's timer has elapsed or cancel */ |
| 175 | static pj_status_t remove_entry(pj_timer_heap_t *th, CPjTimerEntry *entry) |
| 176 | { |
| 177 | int slot = entry->entry_->_timer_id; |
| 178 | |
| 179 | PJ_ASSERT_RETURN(slot >= 0 && slot < (int)th->max_size, PJ_EBUG); |
| 180 | PJ_ASSERT_RETURN(FREECNT(th) < th->max_size, PJ_EBUG); |
| 181 | PJ_ASSERT_RETURN(th->entries[slot]==entry, PJ_EBUG); |
| 182 | PJ_ASSERT_RETURN(th->free_slots[FREECNT(th)]==-1, PJ_EBUG); |
| 183 | |
| 184 | th->entries[slot] = NULL; |
| 185 | th->free_slots[FREECNT(th)] = slot; |
| 186 | entry->entry_->_timer_id = -1; |
| 187 | --th->cur_size; |
| 188 | |
| 189 | return PJ_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | CPjTimerEntry::CPjTimerEntry(pj_timer_heap_t *timer_heap, |
| 194 | pj_timer_entry *entry) |
| 195 | : CActive(PJ_SYMBIAN_TIMER_PRIORITY), entry_(entry), timer_heap_(timer_heap), |
| 196 | interval_left_(0) |
| 197 | { |
| 198 | } |
| 199 | |
| 200 | CPjTimerEntry::~CPjTimerEntry() |
| 201 | { |
| 202 | Cancel(); |
| 203 | rtimer_.Close(); |
| 204 | } |
| 205 | |
| 206 | void CPjTimerEntry::Schedule() |
| 207 | { |
| 208 | pj_int32_t interval; |
| 209 | |
| 210 | if (interval_left_ > MAX_RTIMER_INTERVAL) { |
| 211 | interval = MAX_RTIMER_INTERVAL; |
| 212 | } else { |
| 213 | interval = interval_left_; |
| 214 | } |
| 215 | |
| 216 | interval_left_ -= interval; |
| 217 | rtimer_.After(iStatus, interval * 1000); |
| 218 | SetActive(); |
| 219 | } |
| 220 | |
| 221 | void CPjTimerEntry::ConstructL(const pj_time_val *delay) |
| 222 | { |
| 223 | rtimer_.CreateLocal(); |
| 224 | CActiveScheduler::Add(this); |
| 225 | |
| 226 | interval_left_ = PJ_TIME_VAL_MSEC(*delay); |
| 227 | Schedule(); |
| 228 | } |
| 229 | |
| 230 | CPjTimerEntry* CPjTimerEntry::NewL(pj_timer_heap_t *timer_heap, |
| 231 | pj_timer_entry *entry, |
| 232 | const pj_time_val *delay) |
| 233 | { |
| 234 | CPjTimerEntry *self = new CPjTimerEntry(timer_heap, entry); |
| 235 | CleanupStack::PushL(self); |
| 236 | self->ConstructL(delay); |
| 237 | CleanupStack::Pop(self); |
| 238 | |
| 239 | return self; |
| 240 | } |
| 241 | |
| 242 | void CPjTimerEntry::RunL() |
| 243 | { |
| 244 | if (interval_left_ > 0) { |
| 245 | Schedule(); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | remove_entry(timer_heap_, this); |
| 250 | entry_->cb(timer_heap_, entry_); |
| 251 | |
| 252 | // Finger's crossed! |
| 253 | delete this; |
| 254 | } |
| 255 | |
| 256 | void CPjTimerEntry::DoCancel() |
| 257 | { |
| 258 | /* It's possible that _timer_id is -1, see schedule(). In this case, |
| 259 | * the entry has not been added to the timer heap, so don't remove |
| 260 | * it. |
| 261 | */ |
| 262 | if (entry_ && entry_->_timer_id != -1) |
| 263 | remove_entry(timer_heap_, this); |
| 264 | |
| 265 | rtimer_.Cancel(); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | ////////////////////////////////////////////////////////////////////////////// |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * Calculate memory size required to create a timer heap. |
| 274 | */ |
| 275 | PJ_DEF(pj_size_t) pj_timer_heap_mem_size(pj_size_t count) |
| 276 | { |
| 277 | return /* size of the timer heap itself: */ |
| 278 | sizeof(pj_timer_heap_t) + |
| 279 | /* size of each entry: */ |
| 280 | (count+2) * (sizeof(void*)+sizeof(int)) + |
| 281 | /* lock, pool etc: */ |
| 282 | 132; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Create a new timer heap. |
| 287 | */ |
| 288 | PJ_DEF(pj_status_t) pj_timer_heap_create( pj_pool_t *pool, |
| 289 | pj_size_t size, |
| 290 | pj_timer_heap_t **p_heap) |
| 291 | { |
| 292 | pj_timer_heap_t *ht; |
| 293 | pj_status_t status; |
| 294 | |
| 295 | PJ_ASSERT_RETURN(pool && p_heap, PJ_EINVAL); |
| 296 | |
| 297 | *p_heap = NULL; |
| 298 | |
| 299 | /* Allocate timer heap data structure from the pool */ |
| 300 | ht = PJ_POOL_ZALLOC_T(pool, pj_timer_heap_t); |
| 301 | if (!ht) |
| 302 | return PJ_ENOMEM; |
| 303 | |
| 304 | /* Allocate slots */ |
| 305 | status = realloc_timer_heap(ht, size); |
| 306 | if (status != PJ_SUCCESS) |
| 307 | return status; |
| 308 | |
| 309 | *p_heap = ht; |
| 310 | return PJ_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | PJ_DEF(void) pj_timer_heap_destroy( pj_timer_heap_t *ht ) |
| 314 | { |
| 315 | /* Cancel and delete pending active objects */ |
| 316 | if (ht->entries) { |
| 317 | unsigned i; |
| 318 | for (i=0; i<ht->max_size; ++i) { |
| 319 | if (ht->entries[i]) { |
| 320 | ht->entries[i]->entry_ = NULL; |
| 321 | ht->entries[i]->Cancel(); |
| 322 | delete ht->entries[i]; |
| 323 | ht->entries[i] = NULL; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | delete [] ht->entries; |
| 329 | delete [] ht->free_slots; |
| 330 | |
| 331 | ht->entries = NULL; |
| 332 | ht->free_slots = NULL; |
| 333 | } |
| 334 | |
| 335 | PJ_DEF(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht, |
| 336 | pj_lock_t *lock, |
| 337 | pj_bool_t auto_del ) |
| 338 | { |
| 339 | PJ_UNUSED_ARG(ht); |
| 340 | if (auto_del) |
| 341 | pj_lock_destroy(lock); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | PJ_DEF(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht, |
| 346 | unsigned count ) |
| 347 | { |
| 348 | /* Not applicable */ |
| 349 | PJ_UNUSED_ARG(count); |
| 350 | return ht->max_size; |
| 351 | } |
| 352 | |
| 353 | PJ_DEF(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry, |
| 354 | int id, |
| 355 | void *user_data, |
| 356 | pj_timer_heap_callback *cb ) |
| 357 | { |
| 358 | pj_assert(entry && cb); |
| 359 | |
| 360 | entry->_timer_id = -1; |
| 361 | entry->id = id; |
| 362 | entry->user_data = user_data; |
| 363 | entry->cb = cb; |
| 364 | |
| 365 | return entry; |
| 366 | } |
| 367 | |
| 368 | PJ_DEF(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht, |
| 369 | pj_timer_entry *entry, |
| 370 | const pj_time_val *delay) |
| 371 | { |
| 372 | CPjTimerEntry *timerObj; |
| 373 | pj_status_t status; |
| 374 | |
| 375 | PJ_ASSERT_RETURN(ht && entry && delay, PJ_EINVAL); |
| 376 | PJ_ASSERT_RETURN(entry->cb != NULL, PJ_EINVAL); |
| 377 | |
| 378 | /* Prevent same entry from being scheduled more than once */ |
| 379 | PJ_ASSERT_RETURN(entry->_timer_id < 1, PJ_EINVALIDOP); |
| 380 | |
| 381 | entry->_timer_id = -1; |
| 382 | |
| 383 | timerObj = CPjTimerEntry::NewL(ht, entry, delay); |
| 384 | status = add_entry(ht, timerObj); |
| 385 | if (status != PJ_SUCCESS) { |
| 386 | timerObj->Cancel(); |
| 387 | delete timerObj; |
| 388 | return status; |
| 389 | } |
| 390 | |
| 391 | return PJ_SUCCESS; |
| 392 | } |
| 393 | |
| 394 | PJ_DEF(pj_status_t) pj_timer_heap_schedule_w_grp_lock(pj_timer_heap_t *ht, |
| 395 | pj_timer_entry *entry, |
| 396 | const pj_time_val *delay, |
| 397 | int id_val, |
| 398 | pj_grp_lock_t *grp_lock) |
| 399 | { |
| 400 | pj_status_t status; |
| 401 | |
| 402 | PJ_UNUSED_ARG(grp_lock); |
| 403 | |
| 404 | status = pj_timer_heap_schedule(ht, entry, delay); |
| 405 | |
| 406 | if (status == PJ_SUCCESS) |
| 407 | entry->id = id_val; |
| 408 | |
| 409 | return status; |
| 410 | } |
| 411 | |
| 412 | PJ_DEF(int) pj_timer_heap_cancel( pj_timer_heap_t *ht, |
| 413 | pj_timer_entry *entry) |
| 414 | { |
| 415 | PJ_ASSERT_RETURN(ht && entry, PJ_EINVAL); |
| 416 | |
| 417 | if (entry->_timer_id >= 0 && entry->_timer_id < (int)ht->max_size) { |
| 418 | CPjTimerEntry *timerObj = ht->entries[entry->_timer_id]; |
| 419 | if (timerObj) { |
| 420 | timerObj->Cancel(); |
| 421 | delete timerObj; |
| 422 | return 1; |
| 423 | } else { |
| 424 | return 0; |
| 425 | } |
| 426 | } else { |
| 427 | return 0; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | PJ_DEF(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht, |
| 432 | pj_timer_entry *entry, |
| 433 | int id_val) |
| 434 | { |
| 435 | int count = pj_timer_heap_cancel(ht, entry); |
| 436 | if (count == 1) |
| 437 | entry->id = id_val; |
| 438 | |
| 439 | return count; |
| 440 | } |
| 441 | |
| 442 | PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht, |
| 443 | pj_time_val *next_delay ) |
| 444 | { |
| 445 | /* Polling is not necessary on Symbian, since all async activities |
| 446 | * are registered to active scheduler. |
| 447 | */ |
| 448 | PJ_UNUSED_ARG(ht); |
| 449 | if (next_delay) { |
| 450 | next_delay->sec = 1; |
| 451 | next_delay->msec = 0; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | PJ_DEF(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht ) |
| 457 | { |
| 458 | PJ_ASSERT_RETURN(ht, 0); |
| 459 | |
| 460 | return ht->cur_size; |
| 461 | } |
| 462 | |
| 463 | PJ_DEF(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t * ht, |
| 464 | pj_time_val *timeval) |
| 465 | { |
| 466 | /* We don't support this! */ |
| 467 | PJ_UNUSED_ARG(ht); |
| 468 | |
| 469 | timeval->sec = 1; |
| 470 | timeval->msec = 0; |
| 471 | |
| 472 | return PJ_SUCCESS; |
| 473 | } |
| 474 |