From 857e4c762a2c6f6c7bbf2e0d09037a2690b32c69 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Sat, 7 Feb 2015 19:33:19 +0100 Subject: [PATCH] add a jnius.detach() method to detach a thread that used some jni. This cannot be automatic as jnius do not control the thread creation, so it cannot add a callabck to autocall this method. It must be added by the user before the thread leave, if it used pyjnius inside. Ref #107 Ref #97 --- jnius/jni.pxi | 1 + jnius/jnius.pyx | 2 +- jnius/jnius_env.pxi | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/jnius/jni.pxi b/jnius/jni.pxi index afcd661..4e6c546 100644 --- a/jnius/jni.pxi +++ b/jnius/jni.pxi @@ -401,4 +401,5 @@ cdef extern from "jni.h": ctypedef struct JNIInvokeInterface: jint (*AttachCurrentThread)(JavaVM *, JNIEnv **, void *) + jint (*DetachCurrentThread)(JavaVM *) diff --git a/jnius/jnius.pyx b/jnius/jnius.pyx index 08f619a..dc074ba 100644 --- a/jnius/jnius.pyx +++ b/jnius/jnius.pyx @@ -87,7 +87,7 @@ Python:: __all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaField', 'MetaJavaClass', 'JavaException', 'cast', 'find_javaclass', - 'PythonJavaClass', 'java_method') + 'PythonJavaClass', 'java_method', 'detach') from libc.stdlib cimport malloc, free from functools import partial diff --git a/jnius/jnius_env.pxi b/jnius/jnius_env.pxi index 84b58ff..336ab0e 100644 --- a/jnius/jnius_env.pxi +++ b/jnius/jnius_env.pxi @@ -19,3 +19,7 @@ cdef JNIEnv *get_jnienv() except NULL: jvm[0].AttachCurrentThread(jvm, &env, NULL) return env + +def detach(): + jvm[0].DetachCurrentThread(jvm) +