[libbluray-devel] Fixed threading issues in C<->Java interface

hpi1 git at videolan.org
Wed Sep 26 15:26:39 CEST 2012


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Sep 26 13:11:56 2012 +0300| [8f3eb2ee110d8c1b6012e706027c228f0fb6ccfb] | committer: hpi1

Fixed threading issues in C<->Java interface
(merge from dslibbluray)

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=8f3eb2ee110d8c1b6012e706027c228f0fb6ccfb
---

 src/libbluray/bdj/bdj.c |   48 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/libbluray/bdj/bdj.c b/src/libbluray/bdj/bdj.c
index 149cf3b..8d7f64f 100644
--- a/src/libbluray/bdj/bdj.c
+++ b/src/libbluray/bdj/bdj.c
@@ -149,22 +149,33 @@ BDJAVA* bdj_open(const char *path,
 
 int bdj_start(BDJAVA *bdjava, unsigned title)
 {
-    JNIEnv* env = bdjava->env;
+    JNIEnv* env;
+    int attach = 0;
     jboolean status = JNI_FALSE;
     jclass loader_class;
     jmethodID load_id;
 
+    if ((*bdjava->jvm)->GetEnv(bdjava->jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
+        (*bdjava->jvm)->AttachCurrentThread(bdjava->jvm, (void**)&env, NULL);
+        attach = 1;
+    }
+
     if (bdj_get_method(env, &loader_class, &load_id,
                        "org/videolan/BDJLoader", "load", "(I)Z")) {
         status = (*env)->CallStaticBooleanMethod(env, loader_class, load_id, (jint)title);
     }
 
+    if (attach) {
+        (*bdjava->jvm)->DetachCurrentThread(bdjava->jvm);
+    }
+
     return (status == JNI_TRUE) ? BDJ_SUCCESS : BDJ_ERROR;
 }
 
 int bdj_stop(BDJAVA *bdjava)
 {
-    JNIEnv* env = bdjava->env;
+    JNIEnv *env;
+    int attach;
     jboolean status = JNI_FALSE;
     jclass loader_class;
     jmethodID unload_id;
@@ -173,17 +184,27 @@ int bdj_stop(BDJAVA *bdjava)
         return BDJ_ERROR;
     }
 
+    if ((*bdjava->jvm)->GetEnv(bdjava->jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
+        (*bdjava->jvm)->AttachCurrentThread(bdjava->jvm, (void**)&env, NULL);
+        attach = 1;
+    }
+
     if (bdj_get_method(env, &loader_class, &unload_id,
                        "org/videolan/BDJLoader", "unload", "()Z")) {
         status = (*env)->CallStaticBooleanMethod(env, loader_class, unload_id);
     }
 
+    if (attach) {
+        (*bdjava->jvm)->DetachCurrentThread(bdjava->jvm);
+    }
+
     return (status == JNI_TRUE) ? BDJ_SUCCESS : BDJ_ERROR;
 }
 
 void bdj_close(BDJAVA *bdjava)
 {
-    JNIEnv* env = bdjava->env;
+    JNIEnv *env;
+    int attach;
     jclass shutdown_class;
     jmethodID shutdown_id;
 
@@ -191,11 +212,20 @@ void bdj_close(BDJAVA *bdjava)
         return;
     }
 
+    if ((*bdjava->jvm)->GetEnv(bdjava->jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
+        (*bdjava->jvm)->AttachCurrentThread(bdjava->jvm, (void**)&env, NULL);
+        attach = 1;
+    }
+
     if (bdj_get_method(env, &shutdown_class, &shutdown_id,
                        "org/videolan/Libbluray", "shutdown", "()V")) {
         (*env)->CallStaticVoidMethod(env, shutdown_class, shutdown_id);
     }
 
+    if (attach) {
+        (*bdjava->jvm)->DetachCurrentThread(bdjava->jvm);
+    }
+
     (*bdjava->jvm)->DestroyJavaVM(bdjava->jvm);
 
     free(bdjava);
@@ -203,12 +233,22 @@ void bdj_close(BDJAVA *bdjava)
 
 void bdj_process_event(BDJAVA *bdjava, unsigned ev, unsigned param)
 {
-    JNIEnv* env = bdjava->env;
+    JNIEnv* env;
+    int attach = 0;
     jclass event_class;
     jmethodID event_id;
 
+    if ((*bdjava->jvm)->GetEnv(bdjava->jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
+        (*bdjava->jvm)->AttachCurrentThread(bdjava->jvm, (void**)&env, NULL);
+        attach = 1;
+    }
+
     if (bdj_get_method(env, &event_class, &event_id,
                        "org/videolan/Libbluray", "processEvent", "(II)V")) {
         (*env)->CallStaticVoidMethod(env, event_class, event_id, ev, param);
     }
+
+    if (attach) {
+        (*bdjava->jvm)->DetachCurrentThread(bdjava->jvm);
+    }
 }



More information about the libbluray-devel mailing list