about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-01-12 16:24:23 -0800
committerEric Wong <normalperson@yhbt.net>2011-01-12 16:24:23 -0800
commit6cc7e96f972d9a648bef9d49ca245282250a5502 (patch)
tree213ed552f782355f3e7a0f2e1fd86f2650aeb1c7
parenteeae500effcddece4e9186d7b647253c48b8a3a7 (diff)
clock_gettime() is not available on some systems and/or
CLOCK_MONOTONIC.  This is totally broken considering the
POSIX standard was 10 years ago, now.

Nothing in gnulib, either, wtf?!
 http://www.gnu.org/software/gnulib/manual/html_node/clock_005fgettime.html
-rw-r--r--ext/clogger_ext/broken_system_compat.h27
-rw-r--r--ext/clogger_ext/clogger.c1
-rw-r--r--ext/clogger_ext/extconf.rb1
3 files changed, 29 insertions, 0 deletions
diff --git a/ext/clogger_ext/broken_system_compat.h b/ext/clogger_ext/broken_system_compat.h
new file mode 100644
index 0000000..f58307e
--- /dev/null
+++ b/ext/clogger_ext/broken_system_compat.h
@@ -0,0 +1,27 @@
+/*
+ * this header includes functions to support broken systems
+ * without clock_gettime() or CLOCK_MONOTONIC
+ */
+
+#ifndef HAVE_CLOCK_GETTIME
+#  ifndef CLOCK_REALTIME
+#    define CLOCK_REALTIME 0 /* whatever */
+#  endif
+static int fake_clock_gettime(int clk_id, struct timespec *res)
+{
+        struct timeval tv;
+        int r = gettimeofday(&tv, NULL);
+
+        assert(0 == r && "gettimeofday() broke!?");
+        res->tv_sec = tv.tv_sec;
+        res->tv_nsec = tv.tv_usec * 1000;
+
+        return r;
+}
+#  define clock_gettime fake_clock_gettime
+#endif /* broken systems w/o clock_gettime() */
+
+/* UGH */
+#ifndef _POSIX_MONOTONIC_CLOCK
+#  define CLOCK_MONOTONIC CLOCK_REALTIME
+#endif
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 9703f69..7e01e6d 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -16,6 +16,7 @@
 #define _POSIX_C_SOURCE 200112L
 #include <time.h>
 #include "ruby_1_9_compat.h"
+#include "broken_system_compat.h"
 
 static void clock_diff(struct timespec *a, const struct timespec *b)
 {
diff --git a/ext/clogger_ext/extconf.rb b/ext/clogger_ext/extconf.rb
index 60c46a7..d87d8c2 100644
--- a/ext/clogger_ext/extconf.rb
+++ b/ext/clogger_ext/extconf.rb
@@ -17,6 +17,7 @@ begin
     $CPPFLAGS += '-D_POSIX_SOURCE_200112L'
     have_func('CLOCK_MONOTONIC', 'time.h')
   end
+  have_func('clock_gettime', 'time.h')
   have_func('localtime_r', 'time.h') or raise "localtime_r needed"
   have_func('gmtime_r', 'time.h') or raise "gmtime_r needed"
   have_func('rb_str_set_len', 'ruby.h')