Browse Source

timeGetTime uses huge resources on windows so revert to using timevals for its implementation of cgtimer_t

nfactor-troky
Con Kolivas 12 years ago
parent
commit
7d448cd754
  1. 70
      util.c
  2. 4
      util.h

70
util.c

@ -812,48 +812,17 @@ void cgtime(struct timeval *tv)
{ {
gettimeofday(tv, NULL); gettimeofday(tv, NULL);
} }
void timeval_to_cgtimer(cgtimer_t *cgt, const struct timeval *tv)
{
timeval_to_spec(cgt, tv);
}
void cgtimer_to_timeval(struct timeval *tv, const cgtimer_t *cgt)
{
timespec_to_val(tv, cgt);
}
#else #else
static void dtime_to_timeval(struct timeval *tv, DWORD dtime) static void __cgtime(struct timeval *tv)
{ {
ldiv_t tvdiv = ldiv(dtime, 1000); gettimeofday(tv, NULL);
tv->tv_sec = tvdiv.quot;
tv->tv_usec = tvdiv.rem * 1000;
} }
void cgtime(struct timeval *tv) void cgtime(struct timeval *tv)
{ {
DWORD dtime; timeBeginPeriod(1);
__cgtime(tv);
//timeBeginPeriod(1); timeEndPeriod(1);
dtime = timeGetTime();
//timeEndPeriod(1);
dtime_to_timeval(tv, dtime);
}
static void timeval_to_dtime(DWORD *dtime, const struct timeval *tv)
{
*dtime = tv->tv_sec * 1000 + tv->tv_usec / 1000;
}
void timeval_to_cgtimer(cgtimer_t *cgt, const struct timeval *tv)
{
timeval_to_dtime(cgt, tv);
}
void cgtimer_to_timeval(struct timeval *tv, const cgtimer_t *cgt)
{
dtime_to_timeval(tv, *cgt);
} }
#endif #endif
@ -967,31 +936,32 @@ void cgsleep_us_r(cgtimer_t *ts_start, int64_t us)
} }
#else #else
static void dtime_to_timespec(struct timespec *ts, DWORD dtime) void cgsleep_prepare_r(cgtimer_t *ts_start)
{ {
ldiv_t tsdiv = ldiv(dtime, 1000); timeBeginPeriod(1);
__cgtime(ts_start);
ts->tv_sec = tsdiv.quot;
ts->tv_nsec = tsdiv.rem * 1000000;
} }
void cgsleep_prepare_r(cgtimer_t *ts_start) static void ms_to_timeval(struct timeval *val, int ms)
{ {
timeBeginPeriod(1); ldiv_t tvdiv = ldiv(ms, 1000);
*ts_start = timeGetTime();
val->tv_sec = tvdiv.quot;
val->tv_usec = tvdiv.rem * 1000;
} }
void cgsleep_ms_r(cgtimer_t *ts_start, int ms) void cgsleep_ms_r(cgtimer_t *ts_start, int ms)
{ {
DWORD dnow, dend, ddiff; struct timeval now, tv_end, tv_diff;
struct timespec ts_diff; struct timespec ts_diff;
dend = *ts_start + ms; ms_to_timeval(&tv_diff, ms);
dnow = timeGetTime(); timeradd(ts_start, &tv_diff, &tv_end);
if (unlikely(dnow >= dend)) __cgtime(&now);
if (unlikely(time_more(&now, &tv_end)))
goto out; goto out;
ddiff = dend - dnow; timersub(&tv_end, &now, &tv_diff);
dtime_to_timespec(&ts_diff, ddiff); timeval_to_spec(&ts_diff, &tv_diff);
nanosleep(&ts_diff, NULL); nanosleep(&ts_diff, NULL);
out: out:
timeEndPeriod(1); timeEndPeriod(1);

4
util.h

@ -45,7 +45,7 @@
#ifndef in_addr_t #ifndef in_addr_t
#define in_addr_t uint32_t #define in_addr_t uint32_t
#endif #endif
typedef DWORD cgtimer_t; typedef struct timeval cgtimer_t;
#endif #endif
#if JANSSON_MAJOR_VERSION >= 2 #if JANSSON_MAJOR_VERSION >= 2
@ -75,8 +75,6 @@ void thr_info_cancel(struct thr_info *thr);
void nmsleep(unsigned int msecs); void nmsleep(unsigned int msecs);
void nusleep(unsigned int usecs); void nusleep(unsigned int usecs);
void cgtime(struct timeval *tv); void cgtime(struct timeval *tv);
void timeval_to_cgtimer(cgtimer_t *cgt, const struct timeval *tv);
void cgtimer_to_timeval(struct timeval *tv, const cgtimer_t *cgt);
void subtime(struct timeval *a, struct timeval *b); void subtime(struct timeval *a, struct timeval *b);
void addtime(struct timeval *a, struct timeval *b); void addtime(struct timeval *a, struct timeval *b);
bool time_more(struct timeval *a, struct timeval *b); bool time_more(struct timeval *a, struct timeval *b);

Loading…
Cancel
Save