Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: base/android/jni_android.cc

Issue 330823004: Set chrome thread name in JVM. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698