+ { CLOCK_REALTIME, NO_SYSCONF_CHECK },
+ };
+ size_t 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_clock_resolution = (double) r.tv_sec + r.tv_nsec / 1e9;
+ /* Guard against nonsense returned by a broken clock_getres. */
+ if (posix_clock_resolution == 0)
+ posix_clock_resolution = 1e-3;
+ 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_clock_resolution = 1e-3;
+ }
+}
+
+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)
+ + (pst1->tv_nsec - pst2->tv_nsec) / 1e9);
+}
+
+static inline double
+posix_resolution (void)
+{
+ return posix_clock_resolution;
+}
+#endif /* PTIMER_POSIX */