Skip to content

Commit 017d151

Browse files
author
Doug Simon
committed
8254842: [JVMCI] copy thread name when attaching libgraal thread to HotSpot
Reviewed-by: kvn, never
1 parent 5d1397f commit 017d151

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,13 +2349,25 @@ C2V_VMENTRY_PREFIX(jlong, getCurrentJavaThread, (JNIEnv* env, jobject c2vm))
23492349
return (jlong) p2i(thread);
23502350
C2V_END
23512351

2352-
C2V_VMENTRY_PREFIX(jboolean, attachCurrentThread, (JNIEnv* env, jobject c2vm, jboolean as_daemon))
2352+
C2V_VMENTRY_PREFIX(jboolean, attachCurrentThread, (JNIEnv* env, jobject c2vm, jbyteArray name, jboolean as_daemon))
23532353
if (thread == NULL) {
23542354
// Called from unattached JVMCI shared library thread
2355+
guarantee(name != NULL, "libjvmci caller must pass non-null name");
2356+
23552357
extern struct JavaVM_ main_vm;
23562358
JNIEnv* hotspotEnv;
2357-
jint res = as_daemon ? main_vm.AttachCurrentThreadAsDaemon((void**) &hotspotEnv, NULL) :
2358-
main_vm.AttachCurrentThread((void**) &hotspotEnv, NULL);
2359+
2360+
int name_len = env->GetArrayLength(name);
2361+
char name_buf[64]; // Cannot use Resource heap as it requires a current thread
2362+
int to_copy = MIN2(name_len, (int) sizeof(name_buf) - 1);
2363+
env->GetByteArrayRegion(name, 0, to_copy, (jbyte*) name_buf);
2364+
name_buf[to_copy] = '\0';
2365+
JavaVMAttachArgs attach_args;
2366+
attach_args.version = JNI_VERSION_1_2;
2367+
attach_args.name = name_buf;
2368+
attach_args.group = NULL;
2369+
jint res = as_daemon ? main_vm.AttachCurrentThreadAsDaemon((void**) &hotspotEnv, &attach_args) :
2370+
main_vm.AttachCurrentThread((void**) &hotspotEnv, &attach_args);
23592371
if (res != JNI_OK) {
23602372
JNI_THROW_("attachCurrentThread", InternalError, err_msg("Trying to attach thread returned %d", res), false);
23612373
}
@@ -2803,7 +2815,7 @@ JNINativeMethod CompilerToVM::methods[] = {
28032815
{CC "registerNativeMethods", CC "(" CLASS ")[J", FN_PTR(registerNativeMethods)},
28042816
{CC "isCurrentThreadAttached", CC "()Z", FN_PTR(isCurrentThreadAttached)},
28052817
{CC "getCurrentJavaThread", CC "()J", FN_PTR(getCurrentJavaThread)},
2806-
{CC "attachCurrentThread", CC "(Z)Z", FN_PTR(attachCurrentThread)},
2818+
{CC "attachCurrentThread", CC "([BZ)Z", FN_PTR(attachCurrentThread)},
28072819
{CC "detachCurrentThread", CC "()V", FN_PTR(detachCurrentThread)},
28082820
{CC "translate", CC "(" OBJECT ")J", FN_PTR(translate)},
28092821
{CC "unhand", CC "(J)" OBJECT, FN_PTR(unhand)},

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/CompilerToVM.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,10 @@ HotSpotResolvedObjectTypeImpl getResolvedJavaType(long displacement, boolean com
977977
native long getCurrentJavaThread();
978978

979979
/**
980+
* @param name name of current thread if in a native image otherwise {@code null}
980981
* @see HotSpotJVMCIRuntime#attachCurrentThread
981982
*/
982-
native boolean attachCurrentThread(boolean asDaemon);
983+
native boolean attachCurrentThread(byte[] name, boolean asDaemon);
983984

984985
/**
985986
* @see HotSpotJVMCIRuntime#detachCurrentThread()

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,8 @@ public long getCurrentJavaThread() {
11171117
* the length of the array returned by {@link #registerNativeMethods}
11181118
*/
11191119
public boolean attachCurrentThread(boolean asDaemon) {
1120-
return compilerToVm.attachCurrentThread(asDaemon);
1120+
byte[] name = IS_IN_NATIVE_IMAGE ? Thread.currentThread().getName().getBytes() : null;
1121+
return compilerToVm.attachCurrentThread(name, asDaemon);
11211122
}
11221123

11231124
/**

0 commit comments

Comments
 (0)