[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 03/18] graph-lock: Implement guard macros
From: |
Kevin Wolf |
Subject: |
[PATCH 03/18] graph-lock: Implement guard macros |
Date: |
Wed, 7 Dec 2022 14:18:23 +0100 |
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Similar to the implementation in lockable.h, implement macros to
automatically take and release the rdlock.
Create the empty GraphLockable and GraphLockableMainloop structs only to
use it as a type for G_DEFINE_AUTOPTR_CLEANUP_FUNC.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
include/block/graph-lock.h | 66 ++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h
index 82edb62cfa..b27d8a5fb1 100644
--- a/include/block/graph-lock.h
+++ b/include/block/graph-lock.h
@@ -135,5 +135,71 @@ void coroutine_fn bdrv_graph_co_rdunlock(void);
void bdrv_graph_rdlock_main_loop(void);
void bdrv_graph_rdunlock_main_loop(void);
+typedef struct GraphLockable { } GraphLockable;
+
+/*
+ * In C, compound literals have the lifetime of an automatic variable.
+ * In C++ it would be different, but then C++ wouldn't need QemuLockable
+ * either...
+ */
+#define GML_OBJ_() (&(GraphLockable) { })
+
+static inline GraphLockable *graph_lockable_auto_lock(GraphLockable *x)
+{
+ bdrv_graph_co_rdlock();
+ return x;
+}
+
+static inline void graph_lockable_auto_unlock(GraphLockable *x)
+{
+ bdrv_graph_co_rdunlock();
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GraphLockable, graph_lockable_auto_unlock)
+
+#define WITH_GRAPH_RDLOCK_GUARD_(var) \
+ for (g_autoptr(GraphLockable) var = graph_lockable_auto_lock(GML_OBJ_()); \
+ var; \
+ graph_lockable_auto_unlock(var), var = NULL)
+
+#define WITH_GRAPH_RDLOCK_GUARD() \
+ WITH_GRAPH_RDLOCK_GUARD_(glue(graph_lockable_auto, __COUNTER__))
+
+#define GRAPH_RDLOCK_GUARD(x) \
+ g_autoptr(GraphLockable) \
+ glue(graph_lockable_auto, __COUNTER__) G_GNUC_UNUSED = \
+ graph_lockable_auto_lock(GML_OBJ_())
+
+
+typedef struct GraphLockableMainloop { } GraphLockableMainloop;
+
+/*
+ * In C, compound literals have the lifetime of an automatic variable.
+ * In C++ it would be different, but then C++ wouldn't need QemuLockable
+ * either...
+ */
+#define GMLML_OBJ_() (&(GraphLockableMainloop) { })
+
+static inline GraphLockableMainloop *
+graph_lockable_auto_lock_mainloop(GraphLockableMainloop *x)
+{
+ bdrv_graph_rdlock_main_loop();
+ return x;
+}
+
+static inline void
+graph_lockable_auto_unlock_mainloop(GraphLockableMainloop *x)
+{
+ bdrv_graph_rdunlock_main_loop();
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GraphLockableMainloop,
+ graph_lockable_auto_unlock_mainloop)
+
+#define GRAPH_RDLOCK_GUARD_MAINLOOP(x) \
+ g_autoptr(GraphLockableMainloop) \
+ glue(graph_lockable_auto, __COUNTER__) G_GNUC_UNUSED = \
+ graph_lockable_auto_lock_mainloop(GMLML_OBJ_())
+
#endif /* GRAPH_LOCK_H */
--
2.38.1
- [PATCH 00/18] block: Introduce a block graph rwlock, Kevin Wolf, 2022/12/07
- [PATCH 03/18] graph-lock: Implement guard macros,
Kevin Wolf <=
- [PATCH 01/18] block: Factor out bdrv_drain_all_begin_nopoll(), Kevin Wolf, 2022/12/07
- [PATCH 02/18] graph-lock: Introduce a lock to protect block graph operations, Kevin Wolf, 2022/12/07
- [PATCH 04/18] async: Register/unregister aiocontext in graph lock list, Kevin Wolf, 2022/12/07
- [PATCH 08/18] configure: Enable -Wthread-safety if present, Kevin Wolf, 2022/12/07
- [PATCH 07/18] clang-tsa: Add macros for shared locks, Kevin Wolf, 2022/12/07
- [PATCH 05/18] Import clang-tsa.h, Kevin Wolf, 2022/12/07
- [PATCH 06/18] clang-tsa: Add TSA_ASSERT() macro, Kevin Wolf, 2022/12/07
- [PATCH 10/18] block: Fix locking in external_snapshot_prepare(), Kevin Wolf, 2022/12/07
- [PATCH 12/18] block: remove unnecessary assert_bdrv_graph_writable(), Kevin Wolf, 2022/12/07
- [PATCH 11/18] block: wrlock in bdrv_replace_child_noperm, Kevin Wolf, 2022/12/07