# Re: JavaMemoryModel: Use-case for strong volatile

From: Sylvia Else (sylviae@optushome.com.au)
Date: Sun Apr 11 2004 - 01:18:58 EDT

At 08:41 AM 8/04/2004 -0400, Bill Pugh wrote:
>I'll admit that I am conflicted with regard to the strong vs. weak
>interpretation. If we had really worked this out 5 months ago, I'd be much
>more open to considering a change.
>
>The problem is that the strong interpretation has been documented in both
>the community review document and the public review document. There have
>also been previous postings in which coding idioms where recommended that
>only work under the strong interpretation (for the optimistic readers
>pattern, which deliberating uses data races and detects and recovers when
>a data race occurs).

Thanks to David and Jeremy for providing the link that I had unaccountably
failed to find.

This is a section of example code from Rob Strom's posting back in 2000.

volatile v;
/* Assume for this example that
the first write has already begun, with v=1, a=any, b=any */
...
a = 2;
b = 3;
v++;
... /* other operations between first and second write */
v++;
a=5;
b=7;
v++;

t1 = v;
x = a;
y = b;
r = a*b;
t2 = v;
if (t1 == t2 && t1%2 == 0) return(r);
else /* redo */

I'm trying to understand how this works, or is modified to work, under the
new memory model.

If we assume that the reader thread has set t1 = 2, by the assignment t1 =
v, then
a = 2 hb t1 = 2 and
b = 3 hb t1 = 2

Now assume that the reader thread later sets t2 = 2, by the assignment t2 =
v. We expect to find that x = 2, y = 3, but to show that that is true we need

x = a hb a = 5 and
y = b hb b = 6

At some time, the writer thread will set v = 3 by v++ and
v = 3 hb a = 5 and
v = 3 hb b = 6

Since they occur in the same thread, it's obvious that
x = a hb t2 = 2
y = b hb t2 = 2

so all we need is t2 = 2 hb v = 3, which seems reasonable since the read
obviously precedes the write in the synchronization order. But as I
understand it, this is not sufficient to create a hb edge. In particular,
there is no statement of the existence of a happens-before edge between a
read of a volatile variable and every subsequent write of that variable.

At the time of Rob's posting, it seemed that adding an additional volatile
read of v after the write would solve the problem but I cannot see that it
does now.

My interest in this came from the suggestion that this pattern breaks under
the weak interpretation, but at the moment I don't see how it works under
the strong interpretation either.

What have I missed?

Sylvia.

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:03 EDT