sleepy_penguin RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Lin Jen-Shin <godfat@godfat.org>
To: sleepy.penguin@librelist.org
Subject: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
Date: Tue, 24 Sep 2013 16:55:47 +0800	[thread overview]
Message-ID: <1380012947-91938-1-git-send-email-godfat@godfat.org> (raw)
In-Reply-To: <1380012947-91938-1-git-send-email-godfat@godfat.org>

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



       reply	other threads:[~2013-09-24  8:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-24  8:55 Lin Jen-Shin [this message]
2013-09-24 18:09 ` [sleepy.penguin] [PATCH] Support for broken system without clock_gettime 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/sleepy_penguin/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380012947-91938-1-git-send-email-godfat@godfat.org \
    --to=godfat@godfat.org \
    --cc=sleepy.penguin@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).