Skip to content

Commit

Permalink
SoLoader to report load result
Browse files Browse the repository at this point in the history
Summary:
This sets the return value of SoLoader.load to a boolean, which reports
whether the library was loaded by the call (true) or was already loaded
(false). Note that load failures are reported via exceptions.

Reviewed By: michalgr

Differential Revision: D7495326

fbshipit-source-id: 0d033ab2365ef45c49120f47931994ea8361fbb8
  • Loading branch information
Peter Ammon authored and facebook-github-bot committed May 10, 2018
1 parent 60131ea commit 9169960
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ public static final class WrongAbiError extends UnsatisfiedLinkError {
}
}

public static void loadLibrary(String shortName) {
loadLibrary(shortName, 0);
public static boolean loadLibrary(String shortName) {
return loadLibrary(shortName, 0);
}

/**
Expand All @@ -450,8 +450,10 @@ public static void loadLibrary(String shortName) {
* @param shortName Name of library to find, without "lib" prefix or ".so" suffix
* @param loadFlags Control flags for the loading behavior. See available flags under {@link
* SoSource} (LOAD_FLAG_XXX).
* @return Whether the library was loaded as a result of this call (true), or was already loaded
* through a previous call to SoLoader (false).
*/
public static void loadLibrary(String shortName, int loadFlags) throws UnsatisfiedLinkError {
public static boolean loadLibrary(String shortName, int loadFlags) throws UnsatisfiedLinkError {
sSoSourcesLock.readLock().lock();
try {
if (sSoSources == null) {
Expand All @@ -464,12 +466,15 @@ public static void loadLibrary(String shortName, int loadFlags) throws Unsatisfi
} else {
// Not on an Android system. Ask the JVM to load for us.
synchronized (SoLoader.class) {
if (sSystemLoadLibraryWrapper != null) {
sSystemLoadLibraryWrapper.loadLibrary(shortName);
} else {
System.loadLibrary(shortName);
boolean needsLoad = !sLoadedLibraries.contains(shortName);
if (needsLoad) {
if (sSystemLoadLibraryWrapper != null) {
sSystemLoadLibraryWrapper.loadLibrary(shortName);
} else {
System.loadLibrary(shortName);
}
}
return;
return needsLoad;
}
}
}
Expand All @@ -481,7 +486,8 @@ public static void loadLibrary(String shortName, int loadFlags) throws Unsatisfi

String soName = mergedLibName != null ? mergedLibName : shortName;

loadLibraryBySoName(System.mapLibraryName(soName), shortName, mergedLibName, loadFlags, null);
return loadLibraryBySoName(
System.mapLibraryName(soName), shortName, mergedLibName, loadFlags, null);
}

/* package */ static void loadLibraryBySoName(
Expand All @@ -495,7 +501,7 @@ public static Set<String> getLoadedLibraries() {
}
}

private static void loadLibraryBySoName(
private static boolean loadLibraryBySoName(
String soName,
@Nullable String shortName,
@Nullable String mergedLibName,
Expand All @@ -506,7 +512,7 @@ private static void loadLibraryBySoName(
// by tracking loaded and merged libs in Java we can avoid undue synchronization and blocking
// waits (which may occur on the UI thread and thus trigger ANRs).
if (!TextUtils.isEmpty(shortName) && sLoadedAndMergedLibraries.contains(shortName)) {
return;
return false;
}

Object loadingLibLock;
Expand All @@ -515,7 +521,7 @@ private static void loadLibraryBySoName(
if (sLoadedLibraries.contains(soName)) {
if (mergedLibName == null) {
// Not a merged lib, no need to init
return;
return false;
}
loaded = true;
}
Expand All @@ -534,7 +540,7 @@ private static void loadLibraryBySoName(
// Library was successfully loaded by other thread while we waited
if (mergedLibName == null) {
// Not a merged lib, no need to init
return;
return false;
}
loaded = true;
}
Expand Down Expand Up @@ -578,6 +584,7 @@ private static void loadLibraryBySoName(
}
}
}
return !loaded;
}

/**
Expand Down

0 comments on commit 9169960

Please sign in to comment.