diff options
author | Paul Duffin <paulduffin@google.com> | 2015-01-09 13:24:59 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-01-09 13:24:59 +0000 |
commit | 3d9d19c6f7dccc9d8012557a91c38f4c4c86564e (patch) | |
tree | f46c5be8d52e212d97e2636d9eb51de46f566d30 | |
parent | a1743a02ffb897a02210f7b2b6995ea57119d611 (diff) | |
parent | 247c5d28a6219d2f7c4bea37b7d3f6e681869ad4 (diff) | |
download | android_external_guava-3d9d19c6f7dccc9d8012557a91c38f4c4c86564e.tar.gz android_external_guava-3d9d19c6f7dccc9d8012557a91c38f4c4c86564e.tar.bz2 android_external_guava-3d9d19c6f7dccc9d8012557a91c38f4c4c86564e.zip |
am 247c5d28: Merge "Replace usages of Unsafe.compareAndSwap(Int|Long)"
* commit '247c5d28a6219d2f7c4bea37b7d3f6e681869ad4':
Replace usages of Unsafe.compareAndSwap(Int|Long)
-rw-r--r-- | guava/src/com/google/common/cache/Striped64.java | 75 |
1 files changed, 11 insertions, 64 deletions
diff --git a/guava/src/com/google/common/cache/Striped64.java b/guava/src/com/google/common/cache/Striped64.java index e045453..be2f64d 100644 --- a/guava/src/com/google/common/cache/Striped64.java +++ b/guava/src/com/google/common/cache/Striped64.java @@ -12,6 +12,8 @@ package com.google.common.cache; import java.util.Random; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; /** * A package-local class holding common representation and mechanics @@ -97,23 +99,11 @@ abstract class Striped64 extends Number { Cell(long x) { value = x; } final boolean cas(long cmp, long val) { - return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val); - } - - // Unsafe mechanics - private static final sun.misc.Unsafe UNSAFE; - private static final long valueOffset; - static { - try { - UNSAFE = getUnsafe(); - Class<?> ak = Cell.class; - valueOffset = UNSAFE.objectFieldOffset - (ak.getDeclaredField("value")); - } catch (Exception e) { - throw new Error(e); - } + return valueUpdater.compareAndSet(this, cmp, value); } + private static final AtomicLongFieldUpdater<Cell> valueUpdater = + AtomicLongFieldUpdater.newUpdater(Cell.class, "value"); } /** @@ -173,14 +163,14 @@ abstract class Striped64 extends Number { * CASes the base field. */ final boolean casBase(long cmp, long val) { - return UNSAFE.compareAndSwapLong(this, baseOffset, cmp, val); + return baseUpdater.compareAndSet(this, cmp, val); } /** * CASes the busy field from 0 to 1 to acquire lock. */ final boolean casBusy() { - return UNSAFE.compareAndSwapInt(this, busyOffset, 0, 1); + return busyUpdater.compareAndSet(this, 0, 1); } /** @@ -297,51 +287,8 @@ abstract class Striped64 extends Number { } } - // Unsafe mechanics - private static final sun.misc.Unsafe UNSAFE; - private static final long baseOffset; - private static final long busyOffset; - static { - try { - UNSAFE = getUnsafe(); - Class<?> sk = Striped64.class; - baseOffset = UNSAFE.objectFieldOffset - (sk.getDeclaredField("base")); - busyOffset = UNSAFE.objectFieldOffset - (sk.getDeclaredField("busy")); - } catch (Exception e) { - throw new Error(e); - } - } - - /** - * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. - * Replace with a simple call to Unsafe.getUnsafe when integrating - * into a jdk. - * - * @return a sun.misc.Unsafe - */ - private static sun.misc.Unsafe getUnsafe() { - try { - return sun.misc.Unsafe.getUnsafe(); - } catch (SecurityException tryReflectionInstead) {} - try { - return java.security.AccessController.doPrivileged - (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { - public sun.misc.Unsafe run() throws Exception { - Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; - for (java.lang.reflect.Field f : k.getDeclaredFields()) { - f.setAccessible(true); - Object x = f.get(null); - if (k.isInstance(x)) - return k.cast(x); - } - throw new NoSuchFieldError("the Unsafe"); - }}); - } catch (java.security.PrivilegedActionException e) { - throw new RuntimeException("Could not initialize intrinsics", - e.getCause()); - } - } - + private static final AtomicLongFieldUpdater<Striped64> baseUpdater = + AtomicLongFieldUpdater.newUpdater(Striped64.class, "base"); + private static final AtomicIntegerFieldUpdater<Striped64> busyUpdater = + AtomicIntegerFieldUpdater.newUpdater(Striped64.class, "busy"); } |