Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8273000: Remove WeakReference-based class initialisation barrier implementation #5258

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -33,7 +33,6 @@
import sun.invoke.util.VerifyType;
import sun.invoke.util.Wrapper;

import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
@@ -363,27 +362,12 @@ static boolean shouldBeInitialized(MemberName member) {
VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
// It is a system class. It is probably in the process of
// being initialized, but we will help it along just to be safe.
if (UNSAFE.shouldBeInitialized(cls)) {
UNSAFE.ensureClassInitialized(cls);
}
UNSAFE.ensureClassInitialized(cls);
return false;
}
return UNSAFE.shouldBeInitialized(cls);
}

private static class EnsureInitialized extends ClassValue<WeakReference<Thread>> {
@Override
protected WeakReference<Thread> computeValue(Class<?> type) {
UNSAFE.ensureClassInitialized(type);
if (UNSAFE.shouldBeInitialized(type))
// If the previous call didn't block, this can happen.
// We are executing inside <clinit>.
return new WeakReference<>(Thread.currentThread());
return null;
}
static final EnsureInitialized INSTANCE = new EnsureInitialized();
}

private void ensureInitialized() {
if (checkInitialized(member)) {
// The coast is clear. Delete the <clinit> barrier.
@@ -397,24 +381,8 @@ public LambdaForm apply(LambdaForm oldForm) {
}
private static boolean checkInitialized(MemberName member) {
Class<?> defc = member.getDeclaringClass();
WeakReference<Thread> ref = EnsureInitialized.INSTANCE.get(defc);
if (ref == null) {
return true; // the final state
}
// Somebody may still be running defc.<clinit>.
if (ref.refersTo(Thread.currentThread())) {
// If anybody is running defc.<clinit>, it is this thread.
if (UNSAFE.shouldBeInitialized(defc))
// Yes, we are running it; keep the barrier for now.
return false;
} else {
// We are in a random thread. Block.
UNSAFE.ensureClassInitialized(defc);
}
assert(!UNSAFE.shouldBeInitialized(defc));
// put it into the final state
EnsureInitialized.INSTANCE.remove(defc);
return true;
UNSAFE.ensureClassInitialized(defc); // initialization barrier; blocks unless called by the initializing thread
return !UNSAFE.shouldBeInitialized(defc); // keep the barrier if the initialization is still in progress
Copy link
Member

@dholmes-ora dholmes-ora Aug 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

@iwanowww iwanowww Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, David. I incorporated your suggestion in the latest version.

}

/*non-public*/