android: thread local env

This commit is contained in:
weihuoya
2019-05-29 20:22:26 +08:00
parent 3132839113
commit 0dec8feadb
6 changed files with 33 additions and 70 deletions

View File

@ -29,12 +29,7 @@ void WiimoteScannerAndroid::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
NOTICE_LOG(WIIMOTE, "Finding Wiimotes");
JNIEnv* env;
int get_env_status =
IDCache::GetJavaVM()->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
if (get_env_status == JNI_EDETACHED)
IDCache::GetJavaVM()->AttachCurrentThread(&env, nullptr);
JNIEnv* env = IDCache::GetEnvForThread();
jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z");
jmethodID queryadapter_func = env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z");
@ -45,9 +40,6 @@ void WiimoteScannerAndroid::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
for (int i = 0; i < MAX_WIIMOTES; ++i)
found_wiimotes.emplace_back(new WiimoteAndroid(i));
}
if (get_env_status == JNI_EDETACHED)
IDCache::GetJavaVM()->DetachCurrentThread();
}
WiimoteAndroid::WiimoteAndroid(int index) : Wiimote(), m_mayflash_index(index)
@ -62,7 +54,7 @@ WiimoteAndroid::~WiimoteAndroid()
// Connect to a Wiimote with a known address.
bool WiimoteAndroid::ConnectInternal()
{
IDCache::GetJavaVM()->AttachCurrentThread(&m_env, nullptr);
m_env = IDCache::GetEnvForThread();
jfieldID payload_field = m_env->GetStaticFieldID(s_adapter_class, "wiimote_payload", "[[B");
jobjectArray payload_object =
@ -81,7 +73,6 @@ bool WiimoteAndroid::ConnectInternal()
void WiimoteAndroid::DisconnectInternal()
{
IDCache::GetJavaVM()->DetachCurrentThread();
}
bool WiimoteAndroid::IsConnected() const
@ -119,9 +110,7 @@ int WiimoteAndroid::IOWrite(u8 const* buf, size_t len)
void InitAdapterClass()
{
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, nullptr);
JNIEnv* env = IDCache::GetEnvForThread();
jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_WiimoteAdapter");
s_adapter_class = reinterpret_cast<jclass>(env->NewGlobalRef(adapter_class));
}

View File

@ -237,10 +237,8 @@ void Touchscreen::Motor::SetState(ControlState state)
void Touchscreen::Motor::Rumble(int padID, double state)
{
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, nullptr);
JNIEnv* env = IDCache::GetEnvForThread();
env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), IDCache::GetDoRumble(), padID, state);
IDCache::GetJavaVM()->DetachCurrentThread();
}
} // namespace Android
} // namespace ciface

View File

@ -65,8 +65,7 @@ static void ScanThreadFunc()
Common::SetCurrentThreadName("GC Adapter Scanning Thread");
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread started");
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, NULL);
JNIEnv* env = IDCache::GetEnvForThread();
jmethodID queryadapter_func = env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z");
@ -77,7 +76,6 @@ static void ScanThreadFunc()
Setup();
Common::SleepCurrentThread(1000);
}
IDCache::GetJavaVM()->DetachCurrentThread();
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped");
}
@ -87,8 +85,7 @@ static void Write()
Common::SetCurrentThreadName("GC Adapter Write Thread");
NOTICE_LOG(SERIALINTERFACE, "GC Adapter write thread started");
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, NULL);
JNIEnv* env = IDCache::GetEnvForThread();
jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I");
while (s_write_adapter_thread_running.IsSet())
@ -118,8 +115,6 @@ static void Write()
Common::YieldCPU();
}
IDCache::GetJavaVM()->DetachCurrentThread();
NOTICE_LOG(SERIALINTERFACE, "GC Adapter write thread stopped");
}
@ -129,8 +124,7 @@ static void Read()
NOTICE_LOG(SERIALINTERFACE, "GC Adapter read thread started");
bool first_read = true;
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, NULL);
JNIEnv* env = IDCache::GetEnvForThread();
jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B");
jobject payload_object = env->GetStaticObjectField(s_adapter_class, payload_field);
@ -184,8 +178,6 @@ static void Read()
s_fd = 0;
s_detected = false;
IDCache::GetJavaVM()->DetachCurrentThread();
NOTICE_LOG(SERIALINTERFACE, "GC Adapter read thread stopped");
}
@ -202,8 +194,7 @@ void Init()
s_last_init = CoreTiming::GetTicks();
}
JNIEnv* env;
IDCache::GetJavaVM()->AttachCurrentThread(&env, NULL);
JNIEnv* env = IDCache::GetEnvForThread();
jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter");
s_adapter_class = reinterpret_cast<jclass>(env->NewGlobalRef(adapter_class));