forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime init the cleaner instance in ResourceCleaner
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
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...runtime/src/main/java/io/quarkus/restclient/runtime/graal/ResourceCleanerReplacement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |