Index: base/android/jni_android.cc |
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc |
index 59f25e2e817dbe6ea258d16b65b7d71e5bedb9ec..d89fd89f33c782927fa9fbf4fb9a48ec1c0337df 100644 |
--- a/base/android/jni_android.cc |
+++ b/base/android/jni_android.cc |
@@ -10,6 +10,8 @@ |
#include "base/android/jni_string.h" |
#include "base/lazy_instance.h" |
#include "base/logging.h" |
+#include "base/threading/platform_thread.h" |
+#include "base/threading/thread_local.h" |
namespace { |
using base::android::GetClass; |
@@ -22,6 +24,10 @@ JavaVM* g_jvm = NULL; |
base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky |
g_application_context = LAZY_INSTANCE_INITIALIZER; |
+// Whether or not AttachCurrentThread() has been called before. |
+base::LazyInstance<base::ThreadLocalBoolean>::Leaky already_attached = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
ScopedJavaLocalRef<jclass> throwable_clazz = |
GetClass(env, "java/lang/Throwable"); |
@@ -76,7 +82,18 @@ namespace android { |
JNIEnv* AttachCurrentThread() { |
DCHECK(g_jvm); |
JNIEnv* env = NULL; |
- jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
+ jint ret = JNI_OK; |
+ // Passes thread name in args for first call. |
+ if (!already_attached.Get().Get()) { |
bulach
2014/06/17 17:26:55
hmm... AttachCurrentThread is a somewhat hot funct
byungchul
2014/06/17 17:51:05
Do you mean call AttachCurrentThread() in Platform
bulach
2014/06/17 20:45:33
tough choice :)
how many named threads which are n
byungchul
2014/06/17 21:11:11
I am not working on android chrome, but a differen
|
+ already_attached.Get().Set(true); |
+ JavaVMAttachArgs args; |
+ args.version = JNI_VERSION_1_2; |
+ args.name = PlatformThread::GetName(); |
+ args.group = NULL; |
+ ret = g_jvm->AttachCurrentThread(&env, &args); |
+ } else { |
+ ret = g_jvm->AttachCurrentThread(&env, NULL); |
+ } |
DCHECK_EQ(JNI_OK, ret); |
return env; |
} |
@@ -84,8 +101,10 @@ JNIEnv* AttachCurrentThread() { |
void DetachFromVM() { |
// Ignore the return value, if the thread is not attached, DetachCurrentThread |
// will fail. But it is ok as the native thread may never be attached. |
- if (g_jvm) |
+ if (g_jvm) { |
g_jvm->DetachCurrentThread(); |
+ already_attached.Get().Set(false); |
+ } |
} |
void InitVM(JavaVM* vm) { |