Skip to content

Commit

Permalink
prefer clock_gettime
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Jun 14, 2018
1 parent 93f7a11 commit a03ea37
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions configure.ac
Expand Up @@ -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], [
Expand Down
12 changes: 12 additions & 0 deletions mjit.c
Expand Up @@ -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. */
Expand Down
6 changes: 4 additions & 2 deletions process.c
Expand Up @@ -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.
*/
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions random.c
Expand Up @@ -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);
Expand Down

0 comments on commit a03ea37

Please sign in to comment.