Skip to content

Commit fe42313

Browse files
authored
Remove dependency on sun.misc.Unsafe (#547)
This replaces `sun.misc.Unsafe` with `java.util.concurrent.atomic.AtomicLongFieldUpdater` and `AtomicIntegerFieldUpdater`. This is to enable compatibility with Jigsaw modules where `sun.misc.Unsafe` is no longer available. Closes #468 Signed-off-by: Andre Masella <[email protected]>
1 parent ac5136b commit fe42313

File tree

1 file changed

+8
-61
lines changed

1 file changed

+8
-61
lines changed

simpleclient/src/main/java/io/prometheus/client/Striped64.java

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package io.prometheus.client;
1010

1111
import java.util.Random;
12+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
13+
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
1214

1315
/**
1416
* A package-local class holding common representation and mechanics
@@ -94,22 +96,10 @@ static final class Cell {
9496
Cell(long x) { value = x; }
9597

9698
final boolean cas(long cmp, long val) {
97-
return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);
99+
return CAS_VALUE.compareAndSet(this, cmp, val);
98100
}
99101

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");
113103

114104
}
115105

@@ -155,14 +145,14 @@ final boolean cas(long cmp, long val) {
155145
* CASes the base field.
156146
*/
157147
final boolean casBase(long cmp, long val) {
158-
return UNSAFE.compareAndSwapLong(this, baseOffset, cmp, val);
148+
return CAS_BASE.compareAndSet(this, cmp, val);
159149
}
160150

161151
/**
162152
* CASes the busy field from 0 to 1 to acquire lock.
163153
*/
164154
final boolean casBusy() {
165-
return UNSAFE.compareAndSwapInt(this, busyOffset, 0, 1);
155+
return CAS_BUSY.compareAndSet(this, 0, 1);
166156
}
167157

168158
/**
@@ -287,50 +277,7 @@ final void internalReset(long initialValue) {
287277
}
288278
}
289279

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");
306282

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-
}
336283
}

0 commit comments

Comments
 (0)