spinlock: A new lockref structure for lockless update of refcount
Just noticing that you are adding several functions that return a boolean value as an int. And a "gotref" local variable.
Is that just not wanting to bother with thse newfangled C99 innovations, or do you dislike the "bool" type for some reason?
Even if it doesn't change the code in the slightest, I like to declare things with the bool type for documentation. I can see avoiding code churn, but this is all new code, so I thought I'd ask.
(FWIW, stdbool.h was in gcc 3.2, which README says is the minimum supported version, although that's probably outdated information.)
Quoted 5 lines · George Spelvin
Just noticing that you are adding several functions that return a boolean value as an int. And a "gotref" local variable.
Is that just not wanting to bother with thse newfangled C99 innovations, or do you dislike the "bool" type for some reason?
I don't use "bool" in code I write. I don't think it adds any actual value, and I think the data type is badly designed and of dubious value in C. It has very few actual advantages.
That said, it's not like I *hate* the type, and I won't remove bool from code other people write. I just think it's superfluous and stupid, and another case of C++ people thinking too much "this is a cool feature" without then actually doing it well. The C people then picked it up because it was less onerous than some other C++ features, and all the compilers had the logic anyway.
If "bool" had real advantages (like having a dense array representation, for example), that would be one thing. It doesn't. Sure, now you can take an address of a bool (which you couldn't generally do efficiently if it really was a bit array), but it also means that in practice, "bool" is normally nothing but "char" with some really odd and special implicit type casting rules.
I doubt most people really even understand how "bool" casting works.
And bool is actually really *dangerous* to use if you don't understand it. There are people who use "bool", but then because they want to be portable, they have a compatibility #ifdef or other configuration thing that does something like
typedef int bool; #define true 1 #define false 0
and it will actually work. Most of the time. And then the semantic differences from a _real_ C compiler that supports the C99 _Bool/bool type are really really subtle.
IOW, bool has very few real upsides, and it has a real downside: it's subtle, and people really never even seem to _realize_ just how subtle it is. I suspect that if you ask ten random average C programmers if the above is equivalent to stdbool.h, nine of them will say "sure".
And they'd be *COMPLETELY* wrong.
So no. I'm definitely not a fan of bool. I think there are better types, and I think there are better ways to document things.
Linus
Quoted 6 lines
If "bool" had real advantages (like having a dense array representation, for example), that would be one thing. It doesn't. Sure, now you can take an address of a bool (which you couldn't generally do efficiently if it really was a bit array), but it also means that in practice, "bool" is normally nothing but "char" with some really odd and special implicit type casting rules.
Huh. For me, the big advantage is, to paraphrase Michael Palin, that the number of possible values is two, no more, and no less. Two is the number of possibilities, and the number of possibilities is two. Three is Right Out.
I agree that as a *storage* type (in a data structure), it's of limited usefulness. But as a *parameter* type, to pass to and return from functions, it's wonderful.
There are lots of naming conventions (like the word "flag", or a function name starting with "is") to indicate that the return value is a simple true/false, but declaring it that way provides compile-time checking.
Quoted 1 line
I doubt most people really even understand how "bool" casting works.
There's a reason for that; you rarely need to cast to bool, and you could forbid it outright with very little impact on bool's usefulness. What fraction of C programmers remember off the top of their heads that casting from float to integer rounds toward zero?
Heck, if you asked me which conversions were defined and which were undefined, I'd have to look it up. (I'd guess it's the same as the different overflow rules for signed and unsigned types, but I'd have to check.)
The place bool is most useful is control flow flags and other things where all you're doing is assigning literal values and testing it. And, if you want to get fancy, assigning to bool variables from boolean-valued expressions like "flag = value >= limit;"
That said, I agree that the fact that 2 != (bool)2 is a bit subtle, but doesn't that basic problem apply to *any* narrowing cast? (And thus, is not really an *additional* subtlety that a progammer needs to learn about.) And in this case, I can't really think of a *better* choice that could have been made.
I can only see three plausible alternatives:
1) Use the lsbit, (bool)x == x & 1
2) Use zero/nonzero, and make (bool)x == !!x
3) Forbid casting to bool entirely.
Option 1 matches all other narrowing casts in C, and would be how I'd expect a "bit" type to work.
Option 2 is how C conditional statements already work, so for any value x, "if (x)" is the same as "if ((bool)x)". It's a rule that C programmers already need to know; they only need to know to *apply* it in this one extra case.
In fact, it arguably makes things *simpler* to explain, since it at least gives a name to the magic that C condition expressions are subject to.
Option 3 is attractive, but ends up breaking the analogy to conditional expressions. I'd recommend it as a coding style, however.
Quoted 5 lines
And bool is actually really *dangerous* to use if you don't understand it. There are people who use "bool", but then because they want to be portable, they have a compatibility #ifdef or other configuration thing that does something like
Quoted 7 lines
typedef int bool; #define true 1 #define false 0and it will actually work. Most of the time. And then the semantic differences from a _real_ C compiler that supports the C99 _Bool/bool type are really really subtle.
But *all* of the subtleties arise when casting other types to bool. If you just avoid that one thing (which it's hopefully obvious won't be faithfully emulated by a compatibility kludge anyway), it all goes away.
And this rule is itself just a special case of "be very careful with narrowing casts", which is already common wisdom.
For that kludge, I think a better equivalent would be
typedef enum { false, true } bool;
which at least communicates the idea (again, this is an annoying sutlety that C programmers *already* have to deal with, so no additional cognitive effort) that "the compiler might not catch you assigning out-of-range values, but if you do, demons might fly out of your nose."
Anyway, thanks for sharing your opition. To me, it just looked like exactly the sort of code where the bool type was a natural fit.
Speaking of bool (and I'm not a fan of it either), is this warning just noise (which is bad enough since it masks real warnings), or is this going to cause serious problems?
CHECK /usr/projects/linux/ext4/kernel/trace/trace.c/usr/projects/linux/ext4/kernel/trace/trace.c:559:6: warning: symbol 'free_snapshot' was not declared. Should it be static? /usr/projects/linux/ext4/kernel/trace/trace.c:1489:14: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1489:14: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1489:14: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1489:14: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1492:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1492:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1492:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1492:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1539:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1539:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1539:9: warning: expression using sizeof bool /usr/projects/linux/ext4/kernel/trace/trace.c:1539:9: warning: expression using sizeof bool
(i.e., is a C compiler allowed to pack multiple bools stored in a data structure into a single byte, with potentially hilarious results for C code trying to do magic utilizing sizeof and offsetof (which some of our kernel macros do use, IIRC)?
- Ted
P.S. BTW, this is not the only set of sparse warnings that are emitted by files in kernel/trace/*; maybe it would be good if the ftrace maintianers worked on nuking them all? There are also a bunch of warnings like which I've only recently learned indicated potential RCU bugs:
/usr/projects/linux/ext4/kernel/trace/trace.c:1885:17: warning: incorrect type in assignment (different address spaces)/usr/projects/linux/ext4/kernel/trace/trace.c:1885:17: expected struct trace_buffer_struct *buffers/usr/projects/linux/ext4/kernel/trace/trace.c:1885:17: got struct trace_buffer_struct [noderef] <asn:3>*<noident>
Quoted 7 lines · Theodore Ts'o
Speaking of bool (and I'm not a fan of it either), is this warning just noise (which is bad enough since it masks real warnings), or is this going to cause serious problems?
CHECK
/usr/projects/linux/ext4/kernel/trace/trace.c/usr/projects/linux/ext4/kernel/trace/trace.c:559:6: warning: symbol 'free_snapshot' was not declared. Should it be static?/usr/projects/linux/ext4/kernel/trace/trace.c:1489:14: warning: expression using sizeof bool
It's just because sparse is being a bit odd. Internally, sparse thinks that "bool" has a size of one bit. So it then the sizeof code has a special case to make it one byte, and when that special case was added, people added the warning too.
I suspect we sparse should just make the size of "bool" be 8 bits internally, and we should drop the warning. As it is, sparse will actually do odd and wrong things due to the "bool is one bit" if you put "bool" types in structures or unions, I think.
Nobody used to care, because we used to not use that broken type in the kernel.
Linus
Quoted 1 line · On Sun, 1 Sep 2013 08:49:48 -0700 Linus Torvalds…
Nobody used to care, because we used to not use that broken type in the kernel.
I've been told that gcc works better with 'bool' than with an int. Should I replace those bools with bit fields in the structure?
-- Steve
Quoted 3 lines · Steven Rostedt
I've been told that gcc works better with 'bool' than with an int. Should I replace those bools with bit fields in the structure?
I think bitfields are a better idea in a struct, yes. They take less space, and there's a possibility to generate better code with a bitfield test than with a bool, especially if you have multiple values and you test them together.
If it's a single bool in a structure, it really doesn't matter. It's going to take up at least a char (and usually more due to padding of other fields) regardless of bool-vs-bitfield issues. But I'd much rather see bitfields in structs because they *can* work better, and they are more flexible than "bool" anyway.
Bools generally should generate the same code as "char", and yes, that can be better than "int". On x86, for example, the "setcc" instruction always sets a char, so if you use an "int" for boolean values, the compiler usually generates something like "xor %eax,%eax; test ..; setne %al" or similar. A bool or a char will skip the "xor", because the "setne" will set the low bits that are sufficient. That said, it's not actually noticeable in practice, and most routines that return true/false will just do plain "return 0" or whatever, so there's no use for "setcc" to begin with.
Linus