< prev index next >

src/java.base/share/classes/jdk/internal/loader/NativeLibraries.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

*** 153,11 ***
              if (cached != null) {
                  return cached;
              }
  
              // cannot be loaded by other class loaders
!             if (loadedLibraryNames.contains(name)) {
                  throw new UnsatisfiedLinkError("Native Library " + name +
                          " already loaded in another classloader");
              }
  
              /*
--- 153,11 ---
              if (cached != null) {
                  return cached;
              }
  
              // cannot be loaded by other class loaders
!             if (Holder.loadedLibraryNames.contains(name)) {
                  throw new UnsatisfiedLinkError("Native Library " + name +
                          " already loaded in another classloader");
              }
  
              /*

*** 203,11 ***
                  }
              } finally {
                  NativeLibraryContext.pop();
              }
              // register the loaded native library
!             loadedLibraryNames.add(name);
              libraries.put(name, lib);
              return lib;
          } finally {
              releaseNativeLibraryLock(name);
          }
--- 203,11 ---
                  }
              } finally {
                  NativeLibraryContext.pop();
              }
              // register the loaded native library
!             Holder.loadedLibraryNames.add(name);
              libraries.put(name, lib);
              return lib;
          } finally {
              releaseNativeLibraryLock(name);
          }

*** 243,10 ***
--- 243,15 ---
              lib = findFromPaths(LibraryPaths.USER_PATHS, fromClass, name);
          }
          return lib;
      }
  
+     // Called at the end of AOTCache assembly phase.
+     public void clear() {
+         libraries.clear();
+     }
+ 
      private NativeLibrary findFromPaths(String[] paths, Class<?> fromClass, String name) {
          for (String path : paths) {
              File libfile = new File(path, System.mapLibraryName(name));
              NativeLibrary nl = loadLibrary(fromClass, libfile);
              if (nl != null) {

*** 373,11 ***
          @Override
          public void run() {
              acquireNativeLibraryLock(name);
              try {
                  /* remove the native library name */
!                 if (!loadedLibraryNames.remove(name)) {
                      throw new IllegalStateException(name + " has already been unloaded");
                  }
                  NativeLibraryContext.push(UNLOADER);
                  try {
                      unload(name, isBuiltin, handle);
--- 378,11 ---
          @Override
          public void run() {
              acquireNativeLibraryLock(name);
              try {
                  /* remove the native library name */
!                 if (!Holder.loadedLibraryNames.remove(name)) {
                      throw new IllegalStateException(name + " has already been unloaded");
                  }
                  NativeLibraryContext.push(UNLOADER);
                  try {
                      unload(name, isBuiltin, handle);

*** 400,13 ***
          // The paths searched for libraries
          static final String[] SYS_PATHS = ClassLoaderHelper.parsePath(StaticProperty.sunBootLibraryPath());
          static final String[] USER_PATHS = ClassLoaderHelper.parsePath(StaticProperty.javaLibraryPath());
      }
  
!     // All native libraries we've loaded.
!     private static final Set<String> loadedLibraryNames =
              ConcurrentHashMap.newKeySet();
  
      // reentrant lock class that allows exact counting (with external synchronization)
      @SuppressWarnings("serial")
      private static final class CountedLock extends ReentrantLock {
  
--- 405,17 ---
          // The paths searched for libraries
          static final String[] SYS_PATHS = ClassLoaderHelper.parsePath(StaticProperty.sunBootLibraryPath());
          static final String[] USER_PATHS = ClassLoaderHelper.parsePath(StaticProperty.javaLibraryPath());
      }
  
!     // Holder has the fields that need to be initialized during JVM bootstrap even if
!     // the outer is aot-initialized.
+     static class Holder {
+         // All native libraries we've loaded.
+         private static final Set<String> loadedLibraryNames =
              ConcurrentHashMap.newKeySet();
+     }
  
      // reentrant lock class that allows exact counting (with external synchronization)
      @SuppressWarnings("serial")
      private static final class CountedLock extends ReentrantLock {
  
< prev index next >