| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <pj/timer.h> |
| 30 |
#include <pj/pool.h> |
| 31 |
#include <pj/os.h> |
| 32 |
#include <pj/string.h> |
| 33 |
#include <pj/assert.h> |
| 34 |
#include <pj/errno.h> |
| 35 |
#include <pj/lock.h> |
| 36 |
#include <pj/log.h> |
| 37 |
#include <pj/rand.h> |
| 38 |
#include <pj/limits.h> |
| 39 |
|
| 40 |
#define THIS_FILE "timer.c" |
| 41 |
|
| 42 |
#define HEAP_PARENT(X) (X == 0 ? 0 : (((X) - 1) / 2)) |
| 43 |
#define HEAP_LEFT(X) (((X)+(X))+1) |
| 44 |
|
| 45 |
|
| 46 |
#define DEFAULT_MAX_TIMED_OUT_PER_POLL (64) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#define ASSERT_IF_ENTRY_DESTROYED (PJ_TIMER_USE_COPY? 0: 0) |
| 55 |
|
| 56 |
|
| 57 |
enum |
| 58 |
{ |
| 59 |
F_DONT_CALL = 1, |
| 60 |
F_DONT_ASSERT = 2, |
| 61 |
F_SET_ID = 4 |
| 62 |
}; |
| 63 |
|
| 64 |
#if PJ_TIMER_USE_COPY |
| 65 |
|
| 66 |
|
| 67 |
typedef struct pj_timer_entry_dup |
| 68 |
{ |
| 69 |
#if PJ_TIMER_USE_LINKED_LIST |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
PJ_DECL_LIST_MEMBER(struct pj_timer_entry_dup); |
| 74 |
#endif |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pj_timer_entry dup; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pj_timer_entry *entry; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
pj_time_val _timer_value; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pj_grp_lock_t *_grp_lock; |
| 97 |
|
| 98 |
#if PJ_TIMER_DEBUG |
| 99 |
const char *src_file; |
| 100 |
int src_line; |
| 101 |
#endif |
| 102 |
|
| 103 |
} pj_timer_entry_dup; |
| 104 |
|
| 105 |
#define GET_TIMER(ht, node) &ht->timer_dups[node->_timer_id] |
| 106 |
#define GET_ENTRY(node) node->entry |
| 107 |
#define GET_FIELD(node, _timer_id) node->dup._timer_id |
| 108 |
|
| 109 |
#else |
| 110 |
|
| 111 |
typedef pj_timer_entry pj_timer_entry_dup; |
| 112 |
|
| 113 |
#define GET_TIMER(ht, node) node |
| 114 |
#define GET_ENTRY(node) node |
| 115 |
#define GET_FIELD(node, _timer_id) node->_timer_id |
| 116 |
|
| 117 |
#endif |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
struct pj_timer_heap_t |
| 123 |
{ |
| 124 |
|
| 125 |
pj_pool_t *pool; |
| 126 |
|
| 127 |
|
| 128 |
pj_size_t max_size; |
| 129 |
|
| 130 |
|
| 131 |
pj_size_t cur_size; |
| 132 |
|
| 133 |
|
| 134 |
unsigned max_entries_per_poll; |
| 135 |
|
| 136 |
|
| 137 |
pj_lock_t *lock; |
| 138 |
|
| 139 |
|
| 140 |
pj_bool_t auto_delete_lock; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pj_timer_entry_dup **heap; |
| 149 |
|
| 150 |
#if PJ_TIMER_USE_LINKED_LIST |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
pj_timer_entry_dup head_list; |
| 156 |
#endif |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
pj_timer_id_t *timer_ids; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
pj_timer_entry_dup *timer_dups; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pj_timer_id_t timer_ids_freelist; |
| 180 |
|
| 181 |
|
| 182 |
pj_timer_heap_callback *callback; |
| 183 |
|
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
PJ_INLINE(void) lock_timer_heap( pj_timer_heap_t *ht ) |
| 189 |
{ |
| 190 |
if (ht->lock) { |
| 191 |
pj_lock_acquire(ht->lock); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
PJ_INLINE(void) unlock_timer_heap( pj_timer_heap_t *ht ) |
| 196 |
{ |
| 197 |
if (ht->lock) { |
| 198 |
pj_lock_release(ht->lock); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
static void copy_node( pj_timer_heap_t *ht, pj_size_t slot, |
| 204 |
pj_timer_entry_dup *moved_node ) |
| 205 |
{ |
| 206 |
PJ_CHECK_STACK(); |
| 207 |
|
| 208 |
|
| 209 |
ht->heap[slot] = moved_node; |
| 210 |
|
| 211 |
|
| 212 |
ht->timer_ids[GET_FIELD(moved_node, _timer_id)] = (int)slot; |
| 213 |
} |
| 214 |
|
| 215 |
static pj_timer_id_t pop_freelist( pj_timer_heap_t *ht ) |
| 216 |
{ |
| 217 |
|
| 218 |
pj_timer_id_t new_id = ht->timer_ids_freelist; |
| 219 |
|
| 220 |
PJ_CHECK_STACK(); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ht->timer_ids_freelist = |
| 225 |
-ht->timer_ids[ht->timer_ids_freelist]; |
| 226 |
|
| 227 |
return new_id; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
static void push_freelist (pj_timer_heap_t *ht, pj_timer_id_t old_id) |
| 232 |
{ |
| 233 |
PJ_CHECK_STACK(); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ht->timer_ids[old_id] = -ht->timer_ids_freelist; |
| 238 |
ht->timer_ids_freelist = old_id; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
static void reheap_down(pj_timer_heap_t *ht, pj_timer_entry_dup *moved_node, |
| 243 |
size_t slot, size_t child) |
| 244 |
{ |
| 245 |
PJ_CHECK_STACK(); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
while (child < ht->cur_size) |
| 250 |
{ |
| 251 |
|
| 252 |
if (child + 1 < ht->cur_size && |
| 253 |
PJ_TIME_VAL_LT(ht->heap[child + 1]->_timer_value, |
| 254 |
ht->heap[child]->_timer_value)) |
| 255 |
{ |
| 256 |
child++; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if (PJ_TIME_VAL_LT(ht->heap[child]->_timer_value, |
| 262 |
moved_node->_timer_value)) |
| 263 |
{ |
| 264 |
copy_node( ht, slot, ht->heap[child]); |
| 265 |
slot = child; |
| 266 |
child = HEAP_LEFT(child); |
| 267 |
} |
| 268 |
else |
| 269 |
|
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
copy_node( ht, slot, moved_node); |
| 274 |
} |
| 275 |
|
| 276 |
static void reheap_up( pj_timer_heap_t *ht, pj_timer_entry_dup *moved_node, |
| 277 |
size_t slot, size_t parent) |
| 278 |
{ |
| 279 |
|
| 280 |
|
| 281 |
while (slot > 0) |
| 282 |
{ |
| 283 |
|
| 284 |
|
| 285 |
if (PJ_TIME_VAL_LT(moved_node->_timer_value, |
| 286 |
ht->heap[parent]->_timer_value)) |
| 287 |
{ |
| 288 |
copy_node(ht, slot, ht->heap[parent]); |
| 289 |
slot = parent; |
| 290 |
parent = HEAP_PARENT(slot); |
| 291 |
} |
| 292 |
else |
| 293 |
break; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
copy_node(ht, slot, moved_node); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
static pj_timer_entry_dup * remove_node( pj_timer_heap_t *ht, size_t slot) |
| 303 |
{ |
| 304 |
pj_timer_entry_dup *removed_node = ht->heap[slot]; |
| 305 |
|
| 306 |
|
| 307 |
push_freelist( ht, GET_FIELD(removed_node, _timer_id) ); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
ht->cur_size--; |
| 312 |
|
| 313 |
|
| 314 |
if (GET_FIELD(removed_node, _timer_id) != |
| 315 |
GET_ENTRY(removed_node)->_timer_id) |
| 316 |
{ |
| 317 |
#if PJ_TIMER_DEBUG |
| 318 |
PJ_LOG(3,(THIS_FILE, "Bug! Trying to remove entry %p from %s " |
| 319 |
"line %d, which has been deallocated " |
| 320 |
"without being cancelled", |
| 321 |
GET_ENTRY(removed_node), |
| 322 |
removed_node->src_file, |
| 323 |
removed_node->src_line)); |
| 324 |
#else |
| 325 |
PJ_LOG(3,(THIS_FILE, "Bug! Trying to remove entry %p " |
| 326 |
"which has been deallocated " |
| 327 |
"without being cancelled", |
| 328 |
GET_ENTRY(removed_node))); |
| 329 |
#endif |
| 330 |
#if ASSERT_IF_ENTRY_DESTROYED |
| 331 |
pj_assert(removed_node->dup._timer_id==removed_node->entry->_timer_id); |
| 332 |
#endif |
| 333 |
} else { |
| 334 |
GET_ENTRY(removed_node)->_timer_id = -1; |
| 335 |
} |
| 336 |
GET_FIELD(removed_node, _timer_id) = -1; |
| 337 |
|
| 338 |
#if !PJ_TIMER_USE_LINKED_LIST |
| 339 |
|
| 340 |
|
| 341 |
if (slot < ht->cur_size) |
| 342 |
{ |
| 343 |
pj_size_t parent; |
| 344 |
pj_timer_entry_dup *moved_node = ht->heap[ht->cur_size]; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
copy_node( ht, slot, moved_node); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
parent = HEAP_PARENT (slot); |
| 353 |
|
| 354 |
if (PJ_TIME_VAL_GTE(moved_node->_timer_value, |
| 355 |
ht->heap[parent]->_timer_value)) |
| 356 |
{ |
| 357 |
reheap_down( ht, moved_node, slot, HEAP_LEFT(slot)); |
| 358 |
} else { |
| 359 |
reheap_up( ht, moved_node, slot, parent); |
| 360 |
} |
| 361 |
} |
| 362 |
#else |
| 363 |
pj_list_erase(removed_node); |
| 364 |
#endif |
| 365 |
|
| 366 |
return removed_node; |
| 367 |
} |
| 368 |
|
| 369 |
static pj_status_t grow_heap(pj_timer_heap_t *ht) |
| 370 |
{ |
| 371 |
|
| 372 |
pj_size_t new_size = ht->max_size * 2; |
| 373 |
#if PJ_TIMER_USE_COPY |
| 374 |
pj_timer_entry_dup *new_timer_dups = 0; |
| 375 |
#endif |
| 376 |
pj_timer_id_t *new_timer_ids; |
| 377 |
pj_size_t i; |
| 378 |
pj_timer_entry_dup **new_heap = 0; |
| 379 |
|
| 380 |
#if PJ_TIMER_USE_LINKED_LIST |
| 381 |
pj_timer_entry_dup *tmp_dup = NULL; |
| 382 |
pj_timer_entry_dup *new_dup; |
| 383 |
#endif |
| 384 |
|
| 385 |
PJ_LOG(6,(THIS_FILE, "Growing heap size from %lu to %lu", |
| 386 |
(unsigned long)ht->max_size, |
| 387 |
(unsigned long)new_size)); |
| 388 |
|
| 389 |
|
| 390 |
new_heap = (pj_timer_entry_dup**) |
| 391 |
pj_pool_calloc(ht->pool, (unsigned)new_size, |
| 392 |
sizeof(pj_timer_entry_dup*)); |
| 393 |
if (!new_heap) |
| 394 |
return PJ_ENOMEM; |
| 395 |
|
| 396 |
#if PJ_TIMER_USE_COPY |
| 397 |
|
| 398 |
|
| 399 |
new_timer_dups = (pj_timer_entry_dup*) |
| 400 |
pj_pool_alloc(ht->pool, |
| 401 |
sizeof(pj_timer_entry_dup) * new_size); |
| 402 |
if (!new_timer_dups) |
| 403 |
return PJ_ENOMEM; |
| 404 |
|
| 405 |
memcpy(new_timer_dups, ht->timer_dups, |
| 406 |
ht->max_size * sizeof(pj_timer_entry_dup)); |
| 407 |
for (i = 0; i < ht->cur_size; i++) { |
| 408 |
int idx = (int)(ht->heap[i] - ht->timer_dups); |
| 409 |
|
| 410 |
pj_assert(idx >= 0 && idx < (int)ht->max_size); |
| 411 |
new_heap[i] = &new_timer_dups[idx]; |
| 412 |
} |
| 413 |
ht->timer_dups = new_timer_dups; |
| 414 |
#else |
| 415 |
memcpy(new_heap, ht->heap, ht->max_size * sizeof(pj_timer_entry *)); |
| 416 |
#endif |
| 417 |
|
| 418 |
#if PJ_TIMER_USE_LINKED_LIST |
| 419 |
tmp_dup = ht->head_list.next; |
| 420 |
pj_list_init(&ht->head_list); |
| 421 |
for (; tmp_dup != &ht->head_list; tmp_dup = tmp_dup->next) |
| 422 |
{ |
| 423 |
int slot = ht->timer_ids[GET_FIELD(tmp_dup, _timer_id)]; |
| 424 |
new_dup = new_heap[slot]; |
| 425 |
pj_list_push_back(&ht->head_list, new_dup); |
| 426 |
} |
| 427 |
#endif |
| 428 |
|
| 429 |
ht->heap = new_heap; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
new_timer_ids = 0; |
| 434 |
new_timer_ids = (pj_timer_id_t*) |
| 435 |
pj_pool_alloc(ht->pool, new_size * sizeof(pj_timer_id_t)); |
| 436 |
if (!new_timer_ids) |
| 437 |
return PJ_ENOMEM; |
| 438 |
|
| 439 |
memcpy( new_timer_ids, ht->timer_ids, ht->max_size * sizeof(pj_timer_id_t)); |
| 440 |
|
| 441 |
|
| 442 |
ht->timer_ids = new_timer_ids; |
| 443 |
|
| 444 |
|
| 445 |
for (i = ht->max_size; i < new_size; i++) |
| 446 |
ht->timer_ids[i] = -((pj_timer_id_t) (i + 1)); |
| 447 |
|
| 448 |
ht->max_size = new_size; |
| 449 |
|
| 450 |
return PJ_SUCCESS; |
| 451 |
} |
| 452 |
|
| 453 |
static pj_status_t insert_node(pj_timer_heap_t *ht, |
| 454 |
pj_timer_entry *new_node, |
| 455 |
const pj_time_val *future_time) |
| 456 |
{ |
| 457 |
pj_timer_entry_dup *timer_copy; |
| 458 |
|
| 459 |
#if PJ_TIMER_USE_LINKED_LIST |
| 460 |
pj_timer_entry_dup *tmp_node = NULL; |
| 461 |
#endif |
| 462 |
|
| 463 |
if (ht->cur_size + 2 >= ht->max_size) { |
| 464 |
pj_status_t status = grow_heap(ht); |
| 465 |
if (status != PJ_SUCCESS) |
| 466 |
return status; |
| 467 |
} |
| 468 |
|
| 469 |
timer_copy = GET_TIMER(ht, new_node); |
| 470 |
#if PJ_TIMER_USE_COPY |
| 471 |
|
| 472 |
pj_bzero(timer_copy, sizeof(*timer_copy)); |
| 473 |
pj_memcpy(&timer_copy->dup, new_node, sizeof(*new_node)); |
| 474 |
timer_copy->entry = new_node; |
| 475 |
#endif |
| 476 |
|
| 477 |
#if PJ_TIMER_USE_LINKED_LIST |
| 478 |
pj_list_init(timer_copy); |
| 479 |
#endif |
| 480 |
|
| 481 |
timer_copy->_timer_value = *future_time; |
| 482 |
|
| 483 |
#if !PJ_TIMER_USE_LINKED_LIST |
| 484 |
reheap_up(ht, timer_copy, ht->cur_size, HEAP_PARENT(ht->cur_size)); |
| 485 |
#else |
| 486 |
if (ht->cur_size == 0) { |
| 487 |
pj_list_push_back(&ht->head_list, timer_copy); |
| 488 |
} else if (PJ_TIME_VAL_GTE(*future_time, |
| 489 |
ht->head_list.prev->_timer_value)) |
| 490 |
{ |
| 491 |
|
| 492 |
pj_list_insert_before(&ht->head_list, timer_copy); |
| 493 |
} else { |
| 494 |
tmp_node = ht->head_list.next; |
| 495 |
while (tmp_node->next != &ht->head_list && |
| 496 |
PJ_TIME_VAL_GT(*future_time, tmp_node->_timer_value)) |
| 497 |
{ |
| 498 |
tmp_node = tmp_node->next; |
| 499 |
} |
| 500 |
if (PJ_TIME_VAL_LT(*future_time, tmp_node->_timer_value)) { |
| 501 |
pj_list_insert_before(tmp_node, timer_copy); |
| 502 |
} else { |
| 503 |
pj_list_insert_after(tmp_node, timer_copy); |
| 504 |
} |
| 505 |
} |
| 506 |
copy_node(ht, new_node->_timer_id-1, timer_copy); |
| 507 |
#endif |
| 508 |
ht->cur_size++; |
| 509 |
|
| 510 |
return PJ_SUCCESS; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
static pj_status_t schedule_entry( pj_timer_heap_t *ht, |
| 515 |
pj_timer_entry *entry, |
| 516 |
const pj_time_val *future_time ) |
| 517 |
{ |
| 518 |
if (ht->cur_size < ht->max_size) |
| 519 |
{ |
| 520 |
|
| 521 |
|
| 522 |
entry->_timer_id = pop_freelist(ht); |
| 523 |
|
| 524 |
return insert_node( ht, entry, future_time ); |
| 525 |
} |
| 526 |
else |
| 527 |
return -1; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
static int cancel( pj_timer_heap_t *ht, |
| 532 |
pj_timer_entry *entry, |
| 533 |
unsigned flags) |
| 534 |
{ |
| 535 |
long timer_node_slot; |
| 536 |
|
| 537 |
PJ_CHECK_STACK(); |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
timer_node_slot = ht->timer_ids[entry->_timer_id]; |
| 549 |
|
| 550 |
if (timer_node_slot < 0) { |
| 551 |
entry->_timer_id = -1; |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
if (entry != GET_ENTRY(ht->heap[timer_node_slot])) { |
| 556 |
if ((flags & F_DONT_ASSERT) == 0) |
| 557 |
pj_assert(entry == GET_ENTRY(ht->heap[timer_node_slot])); |
| 558 |
entry->_timer_id = -1; |
| 559 |
return 0; |
| 560 |
} else { |
| 561 |
remove_node( ht, timer_node_slot); |
| 562 |
|
| 563 |
if ((flags & F_DONT_CALL) == 0) { |
| 564 |
|
| 565 |
(*ht->callback)(ht, entry); |
| 566 |
} |
| 567 |
return 1; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
PJ_DEF(pj_size_t) pj_timer_heap_mem_size(pj_size_t count) |
| 576 |
{ |
| 577 |
return |
| 578 |
sizeof(pj_timer_heap_t) + |
| 579 |
|
| 580 |
(count+2) * (sizeof(pj_timer_entry_dup*)+sizeof(pj_timer_id_t)+ |
| 581 |
sizeof(pj_timer_entry_dup)) + |
| 582 |
|
| 583 |
132; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
PJ_DEF(pj_status_t) pj_timer_heap_create( pj_pool_t *pool, |
| 590 |
pj_size_t size, |
| 591 |
pj_timer_heap_t **p_heap) |
| 592 |
{ |
| 593 |
pj_timer_heap_t *ht; |
| 594 |
pj_size_t i; |
| 595 |
|
| 596 |
PJ_ASSERT_RETURN(pool && p_heap, PJ_EINVAL); |
| 597 |
|
| 598 |
*p_heap = NULL; |
| 599 |
|
| 600 |
|
| 601 |
size += 2; |
| 602 |
|
| 603 |
|
| 604 |
ht = PJ_POOL_ZALLOC_T(pool, pj_timer_heap_t); |
| 605 |
if (!ht) |
| 606 |
return PJ_ENOMEM; |
| 607 |
|
| 608 |
|
| 609 |
ht->max_size = size; |
| 610 |
ht->cur_size = 0; |
| 611 |
ht->max_entries_per_poll = DEFAULT_MAX_TIMED_OUT_PER_POLL; |
| 612 |
ht->timer_ids_freelist = 1; |
| 613 |
ht->pool = pool; |
| 614 |
|
| 615 |
|
| 616 |
ht->lock = NULL; |
| 617 |
ht->auto_delete_lock = 0; |
| 618 |
|
| 619 |
|
| 620 |
ht->heap = (pj_timer_entry_dup**) |
| 621 |
pj_pool_calloc(pool, (unsigned)size, |
| 622 |
sizeof(pj_timer_entry_dup*)); |
| 623 |
if (!ht->heap) |
| 624 |
return PJ_ENOMEM; |
| 625 |
|
| 626 |
#if PJ_TIMER_USE_COPY |
| 627 |
|
| 628 |
ht->timer_dups = (pj_timer_entry_dup*) |
| 629 |
pj_pool_alloc(pool, sizeof(pj_timer_entry_dup) * size); |
| 630 |
if (!ht->timer_dups) |
| 631 |
return PJ_ENOMEM; |
| 632 |
#endif |
| 633 |
|
| 634 |
|
| 635 |
ht->timer_ids = (pj_timer_id_t *) |
| 636 |
pj_pool_alloc( pool, sizeof(pj_timer_id_t) * size); |
| 637 |
if (!ht->timer_ids) |
| 638 |
return PJ_ENOMEM; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
for (i=0; i<size; ++i) |
| 644 |
ht->timer_ids[i] = -((pj_timer_id_t) (i + 1)); |
| 645 |
|
| 646 |
#if PJ_TIMER_USE_LINKED_LIST |
| 647 |
pj_list_init(&ht->head_list); |
| 648 |
#endif |
| 649 |
|
| 650 |
*p_heap = ht; |
| 651 |
return PJ_SUCCESS; |
| 652 |
} |
| 653 |
|
| 654 |
PJ_DEF(void) pj_timer_heap_destroy( pj_timer_heap_t *ht ) |
| 655 |
{ |
| 656 |
if (ht->lock && ht->auto_delete_lock) { |
| 657 |
pj_lock_destroy(ht->lock); |
| 658 |
ht->lock = NULL; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
PJ_DEF(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht, |
| 663 |
pj_lock_t *lock, |
| 664 |
pj_bool_t auto_del ) |
| 665 |
{ |
| 666 |
if (ht->lock && ht->auto_delete_lock) |
| 667 |
pj_lock_destroy(ht->lock); |
| 668 |
|
| 669 |
ht->lock = lock; |
| 670 |
ht->auto_delete_lock = auto_del; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
PJ_DEF(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht, |
| 675 |
unsigned count ) |
| 676 |
{ |
| 677 |
unsigned old_count = ht->max_entries_per_poll; |
| 678 |
ht->max_entries_per_poll = count; |
| 679 |
return old_count; |
| 680 |
} |
| 681 |
|
| 682 |
PJ_DEF(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry, |
| 683 |
int id, |
| 684 |
void *user_data, |
| 685 |
pj_timer_heap_callback *cb ) |
| 686 |
{ |
| 687 |
pj_assert(entry && cb); |
| 688 |
|
| 689 |
entry->_timer_id = -1; |
| 690 |
entry->id = id; |
| 691 |
entry->user_data = user_data; |
| 692 |
entry->cb = cb; |
| 693 |
#if !PJ_TIMER_USE_COPY |
| 694 |
entry->_grp_lock = NULL; |
| 695 |
#endif |
| 696 |
|
| 697 |
return entry; |
| 698 |
} |
| 699 |
|
| 700 |
PJ_DEF(pj_bool_t) pj_timer_entry_running( pj_timer_entry *entry ) |
| 701 |
{ |
| 702 |
return (entry->_timer_id >= 1); |
| 703 |
} |
| 704 |
|
| 705 |
#if PJ_TIMER_DEBUG |
| 706 |
static pj_status_t schedule_w_grp_lock_dbg(pj_timer_heap_t *ht, |
| 707 |
pj_timer_entry *entry, |
| 708 |
const pj_time_val *delay, |
| 709 |
pj_bool_t set_id, |
| 710 |
int id_val, |
| 711 |
pj_grp_lock_t *grp_lock, |
| 712 |
const char *src_file, |
| 713 |
int src_line) |
| 714 |
#else |
| 715 |
static pj_status_t schedule_w_grp_lock(pj_timer_heap_t *ht, |
| 716 |
pj_timer_entry *entry, |
| 717 |
const pj_time_val *delay, |
| 718 |
pj_bool_t set_id, |
| 719 |
int id_val, |
| 720 |
pj_grp_lock_t *grp_lock) |
| 721 |
#endif |
| 722 |
{ |
| 723 |
pj_status_t status; |
| 724 |
pj_time_val expires; |
| 725 |
|
| 726 |
PJ_ASSERT_RETURN(ht && entry && delay, PJ_EINVAL); |
| 727 |
PJ_ASSERT_RETURN(entry->cb != NULL, PJ_EINVAL); |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
pj_gettickcount(&expires); |
| 733 |
PJ_TIME_VAL_ADD(expires, *delay); |
| 734 |
|
| 735 |
lock_timer_heap(ht); |
| 736 |
|
| 737 |
|
| 738 |
if (pj_timer_entry_running(entry)) { |
| 739 |
unlock_timer_heap(ht); |
| 740 |
PJ_LOG(3,(THIS_FILE, "Warning! Rescheduling outstanding entry (%p)", |
| 741 |
entry)); |
| 742 |
return PJ_EINVALIDOP; |
| 743 |
} |
| 744 |
|
| 745 |
status = schedule_entry(ht, entry, &expires); |
| 746 |
if (status == PJ_SUCCESS) { |
| 747 |
pj_timer_entry_dup *timer_copy = GET_TIMER(ht, entry); |
| 748 |
|
| 749 |
if (set_id) |
| 750 |
GET_FIELD(timer_copy, id) = entry->id = id_val; |
| 751 |
timer_copy->_grp_lock = grp_lock; |
| 752 |
if (timer_copy->_grp_lock) { |
| 753 |
pj_grp_lock_add_ref(timer_copy->_grp_lock); |
| 754 |
} |
| 755 |
#if PJ_TIMER_DEBUG |
| 756 |
timer_copy->src_file = src_file; |
| 757 |
timer_copy->src_line = src_line; |
| 758 |
#endif |
| 759 |
} |
| 760 |
unlock_timer_heap(ht); |
| 761 |
|
| 762 |
return status; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
#if PJ_TIMER_DEBUG |
| 767 |
PJ_DEF(pj_status_t) pj_timer_heap_schedule_dbg( pj_timer_heap_t *ht, |
| 768 |
pj_timer_entry *entry, |
| 769 |
const pj_time_val *delay, |
| 770 |
const char *src_file, |
| 771 |
int src_line) |
| 772 |
{ |
| 773 |
return schedule_w_grp_lock_dbg(ht, entry, delay, PJ_FALSE, 1, NULL, |
| 774 |
src_file, src_line); |
| 775 |
} |
| 776 |
|
| 777 |
PJ_DEF(pj_status_t) pj_timer_heap_schedule_w_grp_lock_dbg( |
| 778 |
pj_timer_heap_t *ht, |
| 779 |
pj_timer_entry *entry, |
| 780 |
const pj_time_val *delay, |
| 781 |
int id_val, |
| 782 |
pj_grp_lock_t *grp_lock, |
| 783 |
const char *src_file, |
| 784 |
int src_line) |
| 785 |
{ |
| 786 |
return schedule_w_grp_lock_dbg(ht, entry, delay, PJ_TRUE, id_val, |
| 787 |
grp_lock, src_file, src_line); |
| 788 |
} |
| 789 |
|
| 790 |
#else |
| 791 |
PJ_DEF(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht, |
| 792 |
pj_timer_entry *entry, |
| 793 |
const pj_time_val *delay) |
| 794 |
{ |
| 795 |
return schedule_w_grp_lock(ht, entry, delay, PJ_FALSE, 1, NULL); |
| 796 |
} |
| 797 |
|
| 798 |
PJ_DEF(pj_status_t) pj_timer_heap_schedule_w_grp_lock(pj_timer_heap_t *ht, |
| 799 |
pj_timer_entry *entry, |
| 800 |
const pj_time_val *delay, |
| 801 |
int id_val, |
| 802 |
pj_grp_lock_t *grp_lock) |
| 803 |
{ |
| 804 |
return schedule_w_grp_lock(ht, entry, delay, PJ_TRUE, id_val, grp_lock); |
| 805 |
} |
| 806 |
#endif |
| 807 |
|
| 808 |
static int cancel_timer(pj_timer_heap_t *ht, |
| 809 |
pj_timer_entry *entry, |
| 810 |
unsigned flags, |
| 811 |
int id_val) |
| 812 |
{ |
| 813 |
pj_timer_entry_dup *timer_copy; |
| 814 |
pj_grp_lock_t *grp_lock; |
| 815 |
int count; |
| 816 |
|
| 817 |
PJ_ASSERT_RETURN(ht && entry, PJ_EINVAL); |
| 818 |
|
| 819 |
lock_timer_heap(ht); |
| 820 |
|
| 821 |
|
| 822 |
if (entry->_timer_id < 1 || (pj_size_t)entry->_timer_id >= ht->max_size) { |
| 823 |
unlock_timer_heap(ht); |
| 824 |
return 0; |
| 825 |
} |
| 826 |
|
| 827 |
timer_copy = GET_TIMER(ht, entry); |
| 828 |
grp_lock = timer_copy->_grp_lock; |
| 829 |
|
| 830 |
count = cancel(ht, entry, flags | F_DONT_CALL); |
| 831 |
if (count > 0) { |
| 832 |
|
| 833 |
if (flags & F_SET_ID) { |
| 834 |
entry->id = id_val; |
| 835 |
} |
| 836 |
if (grp_lock) { |
| 837 |
pj_grp_lock_dec_ref(grp_lock); |
| 838 |
} |
| 839 |
} |
| 840 |
unlock_timer_heap(ht); |
| 841 |
|
| 842 |
return count; |
| 843 |
} |
| 844 |
|
| 845 |
PJ_DEF(int) pj_timer_heap_cancel( pj_timer_heap_t *ht, |
| 846 |
pj_timer_entry *entry) |
| 847 |
{ |
| 848 |
return cancel_timer(ht, entry, 0, 0); |
| 849 |
} |
| 850 |
|
| 851 |
PJ_DEF(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht, |
| 852 |
pj_timer_entry *entry, |
| 853 |
int id_val) |
| 854 |
{ |
| 855 |
return cancel_timer(ht, entry, F_SET_ID | F_DONT_ASSERT, id_val); |
| 856 |
} |
| 857 |
|
| 858 |
PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht, |
| 859 |
pj_time_val *next_delay ) |
| 860 |
{ |
| 861 |
pj_time_val now; |
| 862 |
pj_time_val min_time_node = {0,0}; |
| 863 |
unsigned count; |
| 864 |
pj_timer_id_t slot = 0; |
| 865 |
|
| 866 |
PJ_ASSERT_RETURN(ht, 0); |
| 867 |
|
| 868 |
lock_timer_heap(ht); |
| 869 |
if (!ht->cur_size && next_delay) { |
| 870 |
next_delay->sec = next_delay->msec = PJ_MAXINT32; |
| 871 |
unlock_timer_heap(ht); |
| 872 |
return 0; |
| 873 |
} |
| 874 |
|
| 875 |
count = 0; |
| 876 |
pj_gettickcount(&now); |
| 877 |
|
| 878 |
if (ht->cur_size) { |
| 879 |
#if PJ_TIMER_USE_LINKED_LIST |
| 880 |
slot = ht->timer_ids[GET_FIELD(ht->head_list.next, _timer_id)]; |
| 881 |
#endif |
| 882 |
min_time_node = ht->heap[slot]->_timer_value; |
| 883 |
} |
| 884 |
|
| 885 |
while ( ht->cur_size && |
| 886 |
PJ_TIME_VAL_LTE(min_time_node, now) && |
| 887 |
count < ht->max_entries_per_poll ) |
| 888 |
{ |
| 889 |
pj_timer_entry_dup *node = remove_node(ht, slot); |
| 890 |
pj_timer_entry *entry = GET_ENTRY(node); |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
pj_grp_lock_t *grp_lock; |
| 895 |
pj_bool_t valid = PJ_TRUE; |
| 896 |
|
| 897 |
++count; |
| 898 |
|
| 899 |
grp_lock = node->_grp_lock; |
| 900 |
node->_grp_lock = NULL; |
| 901 |
if (GET_FIELD(node, cb) != entry->cb || |
| 902 |
GET_FIELD(node, user_data) != entry->user_data) |
| 903 |
{ |
| 904 |
valid = PJ_FALSE; |
| 905 |
#if PJ_TIMER_DEBUG |
| 906 |
PJ_LOG(3,(THIS_FILE, "Bug! Polling entry %p from %s line %d has " |
| 907 |
"been deallocated without being cancelled", |
| 908 |
GET_ENTRY(node), |
| 909 |
node->src_file, node->src_line)); |
| 910 |
#else |
| 911 |
PJ_LOG(3,(THIS_FILE, "Bug! Polling entry %p has " |
| 912 |
"been deallocated without being cancelled", |
| 913 |
GET_ENTRY(node))); |
| 914 |
#endif |
| 915 |
#if ASSERT_IF_ENTRY_DESTROYED |
| 916 |
pj_assert(node->dup.cb == entry->cb); |
| 917 |
pj_assert(node->dup.user_data == entry->user_data); |
| 918 |
#endif |
| 919 |
} |
| 920 |
|
| 921 |
unlock_timer_heap(ht); |
| 922 |
|
| 923 |
PJ_RACE_ME(5); |
| 924 |
|
| 925 |
if (valid && entry->cb) |
| 926 |
(*entry->cb)(ht, entry); |
| 927 |
|
| 928 |
if (valid && grp_lock) |
| 929 |
pj_grp_lock_dec_ref(grp_lock); |
| 930 |
|
| 931 |
lock_timer_heap(ht); |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if (ht->cur_size) { |
| 936 |
#if PJ_TIMER_USE_LINKED_LIST |
| 937 |
slot = ht->timer_ids[GET_FIELD(ht->head_list.next, _timer_id)]; |
| 938 |
#endif |
| 939 |
min_time_node = ht->heap[slot]->_timer_value; |
| 940 |
|
| 941 |
pj_gettickcount(&now); |
| 942 |
} |
| 943 |
} |
| 944 |
if (ht->cur_size && next_delay) { |
| 945 |
*next_delay = ht->heap[0]->_timer_value; |
| 946 |
if (count > 0) |
| 947 |
pj_gettickcount(&now); |
| 948 |
PJ_TIME_VAL_SUB(*next_delay, now); |
| 949 |
if (next_delay->sec < 0 || next_delay->msec < 0) |
| 950 |
next_delay->sec = next_delay->msec = 0; |
| 951 |
} else if (next_delay) { |
| 952 |
next_delay->sec = next_delay->msec = PJ_MAXINT32; |
| 953 |
} |
| 954 |
unlock_timer_heap(ht); |
| 955 |
|
| 956 |
return count; |
| 957 |
} |
| 958 |
|
| 959 |
PJ_DEF(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht ) |
| 960 |
{ |
| 961 |
PJ_ASSERT_RETURN(ht, 0); |
| 962 |
|
| 963 |
return ht->cur_size; |
| 964 |
} |
| 965 |
|
| 966 |
PJ_DEF(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t * ht, |
| 967 |
pj_time_val *timeval) |
| 968 |
{ |
| 969 |
pj_assert(ht->cur_size != 0); |
| 970 |
if (ht->cur_size == 0) |
| 971 |
return PJ_ENOTFOUND; |
| 972 |
|
| 973 |
lock_timer_heap(ht); |
| 974 |
*timeval = ht->heap[0]->_timer_value; |
| 975 |
unlock_timer_heap(ht); |
| 976 |
|
| 977 |
return PJ_SUCCESS; |
| 978 |
} |
| 979 |
|
| 980 |
#if PJ_TIMER_DEBUG |
| 981 |
PJ_DEF(void) pj_timer_heap_dump(pj_timer_heap_t *ht) |
| 982 |
{ |
| 983 |
lock_timer_heap(ht); |
| 984 |
|
| 985 |
PJ_LOG(3,(THIS_FILE, "Dumping timer heap:")); |
| 986 |
PJ_LOG(3,(THIS_FILE, " Cur size: %d entries, max: %d", |
| 987 |
(int)ht->cur_size, (int)ht->max_size)); |
| 988 |
|
| 989 |
if (ht->cur_size) { |
| 990 |
#if PJ_TIMER_USE_LINKED_LIST |
| 991 |
pj_timer_entry_dup *tmp_dup; |
| 992 |
#else |
| 993 |
unsigned i; |
| 994 |
#endif |
| 995 |
pj_time_val now; |
| 996 |
|
| 997 |
PJ_LOG(3,(THIS_FILE, " Entries: ")); |
| 998 |
PJ_LOG(3,(THIS_FILE, " _id\tId\tElapsed\tSource")); |
| 999 |
PJ_LOG(3,(THIS_FILE, " ----------------------------------")); |
| 1000 |
|
| 1001 |
pj_gettickcount(&now); |
| 1002 |
|
| 1003 |
#if !PJ_TIMER_USE_LINKED_LIST |
| 1004 |
for (i=0; i<(unsigned)ht->cur_size; ++i) |
| 1005 |
{ |
| 1006 |
pj_timer_entry_dup *e = ht->heap[i]; |
| 1007 |
#else |
| 1008 |
for (tmp_dup = ht->head_list.next; tmp_dup != &ht->head_list; |
| 1009 |
tmp_dup = tmp_dup->next) |
| 1010 |
{ |
| 1011 |
pj_timer_entry_dup *e = tmp_dup; |
| 1012 |
#endif |
| 1013 |
|
| 1014 |
pj_time_val delta; |
| 1015 |
|
| 1016 |
if (PJ_TIME_VAL_LTE(e->_timer_value, now)) |
| 1017 |
delta.sec = delta.msec = 0; |
| 1018 |
else { |
| 1019 |
delta = e->_timer_value; |
| 1020 |
PJ_TIME_VAL_SUB(delta, now); |
| 1021 |
} |
| 1022 |
|
| 1023 |
PJ_LOG(3,(THIS_FILE, " %d\t%d\t%d.%03d\t%s:%d", |
| 1024 |
GET_FIELD(e, _timer_id), GET_FIELD(e, id), |
| 1025 |
(int)delta.sec, (int)delta.msec, |
| 1026 |
e->src_file, e->src_line)); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
unlock_timer_heap(ht); |
| 1031 |
} |
| 1032 |
#endif |
| 1033 |
|