From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS47066 71.19.144.0/20 X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: normalperson@yhbt.net Received: from zedshaw2.xen.prgmr.com (zedshaw2.xen.prgmr.com [71.19.156.177]) by dcvr.yhbt.net (Postfix) with ESMTP id D25CE1F816 for ; Mon, 26 Aug 2013 08:47:04 +0000 (UTC) Received: from zedshaw2.xen.prgmr.com (unknown [IPv6:::1]) by zedshaw2.xen.prgmr.com (Postfix) with ESMTP id 08EEE74E0B for ; Mon, 26 Aug 2013 08:55:22 +0000 (UTC) MIME-Version: 1.0 Date: Mon, 26 Aug 2013 16:46:24 +0800 From: Lin Jen-Shin List-Archive: List-Help: List-Id: List-Post: List-Subscribe: List-Unsubscribe: Message-Id: <1377506784-48542-1-git-send-email-godfat@godfat.org> Precedence: list References: Sender: sleepy.penguin@librelist.org Subject: [sleepy.penguin] [PATCH] Add support for Mac OS X To: sleepy.penguin@librelist.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Status: A Since Mac OS X does not support clock_gettime, we need to wrap around clock_gettime to make kqueue work on Mac OS X. epoll.c does not need this wrapper as it's only on Linux, but I guess it's fine to use the wrapper as well. Maybe someday *BSD could also support epoll in someway? --- ext/sleepy_penguin/clock_gettime.c | 17 +++++++++++++++++ ext/sleepy_penguin/clock_gettime.h | 23 +++++++++++++++++++++++ ext/sleepy_penguin/epoll.c | 3 ++- ext/sleepy_penguin/kqueue.c | 5 +++-- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 ext/sleepy_penguin/clock_gettime.c create mode 100644 ext/sleepy_penguin/clock_gettime.h diff --git a/ext/sleepy_penguin/clock_gettime.c b/ext/sleepy_penguin/clock_gettime.c new file mode 100644 index 0000000..1f42c10 --- /dev/null +++ b/ext/sleepy_penguin/clock_gettime.c @@ -0,0 +1,17 @@ +#ifdef __MACH__ +/* + * Mac OS X does not support clock_gettime + * ref: http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx + */ +#include "clock_gettime.h" + +void clock_gettime_mach(struct timespec *now){ + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + now->tv_sec = mts.tv_sec; + now->tv_nsec = mts.tv_nsec; +} +#endif diff --git a/ext/sleepy_penguin/clock_gettime.h b/ext/sleepy_penguin/clock_gettime.h new file mode 100644 index 0000000..3294176 --- /dev/null +++ b/ext/sleepy_penguin/clock_gettime.h @@ -0,0 +1,23 @@ +#ifndef CLOCK_GETTIME_H +#define CLOCK_GETTIME_H + +#include +#include + +#ifdef __MACH__ +#include +#include +#endif + +#ifdef __MACH__ +/* + * Mac OS X does not support clock_gettime + * ref: http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx + */ +void clock_gettime_mach(struct timespec*); +#define CLOCK_GETTIME(now) clock_gettime_mach(now); +#else +#define CLOCK_GETTIME(now) clock_gettime(CLOCK_MONOTONIC, now); +#endif + +#endif /* CLOCK_GETTIME_H */ diff --git a/ext/sleepy_penguin/epoll.c b/ext/sleepy_penguin/epoll.c index 9010e2d..d4eedf7 100644 --- a/ext/sleepy_penguin/epoll.c +++ b/ext/sleepy_penguin/epoll.c @@ -1,4 +1,5 @@ #include "sleepy_penguin.h" +#include "clock_gettime.h" #ifdef HAVE_SYS_EPOLL_H #include #include @@ -14,7 +15,7 @@ static uint64_t now_ms(void) { struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); + CLOCK_GETTIME(&now); return now.tv_sec * 1000 + (now.tv_nsec + 500000) / 1000000; } diff --git a/ext/sleepy_penguin/kqueue.c b/ext/sleepy_penguin/kqueue.c index 59c3dae..d9e3f16 100644 --- a/ext/sleepy_penguin/kqueue.c +++ b/ext/sleepy_penguin/kqueue.c @@ -1,4 +1,5 @@ #include "sleepy_penguin.h" +#include "clock_gettime.h" #ifdef HAVE_SYS_EVENT_H #include #include @@ -196,7 +197,7 @@ kevent_resume_p(struct timespec *expire_at, struct kq_per_thread *kpt) if (kpt->ts == NULL) return 1; - clock_gettime(CLOCK_MONOTONIC, &now); + CLOCK_GETTIME(&now); if (now.tv_sec > expire_at->tv_sec) return 0; if (now.tv_sec == expire_at->tv_sec && now.tv_nsec > expire_at->tv_nsec) @@ -221,7 +222,7 @@ static VALUE do_kevent(struct kq_per_thread *kpt) struct timespec expire_at; if (kpt->ts) { - clock_gettime(CLOCK_MONOTONIC, &expire_at); + CLOCK_GETTIME(&expire_at); expire_at.tv_sec += kpt->ts->tv_sec; expire_at.tv_nsec += kpt->ts->tv_nsec; -- 1.8.4