Commit 7d448cd7549c7cad18a664cae897a83803bb29fe

Con Kolivas 2013-08-19T23:39:49

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

diff --git a/util.c b/util.c
index 0f64e03..ffba6fa 100644
--- a/util.c
+++ b/util.c
@@ -812,48 +812,17 @@ void cgtime(struct timeval *tv)
 {
 	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
-static void dtime_to_timeval(struct timeval *tv, DWORD dtime)
+static void __cgtime(struct timeval *tv)
 {
-	ldiv_t tvdiv = ldiv(dtime, 1000);
-
-	tv->tv_sec = tvdiv.quot;
-	tv->tv_usec = tvdiv.rem * 1000;
+	gettimeofday(tv, NULL);
 }
 
 void cgtime(struct timeval *tv)
 {
-	DWORD dtime;
-
-	//timeBeginPeriod(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);
+	timeBeginPeriod(1);
+	__cgtime(tv);
+	timeEndPeriod(1);
 }
 #endif
 
@@ -967,31 +936,32 @@ void cgsleep_us_r(cgtimer_t *ts_start, int64_t us)
 }
 
 #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);
-
-	ts->tv_sec = tsdiv.quot;
-	ts->tv_nsec = tsdiv.rem * 1000000;
+	timeBeginPeriod(1);
+	__cgtime(ts_start);
 }
 
-void cgsleep_prepare_r(cgtimer_t *ts_start)
+static void ms_to_timeval(struct timeval *val, int ms)
 {
-	timeBeginPeriod(1);
-	*ts_start = timeGetTime();
+	ldiv_t tvdiv = ldiv(ms, 1000);
+
+	val->tv_sec = tvdiv.quot;
+	val->tv_usec = tvdiv.rem * 1000;
 }
 
 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;
 
-	dend = *ts_start + ms;
-	dnow = timeGetTime();
-	if (unlikely(dnow >= dend))
+	ms_to_timeval(&tv_diff, ms);
+	timeradd(ts_start, &tv_diff, &tv_end);
+	__cgtime(&now);
+	if (unlikely(time_more(&now, &tv_end)))
 		goto out;
-	ddiff = dend - dnow;
-	dtime_to_timespec(&ts_diff, ddiff);
+	timersub(&tv_end, &now, &tv_diff);
+	timeval_to_spec(&ts_diff, &tv_diff);
 	nanosleep(&ts_diff, NULL);
 out:
 	timeEndPeriod(1);
diff --git a/util.h b/util.h
index fefccb2..59b91a7 100644
--- a/util.h
+++ b/util.h
@@ -45,7 +45,7 @@
 	#ifndef in_addr_t
 	#define in_addr_t uint32_t
 	#endif
-	typedef DWORD cgtimer_t;
+	typedef struct timeval cgtimer_t;
 #endif
 
 #if JANSSON_MAJOR_VERSION >= 2
@@ -75,8 +75,6 @@ void thr_info_cancel(struct thr_info *thr);
 void nmsleep(unsigned int msecs);
 void nusleep(unsigned int usecs);
 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 addtime(struct timeval *a, struct timeval *b);
 bool time_more(struct timeval *a, struct timeval *b);