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 27, 2023
1 parent 1d45952 commit e03d177
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.restclient.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() {
Cleaner result = INSTANCE;
if (result == null) {
// double checked locking is ok as INSTANCE is volatile
result = INSTANCE = Cleaner.create();
}
return result;
}

}

}

0 comments on commit e03d177

Please sign in to comment.