Fixed bug 4579 - SDL_android.c s_active not being atomic
authorSam Lantinga <slouken@libsdl.org>
Fri, 05 Apr 2019 08:15:01 -0700
changeset 126963ef8a628853d
parent 12695 0c4d04572622
child 12697 eabdcf59246a
Fixed bug 4579 - SDL_android.c s_active not being atomic

Isaias Brunet

This bug cause a false assert due to multiple threads modifying the same variable without any atomic operation.
src/core/android/SDL_android.c
     1.1 --- a/src/core/android/SDL_android.c	Fri Apr 05 08:10:12 2019 -0700
     1.2 +++ b/src/core/android/SDL_android.c	Fri Apr 05 08:15:01 2019 -0700
     1.3 @@ -1156,7 +1156,7 @@
     1.4               Functions called by SDL into Java
     1.5  *******************************************************************************/
     1.6  
     1.7 -static int s_active = 0;
     1.8 +static SDL_atomic_t s_active;
     1.9  struct LocalReferenceHolder
    1.10  {
    1.11      JNIEnv *m_env;
    1.12 @@ -1181,7 +1181,7 @@
    1.13          SDL_SetError("Failed to allocate enough JVM local references");
    1.14          return SDL_FALSE;
    1.15      }
    1.16 -    ++s_active;
    1.17 +    SDL_AtomicIncRef(&s_active);
    1.18      refholder->m_env = env;
    1.19      return SDL_TRUE;
    1.20  }
    1.21 @@ -1194,10 +1194,15 @@
    1.22      if (refholder->m_env) {
    1.23          JNIEnv *env = refholder->m_env;
    1.24          (*env)->PopLocalFrame(env, NULL);
    1.25 -        --s_active;
    1.26 +        SDL_AtomicDecRef(&s_active);
    1.27      }
    1.28  }
    1.29  
    1.30 +static SDL_bool LocalReferenceHolder_IsActive(void)
    1.31 +{
    1.32 +    return (SDL_AtomicGet(&s_active) > 0);
    1.33 +}
    1.34 +
    1.35  ANativeWindow* Android_JNI_GetNativeWindow(void)
    1.36  {
    1.37      ANativeWindow *anw = NULL;
    1.38 @@ -1601,7 +1606,7 @@
    1.39      jthrowable exception;
    1.40  
    1.41      /* Detect mismatch LocalReferenceHolder_Init/Cleanup */
    1.42 -    SDL_assert((s_active > 0));
    1.43 +    SDL_assert(LocalReferenceHolder_IsActive());
    1.44  
    1.45      exception = (*env)->ExceptionOccurred(env);
    1.46      if (exception != NULL) {