mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-30 13:31:52 -06:00
include/qemu/lockable: Use _Generic instead of QEMU_GENERIC
This is both more and less complicated than our expansion using __builtin_choose_expr and __builtin_types_compatible_p. The expansion through QEMU_MAKE_LOCKABLE_ doesn't work because we're not emumerating all of the types within the same _Generic, which results in errors about unhandled cases. We must also handle void* explicitly, so that the NULL constant can be used. Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Tested-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20210614233143.1221879-7-richard.henderson@linaro.org>
This commit is contained in:
parent
6c98635ed7
commit
4ffb0681d7
1 changed files with 41 additions and 49 deletions
|
@ -24,79 +24,71 @@ struct QemuLockable {
|
||||||
QemuLockUnlockFunc *unlock;
|
QemuLockUnlockFunc *unlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This function gives an error if an invalid, non-NULL pointer type is passed
|
|
||||||
* to QEMU_MAKE_LOCKABLE. For optimized builds, we can rely on dead-code elimination
|
|
||||||
* from the compiler, and give the errors already at link time.
|
|
||||||
*/
|
|
||||||
#if defined(__OPTIMIZE__) && !defined(__SANITIZE_ADDRESS__)
|
|
||||||
void unknown_lock_type(void *);
|
|
||||||
#else
|
|
||||||
static inline void unknown_lock_type(void *unused)
|
|
||||||
{
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static inline __attribute__((__always_inline__)) QemuLockable *
|
static inline __attribute__((__always_inline__)) QemuLockable *
|
||||||
qemu_make_lockable(void *x, QemuLockable *lockable)
|
qemu_make_lockable(void *x, QemuLockable *lockable)
|
||||||
{
|
{
|
||||||
/* We cannot test this in a macro, otherwise we get compiler
|
/*
|
||||||
|
* We cannot test this in a macro, otherwise we get compiler
|
||||||
* warnings like "the address of 'm' will always evaluate as 'true'".
|
* warnings like "the address of 'm' will always evaluate as 'true'".
|
||||||
*/
|
*/
|
||||||
return x ? lockable : NULL;
|
return x ? lockable : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Auxiliary macros to simplify QEMU_MAKE_LOCABLE. */
|
static inline __attribute__((__always_inline__)) QemuLockable *
|
||||||
#define QEMU_LOCK_FUNC(x) ((QemuLockUnlockFunc *) \
|
qemu_null_lockable(void *x)
|
||||||
QEMU_GENERIC(x, \
|
{
|
||||||
(QemuMutex *, qemu_mutex_lock), \
|
if (x != NULL) {
|
||||||
(QemuRecMutex *, qemu_rec_mutex_lock), \
|
qemu_build_not_reached();
|
||||||
(CoMutex *, qemu_co_mutex_lock), \
|
}
|
||||||
(QemuSpin *, qemu_spin_lock), \
|
return NULL;
|
||||||
unknown_lock_type))
|
}
|
||||||
|
|
||||||
#define QEMU_UNLOCK_FUNC(x) ((QemuLockUnlockFunc *) \
|
/*
|
||||||
QEMU_GENERIC(x, \
|
* In C, compound literals have the lifetime of an automatic variable.
|
||||||
(QemuMutex *, qemu_mutex_unlock), \
|
|
||||||
(QemuRecMutex *, qemu_rec_mutex_unlock), \
|
|
||||||
(CoMutex *, qemu_co_mutex_unlock), \
|
|
||||||
(QemuSpin *, qemu_spin_unlock), \
|
|
||||||
unknown_lock_type))
|
|
||||||
|
|
||||||
/* In C, compound literals have the lifetime of an automatic variable.
|
|
||||||
* In C++ it would be different, but then C++ wouldn't need QemuLockable
|
* In C++ it would be different, but then C++ wouldn't need QemuLockable
|
||||||
* either...
|
* either...
|
||||||
*/
|
*/
|
||||||
#define QEMU_MAKE_LOCKABLE_(x) (&(QemuLockable) { \
|
#define QML_OBJ_(x, name) (&(QemuLockable) { \
|
||||||
.object = (x), \
|
.object = (x), \
|
||||||
.lock = QEMU_LOCK_FUNC(x), \
|
.lock = (QemuLockUnlockFunc *) qemu_ ## name ## _lock, \
|
||||||
.unlock = QEMU_UNLOCK_FUNC(x), \
|
.unlock = (QemuLockUnlockFunc *) qemu_ ## name ## _unlock \
|
||||||
})
|
})
|
||||||
|
|
||||||
/* QEMU_MAKE_LOCKABLE - Make a polymorphic QemuLockable
|
/**
|
||||||
|
* QEMU_MAKE_LOCKABLE - Make a polymorphic QemuLockable
|
||||||
*
|
*
|
||||||
* @x: a lock object (currently one of QemuMutex, QemuRecMutex, CoMutex, QemuSpin).
|
* @x: a lock object (currently one of QemuMutex, QemuRecMutex,
|
||||||
|
* CoMutex, QemuSpin).
|
||||||
*
|
*
|
||||||
* Returns a QemuLockable object that can be passed around
|
* Returns a QemuLockable object that can be passed around
|
||||||
* to a function that can operate with locks of any kind, or
|
* to a function that can operate with locks of any kind, or
|
||||||
* NULL if @x is %NULL.
|
* NULL if @x is %NULL.
|
||||||
*/
|
|
||||||
#define QEMU_MAKE_LOCKABLE(x) \
|
|
||||||
QEMU_GENERIC(x, \
|
|
||||||
(QemuLockable *, (x)), \
|
|
||||||
qemu_make_lockable((x), QEMU_MAKE_LOCKABLE_(x)))
|
|
||||||
|
|
||||||
/* QEMU_MAKE_LOCKABLE_NONNULL - Make a polymorphic QemuLockable
|
|
||||||
*
|
*
|
||||||
* @x: a lock object (currently one of QemuMutex, QemuRecMutex, CoMutex, QemuSpin).
|
* Note the special case for void *, so that we may pass "NULL".
|
||||||
|
*/
|
||||||
|
#define QEMU_MAKE_LOCKABLE(x) \
|
||||||
|
_Generic((x), QemuLockable *: (x), \
|
||||||
|
void *: qemu_null_lockable(x), \
|
||||||
|
QemuMutex *: qemu_make_lockable(x, QML_OBJ_(x, mutex)), \
|
||||||
|
QemuRecMutex *: qemu_make_lockable(x, QML_OBJ_(x, rec_mutex)), \
|
||||||
|
CoMutex *: qemu_make_lockable(x, QML_OBJ_(x, co_mutex)), \
|
||||||
|
QemuSpin *: qemu_make_lockable(x, QML_OBJ_(x, spin)))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QEMU_MAKE_LOCKABLE_NONNULL - Make a polymorphic QemuLockable
|
||||||
|
*
|
||||||
|
* @x: a lock object (currently one of QemuMutex, QemuRecMutex,
|
||||||
|
* CoMutex, QemuSpin).
|
||||||
*
|
*
|
||||||
* Returns a QemuLockable object that can be passed around
|
* Returns a QemuLockable object that can be passed around
|
||||||
* to a function that can operate with locks of any kind.
|
* to a function that can operate with locks of any kind.
|
||||||
*/
|
*/
|
||||||
#define QEMU_MAKE_LOCKABLE_NONNULL(x) \
|
#define QEMU_MAKE_LOCKABLE_NONNULL(x) \
|
||||||
QEMU_GENERIC(x, \
|
_Generic((x), QemuLockable *: (x), \
|
||||||
(QemuLockable *, (x)), \
|
QemuMutex *: QML_OBJ_(x, mutex), \
|
||||||
QEMU_MAKE_LOCKABLE_(x))
|
QemuRecMutex *: QML_OBJ_(x, rec_mutex), \
|
||||||
|
CoMutex *: QML_OBJ_(x, co_mutex), \
|
||||||
|
QemuSpin *: QML_OBJ_(x, spin))
|
||||||
|
|
||||||
static inline void qemu_lockable_lock(QemuLockable *x)
|
static inline void qemu_lockable_lock(QemuLockable *x)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue