From 9d82f4e985f8218f6144e4736d576f97c451889e Mon Sep 17 00:00:00 2001 From: Sylvain Becker Date: Fri, 11 Jan 2019 15:27:53 +0100 Subject: [PATCH] Android: use pthread_once for creating thread key 'mThreadKey' --- src/core/android/SDL_android.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c index 7a0cb5e9337fa..3f932795480e7 100644 --- a/src/core/android/SDL_android.c +++ b/src/core/android/SDL_android.c @@ -225,7 +225,8 @@ static void checkJNIReady(void); Globals *******************************************************************************/ static pthread_key_t mThreadKey; -static JavaVM *mJavaVM; +static pthread_once_t key_once = PTHREAD_ONCE_INIT; +static JavaVM *mJavaVM = NULL; /* Main activity */ static jclass mActivityClass; @@ -299,6 +300,24 @@ static void Android_JNI_SetEnv(JNIEnv *env); Functions called by JNI *******************************************************************************/ +static void +Android_JNI_CreateKey() +{ + int status = pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed); + if (status < 0) { + __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing mThreadKey with pthread_key_create() (err=%d)", status); + } +} + +static void +Android_JNI_CreateKey_once() +{ + int status = pthread_once(&key_once, Android_JNI_CreateKey); + if (status < 0) { + __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing mThreadKey with pthread_once() (err=%d)", status); + } +} + /* Library init */ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { @@ -313,9 +332,8 @@ JNIEXPORT jint JNICALL 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) != 0) { - __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key"); - } + Android_JNI_CreateKey_once(); + Android_JNI_SetupThread(); return JNI_VERSION_1_4; @@ -344,7 +362,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c } if (Android_ActivityMutex == NULL) { - __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivityMutex mutex"); + __android_log_print(ANDROID_LOG_ERROR, "SDL", "failed to create Android_ActivityMutex mutex"); } Android_JNI_SetupThread();