ANDROID: Store JNI environment in a thread local variable
This avoids to query JVM every time we need to do a JNI call. A different environment is attached to each thread, hence the TLS variable.
This commit is contained in:
parent
54fd20c36c
commit
e17b34c9dc
3 changed files with 40 additions and 9 deletions
|
@ -58,6 +58,8 @@ jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
|
|||
return JNI::onLoad(vm);
|
||||
}
|
||||
|
||||
pthread_key_t JNI::_env_tls;
|
||||
|
||||
JavaVM *JNI::_vm = 0;
|
||||
jobject JNI::_jobj = 0;
|
||||
jobject JNI::_jobj_audio_track = 0;
|
||||
|
@ -141,6 +143,10 @@ JNI::~JNI() {
|
|||
}
|
||||
|
||||
jint JNI::onLoad(JavaVM *vm) {
|
||||
if (pthread_key_create(&_env_tls, NULL)) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
_vm = vm;
|
||||
|
||||
JNIEnv *env;
|
||||
|
@ -148,6 +154,10 @@ jint JNI::onLoad(JavaVM *vm) {
|
|||
if (_vm->GetEnv((void **)&env, JNI_VERSION_1_2))
|
||||
return JNI_ERR;
|
||||
|
||||
if (pthread_setspecific(_env_tls, env)) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
jclass cls = env->FindClass("org/scummvm/scummvm/ScummVM");
|
||||
if (cls == 0)
|
||||
return JNI_ERR;
|
||||
|
@ -158,8 +168,8 @@ jint JNI::onLoad(JavaVM *vm) {
|
|||
return JNI_VERSION_1_2;
|
||||
}
|
||||
|
||||
JNIEnv *JNI::getEnv() {
|
||||
JNIEnv *env = 0;
|
||||
JNIEnv *JNI::fetchEnv() {
|
||||
JNIEnv *env;
|
||||
|
||||
jint res = _vm->GetEnv((void **)&env, JNI_VERSION_1_2);
|
||||
|
||||
|
@ -168,6 +178,8 @@ JNIEnv *JNI::getEnv() {
|
|||
abort();
|
||||
}
|
||||
|
||||
pthread_setspecific(_env_tls, env);
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
|
@ -180,9 +192,16 @@ void JNI::attachThread() {
|
|||
LOGE("AttachCurrentThread() failed: %d", res);
|
||||
abort();
|
||||
}
|
||||
|
||||
if (pthread_setspecific(_env_tls, env)) {
|
||||
LOGE("pthread_setspecific() failed");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void JNI::detachThread() {
|
||||
pthread_setspecific(_env_tls, NULL);
|
||||
|
||||
jint res = _vm->DetachCurrentThread();
|
||||
|
||||
if (res != JNI_OK) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue