sleepy_penguin RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
@ 2013-09-24  8:55 Lin Jen-Shin
  2013-09-24 18:09 ` Eric Wong
  0 siblings, 1 reply; 6+ messages in thread
From: Lin Jen-Shin @ 2013-09-24  8:55 UTC (permalink / raw)
  To: sleepy.penguin

Fallback mechanism was copied from clogger:

    http://clogger.rubyforge.org/

This would also make sleepy_penguin compiles on Mac OS X,
which is lacking clock_gettime. All tests passed for me.
---
 ext/sleepy_penguin/epoll.c                 |  1 +
 ext/sleepy_penguin/extconf.rb              |  5 +++++
 ext/sleepy_penguin/kqueue.c                |  1 +
 ext/sleepy_penguin/missing_clock_gettime.h | 36 ++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+)
 create mode 100644 ext/sleepy_penguin/missing_clock_gettime.h

diff --git a/ext/sleepy_penguin/epoll.c b/ext/sleepy_penguin/epoll.c
index 9010e2d..5e5cb20 100644
--- a/ext/sleepy_penguin/epoll.c
+++ b/ext/sleepy_penguin/epoll.c
@@ -3,6 +3,7 @@
 #include <sys/epoll.h>
 #include <unistd.h>
 #include <time.h>
+#include "missing_clock_gettime.h"
 #include "missing_epoll.h"
 #include "missing_rb_thread_fd_close.h"
 #include "missing_rb_update_max_fd.h"
diff --git a/ext/sleepy_penguin/extconf.rb b/ext/sleepy_penguin/extconf.rb
index 2ed9b21..0e6977a 100644
--- a/ext/sleepy_penguin/extconf.rb
+++ b/ext/sleepy_penguin/extconf.rb
@@ -13,6 +13,11 @@ have_header('sys/eventfd.h')
 have_header('sys/timerfd.h')
 have_header('sys/inotify.h')
 have_header('ruby/io.h') and have_struct_member('rb_io_t', 'fd', 'ruby/io.h')
+unless have_macro('CLOCK_MONOTONIC', 'time.h')
+  have_func('CLOCK_MONOTONIC', 'time.h')
+end
+have_type('clockid_t', 'time.h')
+have_func('clock_gettime', 'time.h')
 have_func('epoll_create1', %w(sys/epoll.h))
 have_func('rb_thread_call_without_gvl')
 have_func('rb_thread_blocking_region')
diff --git a/ext/sleepy_penguin/kqueue.c b/ext/sleepy_penguin/kqueue.c
index 59c3dae..83f3623 100644
--- a/ext/sleepy_penguin/kqueue.c
+++ b/ext/sleepy_penguin/kqueue.c
@@ -5,6 +5,7 @@
 #include <sys/time.h>
 #include <unistd.h>
 #include <time.h>
+#include "missing_clock_gettime.h"
 #include "missing_rb_thread_fd_close.h"
 #include "missing_rb_update_max_fd.h"
 #include "value2timespec.h"
diff --git a/ext/sleepy_penguin/missing_clock_gettime.h b/ext/sleepy_penguin/missing_clock_gettime.h
new file mode 100644
index 0000000..de36fb2
--- /dev/null
+++ b/ext/sleepy_penguin/missing_clock_gettime.h
@@ -0,0 +1,36 @@
+/*
+ * this header includes functions to support broken systems
+ * without clock_gettime() or CLOCK_MONOTONIC
+ */
+
+#ifndef HAVE_TYPE_CLOCKID_T
+typedef int clockid_t;
+#endif
+
+#ifndef HAVE_CLOCK_GETTIME
+#  ifndef CLOCK_REALTIME
+#    define CLOCK_REALTIME 0 /* whatever */
+#  endif
+static int fake_clock_gettime(clockid_t 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
+ * CLOCK_MONOTONIC is not guaranteed to be a macro, either
+ */
+#ifndef CLOCK_MONOTONIC
+#  if (!defined(_POSIX_MONOTONIC_CLOCK) || !defined(HAVE_CLOCK_MONOTONIC))
+#    define CLOCK_MONOTONIC CLOCK_REALTIME
+#  endif
+#endif
-- 
1.8.4



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-09-25 17:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-24  8:55 [sleepy.penguin] [PATCH] Support for broken system without clock_gettime Lin Jen-Shin
2013-09-24 18:09 ` Eric Wong
2013-09-24 18:15   ` Lin Jen-Shin (godfat)
2013-09-24 21:05     ` Eric Wong
2013-09-25 11:43       ` Lin Jen-Shin (godfat)
2013-09-25 17:37         ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/sleepy_penguin.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).