Re: [RFC PATCH v2] cleanup: Add cond_guard() to conditional guards

From: Fabio M. De Francesco
Date: Tue Jan 30 2024 - 12:55:37 EST


On Tuesday, 30 January 2024 18:02:09 CET Dan Williams wrote:
> Fabio M. De Francesco wrote:

[skip}

> >
> > @@ -165,6 +174,10 @@ static inline class_##_name##_t
> > class_##_name##ext##_constructor(_init_args) \>
> > #define __guard_ptr(_name) class_##_name##_lock_ptr
> >
> > +#define cond_guard(_name, args...) \
> > + CLASS(_name, scope)(args); \
> > + if (!__guard_ptr(_name)(&scope))
>
> This needs to protect against being used within another if () block.
> Imagine a case of:
>
> if (...) {
> cond_guard(...);
> <statement>
> } else if (...)

Could it be made clear in the documentation that cond_guard() shouldn't be
misused as you showed above?

Actually, I don't know how effective the documentation can be in avoiding
incorrect use of cond_guard().

Fabio

> ...does that "else if" belong to the first "if ()" or the hidden one
> inside the macro?
>
> You can steal the embedded "if ()" trick from scoped_cond_guard() and do
> something like (untested):
>
> #define cond_guard(_name, _fail, args...) \
> CLASS(_name, scope)(args); \
> if (!__guard_ptr(_name)(&scope)) _fail; else /* pass */;