From ac267a5b453386579577f104e80e799325f5cb24 Mon Sep 17 00:00:00 2001 From: Adrian Catana Date: Wed, 26 Jun 2024 09:28:34 -0700 Subject: [PATCH] No need for soloader.class synchronization to mutate sLoadedLibraries 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 --- java/com/facebook/soloader/SoLoader.java | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/java/com/facebook/soloader/SoLoader.java b/java/com/facebook/soloader/SoLoader.java index 00fd0fd..2d07513 100644 --- a/java/com/facebook/soloader/SoLoader.java +++ b/java/com/facebook/soloader/SoLoader.java @@ -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 sLoadedLibraries = new HashSet<>(); + private static final Set sLoadedLibraries = + Collections.newSetFromMap(new ConcurrentHashMap()); /** * Libraries that are in the process of being loaded, and lock objects to synchronize on and wait @@ -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 { @@ -1038,9 +1036,7 @@ private static boolean loadLibraryBySoNameImpl( throw ex; } LogUtil.d(TAG, "Loaded: " + soName); - synchronized (SoLoader.class) { - sLoadedLibraries.add(soName); - } + sLoadedLibraries.add(soName); } } }