git » libjio » commit 7b241f1

Support platforms without clock_gettime()

author Alberto Bertogli
2009-06-26 05:15:02 UTC
committer Alberto Bertogli
2009-06-26 06:00:47 UTC
parent 3fcd72c2eed7af5bdae8d7f99f4382a25ad42077

Support platforms without clock_gettime()

Reported by Jonathan Yu.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

libjio/compat.c +31 -2
libjio/compat.h +10 -0

diff --git a/libjio/compat.c b/libjio/compat.c
index 7ea1b2f..394ff39 100644
--- a/libjio/compat.c
+++ b/libjio/compat.c
@@ -10,8 +10,8 @@
 #define _REMOVE_GNU_SOURCE
 #endif
 
-#include <fcntl.h>		/* sync_range_submit(), if possible */
-#include <sys/types.h>		/* off_t, size_t */
+/* Must be down here because otherwise we might try to #include things twice:
+ * once with _GNU_SOURCE and one without it */
 #include "compat.h"
 
 
@@ -19,6 +19,9 @@
  * sync_file_range() support through an internal similar API
  */
 
+#include <fcntl.h>		/* sync_range_submit(), if possible */
+#include <sys/types.h>		/* off_t, size_t */
+
 #ifdef SYNC_FILE_RANGE_WRITE
 const int have_sync_range = 1;
 
@@ -54,7 +57,33 @@ int sync_range_wait(int fd, off_t offset, size_t nbytes)
 
 #endif /* defined SYNC_FILE_RANGE_WRITE */
 
+/* It is no longer needed */
 #ifdef _REMOVE_GNU_SOURCE
 #undef _GNU_SOURCE
 #endif
 
+
+/*
+ * Support for platforms where clock_gettime() is not available.
+ */
+
+#ifdef LACK_CLOCK_GETTIME
+#warning "No clock_gettime() available, falling back to gettimeofday()"
+
+#include <sys/time.h>		/* gettimeofday() */
+
+int clock_gettime(int clk_id, struct timespec *tp)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+
+	tp->tv_sec = tv.tv_sec;
+	tp->tv_nsec = tv.tv_usec / 1000.0;
+
+	return 0;
+}
+
+#endif /* defined LACK_CLOCK_GETTIME */
+
+
diff --git a/libjio/compat.h b/libjio/compat.h
index 517af33..08b9c08 100644
--- a/libjio/compat.h
+++ b/libjio/compat.h
@@ -26,5 +26,15 @@ int sync_range_wait(int fd, off_t offset, size_t nbytes);
 #define posix_fadvise(fd, offset, len, advise)
 #endif
 
+/* Some platforms do not have clock_gettime() so we define an alternative for
+ * them, in compat.c. We should check for _POSIX_TIMERS, but some platforms do
+ * not have it yet they do have clock_gettime() (DragonflyBSD), so we just
+ * check for CLOCK_REALTIME. */
+#include <time.h>
+#ifndef CLOCK_REALTIME
+#define LACK_CLOCK_GETTIME 1
+#define CLOCK_REALTIME 0
+#endif
+
 #endif