Android: use pthread_once for creating thread key 'mThreadKey'
authorSylvain Becker <sylvain.becker@gmail.com>
Fri, 11 Jan 2019 15:27:53 +0100
changeset 125262fd98a88f320
parent 12525 3bd1e8f5cfdf
child 12527 0aa9da921db6
Android: use pthread_once for creating thread key 'mThreadKey'
src/core/android/SDL_android.c
     1.1 --- a/src/core/android/SDL_android.c	Fri Jan 11 14:50:43 2019 +0100
     1.2 +++ b/src/core/android/SDL_android.c	Fri Jan 11 15:27:53 2019 +0100
     1.3 @@ -225,7 +225,8 @@
     1.4                                 Globals
     1.5  *******************************************************************************/
     1.6  static pthread_key_t mThreadKey;
     1.7 -static JavaVM *mJavaVM;
     1.8 +static pthread_once_t key_once = PTHREAD_ONCE_INIT;
     1.9 +static JavaVM *mJavaVM = NULL;
    1.10  
    1.11  /* Main activity */
    1.12  static jclass mActivityClass;
    1.13 @@ -299,6 +300,24 @@
    1.14                   Functions called by JNI
    1.15  *******************************************************************************/
    1.16  
    1.17 +static void
    1.18 +Android_JNI_CreateKey()
    1.19 +{
    1.20 +    int status = pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed);
    1.21 +    if (status < 0) {
    1.22 +        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing mThreadKey with pthread_key_create() (err=%d)", status);
    1.23 +    }
    1.24 +}
    1.25 +
    1.26 +static void 
    1.27 +Android_JNI_CreateKey_once()
    1.28 +{
    1.29 +    int status = pthread_once(&key_once, Android_JNI_CreateKey);
    1.30 +    if (status < 0) {
    1.31 +        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing mThreadKey with pthread_once() (err=%d)", status);
    1.32 +    }
    1.33 +}
    1.34 +
    1.35  /* Library init */
    1.36  JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
    1.37  {
    1.38 @@ -313,9 +332,8 @@
    1.39       * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
    1.40       * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
    1.41       */
    1.42 -    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed) != 0) {
    1.43 -        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
    1.44 -    }
    1.45 +    Android_JNI_CreateKey_once();
    1.46 +    
    1.47      Android_JNI_SetupThread();
    1.48  
    1.49      return JNI_VERSION_1_4;
    1.50 @@ -344,7 +362,7 @@
    1.51      }
    1.52  
    1.53      if (Android_ActivityMutex == NULL) {
    1.54 -        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivityMutex mutex");
    1.55 +        __android_log_print(ANDROID_LOG_ERROR, "SDL", "failed to create Android_ActivityMutex mutex");
    1.56      }
    1.57  
    1.58      Android_JNI_SetupThread();