From 3873e09cad296e374d967d00bc8e2e6e8ec60323 Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 22 Aug 2013 11:19:49 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- process.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'process.c') 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; } -- cgit v1.2.3