From 6ac1e00232c975a210a2bab15f51ab2e3239f03e Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sat, 27 Jun 2015 22:27:10 +0100 Subject: [PATCH] native calls sync; this should fix big library scanning issues --- jni/NativeFormats/JavaNativeFormatPlugin.cpp | 4 +- .../fbreader/formats/NativeFormatPlugin.java | 41 ++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/jni/NativeFormats/JavaNativeFormatPlugin.cpp b/jni/NativeFormats/JavaNativeFormatPlugin.cpp index fb9ab895c..5c31607ca 100644 --- a/jni/NativeFormats/JavaNativeFormatPlugin.cpp +++ b/jni/NativeFormats/JavaNativeFormatPlugin.cpp @@ -354,7 +354,7 @@ JNIEXPORT jint JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin } extern "C" -JNIEXPORT jstring JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readAnnotationInternal(JNIEnv* env, jobject thiz, jobject file) { +JNIEXPORT jstring JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readAnnotationNative(JNIEnv* env, jobject thiz, jobject file) { shared_ptr plugin = findCppPlugin(thiz); if (plugin.isNull()) { return 0; @@ -365,7 +365,7 @@ JNIEXPORT jstring JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlu } extern "C" -JNIEXPORT void JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readCoverInternal(JNIEnv* env, jobject thiz, jobject file, jobjectArray box) { +JNIEXPORT void JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readCoverNative(JNIEnv* env, jobject thiz, jobject file, jobjectArray box) { shared_ptr plugin = findCppPlugin(thiz); if (plugin.isNull()) { return; diff --git a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java index 70b440ed0..4587a3cb0 100644 --- a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java @@ -35,6 +35,8 @@ import org.geometerplus.fbreader.formats.fb2.FB2NativePlugin; import org.geometerplus.fbreader.formats.oeb.OEBNativePlugin; public class NativeFormatPlugin extends BuiltinFormatPlugin { + private static final Object ourNativeLock = new Object(); + public static NativeFormatPlugin create(String fileType) { if ("fb2".equals(fileType)) { return new FB2NativePlugin(); @@ -51,7 +53,10 @@ public class NativeFormatPlugin extends BuiltinFormatPlugin { @Override synchronized public void readMetainfo(AbstractBook book) throws BookReadingException { - final int code = readMetainfoNative(book); + final int code; + synchronized (ourNativeLock) { + code = readMetainfoNative(book); + } if (code != 0) { throw new BookReadingException( "nativeCodeFailure", @@ -65,7 +70,10 @@ public class NativeFormatPlugin extends BuiltinFormatPlugin { @Override public List readEncryptionInfos(AbstractBook book) { - final FileEncryptionInfo[] infos = readEncryptionInfosNative(book); + final FileEncryptionInfo[] infos; + synchronized (ourNativeLock) { + infos = readEncryptionInfosNative(book); + } return infos != null ? Arrays.asList(infos) : Collections.emptyList(); @@ -75,7 +83,9 @@ public class NativeFormatPlugin extends BuiltinFormatPlugin { @Override synchronized public void readUids(AbstractBook book) throws BookReadingException { - readUidsNative(book); + synchronized (ourNativeLock) { + readUidsNative(book); + } if (book.uids().isEmpty()) { book.addUid(BookUtil.createUid(book, "SHA-256")); } @@ -85,15 +95,20 @@ public class NativeFormatPlugin extends BuiltinFormatPlugin { @Override public void detectLanguageAndEncoding(AbstractBook book) { - detectLanguageAndEncodingNative(book); + synchronized (ourNativeLock) { + detectLanguageAndEncodingNative(book); + } } - public native void detectLanguageAndEncodingNative(AbstractBook book); + private native void detectLanguageAndEncodingNative(AbstractBook book); @Override synchronized public void readModel(BookModel model) throws BookReadingException { //android.os.Debug.startMethodTracing("ep.trace", 32 * 1024 * 1024); - final int code = readModelNative(model); + final int code; + synchronized (ourNativeLock) { + code = readModelNative(model); + } //android.os.Debug.stopMethodTracing(); switch (code) { case 0: @@ -112,25 +127,29 @@ public class NativeFormatPlugin extends BuiltinFormatPlugin { private native int readModelNative(BookModel model); @Override - public ZLFileImageProxy readCover(ZLFile file) { + public final ZLFileImageProxy readCover(ZLFile file) { return new ZLFileImageProxy(file) { @Override protected ZLFileImage retrieveRealImage() { final ZLFileImage[] box = new ZLFileImage[1]; - readCoverInternal(File, box); + synchronized (ourNativeLock) { + readCoverNative(File, box); + } return box[0]; } }; } - protected native void readCoverInternal(ZLFile file, ZLFileImage[] box); + private native void readCoverNative(ZLFile file, ZLFileImage[] box); @Override public String readAnnotation(ZLFile file) { - return readAnnotationInternal(file); + synchronized (ourNativeLock) { + return readAnnotationNative(file); + } } - protected native String readAnnotationInternal(ZLFile file); + private native String readAnnotationNative(ZLFile file); @Override public int priority() {