Skip to content

Commit

Permalink
loadLibrary should be a synchronized operation
Browse files Browse the repository at this point in the history
Otherwise it might cause realm#1350
  • Loading branch information
beeender committed Aug 10, 2015
1 parent c90e64c commit c6aceea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.83
* BREAKING CHANGE: Removed deprecated methods and constructors from the Realm class.
* Fixed a bug which might cause failure when loading the native library.

0.82.1
* Fixed a bug where using the wrong encryption key first caused the right key to be seen as invalid.
Expand Down
15 changes: 9 additions & 6 deletions realm/src/main/java/io/realm/internal/RealmCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Utility methods for Realm Core.
Expand All @@ -39,7 +38,7 @@ public class RealmCore {
private static final String JAVA_LIBRARY_PATH = "java.library.path";
//*/

private static AtomicBoolean libraryIsLoaded = new AtomicBoolean(false);
private static volatile boolean libraryIsLoaded = false;

/*
private static String getJniFileName()
Expand Down Expand Up @@ -94,10 +93,14 @@ public static void print(String caption, AbstractCursor<?> cursor) {
}
*/

public static void loadLibrary() {
if (libraryIsLoaded.get())
// only load library once
// Although loadLibrary is synchronized internally from AOSP 4.3, for the compatibility reason,
// KEEP synchronized here for the old devices!
public static synchronized void loadLibrary() {
if (libraryIsLoaded) {
// The java native should ensure only load the lib once, but we met some problems before.
// So keep the flag.
return;
}

if (osIsWindows()) {
loadLibraryWindows();
Expand All @@ -113,7 +116,7 @@ public static void loadLibrary() {
}
System.loadLibrary(jnilib);
}
libraryIsLoaded.set(true);
libraryIsLoaded = true;

Version.coreLibVersionCompatible(true);
}
Expand Down

0 comments on commit c6aceea

Please sign in to comment.