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

timo.kinnunen at gmail.com timo.kinnunen at gmail.com
Fri Sep 4 00:27:21 EDT 2015


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 for Windows 10
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150904/4d18c636/attachment.html>


More information about the Concurrency-interest mailing list