summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--configure.in1
-rw-r--r--process.c19
3 files changed, 24 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 63bad4f057..d442b874dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Thu Aug 22 20:14:59 2013 Tanaka Akira <akr@fsij.org>
+
+ * process.c (unsigned_clock_t): Defined.
+ (rb_clock_gettime): Consider clock_t overflow for
+ ISO_C_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID.
+
+ * configure.in: Check the size of clock_t.
+
Thu Aug 22 16:22:48 2013 Koichi Sasada <ko1@atdot.net>
* compile.c (build_postexe_iseq): fix to setup the local table.
diff --git a/configure.in b/configure.in
index 491b7c3291..f9da1d5274 100644
--- a/configure.in
+++ b/configure.in
@@ -1168,6 +1168,7 @@ RUBY_CHECK_SIZEOF(void*, [int long "long long"], [ILP LP LLP])
RUBY_CHECK_SIZEOF(float)
RUBY_CHECK_SIZEOF(double)
RUBY_CHECK_SIZEOF(time_t, [long "long long"], [], [@%:@include <time.h>])
+RUBY_CHECK_SIZEOF(clock_t)
AC_DEFUN([RUBY_CHECK_PRINTF_PREFIX], [
AC_CACHE_CHECK([for printf prefix for $1], [rb_cv_pri_prefix_]AS_TR_SH($1),[
diff --git a/process.c b/process.c
index 7e9b5e9c08..a48833f93a 100644
--- a/process.c
+++ b/process.c
@@ -196,6 +196,14 @@ static rb_gid_t obj2gid(VALUE id);
# endif
#endif
+#if SIZEOF_CLOCK_T == SIZEOF_INT
+typedef unsigned int unsigned_clock_t;
+#elif SIZEOF_CLOCK_T == SIZEOF_LONG
+typedef unsigned long unsigned_clock_t;
+#elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
+typedef unsigned LONG_LONG unsigned_clock_t;
+#endif
+
/*
* call-seq:
* Process.pid -> fixnum
@@ -6886,15 +6894,18 @@ rb_clock_gettime(int argc, VALUE *argv)
#define RUBY_ISO_C_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
ID2SYM(rb_intern("ISO_C_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID"))
if (clk_id == RUBY_ISO_C_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
- double ns;
+ double s, ns;
clock_t c;
+ unsigned_clock_t uc;
c = clock();
errno = 0;
if (c == (clock_t)-1)
rb_sys_fail("clock");
- ns = c * (1e9 / CLOCKS_PER_SEC);
- ts.tv_sec = (time_t)(ns*1e-9);
- ts.tv_nsec = ns - ts.tv_sec*1e9;
+ uc = (unsigned_clock_t)c;
+ ns = (uc*1e9) / CLOCKS_PER_SEC; /* uc*1e9 doesn't lose data if clock_t is 32bit. */
+ s = floor(ns*1e-9);
+ ts.tv_sec = (time_t)s;
+ ts.tv_nsec = ns - s*1e9;
goto success;
}