Android: don't call Android_JNI_ThreadDestroyed() for Java SDLThread
authorSylvain Becker <sylvain.becker@gmail.com>
Fri, 11 Jan 2019 14:50:43 +0100
changeset 125253bd1e8f5cfdf
parent 12524 d8a3edfbab59
child 12526 2fd98a88f320
Android: don't call Android_JNI_ThreadDestroyed() for Java SDLThread

SDLThread is a Java Thread, it's not needed to call 'Detach' from the JVM.
Clear mThreadKey, so that the pthread_create destructor is not called for this
thread.
src/core/android/SDL_android.c
     1.1 --- a/src/core/android/SDL_android.c	Fri Jan 11 14:25:32 2019 +0100
     1.2 +++ b/src/core/android/SDL_android.c	Fri Jan 11 14:50:43 2019 +0100
     1.3 @@ -292,6 +292,9 @@
     1.4  
     1.5  static SDL_bool bHasEnvironmentVariables = SDL_FALSE;
     1.6  
     1.7 +
     1.8 +static void Android_JNI_SetEnv(JNIEnv *env);
     1.9 +
    1.10  /*******************************************************************************
    1.11                   Functions called by JNI
    1.12  *******************************************************************************/
    1.13 @@ -559,6 +562,10 @@
    1.14      }
    1.15      (*env)->ReleaseStringUTFChars(env, library, library_file);
    1.16  
    1.17 +    /* This is a Java thread, it doesn't need to be Detached from the JVM. 
    1.18 +     * Set to mThreadKey value to NULL not to call pthread_create destructor 'Android_JNI_ThreadDestroyed' */
    1.19 +    Android_JNI_SetEnv(NULL);
    1.20 +
    1.21      /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    1.22      /* exit(status); */
    1.23  
    1.24 @@ -1126,7 +1133,14 @@
    1.25      JNIEnv *env = (JNIEnv *) value;
    1.26      if (env != NULL) {
    1.27          (*mJavaVM)->DetachCurrentThread(mJavaVM);
    1.28 -        pthread_setspecific(mThreadKey, NULL);
    1.29 +        Android_JNI_SetEnv(NULL);
    1.30 +    }
    1.31 +}
    1.32 +
    1.33 +static void Android_JNI_SetEnv(JNIEnv *env) {
    1.34 +    int status = pthread_setspecific(mThreadKey, env);
    1.35 +    if (status < 0) {
    1.36 +        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Failed pthread_setspecific() in Android_JNI_SetEnv() (err=%d)", status);
    1.37      }
    1.38  }
    1.39  
    1.40 @@ -1160,7 +1174,7 @@
    1.41       * Note: You can call this function any number of times for the same thread, there's no harm in it
    1.42       *       (except for some lost CPU cycles)
    1.43       */
    1.44 -    pthread_setspecific(mThreadKey, (void *)env);
    1.45 +    Android_JNI_SetEnv(env);
    1.46  
    1.47      return env;
    1.48  }