Skip to content

Commit

Permalink
No need for soloader.class synchronization to mutate sLoadedLibraries
Browse files Browse the repository at this point in the history
Summary: We can follow what sLoadedAndJniInvoked does and have a ConcurrentSet, that we only need to synchronize on the loadingLibLock specific to the library we're about to load.

Reviewed By: michalgr

Differential Revision: D58954598

fbshipit-source-id: 2e44379c621a69e8420fe0460526e21c5dbd0704
  • Loading branch information
adicatana authored and facebook-github-bot committed Jun 26, 2024
1 parent 02cfe86 commit ac267a5
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public class SoLoader {
private static RecoveryStrategyFactory sRecoveryStrategyFactory = null;

/** Records the sonames (e.g., "libdistract.so") of shared libraries we've loaded. */
@GuardedBy("SoLoader.class")
private static final HashSet<String> sLoadedLibraries = new HashSet<>();
private static final Set<String> sLoadedLibraries =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

/**
* Libraries that are in the process of being loaded, and lock objects to synchronize on and wait
Expand Down Expand Up @@ -1012,17 +1012,15 @@ private static boolean loadLibraryBySoNameImpl(
try {
synchronized (loadingLibLock) {
if (!loaded) {
synchronized (SoLoader.class) {
if (sLoadedLibraries.contains(soName)) {
// Library was successfully loaded by other thread while we waited
if (mergedLibName == null) {
// Not a merged lib, no need to init
return false;
}
loaded = true;
if (sLoadedLibraries.contains(soName)) {
// Library was successfully loaded by other thread while we waited
if (mergedLibName == null) {
// Not a merged lib, no need to init
return false;
}
// Else, load was not successful on other thread. We will try in this one.
loaded = true;
}
// Else, load was not successful on other thread. We will try in this one.

if (!loaded) {
try {
Expand All @@ -1038,9 +1036,7 @@ private static boolean loadLibraryBySoNameImpl(
throw ex;
}
LogUtil.d(TAG, "Loaded: " + soName);
synchronized (SoLoader.class) {
sLoadedLibraries.add(soName);
}
sLoadedLibraries.add(soName);
}
}
}
Expand Down

0 comments on commit ac267a5

Please sign in to comment.