From 75b00c56951b33e251c6c64655109d188fb10349 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Tue, 6 Jan 2015 16:21:47 +0000 Subject: Replace usages of Unsafe.compareAndSwap(Int|Long) This replaces them with Atomic(Integer|Long)FieldUpdater classes respectively in Striped64 to allow it to be compiled for Android. Fixes a build error on Android. Change-Id: I0a8ac53d6063e89e35dac5d4a2be5bfa6823cb35 --- guava/src/com/google/common/cache/Striped64.java | 75 ++++-------------------- 1 file 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 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() { - public sun.misc.Unsafe run() throws Exception { - Class 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 baseUpdater = + AtomicLongFieldUpdater.newUpdater(Striped64.class, "base"); + private static final AtomicIntegerFieldUpdater busyUpdater = + AtomicIntegerFieldUpdater.newUpdater(Striped64.class, "busy"); } -- cgit v1.2.3