diff --git a/java/com/facebook/soloader/SoLoader.java b/java/com/facebook/soloader/SoLoader.java index 66f5c23..c1c7837 100644 --- a/java/com/facebook/soloader/SoLoader.java +++ b/java/com/facebook/soloader/SoLoader.java @@ -205,9 +205,7 @@ public static void init(Context context, int flags, @Nullable SoFileLoader soFil isSystemApp = checkIfSystemApp(context, flags); initSoLoader(soFileLoader); initSoSources(context, flags, soFileLoader); - if (!NativeLoader.isInitialized()) { - NativeLoader.init(new NativeLoaderToSoLoaderDelegate()); - } + NativeLoader.initIfUninitialized(new NativeLoaderToSoLoaderDelegate()); } finally { StrictMode.setThreadPolicy(oldPolicy); } diff --git a/java/com/facebook/soloader/nativeloader/NativeLoader.java b/java/com/facebook/soloader/nativeloader/NativeLoader.java index f1f460d..535d87d 100644 --- a/java/com/facebook/soloader/nativeloader/NativeLoader.java +++ b/java/com/facebook/soloader/nativeloader/NativeLoader.java @@ -112,4 +112,15 @@ public static synchronized void init(NativeLoaderDelegate delegate) { public static synchronized boolean isInitialized() { return sDelegate != null; } + + /** + * Perform an initialization only if {@code NativeLoader} has not already been initialized. This + * protects against race conditions where isInitialized and init are called by multiple threads + * and both threads end up trying to perform an initialization + */ + public static synchronized void initIfUninitialized(NativeLoaderDelegate delegate) { + if (!isInitialized()) { + init(delegate); + } + } }