Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Implement basic automatic attachment/detachment of c++ threads to JVM. (
Browse files Browse the repository at this point in the history
dropbox#405)

* Implement basic automatic attachment/detachment of c++ threads to JVM.

No side effects if not enabled
Compile with -DEXPERIMENTAL_AUTO_CPP_THREAD_ATTACH to enable
Requires a compiler with C++11 support
Borrows code/idea from dropbox#372 (comment)

* Place assignment outside of define.
  • Loading branch information
mjmacleod authored and codingrhythm committed Apr 12, 2019
1 parent 435f13d commit a3d64ab
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion support-lib/jni/djinni_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ void jniShutdown() {
JNIEnv * jniGetThreadEnv() {
assert(g_cachedJVM);
JNIEnv * env = nullptr;
const jint get_res = g_cachedJVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
jint get_res = g_cachedJVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
#ifdef EXPERIMENTAL_AUTO_CPP_THREAD_ATTACH
if (get_res == JNI_EDETACHED) {
get_res = g_cachedJVM->AttachCurrentThread(&env, nullptr);
thread_local struct DetachOnExit {
~DetachOnExit() {
g_cachedJVM->DetachCurrentThread();
}
} detachOnExit;
}
#endif
if (get_res != 0 || !env) {
// :(
std::abort();
Expand Down

0 comments on commit a3d64ab

Please sign in to comment.