title: Atomically modify int value
body: <p>Googling for ABA-problem that may appear when using <code>CompareAndSwap</code>-like <code>Read-Modify-Write</code> instruction set I found <code>Load-Linked/Store-Conditional</code> <strong><em>lock-free</em></strong> approach to that problem. </p>
  
  <p>So, I tried to implement <code>LL/SC</code> for ints using just <code>CAS</code>-set of instruction.</p>
  
  <pre><code>public class LlscInteger {
      private static final Unsafe U = getUnsafe();
      private static final long VALUE_OFFSET;
      private final Map&lt;Integer, Integer&gt; linkTracker;
      private volatile int value;
  
      public int loadLinked(){
          return value;
      }
  
      public boolean storeConditional(int newVal){
          //...
      }
  
      //Unsafe mechanics
  }
  </code></pre>
  
  <p>The problem is I cannot update <code>value</code> and <code>linkTracker</code> atomically. Looking at C++11 <code>std::atomic</code> I also didn't find something similar to <code>LL/SC</code>. <code>sun.misc.Unsafe</code> does not contain <code>LL/SC</code> either.</p>
  
  <p><strong><em>QUESTION:</em></strong> Is it possible to implement <code>LL/SC</code> via <code>CAS</code>? If so, can you give me some advice?</p>
  
  <p>I don't see how LL/SC would be possible to implement just using <code>volatile</code>. The store conditional would like like the following:</p>
  
  <pre><code>public boolean storeConditional(int newVal){
      if(//the value is not changed since the loadLinked call){
          value = newValue
          return true;
      } else
          return false;
  }
  </code></pre>