util/qemu-thread-posix.c: Replace OS ifdefs with CONFIG_HAVE_SEM_TIMEDWAIT
PATCH 1/1 util/qemu-thread-posix.c: Replace OS ifdefs with CONFIG_HAVE_SEM_TIMEDWAIT
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.
---
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(-)
configure +15 -0
@@ -4448,6 +4448,18 @@ if compile_prog "" "" ; then fi ########################################## +# check if we have sem_timedwait + +sem_timedwait=no +cat > $TMPC << EOF +#include <semaphore.h> +int main(void) { return sem_timedwait(0, 0); } +EOF +if compile_prog "" "" ; then + sem_timedwait=yes +fi + +########################################## # check if trace backend exists $python "$source_path/scripts/tracetool.py" "--backends=$trace_backends" --check-backends > /dev/null 2> /dev/null
@@ -5680,6 +5692,9 @@ fi if test "$inotify1" = "yes" ; then echo "CONFIG_INOTIFY1=y" >> $config_host_mak fi +if test "$sem_timedwait" = "yes" ; then + echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak +fi if test "$byteswap_h" = "yes" ; then echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak fi
include/qemu/thread-posix.h +1 -1
@@ -21,7 +21,7 @@ struct QemuCond { };struct QemuSemaphore { -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_t lock;pthread_cond_t cond;unsigned int count;
util/qemu-thread-posix.c +5 -5
@@ -168,7 +168,7 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc; -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =pthread_mutex_init (&sem->lock,NULL );if (rc !=0 ) {error_exit (rc, __func__);
@@ -196,7 +196,7 @@ void qemu_sem_destroy(QemuSemaphore *sem)assert (sem->initialized); sem->initialized = false; -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =pthread_cond_destroy (&sem->cond);if (rc <0 ) {error_exit (rc, __func__);
@@ -218,7 +218,7 @@ void qemu_sem_post(QemuSemaphore *sem)int rc;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock (&sem->lock);if (sem->count ==UINT_MAX ) { rc =EINVAL ;
@@ -256,7 +256,7 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)struct timespec ts;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =0 ;compute_abs_deadline (&ts, ms);pthread_mutex_lock (&sem->lock);
@@ -304,7 +304,7 @@ void qemu_sem_wait(QemuSemaphore *sem)int rc;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock (&sem->lock);while (sem->count ==0 ) { rc =pthread_cond_wait (&sem->cond, &sem->lock);
Signature · 1 line
2.7.4
Quoted 6 lines · Peter Maydell
In
qemu-thread-posix.cwe 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 supportsem_timedwait().
Quoted 9 lines
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 newCONFIG_HAVE_SEM_TIMEDWAITappropriately.In particular, newer NetBSDs have
sem_timedwait(), so this commit will switch them over to using it. OSX still does not have an implementation.
The NetBSD part looks fine. It builds fine on NetBSD/amd64 8.99.2. All tests pass (with disabled PaX MPROTECT).
Quoted 1 line
Signed-off-by Peter Maydell <[email protected]>
Quoted 107 lines
---
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(-)configure +15
-0@@ -4448,6 +4448,18 @@ if compile_prog "" "" ; then fi ########################################## +# check if we have sem_timedwait + +sem_timedwait=no +cat > $TMPC << EOF +#include <semaphore.h> +int main(void) { return sem_timedwait(0, 0); } +EOF +if compile_prog "" "" ; then + sem_timedwait=yes +fi + +########################################## # check if trace backend exists $python "$source_path/scripts/tracetool.py" "--backends=$trace_backends" --check-backends > /dev/null 2> /dev/null@@ -5680,6 +5692,9 @@ fi if test "$inotify1" = "yes" ; then echo "CONFIG_INOTIFY1=y" >> $config_host_mak fi +if test "$sem_timedwait" = "yes" ; then + echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak +fi if test "$byteswap_h" = "yes" ; then echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak fiinclude/qemu/thread-posix.h +1
-1@@ -21,7 +21,7 @@ struct QemuCond { };struct QemuSemaphore { -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_t lock;pthread_cond_t cond;unsigned int count;util/qemu-thread-posix.c +5
-5@@ -168,7 +168,7 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc; -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =pthread_mutex_init (&sem->lock,NULL );if (rc !=0 ) {error_exit (rc, __func__);@@ -196,7 +196,7 @@ void qemu_sem_destroy(QemuSemaphore *sem)assert (sem->initialized); sem->initialized = false; -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =pthread_cond_destroy (&sem->cond);if (rc <0 ) {error_exit (rc, __func__);@@ -218,7 +218,7 @@ void qemu_sem_post(QemuSemaphore *sem)int rc;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock (&sem->lock);if (sem->count ==UINT_MAX ) { rc =EINVAL ;@@ -256,7 +256,7 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)struct timespec ts;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT rc =0 ;compute_abs_deadline (&ts, ms);pthread_mutex_lock (&sem->lock);@@ -304,7 +304,7 @@ void qemu_sem_wait(QemuSemaphore *sem)int rc;assert (sem->initialized); -#if defined(__APPLE__) || defined(__NetBSD__) +#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock (&sem->lock);while (sem->count ==0 ) { rc =pthread_cond_wait (&sem->cond, &sem->lock);
Quoted 15 lines · Peter Maydell
In
qemu-thread-posix.cwe 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 supportsem_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 newCONFIG_HAVE_SEM_TIMEDWAITappropriately.In particular, newer NetBSDs have
sem_timedwait(), so this commit will switch them over to using it. OSX still does not have an implementation.
Quoted 5 lines
Signed-off-by Peter Maydell <[email protected]>---
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...
Yes, that's always been autoconf's philosophy: feature tests trump platform tests every time, because platforms can add features over time, and because an ifdef on a single feature test is easier to read than a list of platform names.
Quoted 2 lines
Tested on Linux, NetBSD, OSX.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Michael Tokarev
AppliedQuoted 5 lines · Peter Maydell
In
qemu-thread-posix.cwe 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 supportsem_timedwait().
..
Applied to -trivial, thanks!
/mjt