Ignore:
Timestamp:
Nov 9, 2005 3:37:19 PM (18 years ago)
Author:
bennylp
Message:

Rework pjlib++

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/src/pj/timer.c

    r6 r36  
    1515#include <pj/assert.h> 
    1616#include <pj/errno.h> 
     17#include <pj/lock.h> 
    1718 
    1819#define HEAP_PARENT(X)  (X == 0 ? 0 : (((X) - 1) / 2)) 
    1920#define HEAP_LEFT(X)    (((X)+(X))+1) 
     21 
     22 
     23#define DEFAULT_MAX_TIMED_OUT_PER_POLL  (64) 
    2024 
    2125 
     
    3438    pj_size_t cur_size; 
    3539 
    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; 
    3848 
    3949    /** 
     
    7282PJ_INLINE(void) lock_timer_heap( pj_timer_heap_t *ht ) 
    7383{ 
    74     if (ht->mutex) { 
    75         pj_mutex_lock(ht->mutex); 
     84    if (ht->lock) { 
     85        pj_lock_acquire(ht->lock); 
    7686    } 
    7787} 
     
    7989PJ_INLINE(void) unlock_timer_heap( pj_timer_heap_t *ht ) 
    8090{ 
    81     if (ht->mutex) { 
    82         pj_mutex_unlock(ht->mutex); 
     91    if (ht->lock) { 
     92        pj_lock_release(ht->lock); 
    8393    } 
    8494} 
     
    320330           /* size of each entry: */ 
    321331           (count+2) * (sizeof(pj_timer_entry*)+sizeof(pj_timer_id_t)) + 
    322            /* mutex, pool etc: */ 
     332           /* lock, pool etc: */ 
    323333           132; 
    324334} 
     
    329339PJ_DEF(pj_status_t) pj_timer_heap_create( pj_pool_t *pool, 
    330340                                          pj_size_t size, 
    331                                           unsigned flag, 
    332341                                          pj_timer_heap_t **p_heap) 
    333342{ 
     
    350359    ht->max_size = size; 
    351360    ht->cur_size = 0; 
     361    ht->max_entries_per_poll = DEFAULT_MAX_TIMED_OUT_PER_POLL; 
    352362    ht->timer_ids_freelist = 1; 
    353363    ht->pool = pool; 
    354364 
    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; 
    368368 
    369369    // Create the heap array. 
     
    387387} 
    388388 
     389PJ_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 
     397PJ_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 
     409PJ_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 
    389417PJ_DEF(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry, 
    390418                                             int id, 
     
    434462} 
    435463 
    436 PJ_DEF(int) pj_timer_heap_poll( pj_timer_heap_t *ht, pj_time_val *next_delay ) 
     464PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht,  
     465                                     pj_time_val *next_delay ) 
    437466{ 
    438467    pj_time_val now; 
    439     int count; 
    440  
    441     PJ_ASSERT_RETURN(ht, -1); 
     468    unsigned count; 
     469 
     470    PJ_ASSERT_RETURN(ht, 0); 
    442471 
    443472    if (!ht->cur_size && next_delay) { 
     
    451480    lock_timer_heap(ht); 
    452481    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 )  
    454484    { 
    455485        pj_timer_entry *node = remove_node(ht, 0); 
    456486        ++count; 
    457487 
    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); 
    461489        (*node->cb)(ht, node); 
    462         //lock_timer_heap(ht); 
     490        lock_timer_heap(ht); 
    463491    } 
    464492    if (ht->cur_size && next_delay) { 
     
    475503PJ_DEF(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht ) 
    476504{ 
     505    PJ_ASSERT_RETURN(ht, 0); 
     506 
    477507    return ht->cur_size; 
    478508} 
Note: See TracChangeset for help on using the changeset viewer.