+ { CLOCK_REALTIME, NO_SYSCONF_CHECK },
+ };
+ int i;
+
+ /* Determine the clock we can use. For a clock to be usable, it
+ must be confirmed with sysconf (where applicable) and with
+ clock_getres. If no clock is found, CLOCK_REALTIME is used. */
+
+ for (i = 0; i < countof (clocks); i++)
+ {
+ struct timespec r;
+ if (clocks[i].sysconf_name != NO_SYSCONF_CHECK)
+ if (sysconf (clocks[i].sysconf_name) < 0)
+ continue; /* sysconf claims this clock is unavailable */
+ if (clock_getres (clocks[i].id, &r) < 0)
+ continue; /* clock_getres doesn't work for this clock */
+ posix_clock_id = clocks[i].id;
+ posix_millisec_resolution = r.tv_sec * 1000.0 + r.tv_nsec / 1000000.0;
+ /* Guard against broken clock_getres returning nonsensical
+ values. */
+ if (posix_millisec_resolution == 0)
+ posix_millisec_resolution = 1;
+ break;
+ }
+ if (i == countof (clocks))
+ {
+ /* If no clock was found, it means that clock_getres failed for
+ the realtime clock. */
+ logprintf (LOG_NOTQUIET, _("Cannot get REALTIME clock frequency: %s\n"),
+ strerror (errno));
+ /* Use CLOCK_REALTIME, but invent a plausible resolution. */
+ posix_clock_id = CLOCK_REALTIME;
+ posix_millisec_resolution = 1;
+ }
+}
+
+static inline void
+posix_measure (ptimer_system_time *pst)
+{
+ clock_gettime (posix_clock_id, pst);
+}
+
+static inline double
+posix_diff (ptimer_system_time *pst1, ptimer_system_time *pst2)
+{
+ return ((pst1->tv_sec - pst2->tv_sec) * 1000.0
+ + (pst1->tv_nsec - pst2->tv_nsec) / 1000000.0);
+}
+
+static inline double
+posix_resolution (void)
+{
+ return posix_millisec_resolution;
+}
+#endif /* PTIMER_POSIX */