Browse Source

Set high resolution timing on windows within the cgsleep functions.

nfactor-troky
Con Kolivas 11 years ago
parent
commit
8114473228
  1. 33
      util.c

33
util.c

@ -890,12 +890,18 @@ void cgsleep_ms(int ms)
struct timespec ts_start, ts_end; struct timespec ts_start, ts_end;
int ret; int ret;
#ifdef WIN32
timeBeginPeriod(1);
#endif
clock_gettime(CLOCK_MONOTONIC, &ts_start); clock_gettime(CLOCK_MONOTONIC, &ts_start);
ms_to_timespec(&ts_end, ms); ms_to_timespec(&ts_end, ms);
timeraddspec(&ts_end, &ts_start); timeraddspec(&ts_end, &ts_start);
do { do {
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL); ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL);
} while (ret == EINTR); } while (ret == EINTR);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
void cgsleep_us(int64_t us) void cgsleep_us(int64_t us)
@ -903,12 +909,18 @@ void cgsleep_us(int64_t us)
struct timespec ts_start, ts_end; struct timespec ts_start, ts_end;
int ret; int ret;
#ifdef WIN32
timeBeginPeriod(1);
#endif
clock_gettime(CLOCK_MONOTONIC, &ts_start); clock_gettime(CLOCK_MONOTONIC, &ts_start);
us_to_timespec(&ts_end, us); us_to_timespec(&ts_end, us);
timeraddspec(&ts_end, &ts_start); timeraddspec(&ts_end, &ts_start);
do { do {
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL); ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL);
} while (ret == EINTR); } while (ret == EINTR);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
/* Reentrant version of cgsleep functions allow start time to be set separately /* Reentrant version of cgsleep functions allow start time to be set separately
@ -916,6 +928,9 @@ void cgsleep_us(int64_t us)
* counted in the sleep. */ * counted in the sleep. */
void cgsleep_prepare_r(struct timespec *ts_start) void cgsleep_prepare_r(struct timespec *ts_start)
{ {
#ifdef WIN32
timeBeginPeriod(1);
#endif
clock_gettime(CLOCK_MONOTONIC, ts_start); clock_gettime(CLOCK_MONOTONIC, ts_start);
} }
@ -929,6 +944,9 @@ void cgsleep_ms_r(struct timespec *ts_start, int ms)
do { do {
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL); ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL);
} while (ret == EINTR); } while (ret == EINTR);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
void cgsleep_us_r(struct timespec *ts_start, int us) void cgsleep_us_r(struct timespec *ts_start, int us)
@ -941,31 +959,22 @@ void cgsleep_us_r(struct timespec *ts_start, int us)
do { do {
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL); ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL);
} while (ret == EINTR); } while (ret == EINTR);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
/* Provide a ms based sleep that uses nanosleep to avoid poor usleep accuracy /* Provide a ms based sleep that uses nanosleep to avoid poor usleep accuracy
* on SMP machines */ * on SMP machines */
void nmsleep(unsigned int msecs) void nmsleep(unsigned int msecs)
{ {
#ifdef WIN32
timeBeginPeriod(1);
#endif
cgsleep_ms((int)msecs); cgsleep_ms((int)msecs);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
/* Same for usecs */ /* Same for usecs */
void nusleep(unsigned int usecs) void nusleep(unsigned int usecs)
{ {
#ifdef WIN32
timeBeginPeriod(1);
#endif
cgsleep_us((int64_t)usecs); cgsleep_us((int64_t)usecs);
#ifdef WIN32
timeEndPeriod(1);
#endif
} }
/* Returns the microseconds difference between end and start times as a double */ /* Returns the microseconds difference between end and start times as a double */

Loading…
Cancel
Save