Android: use Mutex instead of Semphore for bug 4142

This commit is contained in:
Sylvain Becker 2019-01-03 20:18:29 +01:00
parent 23478642bd
commit 2e19343df6
5 changed files with 33 additions and 31 deletions

View File

@ -322,15 +322,15 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jclass c
{ {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeSetupJNI()"); __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeSetupJNI()");
/* Use a semaphore to prevent concurrency issues between Java Activity and Native thread code, when using 'Android_Window'. /* Use a mutex to prevent concurrency issues between Java Activity and Native thread code, when using 'Android_Window'.
* (Eg. Java sending Touch events, while native code is destroying the main SDL_Window. ) * (Eg. Java sending Touch events, while native code is destroying the main SDL_Window. )
*/ */
if (Android_ActivitySem == NULL) { if (Android_ActivityMutex == NULL) {
Android_ActivitySem = SDL_CreateSemaphore(1); /* Could this be created twice if onCreate() is called a second time ? */ Android_ActivityMutex = SDL_CreateMutex(); /* Could this be created twice if onCreate() is called a second time ? */
} }
if (Android_ActivitySem == NULL) { if (Android_ActivityMutex == NULL) {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivitySem semaphore"); __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "failed to create Android_ActivityMutex mutex");
} }
Android_JNI_SetupThread(); Android_JNI_SetupThread();
@ -569,11 +569,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
jint surfaceWidth, jint surfaceHeight, jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jint format, jfloat rate) jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
Android_SetScreenResolution(Android_Window, surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate); Android_SetScreenResolution(Android_Window, surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
@ -665,7 +665,7 @@ JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(nativeRemoveHaptic)(
/* Surface Created */ /* Surface Created */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, jclass jcls) JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, jclass jcls)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
if (Android_Window && Android_Window->driverdata) if (Android_Window && Android_Window->driverdata)
{ {
@ -684,13 +684,13 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv *env, j
/* GL Context handling is done in the event loop because this function is run from the Java thread */ /* GL Context handling is done in the event loop because this function is run from the Java thread */
} }
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Surface Destroyed */ /* Surface Destroyed */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env, jclass jcls) JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env, jclass jcls)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
if (Android_Window && Android_Window->driverdata) if (Android_Window && Android_Window->driverdata)
{ {
@ -711,7 +711,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv *env,
/* GL Context handling is done in the event loop because this function is run from the Java thread */ /* GL Context handling is done in the event loop because this function is run from the Java thread */
} }
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Keydown */ /* Keydown */
@ -745,11 +745,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
jint touch_device_id_in, jint pointer_finger_id_in, jint touch_device_id_in, jint pointer_finger_id_in,
jint action, jfloat x, jfloat y, jfloat p) jint action, jfloat x, jfloat y, jfloat p)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
Android_OnTouch(Android_Window, touch_device_id_in, pointer_finger_id_in, action, x, y, p); Android_OnTouch(Android_Window, touch_device_id_in, pointer_finger_id_in, action, x, y, p);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Mouse */ /* Mouse */
@ -757,11 +757,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,
jint button, jint action, jfloat x, jfloat y, jboolean relative) jint button, jint action, jfloat x, jfloat y, jboolean relative)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
Android_OnMouse(Android_Window, button, action, x, y, relative); Android_OnMouse(Android_Window, button, action, x, y, relative);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Accelerometer */ /* Accelerometer */
@ -809,7 +809,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeQuit)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
JNIEnv *env, jclass cls) JNIEnv *env, jclass cls)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()"); __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
@ -824,14 +824,14 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem); if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
} }
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Resume */ /* Resume */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)(
JNIEnv *env, jclass cls) JNIEnv *env, jclass cls)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()"); __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
@ -854,7 +854,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)(
if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem); if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
} }
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(

View File

@ -80,9 +80,9 @@ Android_PumpEvents(_THIS)
#if SDL_ANDROID_BLOCK_ON_PAUSE #if SDL_ANDROID_BLOCK_ON_PAUSE
if (isPaused && !isPausing) { if (isPaused && !isPausing) {
/* Make sure this is the last thing we do before pausing */ /* Make sure this is the last thing we do before pausing */
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
android_egl_context_backup(Android_Window); android_egl_context_backup(Android_Window);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
if (SDL_SemWait(Android_ResumeSem) == 0) { if (SDL_SemWait(Android_ResumeSem) == 0) {
@ -94,9 +94,9 @@ Android_PumpEvents(_THIS)
ANDROIDAUDIO_ResumeDevices(); ANDROIDAUDIO_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */ /* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) { if (!SDL_HasEvent(SDL_QUIT)) {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
android_egl_context_restore(Android_Window); android_egl_context_restore(Android_Window);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
} }
} }
@ -115,9 +115,9 @@ Android_PumpEvents(_THIS)
} }
#else #else
if (SDL_SemTryWait(Android_PauseSem) == 0) { if (SDL_SemTryWait(Android_PauseSem) == 0) {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
android_egl_context_backup(Android_Window); android_egl_context_backup(Android_Window);
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
isPaused = 1; isPaused = 1;

View File

@ -66,7 +66,8 @@ int Android_DeviceHeight = 0;
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN; static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
static int Android_ScreenRate = 0; static int Android_ScreenRate = 0;
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL, *Android_ActivitySem = NULL; SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
SDL_mutex *Android_ActivityMutex = NULL;
static int static int
Android_Available(void) Android_Available(void)

View File

@ -41,7 +41,8 @@ extern int Android_SurfaceWidth;
extern int Android_SurfaceHeight; extern int Android_SurfaceHeight;
extern int Android_DeviceWidth; extern int Android_DeviceWidth;
extern int Android_DeviceHeight; extern int Android_DeviceHeight;
extern SDL_sem *Android_PauseSem, *Android_ResumeSem, *Android_ActivitySem; extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
extern SDL_mutex *Android_ActivityMutex;
#endif /* SDL_androidvideo_h_ */ #endif /* SDL_androidvideo_h_ */

View File

@ -42,7 +42,7 @@ Android_CreateWindow(_THIS, SDL_Window * window)
SDL_WindowData *data; SDL_WindowData *data;
int retval = 0; int retval = 0;
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
if (Android_Window) { if (Android_Window) {
retval = SDL_SetError("Android only supports one window"); retval = SDL_SetError("Android only supports one window");
@ -102,7 +102,7 @@ Android_CreateWindow(_THIS, SDL_Window * window)
endfunction: endfunction:
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
return retval; return retval;
} }
@ -151,7 +151,7 @@ Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display
void void
Android_DestroyWindow(_THIS, SDL_Window *window) Android_DestroyWindow(_THIS, SDL_Window *window)
{ {
SDL_SemWait(Android_ActivitySem); SDL_LockMutex(Android_ActivityMutex);
if (window == Android_Window) { if (window == Android_Window) {
Android_Window = NULL; Android_Window = NULL;
@ -173,7 +173,7 @@ Android_DestroyWindow(_THIS, SDL_Window *window)
} }
} }
SDL_SemPost(Android_ActivitySem); SDL_UnlockMutex(Android_ActivityMutex);
} }
SDL_bool SDL_bool