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.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Kamil Rytarowski <n54@gmx.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Peter Maydell 2017-09-05 13:19:32 +01:00 committed by Michael Tokarev
parent 5746c1cd15
commit 401bc051d7
3 changed files with 21 additions and 6 deletions

15
configure vendored
View File

@ -4424,6 +4424,18 @@ if compile_prog "" "" ; then
posix_syslog=yes posix_syslog=yes
fi 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 # check if trace backend exists
@ -5678,6 +5690,9 @@ fi
if test "$inotify1" = "yes" ; then if test "$inotify1" = "yes" ; then
echo "CONFIG_INOTIFY1=y" >> $config_host_mak echo "CONFIG_INOTIFY1=y" >> $config_host_mak
fi fi
if test "$sem_timedwait" = "yes" ; then
echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak
fi
if test "$byteswap_h" = "yes" ; then if test "$byteswap_h" = "yes" ; then
echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak
fi fi

View File

@ -21,7 +21,7 @@ struct QemuCond {
}; };
struct QemuSemaphore { struct QemuSemaphore {
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
pthread_mutex_t lock; pthread_mutex_t lock;
pthread_cond_t cond; pthread_cond_t cond;
unsigned int count; unsigned int count;

View File

@ -168,7 +168,7 @@ void qemu_sem_init(QemuSemaphore *sem, int init)
{ {
int rc; int rc;
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
rc = pthread_mutex_init(&sem->lock, NULL); rc = pthread_mutex_init(&sem->lock, NULL);
if (rc != 0) { if (rc != 0) {
error_exit(rc, __func__); error_exit(rc, __func__);
@ -196,7 +196,7 @@ void qemu_sem_destroy(QemuSemaphore *sem)
assert(sem->initialized); assert(sem->initialized);
sem->initialized = false; sem->initialized = false;
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
rc = pthread_cond_destroy(&sem->cond); rc = pthread_cond_destroy(&sem->cond);
if (rc < 0) { if (rc < 0) {
error_exit(rc, __func__); error_exit(rc, __func__);
@ -218,7 +218,7 @@ void qemu_sem_post(QemuSemaphore *sem)
int rc; int rc;
assert(sem->initialized); assert(sem->initialized);
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
pthread_mutex_lock(&sem->lock); pthread_mutex_lock(&sem->lock);
if (sem->count == UINT_MAX) { if (sem->count == UINT_MAX) {
rc = EINVAL; rc = EINVAL;
@ -256,7 +256,7 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
struct timespec ts; struct timespec ts;
assert(sem->initialized); assert(sem->initialized);
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
rc = 0; rc = 0;
compute_abs_deadline(&ts, ms); compute_abs_deadline(&ts, ms);
pthread_mutex_lock(&sem->lock); pthread_mutex_lock(&sem->lock);
@ -304,7 +304,7 @@ void qemu_sem_wait(QemuSemaphore *sem)
int rc; int rc;
assert(sem->initialized); assert(sem->initialized);
#if defined(__APPLE__) || defined(__NetBSD__) #ifndef CONFIG_SEM_TIMEDWAIT
pthread_mutex_lock(&sem->lock); pthread_mutex_lock(&sem->lock);
while (sem->count == 0) { while (sem->count == 0) {
rc = pthread_cond_wait(&sem->cond, &sem->lock); rc = pthread_cond_wait(&sem->cond, &sem->lock);