lyc*_*001 5 java-native-interface android android-ndk jnienv
我有一个JNI的Android项目.在实现侦听器类的CPP文件中,有一个回调x().当调用x()函数时,我想调用java类中的另一个函数.但是,为了调用那个java函数,我需要访问JNIEnv*.
我知道在回调的同一个cpp文件中,有一个函数:
static jboolean init (JNIEnv* env, jobject obj) {...}
Run Code Online (Sandbox Code Playgroud)
我应该在cpp文件中保存JNIEnv*作为成员变量init(..)
吗?并在以后回调发生时使用它?
对不起,我是JNI的初学者.
Mic*_*ael 15
缓存a JNIEnv*
并不是一个特别好的主意,因为你不能JNIEnv*
在多个线程中使用它,甚至可能无法在同一个线程上将它用于多个本机调用(请参阅http://android-developers.blogspot. se/2011/11/jni-local-reference-changes-in-ics.html)
编写一个函数来获取JNIEnv*
并在必要时将当前线程附加到VM并不太困难:
bool GetJniEnv(JavaVM *vm, JNIEnv **env) {
bool did_attach_thread = false;
*env = nullptr;
// Check if the current thread is attached to the VM
auto get_env_result = vm->GetEnv((void**)env, JNI_VERSION_1_6);
if (get_env_result == JNI_EDETACHED) {
if (vm->AttachCurrentThread(env, NULL) == JNI_OK) {
did_attach_thread = true;
} else {
// Failed to attach thread. Throw an exception if you want to.
}
} else if (get_env_result == JNI_EVERSION) {
// Unsupported JNI version. Throw an exception if you want to.
}
return did_attach_thread;
}
Run Code Online (Sandbox Code Playgroud)
你使用它的方式是:
JNIEnv *env;
bool did_attach = GetJniEnv(vm, &env);
// Use env...
// ...
if (did_attach) {
vm->DetachCurrentThread();
}
Run Code Online (Sandbox Code Playgroud)
你可以将它包装在一个附着在构造上的类中,并在破坏时分离,RAII风格:
class ScopedEnv {
public:
ScopedEnv() : attached_to_vm_(false) {
attached_to_vm_ = GetJniEnv(g_vm, &env_); // g_vm is a global
}
ScopedEnv(const ScopedEnv&) = delete;
ScopedEnv& operator=(const ScopedEnv&) = delete;
virtual ~ScopedEnv() {
if (attached_to_vm_) {
g_vm->DetachCurrentThread();
attached_to_vm_ = false;
}
}
JNIEnv *GetEnv() const { return env_; }
private:
bool attached_to_env_;
JNIEnv *env_;
};
// Usage:
{
ScopedEnv scoped_env;
scoped_env.GetEnv()->SomeJniFunction();
}
// scoped_env falls out of scope, the thread is automatically detached if necessary
Run Code Online (Sandbox Code Playgroud)
编辑:有时你可能有一个长期运行的本机线程,需要JNIEnv*
多次.在这种情况下,您可能希望避免不断地将线程连接到JVM或从JVM断开线程,但是仍然需要确保在线程销毁时分离线程.
您可以通过连接线只有一次,然后离开它连接,并通过设置使用线程销毁回调做到这一点pthread_key_create
,并pthread_setspecific
说将采取调用的照顾DetachCurrentThread
.
/**
* Get a JNIEnv* valid for this thread, regardless of whether
* we're on a native thread or a Java thread.
* If the calling thread is not currently attached to the JVM
* it will be attached, and then automatically detached when the
* thread is destroyed.
*/
JNIEnv *GetJniEnv() {
JNIEnv *env = nullptr;
// We still call GetEnv first to detect if the thread already
// is attached. This is done to avoid setting up a DetachCurrentThread
// call on a Java thread.
// g_vm is a global.
auto get_env_result = g_vm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (get_env_result == JNI_EDETACHED) {
if (g_vm->AttachCurrentThread(&env, NULL) == JNI_OK) {
DeferThreadDetach(env);
} else {
// Failed to attach thread. Throw an exception if you want to.
}
} else if (get_env_result == JNI_EVERSION) {
// Unsupported JNI version. Throw an exception if you want to.
}
return env;
}
void DeferThreadDetach(JNIEnv *env) {
static pthread_key_t thread_key;
// Set up a Thread Specific Data key, and a callback that
// will be executed when a thread is destroyed.
// This is only done once, across all threads, and the value
// associated with the key for any given thread will initially
// be NULL.
static auto run_once = [] {
const auto err = pthread_key_create(&thread_key, [] (void *ts_env) {
if (ts_env) {
g_vm->DetachCurrentThread();
}
});
if (err) {
// Failed to create TSD key. Throw an exception if you want to.
}
return 0;
}();
// For the callback to actually be executed when a thread exits
// we need to associate a non-NULL value with the key on that thread.
// We can use the JNIEnv* as that value.
const auto ts_env = pthread_getspecific(thread_key);
if (!ts_env) {
if (pthread_setspecific(thread_key, env)) {
// Failed to set thread-specific value for key. Throw an exception if you want to.
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果__cxa_thread_atexit
您可以使用,则可以使用在析构函数thread_local
中调用的某个对象来完成相同的操作DetachCurrentThread
.
归档时间: |
|
查看次数: |
1172 次 |
最近记录: |