From 30bc7778e5ca5a76978b91054cb1debe6c96da06 Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Fri, 28 Jan 2022 15:07:02 -0800 Subject: 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 --- openjdkjvm/OpenjdkJvm.cc | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) (limited to 'openjdkjvm/OpenjdkJvm.cc') diff --git a/openjdkjvm/OpenjdkJvm.cc b/openjdkjvm/OpenjdkJvm.cc index a9fa0fc6c7..63503f459e 100644 --- a/openjdkjvm/OpenjdkJvm.cc +++ b/openjdkjvm/OpenjdkJvm.cc @@ -406,38 +406,12 @@ JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jo 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(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, -- cgit v1.2.3-59-g8ed1b