Re: [PATCH v3 03/57] locking: Introduce __cleanup() based infrastructure

From: Dmitry Torokhov
Date: Sun Jan 14 2024 - 01:49:19 EST


Hi Peter,

On Mon, Jun 12, 2023 at 11:07:16AM +0200, Peter Zijlstra wrote:
>
> +DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
> +DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T))

I got excited with the ability to have mutexes released automatically,
however the code I work with typically uses mutex_lock_interruptible()
and friends, where resource acquisition may fail. Additionally, the
return values are integers and not pointers, which makes them unsuitable
for something like

struct mutex *guard __free(mutex) = mutex_lock_interruptible(...);

I guess we could make wrappers around mutex_lock...() family to return
either the mutex or ERR_PTR-encoded error, but that feels quite ugly.

Do you have any suggestions here?

Thanks.

--
Dmitry