Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release-Only form of CAS

I have accidentally run into Striped64.java class from Kamon Monitoring tool. At line 95 I found this comment:

JVM intrinsics note: It would be possible to use a release-only
form of CAS here, if it were provided.

Although I understand what CAS is, I am unable to find out what a release-only form of CAS is. Could someone shed some light on this?

like image 756
Vladimir G. Avatar asked Feb 01 '17 12:02

Vladimir G.


2 Answers

I am unable to find out what a release-only form of CAS is.

That refers to memory ordering constraints on atomics in the terms of the C++ memory model. Some of those are not fully expressible in the terms of the java memory model which predates the C++ one (see also the JMM cookbook for developers) and thus they are currently not made available through the standard library.

This will change with Java9 where varhandles will expose memory accesses that match C++ semantics, except consume order.

Also note that the java class you linked has been copied from the jsr166 repository, which is the upstream version of the JDK j.u.c. packages.

like image 177
the8472 Avatar answered Sep 30 '22 13:09

the8472


Normally for a volatile write you will get the following barriers

[StoreStore]
[LoadStore]
x=10
[StoreLoad]

The StoreLoad has 2 purposes:

  • it prevents newer loads to float above x=10.
  • it makes all changes that happend before 'x=10' visible on other processors.

The StoreLoad is potentially expensive since it waits for the store buffer to get drained.

The above approach, in combination with the following for the read of X

tmp=x
[LoadLoad]
[LoadStore]

Will make sure that accessing a volatile variable is sequential consistent.

A release only form would look like this

[StoreStore]
[LoadStore]
x=10

As you can see the [StoreLoad] barrier is gone.

On the reading side, it is the same as a regular volatile read.

I don't know how 'release-only form of CAS' fits into the picture

And like the8472 already indicated, the new VarHandles in Java 9 will expose the relaxed 'acquire/release' ordering guarantees.

like image 20
pveentjer Avatar answered Sep 30 '22 14:09

pveentjer