From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=56338 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PzU7Z-00075r-HA for qemu-devel@nongnu.org; Tue, 15 Mar 2011 09:17:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PzU7X-0006Cf-La for qemu-devel@nongnu.org; Tue, 15 Mar 2011 09:17:08 -0400 Received: from mel.act-europe.fr ([194.98.77.210]:41397) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PzU7X-00068P-17 for qemu-devel@nongnu.org; Tue, 15 Mar 2011 09:17:07 -0400 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 17773CB029D for ; Tue, 15 Mar 2011 14:16:44 +0100 (CET) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ztMx9yxX6MAG for ; Tue, 15 Mar 2011 14:16:41 +0100 (CET) Received: from ulanbator.act-europe.fr (ulanbator.act-europe.fr [10.10.1.67]) by mel.act-europe.fr (Postfix) with ESMTP id 15FDACB028D for ; Tue, 15 Mar 2011 14:16:41 +0100 (CET) From: Tristan Gingold Date: Tue, 15 Mar 2011 14:16:41 +0100 Message-Id: <1300195001-51765-1-git-send-email-gingold@adacore.com> Subject: [Qemu-devel] [PATCH] Autodetect clock_gettime List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org 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 --- 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 < #include 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