Similarly to commit 5626f8c6d46 ("rcu: Add automatically released rcu_read_lock variants"):
AIO_CONTEXT_ACQUIRE_GUARD() acquires the aio context and then uses glib's g_auto infrastructure (and thus whatever the compiler's hooks are) to release it on all exits of the block. WITH_AIO_CONTEXT_ACQUIRE_GUARD() is similar but is used as a wrapper for the lock, i.e.: WITH_AIO_CONTEXT_ACQUIRE_GUARD() { stuff with context acquired } Inspired-by: Dr. David Alan Gilbert <dgilb...@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <phi...@redhat.com> --- include/block/aio.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/block/aio.h b/include/block/aio.h index 47fbe9d81f2..4fa5a5c2720 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -294,6 +294,30 @@ void aio_context_acquire(AioContext *ctx); /* Relinquish ownership of the AioContext. */ void aio_context_release(AioContext *ctx); +static inline AioContext *aio_context_auto_acquire(AioContext *ctx) +{ + aio_context_acquire(ctx); + return ctx; +} + +static inline void aio_context_auto_release(AioContext *ctx) +{ + aio_context_release(ctx); +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(AioContext, aio_context_auto_release) + +#define WITH_AIO_CONTEXT_ACQUIRE_GUARD(ctx) \ + WITH_AIO_CONTEXT_ACQUIRE_GUARD_(glue(_aio_context_auto, __COUNTER__), ctx) + +#define WITH_AIO_CONTEXT_ACQUIRE_GUARD_(var, ctx) \ + for (g_autoptr(AioContext) var = aio_context_auto_acquire(ctx); \ + (var); aio_context_auto_release(var), (var) = NULL) + +#define AIO_CONTEXT_ACQUIRE_GUARD(ctx) \ + g_autoptr(AioContext) _aio_context_auto __attribute__((unused)) \ + = aio_context_auto_acquire(ctx) + /** * aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will * run only once and as soon as possible. -- 2.31.1