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

* Re: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
  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)
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Wong @ 2013-09-24 18:09 UTC (permalink / raw)
  To: sleepy.penguin

Lin Jen-Shin <godfat@godfat.org> wrote:
> 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.

Pushed with the following commit addendum:

[ew: fixed indentation
 Note: this project does not and will never officially support
 non-Free OSes, but there are likely other systems without
 clock_gettime but has kqueue (perhaps via libkqueue).]

Signed-off-by: Eric Wong <normalperson@yhbt.net>

> +++ b/ext/sleepy_penguin/missing_clock_gettime.h
> +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!?");

I'm not sure why you switched to spaces for indentation.  I just changed
it back to match the original clogger code.
(my preferred C indentation style is based on Linux kernel conventions)


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

* Re: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
  2013-09-24 18:09 ` Eric Wong
@ 2013-09-24 18:15   ` Lin Jen-Shin (godfat)
  2013-09-24 21:05     ` Eric Wong
  0 siblings, 1 reply; 6+ messages in thread
From: Lin Jen-Shin (godfat) @ 2013-09-24 18:15 UTC (permalink / raw)
  To: sleepy.penguin

On Wed, Sep 25, 2013 at 2:09 AM, Eric Wong <normalperson@yhbt.net> wrote:
> Lin Jen-Shin <godfat@godfat.org> wrote:
> Pushed with the following commit addendum:
[...]

Thank you!

> I'm not sure why you switched to spaces for indentation.  I just changed
> it back to match the original clogger code.
> (my preferred C indentation style is based on Linux kernel conventions)

Doh! I am sorry for that, it's a mistake. Usually I would just follow the
original coding style. I guess I might be too tired recently so was not
careful enough this time :/

My text editor defaults to spaces, and it might fail to infer and switch to
tabs automatically, and I didn't notice that.


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

* Re: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
  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)
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Wong @ 2013-09-24 21:05 UTC (permalink / raw)
  To: sleepy.penguin

"Lin Jen-Shin (godfat)" <godfat@godfat.org> wrote:
> On Wed, Sep 25, 2013 at 2:09 AM, Eric Wong <normalperson@yhbt.net> wrote:
> > Lin Jen-Shin <godfat@godfat.org> wrote:
> > Pushed with the following commit addendum:
> [...]
> 
> Thank you!

Thank you for the patch!

> > I'm not sure why you switched to spaces for indentation.  I just changed
> > it back to match the original clogger code.
> > (my preferred C indentation style is based on Linux kernel conventions)
> 
> Doh! I am sorry for that, it's a mistake. Usually I would just follow the
> original coding style. I guess I might be too tired recently so was not
> careful enough this time :/

No worries.  Btw, do you have time to run/write more tests for the
kqueue code?  I think the RARRAY_PTR usage should be replaced with newer
RARRAY_* macros or even just rb_ary_entry in some places for 2.1.0 and
rbx.

(ref: comments in https://bugs.ruby-lang.org/issues/8399)


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

* Re: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
  2013-09-24 21:05     ` Eric Wong
@ 2013-09-25 11:43       ` Lin Jen-Shin (godfat)
  2013-09-25 17:37         ` Eric Wong
  0 siblings, 1 reply; 6+ messages in thread
From: Lin Jen-Shin (godfat) @ 2013-09-25 11:43 UTC (permalink / raw)
  To: sleepy.penguin

On Wed, Sep 25, 2013 at 5:05 AM, Eric Wong <normalperson@yhbt.net> wrote:
> No worries.  Btw, do you have time to run/write more tests for the
> kqueue code?  I think the RARRAY_PTR usage should be replaced with newer
> RARRAY_* macros or even just rb_ary_entry in some places for 2.1.0 and
> rbx.
>
> (ref: comments in https://bugs.ruby-lang.org/issues/8399)

Would be my pleasure to do so, though maybe not in a day or two.

On the other hand, since I only wrote some naive C extensions
for Ruby from copying and pasting, (i.e. readline_buffer
https://github.com/godfat/readline_buffer )
I don't know why sometimes the API is in the form of macro
and sometimes in the form of function. According to my
C experience, I would prefer to use functions.

Is there any reason why we need macros for some functions?
To inline function calls before C99?

And do we need Ruby version guards at places if some
Ruby API is missing for older Ruby? Or just like how we
did for clock_gettime?


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

* Re: [sleepy.penguin] [PATCH] Support for broken system without clock_gettime.
  2013-09-25 11:43       ` Lin Jen-Shin (godfat)
@ 2013-09-25 17:37         ` Eric Wong
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Wong @ 2013-09-25 17:37 UTC (permalink / raw)
  To: sleepy.penguin

"Lin Jen-Shin (godfat)" <godfat@godfat.org> wrote:
> On Wed, Sep 25, 2013 at 5:05 AM, Eric Wong <normalperson@yhbt.net> wrote:
> > No worries.  Btw, do you have time to run/write more tests for the
> > kqueue code?  I think the RARRAY_PTR usage should be replaced with newer
> > RARRAY_* macros or even just rb_ary_entry in some places for 2.1.0 and
> > rbx.
> >
> > (ref: comments in https://bugs.ruby-lang.org/issues/8399)
> 
> Would be my pleasure to do so, though maybe not in a day or two.

Thanks!  Not a problem, I'm juggling a bunch of projects, too.

> On the other hand, since I only wrote some naive C extensions
> for Ruby from copying and pasting, (i.e. readline_buffer
> https://github.com/godfat/readline_buffer )

Hehe, I pretended to know C for years by copying/editing existing
code/patches until I realized one day I knew it (or at least I think I
do :)

> I don't know why sometimes the API is in the form of macro
> and sometimes in the form of function. According to my
> C experience, I would prefer to use functions.
> 
> Is there any reason why we need macros for some functions?
> To inline function calls before C99?

Yes.  There are also some reasons such as:

  DATA_PTR(foo) = bar;  /* do NOT do this with R{STRING,ARRAY}_PTR */

Sometimes, it's also easier to just do an "#ifdef MACRO" than to
write the have_* checks in mkmf (or autoconf checks).

Anyways, for arrays, I perhaps using the slightly-slower rb_ary_*
functions might be best as ko1 says:
  http://mid.gmane.org/524259E2.9020503@atdot.net

> And do we need Ruby version guards at places if some
> Ruby API is missing for older Ruby? Or just like how we
> did for clock_gettime?

I prefer guarding based on function/macro availability whenever possible.

Only in rare cases would I put version checks (e.g. a function/macro
exists, but is broken for some versions)


^ permalink raw reply	[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).