chiark / gitweb /
configure.ac, rand/noise.c: Get high-res time from `clock_gettime'.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 4 Jun 2016 15:13:19 +0000 (16:13 +0100)
If it's available.

configure.ac
rand/noise.c

index c9107c5b19fa596caead329f279d70a52a46a887..3c8cb37529f8f64dc2de16bc418760fa5804c48a 100644 (file)
@@ -224,6 +224,13 @@ AC_SUBST([limits])
 dnl Functions used for noise-gathering.
 AC_CHECK_FUNCS([setgroups])
 AC_CHECK_HEADERS([linux/random.h])
+mdw_ORIG_LIBS=$LIBS LIBS=$CATACOMB_LIBS
+AC_SEARCH_LIBS([clock_gettime], [rt])
+CATACOMB_LIBS=$LIBS LIBS=$mdw_ORIG_LIBS
+if test $ac_cv_search_clock_gettime != no; then
+  AC_DEFINE([HAVE_CLOCK_GETTIME], [1],
+           [Define if you have the \`clock_gettime' function.])
+fi
 AC_CHECK_FUNCS([getentropy])
 AC_CACHE_CHECK([whether the freewheel noise generator will work],
        [catacomb_cv_freewheel],
index f01af65187d639e6c3cf5b71b0429b100e022f68..de120d8b164823fa3b84ab17719d07f873f72e05 100644 (file)
@@ -35,6 +35,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include <sys/types.h>
 #include <sys/time.h>
 
 #define NOISE_KIDLIFE 100000           /* @noise_filter@ child lifetime */
 
+#if HAVE_CLOCK_GETTIME && _POSIX_TIMERS > 0
+#  define TIMESTRUCT timespec
+#  define tv_SEC tv_sec
+#  define tv_FRAC tv_nsec
+#  define TIMERES 1000000000
+#  if _POSIX_MONOTONIC_CLOCK > 0
+#    define GETTIME(tv) (clock_gettime(CLOCK_MONOTONIC, (tv)))
+#  else
+#    define GETTIME(tv) (clock_gettime(CLOCK_REALTIME, (tv)))
+#  endif
+#  define TOTIMEVAL(tv, xx)                                            \
+       ((tv)->tv_sec = (xx)->tv_sec,                                   \
+        (tv)->tv_usec = ((xx)->tv_nsec + 500)/1000)
+#else
 #  define TIMESTRUCT timeval
 #  define tv_SEC tv_sec
 #  define tv_FRAC tv_usec
 #  define TIMERES 1000000
 #  define GETTIME(tv) (gettimeofday((tv), 0))
 #  define TOTIMEVAL(tv, xx) (*(tv) = *(xx))
+#endif
 
 /*----- Noise source definition -------------------------------------------*/