From GNU document about volatile
:
The minimum requirement is that at a sequence point all previous accesses to volatile objects have stabilized and no subsequent accesses have occurred
Ok, so we know what sequence points are, and we now know how volatile behaves with respect to them in gcc.
So, naively I would look at the following program:
volatile int x = 0;
int y = 0;
x = 1; /* sequence point at the end of the assignment */
y = 1; /* sequence point at the end of the assignment */
x = 2; /* sequence point at the end of the assignment */
And will apply the GNU requirement the following way:
At a sequence point (end of y=1
) access to volatile x = 1
stabilize and no subsequence access x = 2
have occurred.
But that just wrong because non-volatiles y = 1
can be reordered across sequence points, for example y = 1
can actually be performed before x = 1
and x = 2
, and furthermore it can be optimised away (without violating the as-if rule).
So I am very eager to know how can I apply the GNU requirement properly, is there a problem with my understanding? is the requirement written in a wrong way?
Maybe should the requirement be written as something like:
The minimum requirement is that at a sequence point WHICH HAS A SIDE EFFECT all previous accesses to volatile objects have stabilized and no subsequent accesses have occurre
Or as pmg elegantly suggested in the comment:
The minimum requirement is that at a sequence point all UNSEQUENCED previous accesses to volatile objects have stabilized and no subsequent accesses have occurred
so we could only apply it on the sequence points of end of x = 1;
and end of x = 2;
on which is definitely true that previous accesses to volatile objects have stabilized and no subsequent accesses have occurred?