lockable: add lock guards
This patch introduces two lock guard macros that automatically unlock a lock object (QemuMutex and others): void f(void) { QEMU_LOCK_GUARD(&mutex); if (!may_fail()) { return; /* automatically unlocks mutex */ } ... } and: WITH_QEMU_LOCK_GUARD(&mutex) { if (!may_fail()) { return; /* automatically unlocks mutex */ } } /* automatically unlocks mutex here */ ... Convert qemu-timer.c functions that benefit from these macros as an example. Manual qemu_mutex_lock/unlock() callers are left unmodified in cases where clarity would not improve by switching to the macros. Many other QemuMutex users remain in the codebase that might benefit from lock guards. Over time they can be converted, if that is desirable. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> [Use QEMU_MAKE_LOCKABLE_NONNULL. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
8834dcf47e
commit
3284c3ddc4
@ -106,4 +106,69 @@ static inline void qemu_lockable_unlock(QemuLockable *x)
|
||||
x->unlock(x->object);
|
||||
}
|
||||
|
||||
static inline QemuLockable *qemu_lockable_auto_lock(QemuLockable *x)
|
||||
{
|
||||
qemu_lockable_lock(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void qemu_lockable_auto_unlock(QemuLockable *x)
|
||||
{
|
||||
if (x) {
|
||||
qemu_lockable_unlock(x);
|
||||
}
|
||||
}
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuLockable, qemu_lockable_auto_unlock)
|
||||
|
||||
#define WITH_QEMU_LOCK_GUARD_(x, var) \
|
||||
for (g_autoptr(QemuLockable) var = \
|
||||
qemu_lockable_auto_lock(QEMU_MAKE_LOCKABLE_NONNULL((x))); \
|
||||
var; \
|
||||
qemu_lockable_auto_unlock(var), var = NULL)
|
||||
|
||||
/**
|
||||
* WITH_QEMU_LOCK_GUARD - Lock a lock object for scope
|
||||
*
|
||||
* @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin).
|
||||
*
|
||||
* This macro defines a lock scope such that entering the scope takes the lock
|
||||
* and leaving the scope releases the lock. Return statements are allowed
|
||||
* within the scope and release the lock. Break and continue statements leave
|
||||
* the scope early and release the lock.
|
||||
*
|
||||
* WITH_QEMU_LOCK_GUARD(&mutex) {
|
||||
* ...
|
||||
* if (error) {
|
||||
* return; <-- mutex is automatically unlocked
|
||||
* }
|
||||
*
|
||||
* if (early_exit) {
|
||||
* break; <-- leave this scope early
|
||||
* }
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
#define WITH_QEMU_LOCK_GUARD(x) \
|
||||
WITH_QEMU_LOCK_GUARD_((x), qemu_lockable_auto##__COUNTER__)
|
||||
|
||||
/**
|
||||
* QEMU_LOCK_GUARD - Lock an object until the end of the scope
|
||||
*
|
||||
* @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin).
|
||||
*
|
||||
* This macro takes a lock until the end of the scope. Return statements
|
||||
* release the lock.
|
||||
*
|
||||
* ... <-- mutex not locked
|
||||
* QEMU_LOCK_GUARD(&mutex); <-- mutex locked from here onwards
|
||||
* ...
|
||||
* if (error) {
|
||||
* return; <-- mutex is automatically unlocked
|
||||
* }
|
||||
*/
|
||||
#define QEMU_LOCK_GUARD(x) \
|
||||
g_autoptr(QemuLockable) qemu_lockable_auto##__COUNTER__ = \
|
||||
qemu_lockable_auto_lock(QEMU_MAKE_LOCKABLE((x)))
|
||||
|
||||
#endif
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/main-loop.h"
|
||||
#include "qemu/timer.h"
|
||||
#include "qemu/lockable.h"
|
||||
#include "sysemu/replay.h"
|
||||
#include "sysemu/cpus.h"
|
||||
|
||||
@ -186,13 +187,12 @@ bool timerlist_expired(QEMUTimerList *timer_list)
|
||||
return false;
|
||||
}
|
||||
|
||||
qemu_mutex_lock(&timer_list->active_timers_lock);
|
||||
if (!timer_list->active_timers) {
|
||||
qemu_mutex_unlock(&timer_list->active_timers_lock);
|
||||
return false;
|
||||
WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
|
||||
if (!timer_list->active_timers) {
|
||||
return false;
|
||||
}
|
||||
expire_time = timer_list->active_timers->expire_time;
|
||||
}
|
||||
expire_time = timer_list->active_timers->expire_time;
|
||||
qemu_mutex_unlock(&timer_list->active_timers_lock);
|
||||
|
||||
return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
|
||||
}
|
||||
@ -225,13 +225,12 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
|
||||
* value but ->notify_cb() is called when the deadline changes. Therefore
|
||||
* the caller should notice the change and there is no race condition.
|
||||
*/
|
||||
qemu_mutex_lock(&timer_list->active_timers_lock);
|
||||
if (!timer_list->active_timers) {
|
||||
qemu_mutex_unlock(&timer_list->active_timers_lock);
|
||||
return -1;
|
||||
WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
|
||||
if (!timer_list->active_timers) {
|
||||
return -1;
|
||||
}
|
||||
expire_time = timer_list->active_timers->expire_time;
|
||||
}
|
||||
expire_time = timer_list->active_timers->expire_time;
|
||||
qemu_mutex_unlock(&timer_list->active_timers_lock);
|
||||
|
||||
delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user