[jmm-dev] easily observable examples of non-sc behaviours in Java

Brian Demsky bdemsky at uci.edu
Thu Jan 15 19:15:04 UTC 2015


On a Mac, I can confirm that this optimization is observable on Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode).

Brian


> On Jan 15, 2015, at 11:07 AM, Hans Boehm <boehm at acm.org> wrote:
> 
> A similar, though somewhat simpler and more standard, example seems to be
> 
> while (!flag) {}
> 
> getting optimized to tmp = flag; while(!tmp) {}
> 
> I have no idea which JVMs currently do that, but it seems to me that they
> should.
> 
> On Thu, Jan 15, 2015 at 9:31 AM, Francesco Zappa Nardelli <
> francesco.zappa_nardelli at inria.fr> wrote:
> 
>> Dear all
>> 
>> for teaching purposes, I am looking for snippets of code that, when run,
>> exhibit some non-sc behaviours allowed by the Java memory model.
>> 
>> I know Jaroslav examples (eg. those in
>> http://groups.inf.ed.ac.uk/request/jmmexamples.pdf  or
>> http://www.inf.ed.ac.uk/publications/online/1252.pdf ) but in those
>> examples the non-sc behaviours can be spotted only by analysing the
>> generated assembly code (looking at the bytecode is not enough as it is the
>> JIT that performs the “surprising” optimisation), and the sc-behaviours are
>> not practically observable executing the code.
>> 
>> I can e.g. show that Dekker mutual exclusion algorithm using
>> non-volatile/atomic variables (as implemented in:
>> http://tinyurl.com/qylvjfl ) , can deadlock; my guess is that
>> 
>>  if (turn==other) while (turn==other) ;
>> 
>> is JIT optimised into
>> 
>>  if (turn==other) while (true) ;
>> 
>> but I cannot write a simpler snippet of code that makes this observable.
>> 
>> Does any of you have suggestions or snippets of code that highlighlits
>> non-sc behaviours for racy Java programs (possibly on a recent HotSpot VM)?
>> 
>> Thank you in advance.
>> 
>> Best
>> -francesco
> 



More information about the jmm-dev mailing list