Add timer support for Windows.

This commit is contained in:
Jason Evans 2015-07-13 14:35:15 -07:00
parent 92d72eeef0
commit 8693a9ea05
2 changed files with 24 additions and 10 deletions

View File

@ -7,9 +7,12 @@
&& _POSIX_MONOTONIC_CLOCK >= 0
typedef struct {
#if JEMALLOC_CLOCK_GETTIME
struct timespec tv0;
struct timespec tv1;
#ifdef _WIN32
FILETIME ft0;
FILETIME ft1;
#elif JEMALLOC_CLOCK_GETTIME
struct timespec ts0;
struct timespec ts1;
int clock_id;
#else
struct timeval tv0;

View File

@ -4,12 +4,14 @@ void
timer_start(timedelta_t *timer)
{
#if JEMALLOC_CLOCK_GETTIME
#ifdef _WIN32
GetSystemTimeAsFileTime(&timer->ft0);
#elif JEMALLOC_CLOCK_GETTIME
if (sysconf(_SC_MONOTONIC_CLOCK) <= 0)
timer->clock_id = CLOCK_REALTIME;
else
timer->clock_id = CLOCK_MONOTONIC;
clock_gettime(timer->clock_id, &timer->tv0);
clock_gettime(timer->clock_id, &timer->ts0);
#else
gettimeofday(&timer->tv0, NULL);
#endif
@ -19,8 +21,10 @@ void
timer_stop(timedelta_t *timer)
{
#if JEMALLOC_CLOCK_GETTIME
clock_gettime(timer->clock_id, &timer->tv1);
#ifdef _WIN32
GetSystemTimeAsFileTime(&timer->ft0);
#elif JEMALLOC_CLOCK_GETTIME
clock_gettime(timer->clock_id, &timer->ts1);
#else
gettimeofday(&timer->tv1, NULL);
#endif
@ -30,9 +34,16 @@ uint64_t
timer_usec(const timedelta_t *timer)
{
#if JEMALLOC_CLOCK_GETTIME
return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
(timer->tv1.tv_nsec - timer->tv0.tv_nsec) / 1000);
#ifdef _WIN32
uint64_t t0, t1;
t0 = (((uint64_t)timer->ft0.dwHighDateTime) << 32) |
timer->ft0.dwLowDateTime;
t1 = (((uint64_t)timer->ft1.dwHighDateTime) << 32) |
timer->ft1.dwLowDateTime;
return ((t1 - t0) / 10);
#elif JEMALLOC_CLOCK_GETTIME
return (((timer->ts1.tv_sec - timer->ts0.tv_sec) * 1000000) +
(timer->ts1.tv_nsec - timer->ts0.tv_nsec) / 1000);
#else
return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
timer->tv1.tv_usec - timer->tv0.tv_usec);