Declaring it volatile
Declaring it volatile
Posted Aug 6, 2012 17:04 UTC (Mon) by PaulMcKenney (✭ supporter ✭, #9624)In reply to: Declaring it volatile by daglwn
Parent article: ACCESS_ONCE()
Posted Aug 6, 2012 17:23 UTC (Mon)
by daglwn (guest, #65432)
[Link] (15 responses)
The use of a cast is fine.
I'm suggesting the variable be declared volatile for safety reasons and temporaries be used to explicitly avoid pessimizing code when developers know it is safe. That way things are safe by default and explicitly and obviously optimized when possible.
Posted Aug 6, 2012 17:24 UTC (Mon)
by daglwn (guest, #65432)
[Link] (14 responses)
Posted Aug 6, 2012 18:16 UTC (Mon)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (13 responses)
Yes, you can in principle use manually coded temporaries to enable compiler optimizations in cases where a lock is held that prevents changes to the variable being loaded, but it is not at all clear to me why this would be a better solution than using ACCESS_ONCE(), especially in the Linux kernel.
Posted Aug 6, 2012 20:49 UTC (Mon)
by daglwn (guest, #65432)
[Link] (5 responses)
Safety and explicitness.
Posted Aug 6, 2012 23:12 UTC (Mon)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (4 responses)
Posted Aug 7, 2012 3:42 UTC (Tue)
by daglwn (guest, #65432)
[Link] (3 responses)
Posted Aug 7, 2012 14:37 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (2 responses)
Posted Aug 7, 2012 19:44 UTC (Tue)
by daglwn (guest, #65432)
[Link] (1 responses)
I'm not trying to convince you, simply stating what I believe is good programming practice. Maybe I'm wrong. But it's worked for me so far. :)
Posted Aug 8, 2012 15:26 UTC (Wed)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link]
Posted Aug 7, 2012 5:52 UTC (Tue)
by viro (subscriber, #7872)
[Link] (6 responses)
Posted Aug 7, 2012 15:24 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (5 responses)
And yes, gcc also seems quite happy to leak types from "({...})":
Yow!!!
Fortunately for me, ACCESS_ONCE() does not depend on the semantics of typeof() applied to either "({...})"; or bitfields, although only because it is illegal to take the address of either of them. ;-)
Posted Aug 7, 2012 15:35 UTC (Tue)
by nybble41 (subscriber, #55106)
[Link] (4 responses)
Are you sure about the first case? It seemed to work for me in GCC 4.5.1, at least in the case where the result of the "({...})" is a non-local l-value:
int x;
Posted Aug 7, 2012 15:57 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (3 responses)
Posted Aug 7, 2012 18:35 UTC (Tue)
by nybble41 (subscriber, #55106)
[Link] (2 responses)
Is there any particular reason the statement expression can't be an l-value, provided the final form is an l-value, or was that case simply considered too error-prone?
Posted Aug 7, 2012 21:12 UTC (Tue)
by paulj (subscriber, #341)
[Link] (1 responses)
Posted Aug 7, 2012 21:26 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link]
Declaring it volatile
Declaring it volatile
It does sound like it just might be well past time for typeof() in the C/C++ standards. ;-) That said, both gcc and llvm seem to support typeof(), and I bet a lot of other compilers do as well, either directly or indirectly. But perhaps the Linux kernel will eventually move from typeof() to C++11 decltype(), once the commonly-used compiler versions support it.
Declaring it volatile
Declaring it volatile
> compiler optimizations in cases where a lock is held that prevents changes
> to the variable being loaded, but it is not at all clear to me why this
> would be a better solution than using ACCESS_ONCE()
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
IIRC, there had been other places where it didn't play well with very unexpected parts of standard (e.g. bitfield signedness; does special treatment of signed int in there extend to e.g. typeof(signed int)? Taken a few steps further it can, unless you are very careful, extend the distinction between int and signed int through all the type system...)
I must admit that I have never used "typeof(({ printf("From typeof()\n"); j; })) k = 1;" to declare a variable "k" of the same type as "j", but gcc appears quite happy with it.
Declaring it volatile
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = argc;
signed int j = i;
typeof(({ struct foo { int a; } q = { 1 }; printf("From typeof()\n"); q; })) k = { 2 };
printf("i = %d, j = %d, k.a = %d\n", i, j, k.a);
return 0;
}
Declaring it volatile
int *f(void)
{
return &({ x; });
}
I am using gcc 4.6.1, and the following emits a compiler error complaining that an lvalue is needed:
Declaring it volatile
#include <stdio.h>
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
int q = 3;
int main(int argc, char *argv[])
{
int i = argc;
i = ACCESS_ONCE(({ printk("foo"); q; }));
printk("i = %d\n", i);
}
Declaring it volatile
Declaring it volatile
Declaring it volatile