Page MenuHomeFreeBSD

Make mutex/rwlock/sx operation true voids
ClosedPublic

Authored by glebius on Oct 27 2021, 5:38 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Jun 24, 10:50 PM
Unknown Object (File)
Fri, Jun 21, 11:02 AM
Unknown Object (File)
Fri, May 31, 5:58 PM
Unknown Object (File)
Mar 7 2024, 8:01 PM
Unknown Object (File)
Feb 26 2024, 11:21 PM
Unknown Object (File)
Feb 14 2024, 9:25 PM
Unknown Object (File)
Jan 15 2024, 1:03 AM
Unknown Object (File)
Jan 6 2024, 8:20 PM
Subscribers

Details

Summary

This makes them real void expressions, and they can be used anywhere
where a void function call can be used, for example in a conditional
operator.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

glebius retitled this revision from Wrap mutex(9), rwlock(9) and sx(9) macros into __extension__ ({}) instead of do {} while (0). to Wrap mutex(9), rwlock(9) and sx(9) macros into __extension__ ({})instead of do {} while (0)..Oct 27 2021, 5:38 PM
glebius added reviewers: kib, mjg.

Technically this is fine.

My only request would be to consider the type of the expressions. According to gcc manual,

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)

It so happen that the 'last thing' in all cases is some if operator, so added expressions all have void type. Can we make this explicit somehow (I do not have a specific proposal)?

The most laconic way to do add explicitness is to add (void)0; to end of each definition.

To me this looks like linguistic/philosophical question on reading the standard. What actually makes an expression explicitly void? Standard doesn't have official definition of what a void expression is. It briefly defines it in 6.3.2.2 in braces - an expression that has type void. Let's go to primary definition of what an expression is:

6.5 Expressions
1 An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.

Our if statement is already a void expression statement with side effects (see 6.8.3), so it fully qualifies to "The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct".

IMHO, adding (void)0; won't make it better, will only raise questions from future readers of code.

Assuming nothing blows up in tinderbox that's fine with me.

This revision is now accepted and ready to land.Oct 27 2021, 7:31 PM

The most laconic way to do add explicitness is to add (void)0; to end of each definition.

To me this looks like linguistic/philosophical question on reading the standard. What actually makes an expression explicitly void? Standard doesn't have official definition of what a void expression is. It briefly defines it in 6.3.2.2 in braces - an expression that has type void. Let's go to primary definition of what an expression is:

6.5 Expressions
1 An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.

Our if statement is already a void expression statement with side effects (see 6.8.3), so it fully qualifies to "The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct".

IMHO, adding (void)0; won't make it better, will only raise questions from future readers of code.

Adding explicit void statement at the end make the code future-proof against changes that make the resulted expression type non-void. For instance, if some assignment is added before the closing }), it types the expression, suddenly. I want to avoid this gotcha.

So IMO the ending line like (void)0; /* ensure void type for expression */ is useful.

glebius retitled this revision from Wrap mutex(9), rwlock(9) and sx(9) macros into __extension__ ({})instead of do {} while (0). to Make mutex/rwlock/sx operation true voids.Oct 28 2021, 12:25 AM

Add (void)0; where last expression is if.

This revision now requires review to proceed.Oct 28 2021, 12:25 AM

Note: revision has changed title to workaround bug in git-arc. When committing then plan is to use original title line.

This revision is now accepted and ready to land.Oct 28 2021, 12:34 AM