RFR: 8273000: Remove WeakReference-based class initialisation barrier implementation

David Holmes dholmes at openjdk.java.net
Thu Aug 26 23:53:22 UTC 2021


On Wed, 25 Aug 2021 22:05:24 GMT, Vladimir Ivanov <vlivanov at openjdk.org> wrote:

> Get rid of WeakReference-based logic in DirectMethodHandle::checkInitialized() and reimplement it with `Unsafe::ensureClassInitialized()`/`shouldBeInitialized()`. 
> 
> The key observation is that `Unsafe::ensureClassInitialized()` does not block the initializing thread. 
> 
> Also, removed `Unsafe::shouldBeInitialized()` in `DMH::shouldBeInitialized(MemberName)` to save on calling into the VM.
> `Unsafe::ensureClassInitialized()` already has a fast-path check which checks whether the class is fully initialized or not.
> 
> Testing: tier1 - tier6

src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java line 385:

> 383:         Class<?> defc = member.getDeclaringClass();
> 384:         UNSAFE.ensureClassInitialized(defc); // initialization barrier; blocks unless called by the initializing thread
> 385:         return !UNSAFE.shouldBeInitialized(defc); // keep the barrier if the initialization is still in progress

I think some more elaborate commentary about the possibility of this being called while <clinit> of defc is already on the call stack, would be worthwhile - the existing comments are a little too subtle IMO.


UNSAFE.ensureClassInitialized(defc);
// Once we get here either defc was fully initialized by another thread, or
// defc was already being initialized by the current thread. In the latter case
// the barrier must remain. We can detect this simply by checking if initialization
// is still needed.
return !UNSAFE.shouldBeInitialized(defc);

-------------

PR: https://git.openjdk.java.net/jdk/pull/5258


More information about the core-libs-dev mailing list