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
This commit is contained in:
dhewg 2011-02-14 18:48:33 +01:00
parent ea2cfc44c0
commit 2586e15e4e

View file

@ -102,10 +102,14 @@ static jfieldID FID_ScummVM_nativeScummVM;
static jmethodID MID_Object_wait; static jmethodID MID_Object_wait;
JNIEnv* JNU_GetEnv() { JNIEnv* JNU_GetEnv() {
JNIEnv* env; JNIEnv* env = 0;
bool version_unsupported =
cached_jvm->GetEnv((void**)&env, JNI_VERSION_1_2); jint res = cached_jvm->GetEnv((void**)&env, JNI_VERSION_1_2);
assert(! version_unsupported); if (res != JNI_OK) {
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "GetEnv() failed: %d", res);
abort();
}
return env; return env;
} }
@ -453,6 +457,14 @@ void* OSystem_Android::timerThreadFunc(void* arg) {
OSystem_Android* system = (OSystem_Android*)arg; OSystem_Android* system = (OSystem_Android*)arg;
DefaultTimerManager* timer = (DefaultTimerManager*)(system->_timer); 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; struct timespec tv;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_nsec = 100 * 1000 * 1000; // 100ms tv.tv_nsec = 100 * 1000 * 1000; // 100ms
@ -462,6 +474,13 @@ void* OSystem_Android::timerThreadFunc(void* arg) {
nanosleep(&tv, NULL); 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; return NULL;
} }