[前][次][番号順一覧][スレッド一覧]

ruby-changes:51454

From: nobu <ko1@a...>
Date: Thu, 14 Jun 2018 22:10:30 +0900 (JST)
Subject: [ruby-changes:51454] nobu:r63663 (trunk): prefer clock_gettime

nobu	2018-06-14 22:10:25 +0900 (Thu, 14 Jun 2018)

  New Revision: 63663

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63663

  Log:
    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.

  Modified files:
    trunk/configure.ac
    trunk/mjit.c
    trunk/process.c
    trunk/random.c
Index: configure.ac
===================================================================
--- configure.ac	(revision 63662)
+++ configure.ac	(revision 63663)
@@ -2202,8 +2202,10 @@ AS_IF([test "$rb_cv_rshift_sign" = yes], https://github.com/ruby/ruby/blob/trunk/configure.ac#L2202
   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], [
Index: mjit.c
===================================================================
--- mjit.c	(revision 63662)
+++ mjit.c	(revision 63663)
@@ -222,10 +222,22 @@ static void remove_file(const char *file https://github.com/ruby/ruby/blob/trunk/mjit.c#L222
 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. */
Index: process.c
===================================================================
--- process.c	(revision 63662)
+++ process.c	(revision 63663)
@@ -7340,8 +7340,9 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L7340
     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) https://github.com/ruby/ruby/blob/trunk/process.c#L7357
             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) {
Index: random.c
===================================================================
--- random.c	(revision 63662)
+++ random.c	(revision 63663)
@@ -558,15 +558,24 @@ static void https://github.com/ruby/ruby/blob/trunk/random.c#L558
 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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]