[libvirt PATCH 06/11] virthread: Introduce WITH_VIR_MUTEX_LOCK_GUARD

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Modeled after "WITH_QEMU_LOCK_GUARD" (see qemu's include/qemu/lockable.h).
Uses "__LINE__" instead of "__COUNTER__", as the latter is a GNU extension.

See comment for typical usage.

Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 src/util/virthread.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/util/virthread.h b/src/util/virthread.h
index b394dbd226..00a9987a79 100644
--- a/src/util/virthread.h
+++ b/src/util/virthread.h
@@ -209,3 +209,23 @@ int virThreadLocalSet(virThreadLocal *l, void*) G_GNUC_WARN_UNUSED_RESULT;
         return 0; \
     } \
     struct classname ## EatSemicolon
+
+/**
+ * WITH_VIR_MUTEX_LOCK_GUARD:
+ *
+ * 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.
+ *
+ *     virMutex *mutex = ...;
+ *
+ *     WITH_VIR_MUTEX_LOCK_GUARD(mutex) {
+ *         // `mutex` is locked, and released automatically on scope exit
+ *         ...
+ *     }
+ */
+#define WITH_VIR_MUTEX_LOCK_GUARD(m) \
+    for (g_autoptr(virLockGuard) CONCAT(var, __LINE__) = virLockGuardNew(m); \
+         CONCAT(var, __LINE__); \
+         CONCAT(var, __LINE__) = (virLockGuardFree(CONCAT(var, __LINE__)), NULL))
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux