Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java Memory Model Sequential Consistency different from Leslie Lamport Definition?

JLS-17.4.3 defines the program order per thread where any reordering within the program for the thread would preserve intra-thread semantics. And then it defines sequential consistency in terms of the program-order. It says that the sequential consistency is a total order of all actions consistent with the program order.

Now I have this question where the program order is defined per thread and sequential consistency is defined across all threads. Doesn't this violates the sequential consistency defined by Leslie Lapmort ? -

the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program.

For an example what if the compiler reorders store and loads looking at the code for a particular thread (The program order of the thread)


Edited : Following section pertains to this session from from youtube.

Java Memory Model Pragmatics - 48th min

First sample shows two executions by two threads. The order of each execution preserves intra-thread semantics. The next sample shows the same set of executions but the execution on the left side has reordered it's actions for the Thread-1.

Now if we just think of the program order of Thread-1, the reordering is legal. But what the presenter says is that it violates the sequential consistency. But when I read the JLS, I get the impression that the reordered execution is valid and preserves sequential consistency because of having two legal program orders. Am I wrong on this and if so could you please explain what's wrong with that reasoning ?

int a=0, int b=0;

Thread - 1     Thread - 2 
----------     -----------

r1 = a;         b=2;
r2 = b;         a=1;

After reordering

int a=0, int b=0;

Thread - 1     Thread - 2
----------     ----------
r2 = b;          
                    b=2;
                    a=1; 

r1 = a;        
like image 905
psaw.mora Avatar asked Sep 03 '26 11:09

psaw.mora


1 Answers

I don't think the definitions are inconsistent. I think they are (just) stated in different ways.

But this is moot for the Java Memory Model because of the following caveat at the end of JLS 7.4.3

"If we were to use sequential consistency as our memory model, many of the compiler and processor optimizations that we have discussed would be illegal. For example, in the trace in Table 17.3, as soon as the write of 3 to p.x occurred, subsequent reads of that location would be required to see that value."

In other words, the JMM does not use Sequential Consistency as its basis.


Regarding that example in the video. What he is saying is the following (my comments in italics):

  • SC is easier for the programmer to understand. His opinion, but probably true.
  • The example violates SC. True, but the JMM doesn't guarantee SC anyway. Indeed, the JLS itself has an example of a "surprising result" that is due to the JMM not guaranteeing SC!
  • Someone should submit a JEP for SC in the JMM. Debatable whether they should, but they certainly could.
  • Actually analyzing potential optimizations to see if they violate SC is hard. Which is maybe a good reason for the JMM to not guarantee SC. If fewer optimizations are sound with SC versus the existing JMM, then SC is liable to make JIT compiled code slower in some cases.

AFAIK, he is saying nothing that is contentious from a technical perspective.

like image 175
Stephen C Avatar answered Sep 05 '26 01:09

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!