From 0f1baad5a38d279cc03ff9a5a83dca12b9fb26a6 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 9 May 2008 10:17:15 +0000 Subject: * thread.c (timeofday): use monotonic clock. based on a patch from zimbatm in [ruby-core:16627]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16338 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'thread.c') diff --git a/thread.c b/thread.c index f7a4b8ba7a..21d43edc27 100644 --- a/thread.c +++ b/thread.c @@ -674,9 +674,18 @@ rb_thread_sleep_forever() static double timeofday(void) { - struct timeval tv; - gettimeofday(&tv, NULL); - return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6; +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) + struct timespec tp; + + if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) { + return (double)tp.tv_sec + (double)tp.tv_nsec * 1e-9; + } else +#endif + { + struct timeval tv; + gettimeofday(&tv, NULL); + return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6; + } } static void -- cgit v1.2.3