timeGetTime uses huge resources on windows so revert to using timevals for its implementation of cgtimer_t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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);