Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnavBalyan committed Aug 17, 2024
1 parent b7c1338 commit a6c88ed
Showing 1 changed file with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public class JniLibLoader {
}

public static void forceUnloadAll() {
List<String> loaded = new ArrayList<>(REQUIRE_UNLOAD_LIBRARY_PATHS);
List<String> loaded;
synchronized (REQUIRE_UNLOAD_LIBRARY_PATHS) {
loaded = new ArrayList<>(REQUIRE_UNLOAD_LIBRARY_PATHS);
}
Collections.reverse(loaded); // use reversed order to unload
loaded.forEach(JniLibLoader::unloadFromPath);
}
Expand All @@ -86,19 +89,21 @@ private static String toRealPath(String libPath) {

private static void loadFromPath0(String libPath, boolean requireUnload) {
libPath = toRealPath(libPath);
if (LOADED_LIBRARY_PATHS.contains(libPath)) {
LOG.debug("Library in path {} has already been loaded, skipping", libPath);
} else {
System.load(libPath);
LOADED_LIBRARY_PATHS.add(libPath);
LOG.info("Library {} has been loaded using path-loading method", libPath);
synchronized (LOADED_LIBRARY_PATHS) {
if (LOADED_LIBRARY_PATHS.contains(libPath)) {
LOG.debug("Library in path {} has already been loaded, skipping", libPath);
} else {
System.load(libPath);
LOADED_LIBRARY_PATHS.add(libPath);
LOG.info("Library {} has been loaded using path-loading method", libPath);
}
}
if (requireUnload) {
REQUIRE_UNLOAD_LIBRARY_PATHS.add(libPath);
}
}

public static synchronized void loadFromPath(String libPath, boolean requireUnload) {
public static void loadFromPath(String libPath, boolean requireUnload) {
final File file = new File(libPath);
if (!file.isFile() || !file.exists()) {
throw new GlutenException("library at path: " + libPath + " is not a file or does not exist");
Expand All @@ -113,7 +118,6 @@ public static void unloadFromPath(String libPath) {
}
LOG.info("Starting unload library path: {} ", libPath);
REQUIRE_UNLOAD_LIBRARY_PATHS.remove(libPath);

try {
ClassLoader classLoader = JniLibLoader.class.getClassLoader();
Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
Expand Down Expand Up @@ -149,44 +153,51 @@ public static void unloadFromPath(String libPath) {
}

public void mapAndLoad(String unmappedLibName, boolean requireUnload) {
try {
final String mappedLibName = System.mapLibraryName(unmappedLibName);
load(mappedLibName, requireUnload);
} catch (Exception e) {
throw new GlutenException(e);
synchronized (loadedLibraries) {
try {
final String mappedLibName = System.mapLibraryName(unmappedLibName);
load(mappedLibName, requireUnload);
} catch (Exception e) {
throw new GlutenException(e);
}
}
}

public void load(String libName, boolean requireUnload) {
try {
if (loadedLibraries.contains(libName)) {
LOG.debug("Library {} has already been loaded, skipping", libName);
return;
synchronized (loadedLibraries) {
try {
if (loadedLibraries.contains(libName)) {
LOG.debug("Library {} has already been loaded, skipping", libName);
return;
}
File file = moveToWorkDir(workDir, libName);
loadWithLink(file.getAbsolutePath(), null, requireUnload);
loadedLibraries.add(libName);
LOG.info("Successfully loaded library {}", libName);
} catch (IOException e) {
throw new GlutenException(e);
}
File file = moveToWorkDir(workDir, libName);
loadWithLink(file.getAbsolutePath(), null, requireUnload);
loadedLibraries.add(libName);
LOG.info("Successfully loaded library {}", libName);
} catch (IOException e) {
throw new GlutenException(e);
}
}

public void loadAndCreateLink(String libName, String linkName, boolean requireUnload) {
try {
if (loadedLibraries.contains(libName)) {
LOG.debug("Library {} has already been loaded, skipping", libName);
synchronized (loadedLibraries) {
try {
if (loadedLibraries.contains(libName)) {
LOG.debug("Library {} has already been loaded, skipping", libName);
}
File file = moveToWorkDir(workDir, System.mapLibraryName(libName));
loadWithLink(file.getAbsolutePath(), linkName, requireUnload);
loadedLibraries.add(libName);
LOG.info("Successfully loaded library {}", libName);
} catch (IOException e) {
throw new GlutenException(e);
}
File file = moveToWorkDir(workDir, System.mapLibraryName(libName));
loadWithLink(file.getAbsolutePath(), linkName, requireUnload);
loadedLibraries.add(libName);
LOG.info("Successfully loaded library {}", libName);
} catch (IOException e) {
throw new GlutenException(e);
}
}

private File moveToWorkDir(String workDir, String libraryToLoad) throws IOException {
// final File temp = File.createTempFile(workDir, libraryToLoad);
final Path libPath = Paths.get(workDir + "/" + libraryToLoad);
if (Files.exists(libPath)) {
Files.delete(libPath);
Expand Down

0 comments on commit a6c88ed

Please sign in to comment.