JavaMemoryModel: Performing speculative writes ahead of loops that may not terminate

From: Bill Pugh (
Date: Fri May 28 2004 - 10:51:13 EDT

OK, the following issue came up in some discussions:

Say you have a thread:

while (...) {
x = 1

Say the compiler is able to prove that the while loop
cannot possibly throw an exception and that the x is not
read during execution of the while loop.

However, the compiler is not able to prove
that the while loop terminates.

Would any compilers decide that although the loop
may not terminate, it is legal to perform the write
x=1 before the loop, allowing other threads to see
the write?

The current JMM spec implies that x=1 must not be observed
if the loop doesn't terminate (it has implied this
for a long time; this isn't a recent change). And almost
everyone in the discussion is absolutely convinced that this is
the right answer.

However, there is some question as to whether or not
there might be any compilers that would violate this.

Does anyone know if any compiler analysis or transformation
might make this transformation? For example, if an analysis
assumed "the only way to exit this loop is to take this
branch, so this branch must be taken in all executions",
then it might allow such a transformation.

I'd rather not start a discussion on whether or not this is the
desirable semantics, since several expert group members are
convinced it is. But, if you insist, ...


JavaMemoryModel mailing list -

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