Remove race on cached thread-name deletion
Manage the thread name using an RCU-like scheme, in which the name
is an atomic pointer to an immutable string, and readers explicitly
maintain a count of active readers. Writers wait until there are
no readers before deallocating a name that was just replaced.
Currently this potentially blocks writers, but readers should be very
fast, and relatively infrequent, so I can't imagine this is a real
issue.
This also replaces JVM_SetNativeThreadName with a stub. This part of
the CL came from Nicolas' aosp/1706702, which this is otherwise
intended to replace.
Test: Build and boot AOSP.
Bug: 37970289
Change-Id: I7e1d13d24adc10b4009d60ee68bc9e837ce78b34
diff --git a/openjdkjvm/OpenjdkJvm.cc b/openjdkjvm/OpenjdkJvm.cc
index a9fa0fc6c..63503f4 100644
--- a/openjdkjvm/OpenjdkJvm.cc
+++ b/openjdkjvm/OpenjdkJvm.cc
@@ -406,38 +406,12 @@
return soa.Self()->HoldsLock(object);
}
-JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) {
- ScopedUtfChars name(env, java_name);
- {
- art::ScopedObjectAccess soa(env);
- if (soa.Decode<art::mirror::Object>(jthread) == soa.Self()->GetPeer()) {
- soa.Self()->SetThreadName(name.c_str());
- return;
- }
- }
- // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
- // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
- // in the DDMS send code.
- art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
- bool timed_out;
- // Take suspend thread lock to avoid races with threads trying to suspend this one.
- art::Thread* thread;
- {
- thread = thread_list->SuspendThreadByPeer(jthread,
- art::SuspendReason::kInternal,
- &timed_out);
- }
- if (thread != nullptr) {
- {
- art::ScopedObjectAccess soa(env);
- thread->SetThreadName(name.c_str());
- }
- bool resumed = thread_list->Resume(thread, art::SuspendReason::kInternal);
- DCHECK(resumed);
- } else if (timed_out) {
- LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
- "failed to suspend within a generous timeout.";
- }
+JNIEXPORT __attribute__((noreturn)) void JVM_SetNativeThreadName(
+ JNIEnv* env ATTRIBUTE_UNUSED,
+ jobject jthread ATTRIBUTE_UNUSED,
+ jstring java_name ATTRIBUTE_UNUSED) {
+ UNIMPLEMENTED(FATAL) << "JVM_SetNativeThreadName is not implemented";
+ UNREACHABLE();
}
JNIEXPORT __attribute__((noreturn)) jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED,