From a03ea378e79048ac188c8e4211ed2305e4643558 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 14 Jun 2018 13:10:25 +0000 Subject: prefer clock_gettime * configure.ac: clock_gettime or gettimeofday must exist. * process.c (rb_clock_gettime): prefer clock_gettime over gettimeofday, as the latter is obsolete in SUSv4. * random.c (fill_random_seed): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- configure.ac | 6 ++++-- mjit.c | 12 ++++++++++++ process.c | 6 ++++-- random.c | 9 +++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 13065140a6..55504c1399 100644 --- a/configure.ac +++ b/configure.ac @@ -2202,8 +2202,10 @@ AS_IF([test "$rb_cv_rshift_sign" = yes], [ AC_DEFINE(RSHIFT(x,y), (((x)<0) ? ~((~(x))>>(int)(y)) : (x)>>(int)(y))) ]) -AS_IF([test x"$ac_cv_func_gettimeofday" != xyes], [ - AC_MSG_ERROR(gettimeofday() must exist) +AS_CASE(["$ac_cv_func_gettimeofday:$ac_cv_func_clock_gettime"], +[*yes*], [], +[ + AC_MSG_ERROR(clock_gettime() or gettimeofday() must exist) ]) AS_IF([test "$ac_cv_func_sysconf" = yes], [ diff --git a/mjit.c b/mjit.c index c6d7cbbdf1..571f616fe6 100644 --- a/mjit.c +++ b/mjit.c @@ -222,10 +222,22 @@ static void remove_file(const char *filename); static double real_ms_time(void) { +#ifdef HAVE_CLOCK_GETTIME + struct timespec tv; +# ifdef CLOCK_MONOTONIC + const clockid_t c = CLOCK_MONOTONIC; +# else + const clockid_t c = CLOCK_REALTIME; +# endif + + clock_gettime(c, &tv); + return tv.tv_nsec / 1000000.0 + tv.tv_sec * 1000.0; +#else struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_usec / 1000.0 + tv.tv_sec * 1000.0; +#endif } /* Make and return copy of STR in the heap. */ diff --git a/process.c b/process.c index ca172d3e56..9e242a2605 100644 --- a/process.c +++ b/process.c @@ -7340,8 +7340,9 @@ rb_clock_gettime(int argc, VALUE *argv) if (SYMBOL_P(clk_id)) { /* * Non-clock_gettime clocks are provided by symbol clk_id. - * - * gettimeofday is always available on platforms supported by Ruby. + */ +#ifdef HAVE_GETTIMEOFDAY + /* * GETTIMEOFDAY_BASED_CLOCK_REALTIME is used for * CLOCK_REALTIME if clock_gettime is not available. */ @@ -7356,6 +7357,7 @@ rb_clock_gettime(int argc, VALUE *argv) denominators[num_denominators++] = 1000000000; goto success; } +#endif #define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME) if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) { diff --git a/random.c b/random.c index 7a3b6494d5..7165f3c4a3 100644 --- a/random.c +++ b/random.c @@ -558,15 +558,24 @@ static void fill_random_seed(uint32_t *seed, size_t cnt) { static int n = 0; +#if defined HAVE_CLOCK_GETTIME + struct timespec tv; +#elif defined HAVE_GETTIMEOFDAY struct timeval tv; +#endif size_t len = cnt * sizeof(*seed); memset(seed, 0, len); fill_random_bytes(seed, len, FALSE); +#if defined HAVE_CLOCK_GETTIME + clock_gettime(CLOCK_REALTIME, &tv); + seed[0] ^= tv.tv_nsec; +#elif defined HAVE_GETTIMEOFDAY gettimeofday(&tv, 0); seed[0] ^= tv.tv_usec; +#endif seed[1] ^= (uint32_t)tv.tv_sec; #if SIZEOF_TIME_T > SIZEOF_INT seed[0] ^= (uint32_t)((time_t)tv.tv_sec >> SIZEOF_INT * CHAR_BIT); -- cgit v1.2.3