Ignore:
Timestamp:
Mar 16, 2011 9:22:24 AM (13 years ago)
Author:
ming
Message:

Fixed #1211: Add pjlib API pj_gettickcount() that returns a monotonically increasing timestamp

  • Changed the timer_heap to use pj_gettickcount().
  • Changed ioqueue to use pj_gettickcount().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/os_timestamp_posix.c

    r2394 r3456  
    2525#include <ctype.h> 
    2626 
     27#if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0 
     28#   include <unistd.h> 
     29 
     30#   if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && \ 
     31       defined(_POSIX_MONOTONIC_CLOCK) 
     32#       define USE_POSIX_TIMERS 1 
     33#   endif 
     34 
     35#endif 
     36 
    2737#if defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \ 
    2838    defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \ 
     
    111121} 
    112122 
     123#elif defined(PJ_DARWINOS) && PJ_DARWINOS != 0 
     124#include <mach/mach.h> 
     125#include <mach/clock.h> 
     126#include <errno.h> 
     127 
     128#define NSEC_PER_SEC    1000000000 
     129 
     130PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) 
     131{ 
     132    mach_timespec_t tp; 
     133    int ret; 
     134    clock_serv_t serv; 
     135 
     136    ret = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &serv); 
     137    if (ret != KERN_SUCCESS) { 
     138        return PJ_RETURN_OS_ERROR(EINVAL); 
     139    } 
     140 
     141    ret = clock_get_time(serv, &tp); 
     142    if (ret != KERN_SUCCESS) { 
     143        return PJ_RETURN_OS_ERROR(EINVAL); 
     144    } 
     145 
     146    ts->u64 = tp.tv_sec; 
     147    ts->u64 *= NSEC_PER_SEC; 
     148    ts->u64 += tp.tv_nsec; 
     149 
     150    return PJ_SUCCESS; 
     151} 
     152 
     153PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) 
     154{ 
     155    freq->u32.hi = 0; 
     156    freq->u32.lo = NSEC_PER_SEC; 
     157 
     158    return PJ_SUCCESS; 
     159} 
     160 
     161#elif defined(USE_POSIX_TIMERS) && USE_POSIX_TIMERS != 0 
     162#include <sys/time.h> 
     163#include <errno.h> 
     164 
     165#define NSEC_PER_SEC    1000000000 
     166 
     167PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) 
     168{ 
     169    struct timespec tp; 
     170 
     171    if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) { 
     172        return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 
     173    } 
     174 
     175    ts->u64 = tp.tv_sec; 
     176    ts->u64 *= NSEC_PER_SEC; 
     177    ts->u64 += tp.tv_nsec; 
     178 
     179    return PJ_SUCCESS; 
     180} 
     181 
     182PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) 
     183{ 
     184    freq->u32.hi = 0; 
     185    freq->u32.lo = NSEC_PER_SEC; 
     186 
     187    return PJ_SUCCESS; 
     188} 
     189 
    113190#else 
    114191#include <sys/time.h> 
     
    141218 
    142219#endif 
    143  
Note: See TracChangeset for help on using the changeset viewer.