[concurrency-interest] Transformation of multiple volatile reads without intervening writes

Nathan Reynolds nathan.reynolds at oracle.com
Fri Sep 4 13:06:04 EDT 2015


Maybe everyone is still chewing on this.  One way to show if the 
compiler can transform the code is by running it and looking at the 
disassembly.  If the disassembly shows the transformation, then it is 
proven.  If not, then you may have to tweak the code until you figure 
out which line of code is preventing the compiler from doing the 
transformation.

-Nathan

On 9/3/2015 9:27 PM, timo.kinnunen at gmail.com wrote:
>
> Hi,
>
> Hopefully an easy question about permitted transformations.
>
> Initially, q.a == q, q.x == 0, p == q and npe == null. Variables p and 
> q.a are volatile. Thread 1 is reading and threads 2 and 3 could 
> interfere with this with their writes.
>
> The code for thread 1 is:
>
>                 public static void thread1() {
>
>                                 REG r1 = p;
>
>                                 if(r1 == null) return;
>
>                                 REG r2 = p;
>
>                                 if(r2 == null) throw npe;
>
>                                 REG r3 = r2.a;
>
>                                 if(r3 == null) return;
>
>                                 REG r4 = p;
>
>                                 if(r4 == null) throw npe;
>
>                                 REG r5 = r4.a;
>
>                                 if(r5 == null) throw npe;
>
>                                 r5.x = 1;
>
>                 }
>
> The code for threads 2 and 3 is:
>
>                 public static void thread2() {
>
>                                 REG r6 = q;
>
>                                 r6.a = null;
>
>                                 r6.a = r6;
>
>                 }
>
>                 public static void thread3() {
>
>                                 REG r7 = q;
>
>                                 p = null;
>
>                                 p = r7;
>
>                 }
>
> Is a compiler permitted to transform thread 1’s code to this:
>
>                 public static void thread1b() {
>
>                                 REG r4 = p;
>
>                                 if(r4 == null) return;
>
>                                 REG r5 = r4.a;
>
>                                 if(r5 == null) return;
>
>                                 r5.x = 1;
>
>                 }
>
> Because the volatile reads r1,r2,r4 do not synchronize-with reads 
> r3,r5 or each other r2,r4 could be grouped next to r1. Reads r1 and r2 
> then become unnecessary synchronization that the compiler can optimize 
> away, along with some now dead code. The compiler repeats this same 
> optimization with r5, which gives the final transformed code.
>
> Would this be a valid compiler transformation according to the JMM?
>
> Sent from Mail <http://go.microsoft.com/fwlink/?LinkId=550986> for 
> Windows 10
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150904/9e2956d9/attachment.html>


More information about the Concurrency-interest mailing list