Skip to content

Commit

Permalink
Runtime init the cleaner instance in ResourceCleaner
Browse files Browse the repository at this point in the history
Inject an accessor for the ResourceCleaner.CLEANER static variable so
that the associated cleaner won't get initialized at build time.

Closes: quarkusio#31440
  • Loading branch information
jerboaa committed Feb 28, 2023
1 parent 859c915 commit b9d8f4f
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.resteasy.common.runtime.graal;

import java.lang.ref.Cleaner;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.InjectAccessors;
import com.oracle.svm.core.annotate.TargetClass;

/**
* For GraalVM native we runtime-inject the Cleaner instance via an injected
* accessor. This awkward pattern over say, the initialize on demand holder
* pattern, is necessary so that the Graal compiler doesn't inline the accessor
* itself.
*/
@TargetClass(className = "org.jboss.resteasy.spi.ResourceCleaner")
public final class ResourceCleanerReplacement {

@Alias
@InjectAccessors(CleanerAccessor.class) //
private static Cleaner CLEANER;

static class CleanerAccessor {
private static volatile Cleaner INSTANCE;

static Cleaner get() {
Cleaner result = INSTANCE;
if (result == null) {
// lazily initialize instance
result = initializeOnce();
}
return result;
}

private static synchronized Cleaner initializeOnce() {
if (INSTANCE == null) {
// double checked locking is ok as INSTANCE is volatile
INSTANCE = Cleaner.create();
}
return INSTANCE;
}

}

}

0 comments on commit b9d8f4f

Please sign in to comment.