diff --git a/engine/dlib/src/dmsdk/dlib/thread.h b/engine/dlib/src/dmsdk/dlib/thread.h index dfb107fbd29..cae0dcb76dd 100644 --- a/engine/dlib/src/dmsdk/dlib/thread.h +++ b/engine/dlib/src/dmsdk/dlib/thread.h @@ -106,7 +106,7 @@ namespace dmThread * Detach thread. When a detached thread terminates, its resources are * automatically released back to the system without the need for another * thread to join with the terminated thread. - * @name dmThread::Join + * @name dmThread::Detach * @param thread Thread to detach */ void Detach(Thread thread); diff --git a/engine/engine/src/engine_main.cpp b/engine/engine/src/engine_main.cpp index 6442d09d3a2..a69acda9217 100644 --- a/engine/engine/src/engine_main.cpp +++ b/engine/engine/src/engine_main.cpp @@ -117,6 +117,11 @@ int engine_main(int argc, char *argv[]) { glfwAndroidPollEvents(); dmTime::Sleep(0); + if (g_AndroidApp->destroyRequested) { + // App requested exit. It doesn't wait when thread work finished because app is in background already. + // App will never end up here from within the app itself, only using OS functions. + return 0; + } } dmThread::Join(t); diff --git a/engine/glfw/lib/android/android_init.c b/engine/glfw/lib/android/android_init.c index c9bd3e7b875..655906bf623 100644 --- a/engine/glfw/lib/android/android_init.c +++ b/engine/glfw/lib/android/android_init.c @@ -296,9 +296,7 @@ void _glfwAndroidHandleCommand(struct android_app* app, int32_t cmd) { computeIconifiedState(); break; case APP_CMD_DESTROY: - _glfwWin.opened = 0; - final_gl(&_glfwWinAndroid); - computeIconifiedState(); + androidDestroyWindow(); break; } } diff --git a/engine/glfw/lib/android/android_util.h b/engine/glfw/lib/android/android_util.h index c9f8a1578b0..a6b0a4fcb13 100644 --- a/engine/glfw/lib/android/android_util.h +++ b/engine/glfw/lib/android/android_util.h @@ -67,10 +67,12 @@ void* acquire_gl_aux_context(_GLFWwin_android* win); void unacquire_gl_aux_context(_GLFWwin_android* win); void computeIconifiedState(); +void androidDestroyWindow(); void _glfwAndroidHandleCommand(struct android_app* app, int32_t cmd); int32_t _glfwAndroidHandleInput(struct android_app* app, JNIEnv* env, struct InputEvent* event); + // Should only called after an error // returns 1 if we the window/surface was ok // returns 0 if we the window/surface was bad diff --git a/engine/glfw/lib/android/android_window.c b/engine/glfw/lib/android/android_window.c index 0681325dfcc..26533e58bea 100644 --- a/engine/glfw/lib/android/android_window.c +++ b/engine/glfw/lib/android/android_window.c @@ -418,6 +418,15 @@ void glfwAndroidFlushEvents() spinlock_unlock(&g_EventLock); } +void androidDestroyWindow( void ) +{ + if (_glfwWin.opened) { + _glfwWin.opened = 0; + final_gl(&_glfwWinAndroid); + computeIconifiedState(); + } +} + // Called from the engine thread void _glfwPlatformPollEvents( void ) { @@ -438,22 +447,28 @@ void _glfwPlatformPollEvents( void ) // Called from the looper thread void glfwAndroidPollEvents() { - int ident; - int events; - struct android_poll_source* source; - - int timeoutMillis = 0; - if (_glfwWin.iconified) { - timeoutMillis = 1000 * 3; - } - while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0) - { - timeoutMillis = 0; - // Process this event. - if (source != NULL) { - source->process(_glfwWinAndroid.app, source); - } - } + int ident; + int events; + struct android_poll_source* source; + + int timeoutMillis = 0; + if (_glfwWin.iconified) { + timeoutMillis = 1000 * 2; + } + while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0) + { + timeoutMillis = 0; + // Process this event. + if (source != NULL) { + source->process(_glfwWinAndroid.app, source); + } + + if (_glfwWinAndroid.app->destroyRequested) { + androidDestroyWindow(); + // OS is destroyng the app. All the other events doesn't matter in this case. + return; + } + } } //========================================================================