diff --git a/docs/changes.txt b/docs/changes.txt index 2242d8cbb..117c14565 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -42,6 +42,7 @@ - Self Test: Skip self-test for mode 8900 user-configurable scrypt settings are incompatible to fixed settings in the self-test hash - Self Test: Skip self-test for mode 15700 because settings are too high and create a too long startup time - Terminal: Send clear line code to the same output stream as the next message following +- Timer: Switch from gettimeofday() to clock_gettime() to workaround problems on cygwin * changes v4.0.0 -> v4.0.1: diff --git a/include/types.h b/include/types.h index 5cc47137a..b13a3d4cc 100644 --- a/include/types.h +++ b/include/types.h @@ -56,7 +56,7 @@ typedef time_t hc_time_t; #if defined (_WIN) typedef LARGE_INTEGER hc_timer_t; #else -typedef struct timeval hc_timer_t; +typedef struct timespec hc_timer_t; #endif // thread diff --git a/src/timer.c b/src/timer.c index c3511d079..01aa86e10 100644 --- a/src/timer.c +++ b/src/timer.c @@ -24,14 +24,16 @@ inline double hc_timer_get (hc_timer_t a) hc_timer_set (&hr_tmp); - return (double) ((double) (hr_tmp.QuadPart - a.QuadPart) / (double) (hr_freq.QuadPart / 1000)); + double r = ((double) hr_tmp.QuadPart - (double) a.QuadPart) / ((double) hr_freq.QuadPart / 1000); + + return r; } #else inline void hc_timer_set (hc_timer_t* a) { - gettimeofday (a, NULL); + clock_gettime (CLOCK_MONOTONIC, a); } inline double hc_timer_get (hc_timer_t a) @@ -40,7 +42,20 @@ inline double hc_timer_get (hc_timer_t a) hc_timer_set (&hr_tmp); - return (double) (((hr_tmp.tv_sec - (a).tv_sec) * 1000) + ((double) (hr_tmp.tv_usec - (a).tv_usec) / 1000)); + hc_timer_t s; + + s.tv_sec = hr_tmp.tv_sec - a.tv_sec; + s.tv_nsec = hr_tmp.tv_nsec - a.tv_nsec; + + if (s.tv_nsec < 0) + { + s.tv_sec -= 1; + s.tv_nsec += 1000000000; + } + + double r = ((double) s.tv_sec * 1000) + ((double) s.tv_nsec / 1000000); + + return r; } #endif