31
31
import java .nio .file .Files ;
32
32
import java .nio .file .NoSuchFileException ;
33
33
import java .nio .file .Path ;
34
+ import java .util .ArrayList ;
34
35
import java .util .Collection ;
35
36
import java .util .EnumSet ;
36
- import java .util .HashSet ;
37
37
import java .util .List ;
38
- import java .util .Set ;
39
38
import java .util .SortedMap ;
40
39
import java .util .SortedSet ;
41
40
import java .util .TreeMap ;
42
41
import java .util .TreeSet ;
43
- import java .util .concurrent .ConcurrentHashMap ;
44
- import java .util .concurrent .ConcurrentMap ;
42
+ import java .util .concurrent .CopyOnWriteArrayList ;
45
43
import java .util .stream .Stream ;
46
44
47
45
import org .graalvm .nativeimage .ImageSingletons ;
@@ -127,7 +125,7 @@ public void afterAnalysis(AfterAnalysisAccess access) {
127
125
isSunMSCAPIProviderReachable = optSunMSCAPIClass .isPresent () && access .isReachable (optSunMSCAPIClass .get ());
128
126
}
129
127
if (ImageLayerBuildingSupport .buildingExtensionLayer ()) {
130
- for (String library : jniRegistrationSupportSingleton .baseLayerRegisteredLibraries ) {
128
+ for (String library : jniRegistrationSupportSingleton .prevLayerRegisteredLibraries ) {
131
129
addLibrary (library );
132
130
}
133
131
}
@@ -159,7 +157,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
159
157
}
160
158
161
159
void registerLibrary (String libname ) {
162
- if (libname != null && jniRegistrationSupportSingleton .registeredLibraries .putIfAbsent (libname , Boolean .TRUE ) == null ) {
160
+ if (libname != null && !jniRegistrationSupportSingleton .currentLayerRegisteredLibraries .contains (libname )) {
161
+ jniRegistrationSupportSingleton .currentLayerRegisteredLibraries .add (libname );
163
162
addLibrary (libname );
164
163
}
165
164
}
@@ -175,7 +174,7 @@ private void addLibrary(String libname) {
175
174
}
176
175
177
176
public boolean isRegisteredLibrary (String libname ) {
178
- return jniRegistrationSupportSingleton .registeredLibraries . containsKey (libname );
177
+ return jniRegistrationSupportSingleton .currentLayerRegisteredLibraries . contains (libname );
179
178
}
180
179
181
180
/** Adds exports that `jvm` shim should re-export. */
@@ -262,8 +261,8 @@ private void copyJDKLibraries(Path jdkLibDir) {
262
261
DebugContext debug = accessImpl .getDebugContext ();
263
262
try (Scope s = debug .scope ("copy" );
264
263
Indent i = debug .logAndIndent ("from: %s" , jdkLibDir )) {
265
- for (String libname : new TreeSet <>(jniRegistrationSupportSingleton .registeredLibraries . keySet () )) {
266
- if (jniRegistrationSupportSingleton .baseLayerRegisteredLibraries .contains (libname )) {
264
+ for (String libname : new TreeSet <>(jniRegistrationSupportSingleton .currentLayerRegisteredLibraries )) {
265
+ if (jniRegistrationSupportSingleton .prevLayerRegisteredLibraries .contains (libname )) {
267
266
/* Skip libraries copied in the base layer. */
268
267
debug .log (DebugContext .INFO_LEVEL , "%s: SKIPPED" , libname );
269
268
continue ;
@@ -372,8 +371,8 @@ private Path getImageImportLib() {
372
371
}
373
372
374
373
private static final class JNIRegistrationSupportSingleton implements LayeredImageSingleton {
375
- private final ConcurrentMap <String , Boolean > registeredLibraries = new ConcurrentHashMap <>();
376
- private final Set <String > baseLayerRegisteredLibraries = new HashSet <>();
374
+ private final List <String > currentLayerRegisteredLibraries = new CopyOnWriteArrayList <>();
375
+ private final List <String > prevLayerRegisteredLibraries = new ArrayList <>();
377
376
378
377
public static JNIRegistrationSupportSingleton singleton () {
379
378
return ImageSingletons .lookup (JNIRegistrationSupportSingleton .class );
@@ -387,15 +386,15 @@ public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
387
386
@ Override
388
387
public PersistFlags preparePersist (ImageSingletonWriter writer ) {
389
388
var snapshotWriter = ((SVMImageLayerWriter .ImageSingletonWriterImpl ) writer ).getSnapshotBuilder ();
390
- SVMImageLayerWriter .initStringList (snapshotWriter ::initRegisteredJNILibraries , registeredLibraries . keySet () .stream ());
389
+ SVMImageLayerWriter .initStringList (snapshotWriter ::initRegisteredJNILibraries , currentLayerRegisteredLibraries .stream ());
391
390
return PersistFlags .CREATE ;
392
391
}
393
392
394
393
@ SuppressWarnings ("unused" )
395
394
public static Object createFromLoader (ImageSingletonLoader loader ) {
396
395
JNIRegistrationSupportSingleton singleton = new JNIRegistrationSupportSingleton ();
397
396
var snapshotReader = ((SVMImageLayerSingletonLoader .ImageSingletonLoaderImpl ) loader ).getSnapshotReader ();
398
- SVMImageLayerLoader .streamStrings (snapshotReader .getRegisteredJNILibraries ()).forEach (singleton .baseLayerRegisteredLibraries ::add );
397
+ SVMImageLayerLoader .streamStrings (snapshotReader .getRegisteredJNILibraries ()).forEach (singleton .prevLayerRegisteredLibraries ::add );
399
398
return singleton ;
400
399
}
401
400
}
0 commit comments