From e38036e670d234b36053e03be92dd8f27acb16c0 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 22 Aug 2024 22:30:25 +0200 Subject: samba: Cleanup jni layer Remember that jni::GlobalRef can be used on multiple threads so we can't store the env, need to call AttachCurrentThread. Fix AttachCurrentThread (g_vm was forgotten). More consistent move and copy constructors in refs. Removed env() method as it would only be valid for Param and Local anyway. --- libs/samba/src/main/cpp/jni.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'libs/samba/src/main/cpp/jni.cpp') diff --git a/libs/samba/src/main/cpp/jni.cpp b/libs/samba/src/main/cpp/jni.cpp index aac1d28..5a69dc5 100644 --- a/libs/samba/src/main/cpp/jni.cpp +++ b/libs/samba/src/main/cpp/jni.cpp @@ -79,6 +79,22 @@ void _abort_with_exception(const char* file, int line, JNIEnv* env) { } } +JNIEnv* NonFatalAttachCurrentThread() { +#ifdef ANDROID + JNIEnv* env; + auto ret = g_vm->AttachCurrentThread(&env, nullptr); + if (ret == JNI_OK) + return env; + return nullptr; +#else + void* v_env; + auto ret = g_vm->AttachCurrentThread(&v_env, nullptr); + if (ret == JNI_OK) + return reinterpret_cast(v_env); + return nullptr; +#endif +} + } // namespace internal JNIEnv* AttachCurrentThread() { @@ -99,6 +115,7 @@ JNIEnv* OnLoad(JavaVM* vm) { void* v_env; auto ret = vm->GetEnv(&v_env, JNI_VERSION); ABORT_IF_NOT_OK(ret); + g_vm = vm; return reinterpret_cast(v_env); } -- cgit v1.2.3-70-g09d2