From nobody Tue May 7 21:40:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504614032066806.8489340187865; Tue, 5 Sep 2017 05:20:32 -0700 (PDT) Received: from localhost ([::1]:58621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpCqE-0003Lw-Ep for importer@patchew.org; Tue, 05 Sep 2017 08:20:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpCp8-0002ts-BR for qemu-devel@nongnu.org; Tue, 05 Sep 2017 08:19:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dpCp3-0001Sx-HV for qemu-devel@nongnu.org; Tue, 05 Sep 2017 08:19:22 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:37156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dpCp3-0001Ry-9r; Tue, 05 Sep 2017 08:19:17 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1dpCp0-0006zf-CH; Tue, 05 Sep 2017 13:19:14 +0100 From: Peter Maydell To: qemu-devel@nongnu.org, qemu-trivial@nongnu.org Date: Tue, 5 Sep 2017 13:19:32 +0100 Message-Id: <1504613972-15847-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH] util/qemu-thread-posix.c: Replace OS ifdefs with CONFIG_HAVE_SEM_TIMEDWAIT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In qemu-thread-posix.c we have two implementations of the various qemu_sem_* functions, one of which uses native POSIX sem_* and the other of which emulates them with pthread conditions. This is necessary because not all our host OSes support sem_timedwait(). Instead of a hard-coded list of OSes which don't implement sem_timedwait(), which gets out of date, make configure test for the presence of the function and set a new CONFIG_HAVE_SEM_TIMEDWAIT appropriately. In particular, newer NetBSDs have sem_timedwait(), so this commit will switch them over to using it. OSX still does not have an implementation. Signed-off-by: Peter Maydell Reviewed-by: Eric Blake Reviewed-by: Kamil Rytarowski --- It would be nice to gradually reduce the number of places we do per-OS ifdeffery in favour of checking for the specific feature we care about in each case... Tested on Linux, NetBSD, OSX. configure | 15 +++++++++++++++ include/qemu/thread-posix.h | 2 +- util/qemu-thread-posix.c | 10 +++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/configure b/configure index fb7e34a..aa8d4d9 100755 --- a/configure +++ b/configure @@ -4448,6 +4448,18 @@ if compile_prog "" "" ; then fi =20 ########################################## +# check if we have sem_timedwait + +sem_timedwait=3Dno +cat > $TMPC << EOF +#include +int main(void) { return sem_timedwait(0, 0); } +EOF +if compile_prog "" "" ; then + sem_timedwait=3Dyes +fi + +########################################## # check if trace backend exists =20 $python "$source_path/scripts/tracetool.py" "--backends=3D$trace_backends"= --check-backends > /dev/null 2> /dev/null @@ -5680,6 +5692,9 @@ fi if test "$inotify1" =3D "yes" ; then echo "CONFIG_INOTIFY1=3Dy" >> $config_host_mak fi +if test "$sem_timedwait" =3D "yes" ; then + echo "CONFIG_SEM_TIMEDWAIT=3Dy" >> $config_host_mak +fi if test "$byteswap_h" =3D "yes" ; then echo "CONFIG_BYTESWAP_H=3Dy" >> $config_host_mak fi diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index e5e3a0f..f4296d3 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -21,7 +21,7 @@ struct QemuCond { }; =20 struct QemuSemaphore { -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_t lock; pthread_cond_t cond; unsigned int count; diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 4e95d27..7306475 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -168,7 +168,7 @@ void qemu_sem_init(QemuSemaphore *sem, int init) { int rc; =20 -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =3D pthread_mutex_init(&sem->lock, NULL); if (rc !=3D 0) { error_exit(rc, __func__); @@ -196,7 +196,7 @@ void qemu_sem_destroy(QemuSemaphore *sem) =20 assert(sem->initialized); sem->initialized =3D false; -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =3D pthread_cond_destroy(&sem->cond); if (rc < 0) { error_exit(rc, __func__); @@ -218,7 +218,7 @@ void qemu_sem_post(QemuSemaphore *sem) int rc; =20 assert(sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock(&sem->lock); if (sem->count =3D=3D UINT_MAX) { rc =3D EINVAL; @@ -256,7 +256,7 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms) struct timespec ts; =20 assert(sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =3D 0; compute_abs_deadline(&ts, ms); pthread_mutex_lock(&sem->lock); @@ -304,7 +304,7 @@ void qemu_sem_wait(QemuSemaphore *sem) int rc; =20 assert(sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock(&sem->lock); while (sem->count =3D=3D 0) { rc =3D pthread_cond_wait(&sem->cond, &sem->lock); --=20 2.7.4