Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this instruction reordering allowed by the JLS or not?

According to the Java Language Specification (Example 17.4-1) the following snippet (starting in A == B == 0)...

Thread 1             Thread 2
--------             --------
r2 = A;              r1 = B;
B = 1;               A = 2;

... can result in r2 == 2 and r1 == 1. This is because the result of executing B = 1; does not depend on whether or not r2 = A has been executed, thus the JVM is free to swap the order of the execution of those two instructions. In other words, the following interleaving is allowed by the spec:

Thread 1             Thread 2
--------             --------
B = 1;
                     r1 = B;
                     A = 2;
r2 = A;

which clearly results in r2 == 1 and r1 == 1.

My question:

Suppose we tweak the example a little:

Thread 1             Thread 2
--------             --------
r2 = A;              r1 = B;
monitorenter obj     monitorenter obj
monitorexit obj      monitorexit obj
B = 1;               A = 2;

where obj is a reference shared between the threads.

Is the reordering of r2 = A and B = 1 still allowed?


The JLS says...

However, compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation.

...which sort of indicates that the instructions may still be swapped. On the other hand, the following statement

An unlock on a monitor happens-before every subsequent lock on that monitor.

indicates that under certain schedulings we may have a happens-before relation between the statements in the two threads, which presumably disallows instruction reordering.

like image 864
aioobe Avatar asked Jan 03 '13 20:01

aioobe


1 Answers

Informally, it is not allowed. This is known as the "roach motel model"

http://jeremymanson.blogspot.com/2007/05/roach-motels-and-java-memory-model.html

Particularly, an action cannot be moved across a synchronization block.

However, formally, JMM does not speak in terms of reordering. In your example, we can only reason that,

  1. either the sync block 1 is before the sync block 2 in the total synchronization order, therefore r2=A happens-before A=2;r2 must be 0. But there are no constraints between B=1 and r1=B; r1 can be 0 or 1.

  2. or the other way around. r1 must be 0, r2 can be 0 or 2.

So the program still contains data race; nontheless, we can reason that (r1,r2) can only be (0,0), (1,0), (0,2); it is impossible to be (1,2)

like image 124
irreputable Avatar answered Sep 19 '22 05:09

irreputable