Avoid usage of `sun.misc.Unsafe`
We currently still use sun.misc.Unsafe in a number of places. As long-term these interfaces are supposed to go away, it would be nice to try and stop using them.
The bright side is that lots of code is still using this class. Even the Scala runtime - eg https://github.com/scala/scala/blob/2.13.x/src/library/scala/runtime/Statics.java
I'm no expert on migrating away from using sun.misc.Unsafe but MethodHandles and VarHandles might be an easy enough solution for some of the use cases.
Some libraries start to using *Handler from
Jvm 11
VarHandle could replace the usage of Unsafe pretty much 1:1. However, VarHandle is Java 9+.
Looking at the usages, I'm wondering if much of it can be replaced by using AtomicReference instead. For example, consider https://github.com/apache/incubator-pekko/blob/3775a18c2eec1abd46355b656109f282d4115b40/actor/src/main/scala/org/apache/pekko/actor/RepointableActorRef.scala#L62-L80:
To me, the tailrec methods look like overly complicated getAndSet(...) implementations, and underlying and lookup are just get()s.
Unsafe#getAndSetObject was only added in Java 8, so I assume that's the reason it isn't used, but I'm not sure why the code doesn't make use of AtomicReference. Am I missing something?
There is a lot of reluctance to drop Java 8 support. I would favour dropping it because it forces us to support some code usages that are now being discouraged.
One option would to make some pekko jars Multi-Release: true so that we can ship variants of classes that can be used with newer Java Runtimes. The basic classes would support Java 8 but we could have override versions of some classes that work with Java 11+. Those override classes could use features like VarHandles.
refs: https://youtrack.jetbrains.com/issue/IDEA-164460/JDK9-Multi-release-JAR-problem-one-type-shown-many-times refs:https://youtrack.jetbrains.com/issue/IDEA-255308/Maven-import-additional-compile-source-roots-with-multiReleaseOutput-option-as-separate-modules
@raboof @mdedetrich ping~