Page MenuHomeFreeBSD

Make mutex/rwlock/sx operation true voids
ClosedPublic

Authored by glebius on Oct 27 2021, 5:38 PM.
Tags
None
Referenced Files
F102433595: D32696.diff
Tue, Nov 12, 5:22 AM
Unknown Object (File)
Fri, Oct 25, 9:47 PM
Unknown Object (File)
Wed, Oct 16, 3:49 AM
Unknown Object (File)
Oct 4 2024, 8:22 PM
Unknown Object (File)
Oct 1 2024, 3:03 PM
Unknown Object (File)
Oct 1 2024, 11:01 AM
Unknown Object (File)
Sep 25 2024, 3:30 PM
Unknown Object (File)
Sep 24 2024, 11:21 AM
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
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 42408
Build 39296: arc lint + arc unit

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