Betrayed by a bitfield
Betrayed by a bitfield
Posted Feb 7, 2012 0:48 UTC (Tue) by daglwn (guest, #65432)In reply to: Betrayed by a bitfield by dlang
Parent article: Betrayed by a bitfield
No, it's not. Believe me.
The volatile keyword doesn't say anything about when it will change value, be read/written etc. It says simply that it will not be cached in a register such that every read will get the "latest" value expected when executed under the Abstract Machine.
It says nothing about threading.
It says nothing about interrupts.
Simply remember that volatile is not magic. Think of it as the opposite of "register."
Posted Feb 7, 2012 1:03 UTC (Tue)
by dlang (guest, #313)
[Link]
since this sort of thing has been part of C's traditional strength, this doesn't seem like a sane interpretation to me.
Posted Feb 7, 2012 16:54 UTC (Tue)
by chrisV (guest, #43417)
[Link] (6 responses)
I think it does. See §5.1.2.3/5 and /10, which are normative. The principal purpose of volatile is to deal with arbitrary changes of data values outside the program context of the process in which the code is running (ie asynchronous interrupts). (This does not include threads, which are within the program context and which, because they can run on more than one core, require quite different synchronizations, some of which are not async-signal-safe.)
See also the footnote 134 of §6.7.3/8 (which is non-normative despite the "shall not"): "A volatile declaration may be used to describe an object corresponding to a memory-mapped input/output port or an object accessed by an asynchronously interrupting function. Actions on objects so declared shall not be 'optimized out' by an implementation or reordered except as permitted by the rules for evaluating expressions." This is a curious note as, as far as I am aware, it is the one and only reference to memory mapping (and about which I mis-spoke in an earlier posting on this article because it is not in C99 which contains no reference to memory mapping).
Posted Feb 7, 2012 18:38 UTC (Tue)
by daglwn (guest, #65432)
[Link] (5 responses)
Thanks for the correction. But the compiler is still correct here. Volatile doesn't say anything about restricting _when_ it is read or written, only that it will get the "latest" value in a single-thread context.
It's perfectly fine for I/O as long as you can guarantee alignment such that there is no "false sharing."
Posted Feb 7, 2012 19:07 UTC (Tue)
by chrisV (guest, #43417)
[Link] (4 responses)
Posted Feb 7, 2012 20:01 UTC (Tue)
by dlang (guest, #313)
[Link]
Posted Feb 7, 2012 23:32 UTC (Tue)
by daglwn (guest, #65432)
[Link] (1 responses)
No, there isn't.
There isn't. Really.
Volatile does not mean what you think it means.
It's a bit like sequential consistency. Just when you think you understand it, something unexpected happens that is both non-intuitive and perfectly legal.
Posted Feb 8, 2012 15:24 UTC (Wed)
by daglwn (guest, #65432)
[Link]
Posted Feb 8, 2012 13:51 UTC (Wed)
by nix (subscriber, #2304)
[Link]
Betrayed by a bitfield
Betrayed by a bitfield
Betrayed by a bitfield
Betrayed by a bitfield
Betrayed by a bitfield
Betrayed by a bitfield
> where the struct is marked volatile, there is a compiler bug.
Betrayed by a bitfield
Betrayed by a bitfield
