Android: use Mutex instead of Semphore for bug 4142
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 03 Jan 2019 20:18:29 +0100
changeset 12498c2e223dc5da9
parent 12497 9e13f3286831
child 12499 e6130a4cca7a
Android: use Mutex instead of Semphore for bug 4142
src/core/android/SDL_android.c
src/video/android/SDL_androidevents.c
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidvideo.h
src/video/android/SDL_androidwindow.c
     1.1 --- a/src/core/android/SDL_android.c	Thu Jan 03 16:22:33 2019 +0100
     1.2 +++ b/src/core/android/SDL_android.c	Thu Jan 03 20:18:29 2019 +0100
     1.3 @@ -322,15 +322,15 @@
     1.4  {
     1.5      __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeSetupJNI()");
     1.6  
     1.7 -    /* Use a semaphore to prevent concurrency issues between Java Activity and Native thread code, when using 'Android_Window'.
     1.8 +    /* Use a mutex to prevent concurrency issues between Java Activity and Native thread code, when using 'Android_Window'.
     1.9       * (Eg. Java sending Touch events, while native code is destroying the main SDL_Window. )
    1.10       */
    1.11 -    if (Android_ActivitySem == NULL) {
    1.12 -        Android_ActivitySem = SDL_CreateSemaphore(1); /* Could this be created twice if onCreate() is called a second time ? */
    1.13 +    if (Android_ActivityMutex == NULL) {
    1.14 +        Android_ActivityMutex = SDL_CreateMutex(); /* Could this be created twice if onCreate() is called a second time ? */
    1.15      }
    1.16  
    1.17 -    if (Android_ActivitySem == NULL) {
    1.18 -        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivitySem semaphore");
    1.19 +    if (Android_ActivityMutex == NULL) {
    1.20 +        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivityMutex mutex");
    1.21      }
    1.22  
    1.23      Android_JNI_SetupThread();
    1.24 @@ -569,11 +569,11 @@
    1.25                                      jint surfaceWidth, jint surfaceHeight,
    1.26                                      jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
    1.27  {
    1.28 -    SDL_SemWait(Android_ActivitySem);
    1.29 +    SDL_LockMutex(Android_ActivityMutex);
    1.30  
    1.31      Android_SetScreenResolution(Android_Window, surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
    1.32  
    1.33 -    SDL_SemPost(Android_ActivitySem);
    1.34 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.35  }
    1.36  
    1.37  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
    1.38 @@ -665,7 +665,7 @@
    1.39  /* Surface Created */
    1.40  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, jclass jcls)
    1.41  {
    1.42 -    SDL_SemWait(Android_ActivitySem);
    1.43 +    SDL_LockMutex(Android_ActivityMutex);
    1.44  
    1.45      if (Android_Window && Android_Window->driverdata)
    1.46      {
    1.47 @@ -684,13 +684,13 @@
    1.48          /* GL Context handling is done in the event loop because this function is run from the Java thread */
    1.49      }
    1.50  
    1.51 -    SDL_SemPost(Android_ActivitySem);
    1.52 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.53  }
    1.54  
    1.55  /* Surface Destroyed */
    1.56  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env, jclass jcls)
    1.57  {
    1.58 -    SDL_SemWait(Android_ActivitySem);
    1.59 +    SDL_LockMutex(Android_ActivityMutex);
    1.60  
    1.61      if (Android_Window && Android_Window->driverdata)
    1.62      {
    1.63 @@ -711,7 +711,7 @@
    1.64          /* GL Context handling is done in the event loop because this function is run from the Java thread */
    1.65      }
    1.66  
    1.67 -    SDL_SemPost(Android_ActivitySem);
    1.68 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.69  }
    1.70  
    1.71  /* Keydown */
    1.72 @@ -745,11 +745,11 @@
    1.73                                      jint touch_device_id_in, jint pointer_finger_id_in,
    1.74                                      jint action, jfloat x, jfloat y, jfloat p)
    1.75  {
    1.76 -    SDL_SemWait(Android_ActivitySem);
    1.77 +    SDL_LockMutex(Android_ActivityMutex);
    1.78  
    1.79      Android_OnTouch(Android_Window, touch_device_id_in, pointer_finger_id_in, action, x, y, p);
    1.80  
    1.81 -    SDL_SemPost(Android_ActivitySem);
    1.82 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.83  }
    1.84  
    1.85  /* Mouse */
    1.86 @@ -757,11 +757,11 @@
    1.87                                      JNIEnv *env, jclass jcls,
    1.88                                      jint button, jint action, jfloat x, jfloat y, jboolean relative)
    1.89  {
    1.90 -    SDL_SemWait(Android_ActivitySem);
    1.91 +    SDL_LockMutex(Android_ActivityMutex);
    1.92  
    1.93      Android_OnMouse(Android_Window, button, action, x, y, relative);
    1.94  
    1.95 -    SDL_SemPost(Android_ActivitySem);
    1.96 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.97  }
    1.98  
    1.99  /* Accelerometer */
   1.100 @@ -809,7 +809,7 @@
   1.101  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
   1.102                                      JNIEnv *env, jclass cls)
   1.103  {
   1.104 -    SDL_SemWait(Android_ActivitySem);
   1.105 +    SDL_LockMutex(Android_ActivityMutex);
   1.106  
   1.107      __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
   1.108  
   1.109 @@ -824,14 +824,14 @@
   1.110          if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
   1.111      }
   1.112  
   1.113 -    SDL_SemPost(Android_ActivitySem);
   1.114 +    SDL_UnlockMutex(Android_ActivityMutex);
   1.115  }
   1.116  
   1.117  /* Resume */
   1.118  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)(
   1.119                                      JNIEnv *env, jclass cls)
   1.120  {
   1.121 -    SDL_SemWait(Android_ActivitySem);
   1.122 +    SDL_LockMutex(Android_ActivityMutex);
   1.123  
   1.124      __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
   1.125  
   1.126 @@ -854,7 +854,7 @@
   1.127          if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
   1.128      }
   1.129  
   1.130 -    SDL_SemPost(Android_ActivitySem);
   1.131 +    SDL_UnlockMutex(Android_ActivityMutex);
   1.132  }
   1.133  
   1.134  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
     2.1 --- a/src/video/android/SDL_androidevents.c	Thu Jan 03 16:22:33 2019 +0100
     2.2 +++ b/src/video/android/SDL_androidevents.c	Thu Jan 03 20:18:29 2019 +0100
     2.3 @@ -80,9 +80,9 @@
     2.4  #if SDL_ANDROID_BLOCK_ON_PAUSE
     2.5      if (isPaused && !isPausing) {
     2.6          /* Make sure this is the last thing we do before pausing */
     2.7 -        SDL_SemWait(Android_ActivitySem);
     2.8 +        SDL_LockMutex(Android_ActivityMutex);
     2.9          android_egl_context_backup(Android_Window);
    2.10 -        SDL_SemPost(Android_ActivitySem);
    2.11 +        SDL_UnlockMutex(Android_ActivityMutex);
    2.12  
    2.13          ANDROIDAUDIO_PauseDevices();
    2.14          if (SDL_SemWait(Android_ResumeSem) == 0) {
    2.15 @@ -94,9 +94,9 @@
    2.16              ANDROIDAUDIO_ResumeDevices();
    2.17              /* Restore the GL Context from here, as this operation is thread dependent */
    2.18              if (!SDL_HasEvent(SDL_QUIT)) {
    2.19 -                SDL_SemWait(Android_ActivitySem);
    2.20 +                SDL_LockMutex(Android_ActivityMutex);
    2.21                  android_egl_context_restore(Android_Window);
    2.22 -                SDL_SemPost(Android_ActivitySem);
    2.23 +                SDL_UnlockMutex(Android_ActivityMutex);
    2.24              }
    2.25          }
    2.26      }
    2.27 @@ -115,9 +115,9 @@
    2.28          }
    2.29  #else
    2.30          if (SDL_SemTryWait(Android_PauseSem) == 0) {
    2.31 -            SDL_SemWait(Android_ActivitySem);
    2.32 +            SDL_LockMutex(Android_ActivityMutex);
    2.33              android_egl_context_backup(Android_Window);
    2.34 -            SDL_SemPost(Android_ActivitySem);
    2.35 +            SDL_UnlockMutex(Android_ActivityMutex);
    2.36  
    2.37              ANDROIDAUDIO_PauseDevices();
    2.38              isPaused = 1;
     3.1 --- a/src/video/android/SDL_androidvideo.c	Thu Jan 03 16:22:33 2019 +0100
     3.2 +++ b/src/video/android/SDL_androidvideo.c	Thu Jan 03 20:18:29 2019 +0100
     3.3 @@ -66,7 +66,8 @@
     3.4  static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
     3.5  static int Android_ScreenRate = 0;
     3.6  
     3.7 -SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL, *Android_ActivitySem = NULL;
     3.8 +SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
     3.9 +SDL_mutex *Android_ActivityMutex = NULL;
    3.10  
    3.11  static int
    3.12  Android_Available(void)
     4.1 --- a/src/video/android/SDL_androidvideo.h	Thu Jan 03 16:22:33 2019 +0100
     4.2 +++ b/src/video/android/SDL_androidvideo.h	Thu Jan 03 20:18:29 2019 +0100
     4.3 @@ -41,7 +41,8 @@
     4.4  extern int Android_SurfaceHeight;
     4.5  extern int Android_DeviceWidth;
     4.6  extern int Android_DeviceHeight;
     4.7 -extern SDL_sem *Android_PauseSem, *Android_ResumeSem, *Android_ActivitySem;
     4.8 +extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
     4.9 +extern SDL_mutex *Android_ActivityMutex;
    4.10  
    4.11  #endif /* SDL_androidvideo_h_ */
    4.12  
     5.1 --- a/src/video/android/SDL_androidwindow.c	Thu Jan 03 16:22:33 2019 +0100
     5.2 +++ b/src/video/android/SDL_androidwindow.c	Thu Jan 03 20:18:29 2019 +0100
     5.3 @@ -42,7 +42,7 @@
     5.4      SDL_WindowData *data;
     5.5      int retval = 0;
     5.6  
     5.7 -    SDL_SemWait(Android_ActivitySem);
     5.8 +    SDL_LockMutex(Android_ActivityMutex);
     5.9  
    5.10      if (Android_Window) {
    5.11          retval = SDL_SetError("Android only supports one window");
    5.12 @@ -102,7 +102,7 @@
    5.13  
    5.14  endfunction:
    5.15  
    5.16 -    SDL_SemPost(Android_ActivitySem);
    5.17 +    SDL_UnlockMutex(Android_ActivityMutex);
    5.18  
    5.19      return retval;
    5.20  }
    5.21 @@ -151,7 +151,7 @@
    5.22  void
    5.23  Android_DestroyWindow(_THIS, SDL_Window *window)
    5.24  {
    5.25 -    SDL_SemWait(Android_ActivitySem);
    5.26 +    SDL_LockMutex(Android_ActivityMutex);
    5.27  
    5.28      if (window == Android_Window) {
    5.29          Android_Window = NULL;
    5.30 @@ -173,7 +173,7 @@
    5.31          }
    5.32      }
    5.33  
    5.34 -    SDL_SemPost(Android_ActivitySem);
    5.35 +    SDL_UnlockMutex(Android_ActivityMutex);
    5.36  }
    5.37  
    5.38  SDL_bool