Skip to content

Commit

Permalink
[Java] Suppress warnings about Unsafe APIs being deprecated for remov…
Browse files Browse the repository at this point in the history
…al in JDK 22.
  • Loading branch information
vyazelenko committed Oct 25, 2023
1 parent 2b14f99 commit b557923
Showing 1 changed file with 6 additions and 81 deletions.
87 changes: 6 additions & 81 deletions agrona/src/main/java/org/agrona/concurrent/MemoryAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,116 +15,41 @@
*/
package org.agrona.concurrent;

import org.agrona.LangUtil;
import org.agrona.UnsafeAccess;
import sun.misc.Unsafe;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;

/**
* Memory access operations which encapsulate the use of Unsafe.
*/
public final class MemoryAccess
{
private static final MethodHandle ACQUIRE_FENCE;
private static final MethodHandle RELEASE_FENCE;
private static final MethodHandle FULL_FENCE;

static
{
MethodHandle acquireFence = null;
MethodHandle releaseFence = null;
MethodHandle fullFence = null;
final MethodHandles.Lookup lookup = MethodHandles.lookup();
final MethodType voidMethod = MethodType.methodType(void.class);

try
{
final Class<?> versionClass = Class.forName("java.lang.Runtime$Version"); // since JDK 9
final Object version = Runtime.class.getMethod("version").invoke(Runtime.getRuntime());
final int majorRelease = (int)versionClass.getMethod("feature").invoke(version);
if (majorRelease > 21)
{
final Class<?> varhandleClass = Class.forName("java.lang.invoke.VarHandle");
acquireFence = lookup.findStatic(varhandleClass, "acquireFence", voidMethod);
releaseFence = lookup.findStatic(varhandleClass, "releaseFence", voidMethod);
fullFence = lookup.findStatic(varhandleClass, "fullFence", voidMethod);
}
}
catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
InvocationTargetException ignored)
{
}

if (null == acquireFence)
{
try
{
final Class<? extends Unsafe> unsafeClass = UnsafeAccess.UNSAFE.getClass();
acquireFence = lookup.findVirtual(unsafeClass, "loadFence", voidMethod).bindTo(UnsafeAccess.UNSAFE);
releaseFence = lookup.findVirtual(unsafeClass, "storeFence", voidMethod).bindTo(UnsafeAccess.UNSAFE);
fullFence = lookup.findVirtual(unsafeClass, "fullFence", voidMethod).bindTo(UnsafeAccess.UNSAFE);
}
catch (final NoSuchMethodException | IllegalAccessException ex)
{
LangUtil.rethrowUnchecked(ex);
}
}

ACQUIRE_FENCE = acquireFence;
RELEASE_FENCE = releaseFence;
FULL_FENCE = fullFence;
}

private MemoryAccess()
{
}

/**
* Ensures that loads before the fence will not be reordered with loads and stores after the fence.
*/
@SuppressWarnings("removal") // Since JDK 22
public static void acquireFence()
{
try
{
ACQUIRE_FENCE.invokeExact();
}
catch (final Throwable t)
{
LangUtil.rethrowUnchecked(t);
}
UnsafeAccess.UNSAFE.loadFence();
}

/**
* Ensures that loads and stores before the fence will not be reordered with stores after the fence.
*/
@SuppressWarnings("removal") // Since JDK 22
public static void releaseFence()
{
try
{
RELEASE_FENCE.invokeExact();
}
catch (final Throwable t)
{
LangUtil.rethrowUnchecked(t);
}
UnsafeAccess.UNSAFE.storeFence();
}

/**
* Ensures that loads and stores before the fence will not be reordered with loads and stores after the fence.
*/
@SuppressWarnings("removal") // Since JDK 22
public static void fullFence()
{
try
{
FULL_FENCE.invokeExact();
}
catch (final Throwable t)
{
LangUtil.rethrowUnchecked(t);
}
UnsafeAccess.UNSAFE.fullFence();
}
}

0 comments on commit b557923

Please sign in to comment.