hg: lambda/lambda/jdk: Summary: add ThreadLocal(Factory<T>) and compareAndSet methods

Jim Gish jim.gish at oracle.com
Tue Jul 31 08:09:44 PDT 2012


Does this look ok for the AtomicReference change?  (other than the name 
of course):

--- 
old/src/share/classes/java/util/concurrent/atomic/AtomicReference.java 
2012-07-27 13:56:45.023309117 -0400
+++ 
new/src/share/classes/java/util/concurrent/atomic/AtomicReference.java 
2012-07-27 13:56:44.883309121 -0400
@@ -34,6 +34,7 @@
   */

  package java.util.concurrent.atomic;
+import java.util.functions.UnaryOperator;
  import sun.misc.Unsafe;

  /**
@@ -115,6 +116,25 @@
      }

      /**
+     * Sets the value by applying the supplied operator to the old value,
+     * and updating it atomically as in {@link #compareAndSet(V, V)}.
+     *
+     * If the update results in a collision, we back off and retry the 
update
+     * (using the new current value) until successful.
+     *
+     * @param op the operator to use to compute the new value
+     * @return the new value
+     */
+    public final V compareAndSet(UnaryOperator<V> op) {
+        V oldValue, newValue;
+        do {
+            oldValue = get();
+            newValue = op.operate(oldValue);
+        } while (!compareAndSet(oldValue, newValue));
+        return newValue;
+    }
+
+    /**
       * Atomically sets the value to the given updated value
       * if the current value {@code ==} the expected value.
       *

On 07/31/2012 06:35 AM, Aleksey Shipilev wrote:
> Thanks Jim!
>
> Please make sure you have both versions in place (i.e.
> updateAndGet/getAndUpdate). Also, while you are at it, can you also add
> the updaters for AtomicReference and friends?
>
> -Aleksey.
>
> On 07/30/2012 07:23 PM, Jim Gish wrote:
>> That's it Mike -- pass the buck :-)  If we're agreed on the names, I'll
>> be happy to change them.  Any other comments before I do that?
>>
>> Thanks,
>>     Jim
>>
>> On 07/30/2012 11:07 AM, Mike Duigou wrote:
>>> I like your name suggestions. I too was uncomfortable with the
>>> "compare" naming but didn't have good suggestions. At this point the
>>> naming is definitely not final but only exploratory.
>>>
>>> This work was done by Jim Gish, a new OpenJDK contributor. I am only
>>> responsible for helping with a pre-commit review and pushing the
>>> change to the repo.
>>>
>>> Mike
>>>
>>>
>>> On Jul 30 2012, at 04:16 , Aleksey Shipilev wrote:
>>>
>>>> I think these method names are bad:
>>>>    int compareAndSet(IntUnaryOperator op);
>>>>
>>>> Admittedly, these are not the CAS-family operations. Compare with what?
>>>> Set to what? Is this method one-shot, or it runs until successful
>>>> (method name implies it is one-shot, but JavaDoc says otherwise)?
>>>>
>>>> I think then names should be coherent with other high-level methods,
>>>> like incrementAndGet, etc., i.e.:
>>>>    int updateAndGet(IntUnaryOperator updater);
>>>>    int getAndUpdate(IntUnaryOperator updater);
>>>>
>>>> -Aleksey.
>>>>
>>>> On 07/27/2012 11:29 PM, mike.duigou at oracle.com wrote:
>>>>> Changeset: 2fd2a7092fe1
>>>>> Author:    jgish
>>>>> Date:      2012-07-27 15:04 -0400
>>>>> URL:
>>>>> http://hg.openjdk.java.net/lambda/lambda/jdk/rev/2fd2a7092fe1
>>>>>
>>>>> Summary: add ThreadLocal(Factory<T>) and compareAndSet methods
>>>>> Reviewed-by: briangoetz,smarks,mduigo
>>>>> Contributed-by: Jim Gish <jim.gish at oracle.com>
>>>>>
>>>>> ! make/java/java/FILES_java.gmk
>>>>> ! src/share/classes/java/lang/ThreadLocal.java
>>>>> ! src/share/classes/java/util/concurrent/atomic/AtomicInteger.java
>>>>> ! src/share/classes/java/util/concurrent/atomic/AtomicLong.java
>>>>> + src/share/classes/java/util/functions/DoubleUnaryOperator.java
>>>>> + src/share/classes/java/util/functions/IntUnaryOperator.java
>>>>> + src/share/classes/java/util/functions/LongUnaryOperator.java
>>>>> + src/share/classes/java/util/functions/UnaryOperator.java
>>>>> + test-ng/tests/org/openjdk/tests/java/lang/ThreadLocalFactoryTest.java
>>>>> + test-ng/tests/org/openjdk/tests/java/lang/ThreadLocalTest.java
>>>>> +
>>>>> test-ng/tests/org/openjdk/tests/java/util/functions/DoubleUnaryOperatorTest.java
>>>>>
>>>>> +
>>>>> test-ng/tests/org/openjdk/tests/java/util/functions/IntUnaryOperatorTest.java
>>>>>
>>>>> +
>>>>> test-ng/tests/org/openjdk/tests/java/util/functions/LongUnaryOperatorTest.java
>>>>>
>>>>> +
>>>>> test-ng/tests/org/openjdk/tests/java/util/functions/UnaryOperatorTest.java
>>>>>
>>>>> ! test/java/lang/ThreadLocal/Basic.java
>>>>> ! test/java/lang/ThreadLocal/ImmutableLocal.java
>>>>> ! test/java/lang/ThreadLocal/InitialValue.java
>>>>> ! test/java/lang/ThreadLocal/TLRemoveTest.java
>>>>> ! test/java/lang/ThreadLocal/TestThreadId.java
>>>>>
>>>>>

-- 
Jim Gish | Consulting Member of Technical Staff | +1.781.442.0304
Oracle Java Platform Group | Core Libraries Team
35 Network Drive
Burlington, MA 01803
jim.gish at oracle.com



More information about the lambda-dev mailing list