|
9 | 9 | package io.prometheus.client; |
10 | 10 |
|
11 | 11 | import java.util.Random; |
| 12 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 13 | +import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
12 | 14 |
|
13 | 15 | /** |
14 | 16 | * A package-local class holding common representation and mechanics |
@@ -94,22 +96,10 @@ static final class Cell { |
94 | 96 | Cell(long x) { value = x; } |
95 | 97 |
|
96 | 98 | final boolean cas(long cmp, long val) { |
97 | | - return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val); |
| 99 | + return CAS_VALUE.compareAndSet(this, cmp, val); |
98 | 100 | } |
99 | 101 |
|
100 | | - // Unsafe mechanics |
101 | | - private static final sun.misc.Unsafe UNSAFE; |
102 | | - private static final long valueOffset; |
103 | | - static { |
104 | | - try { |
105 | | - UNSAFE = getUnsafe(); |
106 | | - Class<?> ak = Cell.class; |
107 | | - valueOffset = UNSAFE.objectFieldOffset |
108 | | - (ak.getDeclaredField("value")); |
109 | | - } catch (Exception e) { |
110 | | - throw new Error(e); |
111 | | - } |
112 | | - } |
| 102 | + private static final AtomicLongFieldUpdater<Cell> CAS_VALUE = AtomicLongFieldUpdater.newUpdater(Cell.class, "value"); |
113 | 103 |
|
114 | 104 | } |
115 | 105 |
|
@@ -155,14 +145,14 @@ final boolean cas(long cmp, long val) { |
155 | 145 | * CASes the base field. |
156 | 146 | */ |
157 | 147 | final boolean casBase(long cmp, long val) { |
158 | | - return UNSAFE.compareAndSwapLong(this, baseOffset, cmp, val); |
| 148 | + return CAS_BASE.compareAndSet(this, cmp, val); |
159 | 149 | } |
160 | 150 |
|
161 | 151 | /** |
162 | 152 | * CASes the busy field from 0 to 1 to acquire lock. |
163 | 153 | */ |
164 | 154 | final boolean casBusy() { |
165 | | - return UNSAFE.compareAndSwapInt(this, busyOffset, 0, 1); |
| 155 | + return CAS_BUSY.compareAndSet(this, 0, 1); |
166 | 156 | } |
167 | 157 |
|
168 | 158 | /** |
@@ -287,50 +277,7 @@ final void internalReset(long initialValue) { |
287 | 277 | } |
288 | 278 | } |
289 | 279 |
|
290 | | - // Unsafe mechanics |
291 | | - private static final sun.misc.Unsafe UNSAFE; |
292 | | - private static final long baseOffset; |
293 | | - private static final long busyOffset; |
294 | | - static { |
295 | | - try { |
296 | | - UNSAFE = getUnsafe(); |
297 | | - Class<?> sk = Striped64.class; |
298 | | - baseOffset = UNSAFE.objectFieldOffset |
299 | | - (sk.getDeclaredField("base")); |
300 | | - busyOffset = UNSAFE.objectFieldOffset |
301 | | - (sk.getDeclaredField("busy")); |
302 | | - } catch (Exception e) { |
303 | | - throw new Error(e); |
304 | | - } |
305 | | - } |
| 280 | + private static final AtomicLongFieldUpdater<Striped64> CAS_BASE = AtomicLongFieldUpdater.newUpdater(Striped64.class, "base"); |
| 281 | + private static final AtomicIntegerFieldUpdater<Striped64> CAS_BUSY = AtomicIntegerFieldUpdater.newUpdater(Striped64.class, "busy"); |
306 | 282 |
|
307 | | - /** |
308 | | - * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
309 | | - * Replace with a simple call to Unsafe.getUnsafe when integrating |
310 | | - * into a jdk. |
311 | | - * |
312 | | - * @return a sun.misc.Unsafe |
313 | | - */ |
314 | | - private static sun.misc.Unsafe getUnsafe() { |
315 | | - try { |
316 | | - return sun.misc.Unsafe.getUnsafe(); |
317 | | - } catch (SecurityException tryReflectionInstead) {} |
318 | | - try { |
319 | | - return java.security.AccessController.doPrivileged |
320 | | - (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
321 | | - public sun.misc.Unsafe run() throws Exception { |
322 | | - Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
323 | | - for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
324 | | - f.setAccessible(true); |
325 | | - Object x = f.get(null); |
326 | | - if (k.isInstance(x)) |
327 | | - return k.cast(x); |
328 | | - } |
329 | | - throw new NoSuchFieldError("the Unsafe"); |
330 | | - }}); |
331 | | - } catch (java.security.PrivilegedActionException e) { |
332 | | - throw new RuntimeException("Could not initialize intrinsics", |
333 | | - e.getCause()); |
334 | | - } |
335 | | - } |
336 | 283 | } |
0 commit comments