From 2586e15e4ea6a365eced1888759630907b6b5870 Mon Sep 17 00:00:00 2001 From: dhewg Date: Mon, 14 Feb 2011 18:48:33 +0100 Subject: ANDROID: Fix JNI calls for the timer thread JNI calls can happen through the timer mechanism (hence from another thread). Attach the timer thread to the VM to prevent assert()s --- backends/platform/android/android.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'backends') diff --git a/backends/platform/android/android.cpp b/backends/platform/android/android.cpp index b59fc76a59..07fb1d5d98 100644 --- a/backends/platform/android/android.cpp +++ b/backends/platform/android/android.cpp @@ -102,10 +102,14 @@ static jfieldID FID_ScummVM_nativeScummVM; static jmethodID MID_Object_wait; JNIEnv* JNU_GetEnv() { - JNIEnv* env; - bool version_unsupported = - cached_jvm->GetEnv((void**)&env, JNI_VERSION_1_2); - assert(! version_unsupported); + JNIEnv* env = 0; + + jint res = cached_jvm->GetEnv((void**)&env, JNI_VERSION_1_2); + if (res != JNI_OK) { + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "GetEnv() failed: %d", res); + abort(); + } + return env; } @@ -453,6 +457,14 @@ void* OSystem_Android::timerThreadFunc(void* arg) { OSystem_Android* system = (OSystem_Android*)arg; DefaultTimerManager* timer = (DefaultTimerManager*)(system->_timer); + JNIEnv *env = 0; + jint res = cached_jvm->AttachCurrentThread(&env, 0); + + if (res != JNI_OK) { + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "AttachCurrentThread() failed: %d", res); + abort(); + } + struct timespec tv; tv.tv_sec = 0; tv.tv_nsec = 100 * 1000 * 1000; // 100ms @@ -462,6 +474,13 @@ void* OSystem_Android::timerThreadFunc(void* arg) { nanosleep(&tv, NULL); } + res = cached_jvm->DetachCurrentThread(); + + if (res != JNI_OK) { + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "DetachCurrentThread() failed: %d", res); + abort(); + } + return NULL; } -- cgit v1.2.3