Some POSIX OSes (such as Darwin) doesn't have clock_gettime.  This patch
falls back on gettimeofday if clock_gettime is not available.

Signed-off-by: Tristan Gingold <ging...@adacore.com>
---
 configure           |   11 ++++++++---
 qemu-thread-posix.c |   17 +++++++++++++++--
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index c18f571..6e6cd35 100755
--- a/configure
+++ b/configure
@@ -2236,17 +2236,18 @@ if compile_prog "" "" ; then
 fi
 
 ##########################################
-# Do we need librt
+# Do we need clock_gettime + librt
+clock_gettime=no
 cat > $TMPC <<EOF
-#include <signal.h>
 #include <time.h>
 int main(void) { clockid_t id; return clock_gettime(id, NULL); }
 EOF
 
 if compile_prog "" "" ; then
-  :
+  clock_gettime=yes
 elif compile_prog "" "-lrt" ; then
   LIBS="-lrt $LIBS"
+  clock_gettime=yes
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
@@ -2530,6 +2531,7 @@ echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
 echo "madvise           $madvise"
 echo "posix_madvise     $posix_madvise"
+echo "clock_gettime     $clock_gettime"
 echo "uuid support      $uuid"
 echo "vhost-net support $vhost_net"
 echo "Trace backend     $trace_backend"
@@ -2679,6 +2681,9 @@ fi
 if test "$fnmatch" = "yes" ; then
   echo "CONFIG_FNMATCH=y" >> $config_host_mak
 fi
+if test "$clock_gettime" = "yes" ; then
+  echo "CONFIG_CLOCK_GETTIME=y" >> $config_host_mak
+fi
 if test "$uuid" = "yes" ; then
   echo "CONFIG_UUID=y" >> $config_host_mak
 fi
diff --git a/qemu-thread-posix.c b/qemu-thread-posix.c
index 87c1a9f..dbe14c3 100644
--- a/qemu-thread-posix.c
+++ b/qemu-thread-posix.c
@@ -61,6 +61,19 @@ int qemu_mutex_trylock(QemuMutex *mutex)
     return pthread_mutex_trylock(&mutex->lock);
 }
 
+static void qemu_gettime(struct timespec *ts)
+{
+#ifdef CONFIG_CLOCK_GETTIME
+    clock_gettime(CLOCK_REALTIME, ts);
+#else
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    ts->tv_sec = tv.tv_sec;
+    ts->tv_nsec = tv.tv_usec * 1000;
+#endif
+}
+
 static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
 {
     ts->tv_sec = ts->tv_sec + (long)(msecs / 1000);
@@ -76,7 +89,7 @@ int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
     int err;
     struct timespec ts;
 
-    clock_gettime(CLOCK_REALTIME, &ts);
+    qemu_gettime(&ts);
     timespec_add_ms(&ts, msecs);
 
     err = pthread_mutex_timedlock(&mutex->lock, &ts);
@@ -144,7 +157,7 @@ int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, 
uint64_t msecs)
     struct timespec ts;
     int err;
 
-    clock_gettime(CLOCK_REALTIME, &ts);
+    qemu_gettime(&ts);
     timespec_add_ms(&ts, msecs);
 
     err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
-- 
1.7.3.GIT


Reply via email to