[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 36/51] graph-lock: Implement guard macros
From: |
Kevin Wolf |
Subject: |
[PULL 36/51] graph-lock: Implement guard macros |
Date: |
Wed, 14 Dec 2022 14:44:38 +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>
Message-Id: <20221207131838.239125-4-kwolf@redhat.com>
Reviewed-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
- [PULL 20/51] block-io: introduce coroutine_fn duplicates for bdrv_common_block_status_above callers, (continued)
- [PULL 20/51] block-io: introduce coroutine_fn duplicates for bdrv_common_block_status_above callers, Kevin Wolf, 2022/12/14
- [PULL 23/51] block-backend: replace bdrv_*_above with blk_*_above, Kevin Wolf, 2022/12/14
- [PULL 16/51] block: Remove ignore_bds_parents parameter from drain_begin/end., Kevin Wolf, 2022/12/14
- [PULL 11/51] block: Drain individual nodes during reopen, Kevin Wolf, 2022/12/14
- [PULL 22/51] nbd/server.c: add coroutine_fn annotations, Kevin Wolf, 2022/12/14
- [PULL 18/51] block: Don't poll in bdrv_replace_child_noperm(), Kevin Wolf, 2022/12/14
- [PULL 21/51] block-copy: add coroutine_fn annotations, Kevin Wolf, 2022/12/14
- [PULL 17/51] block: Drop out of coroutine in bdrv_do_drained_begin_quiesce(), Kevin Wolf, 2022/12/14
- [PULL 29/51] block-coroutine-wrapper.py: introduce co_wrapper, Kevin Wolf, 2022/12/14
- [PULL 31/51] block-coroutine-wrapper.py: support also basic return types, Kevin Wolf, 2022/12/14
- [PULL 36/51] graph-lock: Implement guard macros,
Kevin Wolf <=
- [PULL 26/51] block: distinguish between bdrv_create running in coroutine and not, Kevin Wolf, 2022/12/14
- [PULL 25/51] block: avoid duplicating filename string in bdrv_create, Kevin Wolf, 2022/12/14
- [PULL 24/51] block/vmdk: add coroutine_fn annotations, Kevin Wolf, 2022/12/14
- [PULL 28/51] block: rename generated_co_wrapper in co_wrapper_mixed, Kevin Wolf, 2022/12/14
- [PULL 27/51] block: bdrv_create_file is a coroutine_fn, Kevin Wolf, 2022/12/14
- [PULL 32/51] block: convert bdrv_create to co_wrapper, Kevin Wolf, 2022/12/14
- [PULL 35/51] graph-lock: Introduce a lock to protect block graph operations, Kevin Wolf, 2022/12/14
- [PULL 38/51] Import clang-tsa.h, Kevin Wolf, 2022/12/14
- [PULL 33/51] block/dirty-bitmap: convert coroutine-only functions to co_wrapper, Kevin Wolf, 2022/12/14
- [PULL 40/51] clang-tsa: Add macros for shared locks, Kevin Wolf, 2022/12/14