JavaMemoryModel: Question about the semantics of volatile

From: Thomas Wang (wang@cup.hp.com)
Date: Wed Mar 17 2004 - 18:01:00 EST


> Initially, x = y = v = 0.
> v is a volatile variable.
>
> Thread 1:
> r1 = x
> v = 0
> r2 = v
> y = 1
>
> Thread 2:
> r3 = y
> v = 0
> r4 = v
> x = 1
>
> Is the behavior r1 == r3 == 1 possible?

I think r1 == r3 == 1 being impossible is more intuitive.

I also believe volatile variables preventing compile time code motion is
a good idea, for the same reason. However, I also like to emit volatile
loads and writes as ld.acq and st.rel (half fence) at run time.

I know our C compiler emits volatile load & stores with ld.acq and st.rel.
Using the full fence can be orders of magnitude slower than the half fence.
This is important if you want to use volatiles to implement a spin lock
in Java. (assuming compare & exchange is available through some mechanism)

Thomas Wang

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



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