Re: [RFC][PATCH 1/2] locking: Introduce __cleanup__ based guards

From: Peter Zijlstra
Date: Fri May 26 2023 - 15:10:43 EST


On Fri, May 26, 2023 at 11:22:36AM -0700, Linus Torvalds wrote:

> But you can actually do the 'bool done' using the exact same type you
> have for the guard - just make it a pointer instead, and use NULL for
> "not done" and non-NULL for "done". It ends up acting exactly like a
> boolean.

Damn; I've actually seen that and should've thought of it.

> IOW, something like this:
>
> #define variable_scope(type, enter, exit) \
> for (type *_done = NULL, _scope __cleanup(exit) = enter;
> !_done; _done = (void *)8)
>
> #define scoped(type, init...) \
> variable_scope(scope_##type##_t, scope_##type##_init(init),
> scope_##type##_cleanup)
>

> I dunno. I didn't *test* the above. Maybe you already tried something
> like the above, and there's a reason why it doesn't work.

I have not; let me go try that. That does look *much* nicer.