diff mbox series

[RFC,v2,7/8] graph-lock: implement WITH_GRAPH_RDLOCK_GUARD and GRAPH_RDLOCK_GUARD macros

Message ID 20220426085114.199647-8-eesposit@redhat.com (mailing list archive)
State New, archived
Headers show
Series Removal of AioContext lock, bs->parents and ->children: new rwlock | expand

Commit Message

Emanuele Giuseppe Esposito April 26, 2022, 8:51 a.m. UTC
Similar to the implementation in lockable.h, implement macros to
automatically take and release the rdlock.
Create the empty GraphLockable struct only to use it as a type for
G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/block/graph-lock.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

Comments

Stefan Hajnoczi April 28, 2022, 3 p.m. UTC | #1
On Tue, Apr 26, 2022 at 04:51:13AM -0400, Emanuele Giuseppe Esposito wrote:
> Similar to the implementation in lockable.h, implement macros to
> automatically take and release the rdlock.
> Create the empty GraphLockable struct only to use it as a type for
> G_DEFINE_AUTOPTR_CLEANUP_FUNC.
> 
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> ---
>  include/block/graph-lock.h | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h
> index 2211d41286..8d8a6513f1 100644
> --- a/include/block/graph-lock.h
> +++ b/include/block/graph-lock.h
> @@ -67,5 +67,35 @@ void assert_bdrv_graph_readable(void);
>   */
>  void assert_bdrv_graph_writable(void);
>  
> +typedef struct GraphLockable { } GraphLockable;
> +
> +#define GML_OBJ_() (&(GraphLockable) { })

The comment for QML_OBJ_() is helpful. This takes advantage of the
lifetime of compound literals and it's different in C and C++. I suggest
keeping it for the benefit for people trying to understand how this
works.
diff mbox series

Patch

diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h
index 2211d41286..8d8a6513f1 100644
--- a/include/block/graph-lock.h
+++ b/include/block/graph-lock.h
@@ -67,5 +67,35 @@  void assert_bdrv_graph_readable(void);
  */
 void assert_bdrv_graph_writable(void);
 
+typedef struct GraphLockable { } GraphLockable;
+
+#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_())
+
 #endif /* BLOCK_LOCK_H */