diff options
author | Liong Sauw Ming <ming@teluu.com> | 2011-03-16 09:22:24 +0000 |
---|---|---|
committer | Liong Sauw Ming <ming@teluu.com> | 2011-03-16 09:22:24 +0000 |
commit | 623fdccd9c92fc2a00516e5cd6ed2d5edc2272a1 (patch) | |
tree | bf30cf0db67ac5c9aa3f431efdaa7d1938656a31 /pjlib/src/pj/os_timestamp_posix.c | |
parent | 2a9988f3e4c9b8d40ffebaa4427908de29324711 (diff) |
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().
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3456 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib/src/pj/os_timestamp_posix.c')
-rw-r--r-- | pjlib/src/pj/os_timestamp_posix.c | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/pjlib/src/pj/os_timestamp_posix.c b/pjlib/src/pj/os_timestamp_posix.c index 258241a0..353177b1 100644 --- a/pjlib/src/pj/os_timestamp_posix.c +++ b/pjlib/src/pj/os_timestamp_posix.c @@ -24,6 +24,16 @@ #include <stdlib.h> #include <ctype.h> +#if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0 +# include <unistd.h> + +# if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && \ + defined(_POSIX_MONOTONIC_CLOCK) +# define USE_POSIX_TIMERS 1 +# endif + +#endif + #if defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \ defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \ defined(PJ_M_I386) && PJ_M_I386!=0 && \ @@ -110,6 +120,73 @@ PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) return 0; } +#elif defined(PJ_DARWINOS) && PJ_DARWINOS != 0 +#include <mach/mach.h> +#include <mach/clock.h> +#include <errno.h> + +#define NSEC_PER_SEC 1000000000 + +PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) +{ + mach_timespec_t tp; + int ret; + clock_serv_t serv; + + ret = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &serv); + if (ret != KERN_SUCCESS) { + return PJ_RETURN_OS_ERROR(EINVAL); + } + + ret = clock_get_time(serv, &tp); + if (ret != KERN_SUCCESS) { + return PJ_RETURN_OS_ERROR(EINVAL); + } + + ts->u64 = tp.tv_sec; + ts->u64 *= NSEC_PER_SEC; + ts->u64 += tp.tv_nsec; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) +{ + freq->u32.hi = 0; + freq->u32.lo = NSEC_PER_SEC; + + return PJ_SUCCESS; +} + +#elif defined(USE_POSIX_TIMERS) && USE_POSIX_TIMERS != 0 +#include <sys/time.h> +#include <errno.h> + +#define NSEC_PER_SEC 1000000000 + +PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) +{ + struct timespec tp; + + if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) { + return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); + } + + ts->u64 = tp.tv_sec; + ts->u64 *= NSEC_PER_SEC; + ts->u64 += tp.tv_nsec; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) +{ + freq->u32.hi = 0; + freq->u32.lo = NSEC_PER_SEC; + + return PJ_SUCCESS; +} + #else #include <sys/time.h> #include <errno.h> @@ -140,4 +217,3 @@ PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) } #endif - |