Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 0bc9ad0..a18cb4f 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -98,12 +98,10 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
* Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
* Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
*/
- if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed)) {
+ if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed) != 0) {
__android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
}
- else {
- Android_JNI_SetupThread();
- }
+ Android_JNI_SetupThread();
return JNI_VERSION_1_4;
}
@@ -454,7 +452,8 @@ SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
return retval;
}
-static void Android_JNI_ThreadDestroyed(void* value) {
+static void Android_JNI_ThreadDestroyed(void* value)
+{
/* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
JNIEnv *env = (JNIEnv*) value;
if (env != NULL) {
@@ -463,7 +462,8 @@ static void Android_JNI_ThreadDestroyed(void* value) {
}
}
-JNIEnv* Android_JNI_GetEnv(void) {
+JNIEnv* Android_JNI_GetEnv(void)
+{
/* From http://developer.android.com/guide/practices/jni.html
* All threads are Linux threads, scheduled by the kernel.
* They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
@@ -483,10 +483,6 @@ JNIEnv* Android_JNI_GetEnv(void) {
return 0;
}
- return env;
-}
-
-int Android_JNI_SetupThread(void) {
/* From http://developer.android.com/guide/practices/jni.html
* Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
* in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
@@ -496,8 +492,14 @@ int Android_JNI_SetupThread(void) {
* Note: You can call this function any number of times for the same thread, there's no harm in it
* (except for some lost CPU cycles)
*/
- JNIEnv *env = Android_JNI_GetEnv();
pthread_setspecific(mThreadKey, (void*) env);
+
+ return env;
+}
+
+int Android_JNI_SetupThread(void)
+{
+ Android_JNI_GetEnv();
return 1;
}