Changeset 6099 for pjproject/trunk/pjlib/src/pj/timer.c
- Timestamp:
- Nov 6, 2019 6:17:53 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/timer.c
r6067 r6099 51 51 * the timer heap will simply remove the destroyed entry (and print log) 52 52 * and resume normally. 53 * This setting only works if PJ_TIMER_ HEAP_USE_COPY is enabled.53 * This setting only works if PJ_TIMER_USE_COPY is enabled. 54 54 */ 55 #define ASSERT_IF_ENTRY_DESTROYED (PJ_TIMER_ HEAP_USE_COPY? 0: 0)55 #define ASSERT_IF_ENTRY_DESTROYED (PJ_TIMER_USE_COPY? 0: 0) 56 56 57 57 … … 63 63 }; 64 64 65 #if PJ_TIMER_ HEAP_USE_COPY65 #if PJ_TIMER_USE_COPY 66 66 67 67 /* Duplicate/copy of the timer entry. */ 68 68 typedef struct pj_timer_entry_dup 69 69 { 70 #if PJ_TIMER_USE_LINKED_LIST 71 /** 72 * Standard list members. 73 */ 74 PJ_DECL_LIST_MEMBER(struct pj_timer_entry_dup); 75 #endif 76 70 77 /** 71 78 * The duplicate copy. … … 141 148 */ 142 149 pj_timer_entry_dup **heap; 150 151 #if PJ_TIMER_USE_LINKED_LIST 152 /** 153 * If timer heap uses linked list, then this will represent the head of 154 * the list. 155 */ 156 pj_timer_entry_dup head_list; 157 #endif 143 158 144 159 /** … … 289 304 { 290 305 pj_timer_entry_dup *removed_node = ht->heap[slot]; 291 306 292 307 // Return this timer id to the freelist. 293 308 push_freelist( ht, GET_FIELD(removed_node, _timer_id) ); … … 321 336 GET_FIELD(removed_node, _timer_id) = -1; 322 337 338 #if !PJ_TIMER_USE_LINKED_LIST 323 339 // Only try to reheapify if we're not deleting the last entry. 324 340 … … 344 360 } 345 361 } 346 362 #else 363 pj_list_erase(removed_node); 364 #endif 365 347 366 return removed_node; 348 367 } … … 352 371 // All the containers will double in size from max_size_ 353 372 size_t new_size = ht->max_size * 2; 373 #if PJ_TIMER_USE_COPY 354 374 pj_timer_entry_dup *new_timer_dups = 0; 375 #endif 355 376 pj_timer_id_t *new_timer_ids; 356 377 pj_size_t i; 357 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 358 384 359 385 PJ_LOG(6,(THIS_FILE, "Growing heap size from %d to %d", … … 366 392 return PJ_ENOMEM; 367 393 368 #if PJ_TIMER_ HEAP_USE_COPY394 #if PJ_TIMER_USE_COPY 369 395 // Grow the array of timer copies. 370 396 … … 387 413 memcpy(new_heap, ht->heap, ht->max_size * sizeof(pj_timer_entry *)); 388 414 #endif 415 416 #if PJ_TIMER_USE_LINKED_LIST 417 tmp_dup = ht->head_list.next; 418 pj_list_init(&ht->head_list); 419 for (; tmp_dup != &ht->head_list; tmp_dup = tmp_dup->next) 420 { 421 int slot = ht->timer_ids[GET_FIELD(tmp_dup, _timer_id)]; 422 new_dup = new_heap[slot]; 423 pj_list_push_back(&ht->head_list, new_dup); 424 } 425 #endif 426 389 427 ht->heap = new_heap; 390 428 … … 417 455 pj_timer_entry_dup *timer_copy; 418 456 457 #if PJ_TIMER_USE_LINKED_LIST 458 pj_timer_entry_dup *tmp_node = NULL; 459 #endif 460 419 461 if (ht->cur_size + 2 >= ht->max_size) { 420 462 pj_status_t status = grow_heap(ht); … … 424 466 425 467 timer_copy = GET_TIMER(ht, new_node); 426 #if PJ_TIMER_ HEAP_USE_COPY468 #if PJ_TIMER_USE_COPY 427 469 // Create a duplicate of the timer entry. 428 470 pj_bzero(timer_copy, sizeof(*timer_copy)); … … 430 472 timer_copy->entry = new_node; 431 473 #endif 474 475 #if PJ_TIMER_USE_LINKED_LIST 476 pj_list_init(timer_copy); 477 #endif 478 432 479 timer_copy->_timer_value = *future_time; 433 480 434 reheap_up( ht, timer_copy, ht->cur_size, HEAP_PARENT(ht->cur_size)); 481 #if !PJ_TIMER_USE_LINKED_LIST 482 reheap_up(ht, timer_copy, ht->cur_size, HEAP_PARENT(ht->cur_size)); 483 #else 484 if (ht->cur_size == 0) { 485 pj_list_push_back(&ht->head_list, timer_copy); 486 } else if (PJ_TIME_VAL_GTE(*future_time, 487 ht->head_list.prev->_timer_value)) 488 { 489 /* Insert the max value to the end of the list. */ 490 pj_list_insert_before(&ht->head_list, timer_copy); 491 } else { 492 tmp_node = ht->head_list.next; 493 while (tmp_node->next != &ht->head_list && 494 PJ_TIME_VAL_GT(*future_time, tmp_node->_timer_value)) 495 { 496 tmp_node = tmp_node->next; 497 } 498 if (PJ_TIME_VAL_LT(*future_time, tmp_node->_timer_value)) { 499 pj_list_insert_before(tmp_node, timer_copy); 500 } else { 501 pj_list_insert_after(tmp_node, timer_copy); 502 } 503 } 504 copy_node(ht, new_node->_timer_id-1, timer_copy); 505 #endif 435 506 ht->cur_size++; 436 507 … … 547 618 return PJ_ENOMEM; 548 619 549 #if PJ_TIMER_ HEAP_USE_COPY620 #if PJ_TIMER_USE_COPY 550 621 // Create the timer entry copies array. 551 622 ht->timer_dups = (pj_timer_entry_dup*) … … 567 638 ht->timer_ids[i] = -((pj_timer_id_t) (i + 1)); 568 639 640 #if PJ_TIMER_USE_LINKED_LIST 641 pj_list_init(&ht->head_list); 642 #endif 643 569 644 *p_heap = ht; 570 645 return PJ_SUCCESS; … … 610 685 entry->user_data = user_data; 611 686 entry->cb = cb; 612 #if !PJ_TIMER_ HEAP_USE_COPY687 #if !PJ_TIMER_USE_COPY 613 688 entry->_grp_lock = NULL; 614 689 #endif … … 772 847 { 773 848 pj_time_val now; 849 pj_time_val min_time_node = {0,0}; 774 850 unsigned count; 851 pj_timer_id_t slot = 0; 775 852 776 853 PJ_ASSERT_RETURN(ht, 0); … … 786 863 pj_gettickcount(&now); 787 864 865 if (ht->cur_size) { 866 #if PJ_TIMER_USE_LINKED_LIST 867 slot = ht->timer_ids[GET_FIELD(ht->head_list.next, _timer_id)]; 868 #endif 869 min_time_node = ht->heap[slot]->_timer_value; 870 } 871 788 872 while ( ht->cur_size && 789 PJ_TIME_VAL_LTE( ht->heap[0]->_timer_value, now) &&873 PJ_TIME_VAL_LTE(min_time_node, now) && 790 874 count < ht->max_entries_per_poll ) 791 875 { 792 pj_timer_entry_dup *node = remove_node(ht, 0);876 pj_timer_entry_dup *node = remove_node(ht, slot); 793 877 pj_timer_entry *entry = GET_ENTRY(node); 794 878 /* Avoid re-use of this timer until the callback is done. */ … … 835 919 /* Now, the timer is really free for re-use. */ 836 920 ///push_freelist(ht, node_timer_id); 921 922 if (ht->cur_size) { 923 #if PJ_TIMER_USE_LINKED_LIST 924 slot = ht->timer_ids[GET_FIELD(ht->head_list.next, _timer_id)]; 925 #endif 926 min_time_node = ht->heap[slot]->_timer_value; 927 } 837 928 } 838 929 if (ht->cur_size && next_delay) { … … 880 971 881 972 if (ht->cur_size) { 973 #if PJ_TIMER_USE_LINKED_LIST 974 pj_timer_entry_dup *tmp_dup; 975 #else 882 976 unsigned i; 977 #endif 883 978 pj_time_val now; 884 979 … … 889 984 pj_gettickcount(&now); 890 985 891 for (i=0; i<(unsigned)ht->cur_size; ++i) { 986 #if !PJ_TIMER_USE_LINKED_LIST 987 for (i=0; i<(unsigned)ht->cur_size; ++i) 988 { 892 989 pj_timer_entry_dup *e = ht->heap[i]; 990 #else 991 for (tmp_dup = ht->head_list.next; tmp_dup != &ht->head_list; 992 tmp_dup = tmp_dup->next) 993 { 994 pj_timer_entry_dup *e = tmp_dup; 995 #endif 996 893 997 pj_time_val delta; 894 998
Note: See TracChangeset
for help on using the changeset viewer.