Convert "T coroutine_fn" annotations to the new type CoroutineFn<T>, and add co_await as needed.
_Generic is replaced by an overloaded constructor. C++ also does not like & on a temporary, so that is replaced by a function qemu_make_co_lockable_nonnull that hides it from the compiler. Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> --- include/qemu/co-lockable.h | 111 ++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 58 deletions(-) diff --git a/include/qemu/co-lockable.h b/include/qemu/co-lockable.h index 09f4620017..13e3cc7a69 100644 --- a/include/qemu/co-lockable.h +++ b/include/qemu/co-lockable.h @@ -16,83 +16,78 @@ #include "qemu/coroutine.h" #include "qemu/thread.h" -typedef void coroutine_fn QemuCoLockUnlockFunc(void *); +typedef CoroutineFn<void> QemuCoLockUnlockFunc(void *); + +extern CoroutineFn<void> qemu_mutex_co_lock(QemuMutex *m); +extern CoroutineFn<void> qemu_mutex_co_unlock(QemuMutex *m); struct QemuCoLockable { void *object; QemuCoLockUnlockFunc *lock; QemuCoLockUnlockFunc *unlock; + + QemuCoLockable() : + object{NULL}, + lock{(QemuCoLockUnlockFunc *) NULL}, + unlock{(QemuCoLockUnlockFunc *) NULL} {} + QemuCoLockable(QemuMutex *x) : + object{x}, + lock{(QemuCoLockUnlockFunc *) qemu_mutex_co_lock}, + unlock{(QemuCoLockUnlockFunc *) qemu_mutex_co_unlock} {} + QemuCoLockable(CoMutex *x) : + object{x}, + lock{(QemuCoLockUnlockFunc *) qemu_co_mutex_lock}, + unlock{(QemuCoLockUnlockFunc *) qemu_co_mutex_unlock} {} }; -static inline __attribute__((__always_inline__)) QemuCoLockable * -qemu_make_co_lockable(void *x, QemuCoLockable *lockable) +template<typename T> +static inline QemuCoLockable qcml_obj_nonnull_(T *x) { - /* - * We cannot test this in a macro, otherwise we get compiler - * warnings like "the address of 'm' will always evaluate as 'true'". - */ - return x ? lockable : NULL; + return QemuCoLockable{x}; } -static inline __attribute__((__always_inline__)) QemuCoLockable * -qemu_null_co_lockable(void *x) +static inline QemuCoLockable const *qemu_make_co_lockable_nonnull(QemuCoLockable const &x) { + return &x; +} + +template<typename T> +static inline QemuCoLockable qcml_obj_(T *x) +{ + return QemuCoLockable{x}; +} +extern void build_not_reached(); + +template<> inline +QemuCoLockable qcml_obj_(void *x) +{ +#ifdef __OPTIMIZE__ if (x != NULL) { - qemu_build_not_reached(); + build_not_reached(); } - return NULL; +#endif + return QemuCoLockable{}; } -/* - * In C, compound literals have the lifetime of an automatic variable. - * In C++ it would be different, but then C++ wouldn't need QemuCoLockable - * either... - */ -#define QMCL_OBJ_(x, name) (&(QemuCoLockable) { \ - .object = (x), \ - .lock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _lock, \ - .unlock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _unlock \ - }) - -/** - * QEMU_MAKE_CO_LOCKABLE - Make a polymorphic QemuCoLockable - * - * @x: a lock object (currently one of QemuMutex, CoMutex). - * - * Returns a QemuCoLockable object that can be passed around - * to a function that can operate with locks of any kind, or - * NULL if @x is %NULL. - * - * Note the speci case for void *, so that we may pass "NULL". - */ -#define QEMU_MAKE_CO_LOCKABLE(x) \ - _Generic((x), QemuCoLockable *: (x), \ - void *: qemu_null_co_lockable(x), \ - QemuMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, mutex)), \ - CoMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, co_mutex))) \ - -/** - * QEMU_MAKE_CO_LOCKABLE_NONNULL - Make a polymorphic QemuCoLockable - * - * @x: a lock object (currently one of QemuMutex, QemuRecMutex, - * CoMutex, QemuSpin). - * - * Returns a QemuCoLockable object that can be passed around - * to a function that can operate with locks of any kind. - */ -#define QEMU_MAKE_CO_LOCKABLE_NONNULL(x) \ - _Generic((x), QemuCoLockable *: (x), \ - QemuMutex *: QMCL_OBJ_(x, mutex), \ - CoMutex *: QMCL_OBJ_(x, co_mutex)) - -static inline void coroutine_fn qemu_co_lockable_lock(QemuCoLockable *x) +static inline QemuCoLockable const *qemu_make_co_lockable(QemuCoLockable const &x) { - x->lock(x->object); + if (x.object) + return &x; + else + return NULL; } -static inline void coroutine_fn qemu_co_lockable_unlock(QemuCoLockable *x) +#define QEMU_MAKE_CO_LOCKABLE_NONNULL(x) qemu_make_co_lockable_nonnull(qcml_obj_nonnull_(x)) +#define QEMU_MAKE_CO_LOCKABLE(x) qemu_make_co_lockable(qcml_obj_(x)) + +static inline CoroutineFn<void> qemu_co_lockable_lock(const QemuCoLockable *x) { - x->unlock(x->object); + co_await x->lock(x->object); +} + +static inline CoroutineFn<void> qemu_co_lockable_unlock(const QemuCoLockable *x) +{ + co_await x->unlock(x->object); } #endif -- 2.35.1