0

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?

izac89
  • 3,790
  • 7
  • 30
  • 46
  • `x=1;/*access to x stabilized*/y=1;/*no worries about x, even with optimization or reordering*/` – pmg Jan 17 '19 at 09:12
  • 1
    means that requirement should be changed to something like `The minimum requirement is that at a sequence point AT THE END OF THE VOLATILE EXPRESSION all previous accesses to volatile objects have stabilized and no subsequent accesses have occurred` ? – izac89 Jan 17 '19 at 09:14
  • there is no need to distinguish sequence point types. If you want to change the paragraph, try "... at a sequence point all *unsequenced* previous accesses ..." – pmg Jan 17 '19 at 09:18
  • so you agree the current paragraph is wrongly wirrten? – izac89 Jan 17 '19 at 09:18
  • I think the paragraph is ok as it is in gnu documentation :) – pmg Jan 17 '19 at 09:20
  • Do you understand the purpose of `volatile` in C/C++? – curiousguy Jan 17 '19 at 17:28
  • @curiousguy I believe I do. The question here is about a specific requirement which I feel to be written wrongly. – izac89 Jan 17 '19 at 17:35
  • @user2162550 `volatile` makes something observable. The abstract machine, by definition, isn't observable. Ergo the question about non volatile writes doesn't make sense: the as if rules says that nothing in the abstract machine needs to be translated sequence point by sequence point. – curiousguy Jan 17 '19 at 21:45

0 Answers0