Don't attach to actual Java threads (or already attached ones)
We check this by trying to retrieve a JNIEnv object from the JVM, if one is returned the current thread is not native (created from Java) or the thread is already attached.
This commit is contained in:
@@ -46,6 +46,11 @@ static void attached_thread_cleanup(void *arg)
|
|||||||
*/
|
*/
|
||||||
void androidjni_attach_thread(JNIEnv **env)
|
void androidjni_attach_thread(JNIEnv **env)
|
||||||
{
|
{
|
||||||
|
if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
|
||||||
|
JNI_VERSION_1_6) == JNI_OK)
|
||||||
|
{ /* already attached or even a Java thread */
|
||||||
|
return;
|
||||||
|
}
|
||||||
(*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
|
(*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
|
||||||
/* use a thread-local value with a destructor that automatically detaches
|
/* use a thread-local value with a destructor that automatically detaches
|
||||||
* the thread from the JVM before it terminates, if not done manually */
|
* the thread from the JVM before it terminates, if not done manually */
|
||||||
@@ -57,8 +62,11 @@ void androidjni_attach_thread(JNIEnv **env)
|
|||||||
*/
|
*/
|
||||||
void androidjni_detach_thread()
|
void androidjni_detach_thread()
|
||||||
{
|
{
|
||||||
androidjni_threadlocal->set(androidjni_threadlocal, NULL);
|
if (androidjni_threadlocal->get(androidjni_threadlocal))
|
||||||
(*android_jvm)->DetachCurrentThread(android_jvm);
|
{ /* only do this if we actually attached this thread */
|
||||||
|
androidjni_threadlocal->set(androidjni_threadlocal, NULL);
|
||||||
|
(*android_jvm)->DetachCurrentThread(android_jvm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,7 +93,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when this library is unloaded by the JVM
|
* Called when this library is unloaded by the JVM (which never happens on
|
||||||
|
* Android)
|
||||||
*/
|
*/
|
||||||
void JNI_OnUnload(JavaVM *vm, void *reserved)
|
void JNI_OnUnload(JavaVM *vm, void *reserved)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user