Changeset 36 for pjproject/main/pjlib/src/pj/timer.c
- Timestamp:
- Nov 9, 2005 3:37:19 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/main/pjlib/src/pj/timer.c
r6 r36 15 15 #include <pj/assert.h> 16 16 #include <pj/errno.h> 17 #include <pj/lock.h> 17 18 18 19 #define HEAP_PARENT(X) (X == 0 ? 0 : (((X) - 1) / 2)) 19 20 #define HEAP_LEFT(X) (((X)+(X))+1) 21 22 23 #define DEFAULT_MAX_TIMED_OUT_PER_POLL (64) 20 24 21 25 … … 34 38 pj_size_t cur_size; 35 39 36 /** Mutex for synchronization, or NULL */ 37 pj_mutex_t *mutex; 40 /** Max timed out entries to process per poll. */ 41 unsigned max_entries_per_poll; 42 43 /** Lock object. */ 44 pj_lock_t *lock; 45 46 /** Autodelete lock. */ 47 pj_bool_t auto_delete_lock; 38 48 39 49 /** … … 72 82 PJ_INLINE(void) lock_timer_heap( pj_timer_heap_t *ht ) 73 83 { 74 if (ht-> mutex) {75 pj_ mutex_lock(ht->mutex);84 if (ht->lock) { 85 pj_lock_acquire(ht->lock); 76 86 } 77 87 } … … 79 89 PJ_INLINE(void) unlock_timer_heap( pj_timer_heap_t *ht ) 80 90 { 81 if (ht-> mutex) {82 pj_ mutex_unlock(ht->mutex);91 if (ht->lock) { 92 pj_lock_release(ht->lock); 83 93 } 84 94 } … … 320 330 /* size of each entry: */ 321 331 (count+2) * (sizeof(pj_timer_entry*)+sizeof(pj_timer_id_t)) + 322 /* mutex, pool etc: */332 /* lock, pool etc: */ 323 333 132; 324 334 } … … 329 339 PJ_DEF(pj_status_t) pj_timer_heap_create( pj_pool_t *pool, 330 340 pj_size_t size, 331 unsigned flag,332 341 pj_timer_heap_t **p_heap) 333 342 { … … 350 359 ht->max_size = size; 351 360 ht->cur_size = 0; 361 ht->max_entries_per_poll = DEFAULT_MAX_TIMED_OUT_PER_POLL; 352 362 ht->timer_ids_freelist = 1; 353 363 ht->pool = pool; 354 364 355 /* Mutex. */ 356 if (flag & PJ_TIMER_HEAP_NO_SYNCHRONIZE) { 357 ht->mutex = NULL; 358 } else { 359 pj_status_t rc; 360 361 /* Mutex must be the recursive types. 362 * See commented code inside pj_timer_heap_poll() 363 */ 364 rc = pj_mutex_create(pool, "tmhp%p", PJ_MUTEX_RECURSE, &ht->mutex); 365 if (rc != PJ_SUCCESS) 366 return rc; 367 } 365 /* Lock. */ 366 ht->lock = NULL; 367 ht->auto_delete_lock = 0; 368 368 369 369 // Create the heap array. … … 387 387 } 388 388 389 PJ_DEF(void) pj_timer_heap_destroy( pj_timer_heap_t *ht ) 390 { 391 if (ht->lock && ht->auto_delete_lock) { 392 pj_lock_destroy(ht->lock); 393 ht->lock = NULL; 394 } 395 } 396 397 PJ_DEF(void) pj_timer_heap_set_lock( pj_timer_heap_t *ht, 398 pj_lock_t *lock, 399 pj_bool_t auto_del ) 400 { 401 if (ht->lock && ht->auto_delete_lock) 402 pj_lock_destroy(ht->lock); 403 404 ht->lock = lock; 405 ht->auto_delete_lock = auto_del; 406 } 407 408 409 PJ_DEF(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht, 410 unsigned count ) 411 { 412 unsigned old_count = ht->max_entries_per_poll; 413 ht->max_entries_per_poll = count; 414 return old_count; 415 } 416 389 417 PJ_DEF(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry, 390 418 int id, … … 434 462 } 435 463 436 PJ_DEF(int) pj_timer_heap_poll( pj_timer_heap_t *ht, pj_time_val *next_delay ) 464 PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht, 465 pj_time_val *next_delay ) 437 466 { 438 467 pj_time_val now; 439 intcount;440 441 PJ_ASSERT_RETURN(ht, -1);468 unsigned count; 469 470 PJ_ASSERT_RETURN(ht, 0); 442 471 443 472 if (!ht->cur_size && next_delay) { … … 451 480 lock_timer_heap(ht); 452 481 while ( ht->cur_size && 453 PJ_TIME_VAL_LTE(ht->heap[0]->_timer_value, now) ) 482 PJ_TIME_VAL_LTE(ht->heap[0]->_timer_value, now) && 483 count < ht->max_entries_per_poll ) 454 484 { 455 485 pj_timer_entry *node = remove_node(ht, 0); 456 486 ++count; 457 487 458 //Better not to temporarily release mutex to save some syscalls. 459 //But then make sure the mutex must be the recursive types (PJ_MUTEX_RECURSE)! 460 //unlock_timer_heap(ht); 488 unlock_timer_heap(ht); 461 489 (*node->cb)(ht, node); 462 //lock_timer_heap(ht);490 lock_timer_heap(ht); 463 491 } 464 492 if (ht->cur_size && next_delay) { … … 475 503 PJ_DEF(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht ) 476 504 { 505 PJ_ASSERT_RETURN(ht, 0); 506 477 507 return ht->cur_size; 478 508 }
Note: See TracChangeset
for help on using the changeset viewer.