Skip to content

Commit 309f794

Browse files
committed
8272736: [JVMCI] Add API for reading and writing JVMCI thread locals
Backport-of: ad92033fccbf4ec9310ea5b3024be61c082ee5bb
1 parent cfe1e95 commit 309f794

File tree

6 files changed

+137
-6
lines changed

6 files changed

+137
-6
lines changed

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,49 @@ C2V_VMENTRY(void, notifyCompilerInliningEvent, (JNIEnv* env, jobject, jint compi
25902590
}
25912591
}
25922592

2593+
C2V_VMENTRY(void, setThreadLocalObject, (JNIEnv* env, jobject, jint id, jobject value))
2594+
requireInHotSpot("setThreadLocalObject", JVMCI_CHECK);
2595+
if (id == 0) {
2596+
thread->set_jvmci_reserved_oop0(JNIHandles::resolve(value));
2597+
return;
2598+
}
2599+
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
2600+
err_msg("%d is not a valid thread local id", id));
2601+
}
2602+
2603+
C2V_VMENTRY_NULL(jobject, getThreadLocalObject, (JNIEnv* env, jobject, jint id))
2604+
requireInHotSpot("getThreadLocalObject", JVMCI_CHECK_NULL);
2605+
if (id == 0) {
2606+
return JNIHandles::make_local(thread->get_jvmci_reserved_oop0());
2607+
}
2608+
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
2609+
err_msg("%d is not a valid thread local id", id));
2610+
}
2611+
2612+
C2V_VMENTRY(void, setThreadLocalLong, (JNIEnv* env, jobject, jint id, jlong value))
2613+
requireInHotSpot("setThreadLocalLong", JVMCI_CHECK);
2614+
if (id == 0) {
2615+
thread->set_jvmci_reserved0(value);
2616+
} else if (id == 1) {
2617+
thread->set_jvmci_reserved1(value);
2618+
} else {
2619+
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
2620+
err_msg("%d is not a valid thread local id", id));
2621+
}
2622+
}
2623+
2624+
C2V_VMENTRY_0(jlong, getThreadLocalLong, (JNIEnv* env, jobject, jint id))
2625+
requireInHotSpot("getThreadLocalLong", JVMCI_CHECK_0);
2626+
if (id == 0) {
2627+
return thread->get_jvmci_reserved0();
2628+
} else if (id == 1) {
2629+
return thread->get_jvmci_reserved1();
2630+
} else {
2631+
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
2632+
err_msg("%d is not a valid thread local id", id));
2633+
}
2634+
}
2635+
25932636
#define CC (char*) /*cast a literal from (const char*)*/
25942637
#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f))
25952638

@@ -2731,6 +2774,10 @@ JNINativeMethod CompilerToVM::methods[] = {
27312774
{CC "addFailedSpeculation", CC "(J[B)Z", FN_PTR(addFailedSpeculation)},
27322775
{CC "callSystemExit", CC "(I)V", FN_PTR(callSystemExit)},
27332776
{CC "ticksNow", CC "()J", FN_PTR(ticksNow)},
2777+
{CC "getThreadLocalObject", CC "(I)" OBJECT, FN_PTR(getThreadLocalObject)},
2778+
{CC "setThreadLocalObject", CC "(I" OBJECT ")V", FN_PTR(setThreadLocalObject)},
2779+
{CC "getThreadLocalLong", CC "(I)J", FN_PTR(getThreadLocalLong)},
2780+
{CC "setThreadLocalLong", CC "(IJ)V", FN_PTR(setThreadLocalLong)},
27342781
{CC "registerCompilerPhase", CC "(" STRING ")I", FN_PTR(registerCompilerPhase)},
27352782
{CC "notifyCompilerPhaseEvent", CC "(JIII)V", FN_PTR(notifyCompilerPhaseEvent)},
27362783
{CC "notifyCompilerInliningEvent", CC "(I" HS_RESOLVED_METHOD HS_RESOLVED_METHOD "ZLjava/lang/String;I)V", FN_PTR(notifyCompilerInliningEvent)},

src/hotspot/share/jvmci/vmStructs_jvmci.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@
183183
nonstatic_field(JavaThread, _pending_failed_speculation, jlong) \
184184
nonstatic_field(JavaThread, _pending_transfer_to_interpreter, bool) \
185185
nonstatic_field(JavaThread, _jvmci_counters, jlong*) \
186-
nonstatic_field(JavaThread, _jvmci_reserved0, intptr_t*) \
187-
nonstatic_field(JavaThread, _jvmci_reserved1, intptr_t*) \
186+
nonstatic_field(JavaThread, _jvmci_reserved0, jlong) \
187+
nonstatic_field(JavaThread, _jvmci_reserved1, jlong) \
188188
nonstatic_field(JavaThread, _jvmci_reserved_oop0, oop) \
189189
nonstatic_field(JavaThread, _should_post_on_exceptions_flag, int) \
190190
nonstatic_field(JavaThread, _jni_environment, JNIEnv) \

src/hotspot/share/runtime/thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ JavaThread::JavaThread() :
10581058
_pending_failed_speculation(0),
10591059
_jvmci{nullptr},
10601060
_jvmci_counters(nullptr),
1061-
_jvmci_reserved0(nullptr),
1062-
_jvmci_reserved1(nullptr),
1061+
_jvmci_reserved0(0),
1062+
_jvmci_reserved1(0),
10631063
_jvmci_reserved_oop0(nullptr),
10641064
#endif // INCLUDE_JVMCI
10651065

src/hotspot/share/runtime/thread.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,8 @@ class JavaThread: public Thread {
958958
jlong* _jvmci_counters;
959959

960960
// Fast thread locals for use by JVMCI
961-
intptr_t* _jvmci_reserved0;
962-
intptr_t* _jvmci_reserved1;
961+
jlong _jvmci_reserved0;
962+
jlong _jvmci_reserved1;
963963
oop _jvmci_reserved_oop0;
964964

965965
public:
@@ -970,6 +970,30 @@ class JavaThread: public Thread {
970970

971971
static bool resize_all_jvmci_counters(int new_size);
972972

973+
void set_jvmci_reserved_oop0(oop value) {
974+
_jvmci_reserved_oop0 = value;
975+
}
976+
977+
oop get_jvmci_reserved_oop0() {
978+
return _jvmci_reserved_oop0;
979+
}
980+
981+
void set_jvmci_reserved0(jlong value) {
982+
_jvmci_reserved0 = value;
983+
}
984+
985+
jlong get_jvmci_reserved0() {
986+
return _jvmci_reserved0;
987+
}
988+
989+
void set_jvmci_reserved1(jlong value) {
990+
_jvmci_reserved1 = value;
991+
}
992+
993+
jlong get_jvmci_reserved1() {
994+
return _jvmci_reserved1;
995+
}
996+
973997
private:
974998
#endif // INCLUDE_JVMCI
975999

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,26 @@ HotSpotResolvedObjectTypeImpl getResolvedJavaType(long displacement, boolean com
952952
*/
953953
native long ticksNow();
954954

955+
/**
956+
* @see HotSpotJVMCIRuntime#setThreadLocalObject(int, Object)
957+
*/
958+
native void setThreadLocalObject(int id, Object value);
959+
960+
/**
961+
* @see HotSpotJVMCIRuntime#getThreadLocalObject(int)
962+
*/
963+
native Object getThreadLocalObject(int id);
964+
965+
/**
966+
* @see HotSpotJVMCIRuntime#setThreadLocalLong(int, long)
967+
*/
968+
native void setThreadLocalLong(int id, long value);
969+
970+
/**
971+
* @see HotSpotJVMCIRuntime#getThreadLocalLong(int)
972+
*/
973+
native long getThreadLocalLong(int id);
974+
955975
/**
956976
* Adds phases in HotSpot JFR.
957977
*

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,46 @@ private HotSpotJVMCIRuntime() {
613613
}
614614
}
615615

616+
/**
617+
* Sets the current thread's {@code JavaThread::_jvmci_reserved_oop<id>} field to {@code value}.
618+
*
619+
* @throws IllegalArgumentException if the {@code JavaThread::_jvmci_reserved_oop<id>} field
620+
* does not exist
621+
*/
622+
public void setThreadLocalObject(int id, Object value) {
623+
compilerToVm.setThreadLocalObject(id, value);
624+
}
625+
626+
/**
627+
* Get the value of the current thread's {@code JavaThread::_jvmci_reserved_oop<id>} field.
628+
*
629+
* @throws IllegalArgumentException if the {@code JavaThread::_jvmci_reserved_oop<id>} field
630+
* does not exist
631+
*/
632+
public Object getThreadLocalObject(int id) {
633+
return compilerToVm.getThreadLocalObject(id);
634+
}
635+
636+
/**
637+
* Sets the current thread's {@code JavaThread::_jvmci_reserved<id>} field to {@code value}.
638+
*
639+
* @throws IllegalArgumentException if the {@code JavaThread::_jvmci_reserved<id>} field does
640+
* not exist
641+
*/
642+
public void setThreadLocalLong(int id, long value) {
643+
compilerToVm.setThreadLocalLong(id, value);
644+
}
645+
646+
/**
647+
* Get the value of the current thread's {@code JavaThread::_jvmci_reserved<id>} field.
648+
*
649+
* @throws IllegalArgumentException if the {@code JavaThread::_jvmci_reserved<id>} field does
650+
* not exist
651+
*/
652+
public long getThreadLocalLong(int id) {
653+
return compilerToVm.getThreadLocalLong(id);
654+
}
655+
616656
HotSpotResolvedJavaType createClass(Class<?> javaClass) {
617657
if (javaClass.isPrimitive()) {
618658
return HotSpotResolvedPrimitiveType.forKind(JavaKind.fromJavaClass(javaClass));

0 commit comments

Comments
 (0)