Changeset 5


Ignore:
Timestamp:
Nov 1, 2005 9:46:17 PM (18 years ago)
Author:
bennylp
Message:

Changed atomic interface and fixed bugs in echo test client

Location:
pjproject/main/pjlib
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/include/pj/os.h

    r4 r5  
    305305 * @param atomic_var    the atomic variable. 
    306306 * @param value         value to be set to the variable. 
    307  * 
    308  * @return the previous value of the variable. 
    309  */ 
    310 PJ_DECL(pj_atomic_value_t) pj_atomic_set(pj_atomic_t *atomic_var,  
    311                                          pj_atomic_value_t value); 
     307 */ 
     308PJ_DECL(void) pj_atomic_set( pj_atomic_t *atomic_var,  
     309                             pj_atomic_value_t value); 
    312310 
    313311/** 
     
    324322 * 
    325323 * @param atomic_var    the atomic variable. 
    326  * 
    327  * @return the result. 
    328  */ 
    329 PJ_DECL(pj_atomic_value_t) pj_atomic_inc(pj_atomic_t *atomic_var); 
     324 */ 
     325PJ_DECL(void) pj_atomic_inc(pj_atomic_t *atomic_var); 
    330326 
    331327/** 
     
    333329 * 
    334330 * @param atomic_var    the atomic variable. 
    335  * 
    336  * @return the result. 
    337  */ 
    338 PJ_DECL(pj_atomic_value_t) pj_atomic_dec(pj_atomic_t *atomic_var); 
     331 */ 
     332PJ_DECL(void) pj_atomic_dec(pj_atomic_t *atomic_var); 
     333 
     334/** 
     335 * Add a value to an atomic type. 
     336 * 
     337 * @param atomic_var    The atomic variable. 
     338 * @param value         Value to be added. 
     339 */ 
     340PJ_DECL(void) pj_atomic_add( pj_atomic_t *atomic_var, 
     341                             pj_atomic_value_t value); 
    339342 
    340343/** 
  • pjproject/main/pjlib/src/pj/os_core_linux_kernel.c

    r4 r5  
    416416PJ_DEF(pj_status_t) pj_atomic_destroy( pj_atomic_t *var ) 
    417417{ 
    418     return 0; 
    419 } 
    420  
    421 PJ_DEF(pj_atomic_value_t) pj_atomic_set(pj_atomic_t *var,  
    422                                         pj_atomic_value_t value) 
    423 { 
    424     pj_atomic_value_t oldval = atomic_read(&var->atom); 
     418    return PJ_SUCCESS; 
     419} 
     420 
     421PJ_DEF(void) pj_atomic_set(pj_atomic_t *var, pj_atomic_value_t value) 
     422{ 
    425423    atomic_set(&var->atom, value); 
    426     return oldval; 
    427424} 
    428425 
     
    432429} 
    433430 
    434 PJ_DEF(pj_atomic_value_t) pj_atomic_inc(pj_atomic_t *var) 
     431PJ_DEF(void) pj_atomic_inc(pj_atomic_t *var) 
    435432{ 
    436433    atomic_inc(&var->atom); 
    437     return atomic_read(&var->atom); 
    438 } 
    439  
    440 PJ_DEF(pj_atomic_value_t) pj_atomic_dec(pj_atomic_t *var) 
     434} 
     435 
     436PJ_DEF(void) pj_atomic_dec(pj_atomic_t *var) 
    441437{ 
    442438    atomic_dec(&var->atom); 
    443     return atomic_read(&var->atom); 
    444 } 
    445  
     439} 
     440 
     441PJ_DEF(void) pj_atomic_add( pj_atomic_t *var, pj_atomic_value_t value ) 
     442{ 
     443    atomic_add(value, &var->atom); 
     444} 
    446445 
    447446 
  • pjproject/main/pjlib/src/pj/os_core_unix.c

    r4 r5  
    527527 * pj_atomic_set() 
    528528 */ 
    529 PJ_DEF(pj_atomic_value_t) pj_atomic_set(pj_atomic_t *atomic_var,  
    530                                         pj_atomic_value_t value) 
    531 { 
    532     pj_atomic_value_t oldval; 
    533      
    534     PJ_CHECK_STACK(); 
    535     PJ_ASSERT_RETURN(atomic_var, 0); 
     529PJ_DEF(void) pj_atomic_set(pj_atomic_t *atomic_var, pj_atomic_value_t value) 
     530{ 
     531    PJ_CHECK_STACK(); 
    536532 
    537533#if PJ_HAS_THREADS 
    538534    pj_mutex_lock( atomic_var->mutex ); 
    539535#endif 
    540     oldval = atomic_var->value; 
    541536    atomic_var->value = value; 
    542537#if PJ_HAS_THREADS 
    543538    pj_mutex_unlock( atomic_var->mutex); 
    544539#endif  
     540} 
     541 
     542/* 
     543 * pj_atomic_get() 
     544 */ 
     545PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var) 
     546{ 
     547    pj_atomic_value_t oldval; 
     548     
     549    PJ_CHECK_STACK(); 
     550 
     551#if PJ_HAS_THREADS 
     552    pj_mutex_lock( atomic_var->mutex ); 
     553#endif 
     554    oldval = atomic_var->value; 
     555#if PJ_HAS_THREADS 
     556    pj_mutex_unlock( atomic_var->mutex); 
     557#endif 
    545558    return oldval; 
    546559} 
    547560 
    548561/* 
    549  * pj_atomic_get() 
    550  */ 
    551 PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var) 
    552 { 
    553     pj_atomic_value_t oldval; 
    554      
    555     PJ_CHECK_STACK(); 
    556     PJ_ASSERT_RETURN(atomic_var, 0); 
     562 * pj_atomic_inc() 
     563 */ 
     564PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var) 
     565{ 
     566    PJ_CHECK_STACK(); 
    557567 
    558568#if PJ_HAS_THREADS 
    559569    pj_mutex_lock( atomic_var->mutex ); 
    560570#endif 
    561     oldval = atomic_var->value; 
     571    ++atomic_var->value; 
    562572#if PJ_HAS_THREADS 
    563573    pj_mutex_unlock( atomic_var->mutex); 
    564574#endif 
    565     return oldval; 
    566 } 
    567  
    568 /* 
    569  * pj_atomic_inc() 
    570  */ 
    571 PJ_DEF(pj_atomic_value_t) pj_atomic_inc(pj_atomic_t *atomic_var) 
    572 { 
    573     pj_atomic_value_t newval; 
    574  
    575     PJ_CHECK_STACK(); 
    576     PJ_ASSERT_RETURN(atomic_var, 0); 
     575} 
     576 
     577/* 
     578 * pj_atomic_dec() 
     579 */ 
     580PJ_DEF(void) pj_atomic_dec(pj_atomic_t *atomic_var) 
     581{ 
     582    PJ_CHECK_STACK(); 
    577583 
    578584#if PJ_HAS_THREADS 
    579585    pj_mutex_lock( atomic_var->mutex ); 
    580586#endif 
    581     newval = ++atomic_var->value; 
     587    --atomic_var->value; 
    582588#if PJ_HAS_THREADS 
    583589    pj_mutex_unlock( atomic_var->mutex); 
    584590#endif 
    585     return newval; 
    586 } 
    587  
    588 /* 
    589  * pj_atomic_dec() 
    590  */ 
    591 PJ_DEF(pj_atomic_value_t) pj_atomic_dec(pj_atomic_t *atomic_var) 
    592 { 
    593     pj_atomic_value_t newval; 
    594  
    595     PJ_CHECK_STACK(); 
    596     PJ_ASSERT_RETURN(atomic_var, 0); 
    597  
    598 #if PJ_HAS_THREADS 
    599     pj_mutex_lock( atomic_var->mutex ); 
    600 #endif 
    601     newval = --atomic_var->value; 
    602 #if PJ_HAS_THREADS 
    603     pj_mutex_unlock( atomic_var->mutex); 
    604 #endif 
    605     return newval; 
     591} 
     592 
     593/* 
     594 * pj_atomic_add() 
     595 */  
     596PJ_DEF(void) pj_atomic_add( pj_atomic_t *atomic_var, pj_atomic_value_t value ) 
     597{ 
     598#if PJ_HAS_THREADS 
     599    pj_mutex_lock(atomic_var->mutex); 
     600#endif 
     601     
     602    atomic_var->value += value; 
     603 
     604#if PJ_HAS_THREADS 
     605    pj_mutex_unlock(atomic_var->mutex); 
     606#endif 
    606607} 
    607608 
  • pjproject/main/pjlib/src/pj/os_core_win32.c

    r4 r5  
    496496 * pj_atomic_set() 
    497497 */ 
    498 PJ_DEF(long) pj_atomic_set(pj_atomic_t *atomic_var, long value) 
     498PJ_DEF(void) pj_atomic_set( pj_atomic_t *atomic_var, pj_atomic_value_t value) 
     499{ 
     500    PJ_CHECK_STACK(); 
     501 
     502    InterlockedExchange(&atomic_var->value, value); 
     503} 
     504 
     505/* 
     506 * pj_atomic_get() 
     507 */ 
     508PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var) 
    499509{ 
    500510    PJ_CHECK_STACK(); 
    501511    PJ_ASSERT_RETURN(atomic_var, 0); 
    502512 
    503     return InterlockedExchange(&atomic_var->value, value); 
    504 } 
    505  
    506 /* 
    507  * pj_atomic_get() 
    508  */ 
    509 PJ_DEF(long) pj_atomic_get(pj_atomic_t *atomic_var) 
    510 { 
    511     PJ_CHECK_STACK(); 
    512     PJ_ASSERT_RETURN(atomic_var, 0); 
    513  
    514513    return atomic_var->value; 
    515514} 
     
    518517 * pj_atomic_inc() 
    519518 */ 
    520 PJ_DEF(long) pj_atomic_inc(pj_atomic_t *atomic_var) 
    521 { 
    522     PJ_CHECK_STACK(); 
    523     PJ_ASSERT_RETURN(atomic_var, 0); 
     519PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var) 
     520{ 
     521    PJ_CHECK_STACK(); 
    524522 
    525523#if defined(PJ_WIN32_WINNT) && PJ_WIN32_WINNT >= 0x0400 
    526     return InterlockedIncrement(&atomic_var->value); 
     524    InterlockedIncrement(&atomic_var->value); 
    527525#else 
    528526#   error Fix Me 
     
    533531 * pj_atomic_dec() 
    534532 */ 
    535 PJ_DEF(long) pj_atomic_dec(pj_atomic_t *atomic_var) 
    536 { 
    537     PJ_CHECK_STACK(); 
    538     PJ_ASSERT_RETURN(atomic_var, 0); 
     533PJ_DEF(void) pj_atomic_dec(pj_atomic_t *atomic_var) 
     534{ 
     535    PJ_CHECK_STACK(); 
    539536 
    540537#if defined(PJ_WIN32_WINNT) && PJ_WIN32_WINNT >= 0x0400 
    541     return InterlockedDecrement(&atomic_var->value); 
     538    InterlockedDecrement(&atomic_var->value); 
    542539#else 
    543540#   error Fix me 
     
    545542} 
    546543 
    547  
     544/* 
     545 * pj_atomic_add() 
     546 */ 
     547PJ_DEF(void) pj_atomic_add( pj_atomic_t *atomic_var, 
     548                            pj_atomic_value_t value ) 
     549{ 
     550    InterlockedExchangeAdd( &atomic_var->value, value ); 
     551} 
     552 
     553         
    548554/////////////////////////////////////////////////////////////////////////////// 
    549555/* 
  • pjproject/main/pjlib/src/pjlib-test/echo_clt.c

    r4 r5  
    2929}; 
    3030 
    31 static pj_sem_t *sem; 
    32 static pj_mutex_t *mutex; 
    33 static pj_size_t total_bw; 
    34 static unsigned total_poster; 
    35 static pj_time_val first_report; 
     31static pj_atomic_t *totalBytes; 
    3632 
    3733#define MSEC_PRINT_DURATION 1000 
     
    6258    struct client *client = arg; 
    6359    pj_status_t last_recv_err = PJ_SUCCESS, last_send_err = PJ_SUCCESS; 
    64  
    65     pj_time_val last_report, next_report; 
    66     pj_size_t thread_total; 
     60    unsigned counter = 0; 
    6761 
    6862    rc = app_socket(PJ_AF_INET, client->sock_type, 0, -1, &sock); 
     
    9084                  pj_ntohs(addr.sin_port))); 
    9185 
    92     pj_create_random_string(send_buf, BUF_SIZE); 
    93     thread_total = 0; 
     86    pj_memset(send_buf, 'A', BUF_SIZE); 
     87    send_buf[BUF_SIZE-1]='\0'; 
    9488 
    9589    /* Give other thread chance to initialize themselves! */ 
    96     pj_thread_sleep(500); 
    97  
    98     pj_gettimeofday(&last_report); 
    99     next_report = first_report; 
     90    pj_thread_sleep(200); 
    10091 
    10192    //PJ_LOG(3,("", "...thread %p running", pj_thread_this())); 
     
    10495        int rc; 
    10596        pj_ssize_t bytes; 
    106         pj_time_val now; 
     97 
     98        ++counter; 
    10799 
    108100        /* Send a packet. */ 
     
    148140        } 
    149141 
    150         /* Accumulate total received. */ 
    151         thread_total = thread_total + bytes; 
    152  
    153         /* Report current bandwidth on due. */ 
    154         pj_gettimeofday(&now); 
    155  
    156         if (PJ_TIME_VAL_GTE(now, next_report)) { 
    157             pj_uint32_t bw; 
    158             pj_bool_t signal_parent = 0; 
    159             pj_time_val duration; 
    160             pj_uint32_t msec; 
    161  
    162             duration = now; 
    163             PJ_TIME_VAL_SUB(duration, last_report); 
    164             msec = PJ_TIME_VAL_MSEC(duration); 
    165  
    166             bw = thread_total * 1000 / msec; 
    167  
    168             /* Post result to parent */ 
    169             pj_mutex_lock(mutex); 
    170             total_bw += bw; 
    171             total_poster++; 
    172             //PJ_LOG(3,("", "...thread %p posting result", pj_thread_this())); 
    173             if (total_poster >= ECHO_CLIENT_MAX_THREADS) 
    174                 signal_parent = 1; 
    175             pj_mutex_unlock(mutex); 
    176  
    177             thread_total = 0; 
    178             last_report = now; 
    179             next_report.sec++; 
    180  
    181             if (signal_parent) { 
    182                 pj_sem_post(sem); 
    183             } 
    184  
    185             pj_thread_sleep(0); 
    186         } 
    187  
    188142        if (bytes == 0) 
    189143            continue; 
    190144 
    191145        if (pj_memcmp(send_buf, recv_buf, BUF_SIZE) != 0) { 
    192             //PJ_LOG(3,("", "...error: buffer has changed!")); 
    193             break; 
    194         } 
     146            recv_buf[BUF_SIZE-1] = '\0'; 
     147            PJ_LOG(3,("", "...error: buffer %u has changed!\n" 
     148                          "send_buf=%s\n" 
     149                          "recv_buf=%s\n",  
     150                          counter, send_buf, recv_buf)); 
     151            //break; 
     152        } 
     153 
     154        /* Accumulate total received. */ 
     155        pj_atomic_add(totalBytes, bytes); 
    195156    } 
    196157 
     
    206167    struct client client; 
    207168    int i; 
     169    pj_atomic_value_t last_received; 
     170    pj_timestamp last_report; 
    208171 
    209172    client.sock_type = sock_type; 
     
    213176    pool = pj_pool_create( mem, NULL, 4000, 4000, NULL ); 
    214177 
    215     rc = pj_sem_create(pool, NULL, 0, ECHO_CLIENT_MAX_THREADS+1, &sem); 
    216     if (rc != PJ_SUCCESS) { 
    217         PJ_LOG(3,("", "...error: unable to create semaphore", rc)); 
    218         return -10; 
    219     } 
    220  
    221     rc = pj_mutex_create_simple(pool, NULL, &mutex); 
    222     if (rc != PJ_SUCCESS) { 
    223         PJ_LOG(3,("", "...error: unable to create mutex", rc)); 
    224         return -20; 
    225     } 
    226  
    227     /* 
    228     rc = pj_atomic_create(pool, 0, &atom); 
     178    rc = pj_atomic_create(pool, 0, &totalBytes); 
    229179    if (rc != PJ_SUCCESS) { 
    230180        PJ_LOG(3,("", "...error: unable to create atomic variable", rc)); 
    231181        return -30; 
    232182    } 
    233     */ 
    234183 
    235184    PJ_LOG(3,("", "Echo client started")); 
     
    237186                  ECHO_SERVER_ADDRESS, ECHO_SERVER_START_PORT)); 
    238187    PJ_LOG(3,("", "  Press Ctrl-C to exit")); 
    239  
    240     pj_gettimeofday(&first_report); 
    241     first_report.sec += 2; 
    242188 
    243189    for (i=0; i<ECHO_CLIENT_MAX_THREADS; ++i) { 
     
    251197    } 
    252198 
     199    last_received = 0; 
     200    pj_get_timestamp(&last_report); 
     201 
    253202    for (;;) { 
    254         pj_uint32_t bw; 
    255  
    256         pj_sem_wait(sem); 
    257  
    258         pj_mutex_lock(mutex); 
    259         bw = total_bw; 
    260         total_bw = 0; 
    261         total_poster = 0; 
    262         pj_mutex_unlock(mutex); 
     203        pj_timestamp now; 
     204        unsigned long received, cur_received; 
     205        unsigned msec; 
     206        pj_highprec_t bw; 
     207        pj_time_val elapsed; 
     208        unsigned bw32; 
     209 
     210        pj_thread_sleep(1000); 
     211 
     212        pj_get_timestamp(&now); 
     213        elapsed = pj_elapsed_time(&last_report, &now); 
     214        msec = PJ_TIME_VAL_MSEC(elapsed); 
     215 
     216        received = pj_atomic_get(totalBytes); 
     217        cur_received = received - last_received; 
     218         
     219        bw = cur_received; 
     220        pj_highprec_mul(bw, 1000); 
     221        pj_highprec_div(bw, msec); 
     222 
     223        bw32 = (unsigned)bw; 
     224         
     225        last_report = now; 
     226        last_received = received; 
    263227 
    264228        PJ_LOG(3,("", "...%d threads, total bandwidth: %d KB/s",  
    265                   ECHO_CLIENT_MAX_THREADS, bw/1000)); 
     229                  ECHO_CLIENT_MAX_THREADS, bw32/1000)); 
    266230    } 
    267231 
  • pjproject/main/pjlib/src/pjlib-test/test.h

    r4 r5  
    4545#define ECHO_SERVER_DURATION_MSEC   (60*60*1000) 
    4646 
    47 #define ECHO_CLIENT_MAX_THREADS     2 
     47#define ECHO_CLIENT_MAX_THREADS     6 
    4848 
    4949PJ_BEGIN_DECL 
Note: See TracChangeset for help on using the changeset viewer.