From 38ae8629788b21ae653943f8ef4f02f9bbd74b96 Mon Sep 17 00:00:00 2001 From: Matt Holgate Date: Mon, 23 Jun 2014 16:51:16 +0100 Subject: Prevent the creation/destruction of the fz_cookies from trampling the env/clazz pointers in globals. The env/clazz pointers are stashed in the globals structure so that they can be accessed by callback functions in mupdf.c (such as bufferStreamSeek()). The intention is that only one thread (i.e. the AsyncTask background thread) reads/writes these stashed pointers. Because cookies are created/destroyed in the main thread, we add a new version of get_globals() which doesn't trample these pointers. --- platform/android/jni/mupdf.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'platform/android') diff --git a/platform/android/jni/mupdf.c b/platform/android/jni/mupdf.c index 01a8db9b..574a9d6f 100644 --- a/platform/android/jni/mupdf.c +++ b/platform/android/jni/mupdf.c @@ -249,6 +249,7 @@ static void alerts_fin(globals *glo) glo->alerts_initialised = 0; } +// Should only be called from the single background AsyncTask thread static globals *get_globals(JNIEnv *env, jobject thiz) { globals *glo = (globals *)(void *)((*env)->GetLongField(env, thiz, global_fid)); @@ -260,6 +261,13 @@ static globals *get_globals(JNIEnv *env, jobject thiz) return glo; } +// May be called from any thread, provided the values of glo->env and glo->thiz +// are not used. +static globals *get_globals_any_thread(JNIEnv *env, jobject thiz) +{ + return (globals *)(void *)((*env)->GetLongField(env, thiz, global_fid)); +} + JNIEXPORT jlong JNICALL JNI_FN(MuPDFCore_openFile)(JNIEnv * env, jobject thiz, jstring jfilename) { @@ -2594,7 +2602,7 @@ JNI_FN(MuPDFCore_dumpMemoryInternal)(JNIEnv * env, jobject thiz) JNIEXPORT jlong JNICALL JNI_FN(MuPDFCore_createCookie)(JNIEnv * env, jobject thiz) { - globals *glo = get_globals(env, thiz); + globals *glo = get_globals_any_thread(env, thiz); if (glo == NULL) return 0; fz_context *ctx = glo->ctx; @@ -2606,7 +2614,7 @@ JNIEXPORT void JNICALL JNI_FN(MuPDFCore_destroyCookie)(JNIEnv * env, jobject thiz, jlong cookiePtr) { fz_cookie *cookie = (fz_cookie *) (unsigned int) cookiePtr; - globals *glo = get_globals(env, thiz); + globals *glo = get_globals_any_thread(env, thiz); if (glo == NULL) return; fz_context *ctx = glo->ctx; -- cgit v1.2.3