Skip to content

Commit f6ec142

Browse files
committed
remove graal
1 parent 0fb2e71 commit f6ec142

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

core/src/main/java/io/art/core/property/DisposableProperty.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
import static io.art.core.factory.ListFactory.*;
1313
import static io.art.core.factory.QueueFactory.*;
1414
import static java.util.Objects.*;
15+
16+
import java.lang.invoke.MethodHandles;
17+
import java.lang.invoke.VarHandle;
1518
import java.util.*;
1619
import java.util.function.*;
1720

1821
@RequiredArgsConstructor
1922
@SuppressWarnings(SUN_API)
2023
public class DisposableProperty<T> implements Supplier<T> {
2124
private static final Object UNINITIALIZED = new Object();
22-
private static final sun.misc.Unsafe UNSAFE = UnsafeAccess.UNSAFE;
23-
private static final long VALUE_OFFSET;
25+
private static final VarHandle VALUE_HANDLE;
2426

2527
static {
2628
try {
27-
VALUE_OFFSET = UNSAFE.objectFieldOffset(DisposableProperty.class.getDeclaredField(VALUE_FIELD_NAME));
29+
MethodHandles.Lookup lookup = MethodHandles.lookup();
30+
VALUE_HANDLE = lookup.findVarHandle(DisposableProperty.class, VALUE_FIELD_NAME, Object.class);
2831
} catch (Throwable throwable) {
2932
throw new InternalRuntimeException(throwable);
3033
}
@@ -77,13 +80,13 @@ public void dispose() {
7780
if (current == UNINITIALIZED) {
7881
return;
7982
}
80-
current = cast(UNSAFE.getObjectVolatile(this, VALUE_OFFSET));
83+
current = cast(VALUE_HANDLE.getVolatile(this));
8184
if (current == UNINITIALIZED) {
8285
return;
8386
}
8487

8588
final T currentValue = current;
86-
if (UNSAFE.compareAndSwapObject(this, VALUE_OFFSET, current, UNINITIALIZED)) {
89+
if (VALUE_HANDLE.compareAndSet(this, current, UNINITIALIZED)) {
8790
apply(disposeConsumers, consumers -> consumers.forEach(consumer -> consumer.accept(currentValue)));
8891
}
8992
}
@@ -92,12 +95,12 @@ public void dispose() {
9295
public T get() {
9396
T localValue = value;
9497
if (localValue == UNINITIALIZED) {
95-
localValue = cast(UNSAFE.getObjectVolatile(this, VALUE_OFFSET));
98+
localValue = cast(VALUE_HANDLE.getVolatile(this));
9699
if (localValue == UNINITIALIZED) {
97100
T loaded = orThrow(loader.get(), new InternalRuntimeException(PROPERTY_VALUE_IS_NULL));
98-
localValue = UNSAFE.compareAndSwapObject(this, VALUE_OFFSET, UNINITIALIZED, loaded)
101+
localValue = VALUE_HANDLE.compareAndSet(this, UNINITIALIZED, loaded)
99102
? loaded
100-
: cast(UNSAFE.getObjectVolatile(this, VALUE_OFFSET));
103+
: cast(VALUE_HANDLE.getVolatile(this));
101104
apply(creationConsumers, consumers -> erase(consumers, consumer -> consumer.accept(loaded)));
102105
apply(initializationConsumers, consumers -> consumers.forEach(consumer -> consumer.accept(loaded)));
103106
}

0 commit comments

Comments
 (0)