66 #include "runtime/handles.inline.hpp"
67 #include "runtime/handshake.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/javaThread.inline.hpp"
72 #include "runtime/jniHandles.inline.hpp"
73 #include "runtime/lockStack.inline.hpp"
74 #include "runtime/mutexLocker.hpp"
75 #include "runtime/orderAccess.hpp"
76 #include "runtime/os.inline.hpp"
77 #include "runtime/osThread.hpp"
78 #include "runtime/safepoint.hpp"
79 #include "runtime/safepointMechanism.inline.hpp"
80 #include "runtime/safepointVerifiers.hpp"
81 #include "runtime/serviceThread.hpp"
82 #include "runtime/stackFrameStream.inline.hpp"
83 #include "runtime/stackWatermarkSet.hpp"
84 #include "runtime/synchronizer.hpp"
85 #include "runtime/threadCritical.hpp"
86 #include "runtime/threadSMR.inline.hpp"
87 #include "runtime/threadStatisticalInfo.hpp"
88 #include "runtime/threadWXSetters.inline.hpp"
89 #include "runtime/timer.hpp"
90 #include "runtime/timerTrace.hpp"
91 #include "runtime/vframe.inline.hpp"
92 #include "runtime/vframeArray.hpp"
93 #include "runtime/vframe_hp.hpp"
94 #include "runtime/vmThread.hpp"
95 #include "runtime/vmOperations.hpp"
96 #include "services/threadService.hpp"
97 #include "utilities/copy.hpp"
98 #include "utilities/defaultStream.hpp"
99 #include "utilities/dtrace.hpp"
100 #include "utilities/events.hpp"
101 #include "utilities/macros.hpp"
102 #include "utilities/preserveException.hpp"
103 #include "utilities/spinYield.hpp"
104 #include "utilities/vmError.hpp"
105 #if INCLUDE_JVMCI
218 // before the java.lang.Thread instance has been created.
219 if (vthread_oop != nullptr) {
220 java_lang_Thread::clear_scopedValueBindings(vthread_oop);
221 }
222 }
223
224 void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name,
225 bool daemon, TRAPS) {
226 assert(thread_group.not_null(), "thread group should be specified");
227 assert(threadObj() == nullptr, "should only create Java thread object once");
228
229 InstanceKlass* ik = vmClasses::Thread_klass();
230 assert(ik->is_initialized(), "must be");
231 instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
232
233 // We are called from jni_AttachCurrentThread/jni_AttachCurrentThreadAsDaemon.
234 // We cannot use JavaCalls::construct_new_instance because the java.lang.Thread
235 // constructor calls Thread.current(), which must be set here.
236 java_lang_Thread::set_thread(thread_oop(), this);
237 set_threadOopHandles(thread_oop());
238
239 JavaValue result(T_VOID);
240 if (thread_name != nullptr) {
241 Handle name = java_lang_String::create_from_str(thread_name, CHECK);
242 // Thread gets assigned specified name and null target
243 JavaCalls::call_special(&result,
244 thread_oop,
245 ik,
246 vmSymbols::object_initializer_name(),
247 vmSymbols::threadgroup_string_void_signature(),
248 thread_group,
249 name,
250 CHECK);
251 } else {
252 // Thread gets assigned name "Thread-nnn" and null target
253 // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
254 JavaCalls::call_special(&result,
255 thread_oop,
256 ik,
257 vmSymbols::object_initializer_name(),
258 vmSymbols::threadgroup_runnable_void_signature(),
259 thread_group,
260 Handle(),
261 CHECK);
262 }
263 os::set_priority(this, NormPriority);
264
265 if (daemon) {
266 java_lang_Thread::set_daemon(thread_oop());
267 }
268 }
269
270 // ======= JavaThread ========
271
272 #if INCLUDE_JVMCI
273
274 jlong* JavaThread::_jvmci_old_thread_counters;
275
276 static bool jvmci_counters_include(JavaThread* thread) {
277 return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
278 }
279
280 void JavaThread::collect_counters(jlong* array, int length) {
281 assert(length == JVMCICounterSize, "wrong value");
282 for (int i = 0; i < length; i++) {
412 JavaThread::JavaThread(MEMFLAGS flags) :
413 Thread(flags),
414 // Initialize fields
415 _on_thread_list(false),
416 DEBUG_ONLY(_java_call_counter(0) COMMA)
417 _entry_point(nullptr),
418 _deopt_mark(nullptr),
419 _deopt_nmethod(nullptr),
420 _vframe_array_head(nullptr),
421 _vframe_array_last(nullptr),
422 _jvmti_deferred_updates(nullptr),
423 _callee_target(nullptr),
424 _vm_result(nullptr),
425 _vm_result_2(nullptr),
426
427 _current_pending_monitor(nullptr),
428 _current_pending_monitor_is_from_java(true),
429 _current_waiting_monitor(nullptr),
430 _active_handles(nullptr),
431 _free_handle_block(nullptr),
432
433 _suspend_flags(0),
434
435 _thread_state(_thread_new),
436 _saved_exception_pc(nullptr),
437 #ifdef ASSERT
438 _no_safepoint_count(0),
439 _visited_for_critical_count(false),
440 #endif
441
442 _terminated(_not_terminated),
443 _in_deopt_handler(0),
444 _doing_unsafe_access(false),
445 _do_not_unlock_if_synchronized(false),
446 #if INCLUDE_JVMTI
447 _carrier_thread_suspended(false),
448 _is_in_VTMS_transition(false),
449 _is_in_tmp_VTMS_transition(false),
450 _is_disable_suspend(false),
451 _VTMS_transition_mark(false),
452 #ifdef ASSERT
453 _is_VTMS_transition_disabler(false),
454 #endif
455 #endif
456 _jni_attach_state(_not_attaching_via_jni),
457 _is_in_internal_oome_mark(false),
458 #if INCLUDE_JVMCI
459 _pending_deoptimization(-1),
460 _pending_monitorenter(false),
461 _pending_transfer_to_interpreter(false),
462 _pending_failed_speculation(0),
463 _jvmci{nullptr},
464 _libjvmci_runtime(nullptr),
465 _jvmci_counters(nullptr),
466 _jvmci_reserved0(0),
467 _jvmci_reserved1(0),
468 _jvmci_reserved_oop0(nullptr),
469 _live_nmethod(nullptr),
470 #endif // INCLUDE_JVMCI
471
472 _exception_oop(oop()),
473 _exception_pc(0),
474 _exception_handler_pc(0),
475 _is_method_handle_return(0),
476
477 _jni_active_critical(0),
478 _pending_jni_exception_check_fn(nullptr),
479 _depth_first_number(0),
480
481 // JVMTI PopFrame support
482 _popframe_condition(popframe_inactive),
483 _frames_to_pop_failed_realloc(0),
484
485 _cont_entry(nullptr),
486 _cont_fastpath(0),
487 _cont_fastpath_thread_state(1),
488 _held_monitor_count(0),
489 _jni_monitor_count(0),
490
491 _handshake(this),
492
493 _popframe_preserved_args(nullptr),
494 _popframe_preserved_args_size(0),
495
496 _jvmti_thread_state(nullptr),
497 _interp_only_mode(0),
498 _should_post_on_exceptions_flag(JNI_FALSE),
499 _thread_stat(new ThreadStatistics()),
500
501 _parker(),
502
503 _class_to_be_initialized(nullptr),
504
505 _SleepEvent(ParkEvent::Allocate(this)),
506
507 _lock_stack(this) {
508 set_jni_functions(jni_functions());
509
913 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
914 held_monitor_count(), jni_monitor_count());
915 // All in-use monitors, including JNI-locked ones, should have been released above.
916 assert(held_monitor_count() == 0, "Failed to unlock " INTX_FORMAT " object monitors",
917 held_monitor_count());
918 } else {
919 // Check for monitor counts being out of sync.
920 assert(held_monitor_count() == jni_monitor_count(),
921 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
922 held_monitor_count(), jni_monitor_count());
923 // It is possible that a terminating thread failed to unlock monitors it locked
924 // via JNI so we don't assert the count is zero.
925 }
926
927 if (CheckJNICalls && jni_monitor_count() > 0) {
928 // We would like a fatal here, but due to we never checked this before there
929 // is a lot of tests which breaks, even with an error log.
930 log_debug(jni)("JavaThread %s (tid: " UINTX_FORMAT ") with Objects still locked by JNI MonitorEnter.",
931 exit_type == JavaThread::normal_exit ? "exiting" : "detaching", os::current_thread_id());
932 }
933
934 // These things needs to be done while we are still a Java Thread. Make sure that thread
935 // is in a consistent state, in case GC happens
936 JFR_ONLY(Jfr::on_thread_exit(this);)
937
938 if (active_handles() != nullptr) {
939 JNIHandleBlock* block = active_handles();
940 set_active_handles(nullptr);
941 JNIHandleBlock::release_block(block);
942 }
943
944 if (free_handle_block() != nullptr) {
945 JNIHandleBlock* block = free_handle_block();
946 set_free_handle_block(nullptr);
947 JNIHandleBlock::release_block(block);
948 }
949
950 // These have to be removed while this is still a valid thread.
951 _stack_overflow_state.remove_stack_guard_pages();
952
1142 HandshakeClosure("InstallAsyncException"), _aeh(aeh) {}
1143 ~InstallAsyncExceptionHandshake() {
1144 // If InstallAsyncExceptionHandshake was never executed we need to clean up _aeh.
1145 delete _aeh;
1146 }
1147 void do_thread(Thread* thr) {
1148 JavaThread* target = JavaThread::cast(thr);
1149 target->install_async_exception(_aeh);
1150 _aeh = nullptr;
1151 }
1152 };
1153
1154 void JavaThread::send_async_exception(JavaThread* target, oop java_throwable) {
1155 OopHandle e(Universe::vm_global(), java_throwable);
1156 InstallAsyncExceptionHandshake iaeh(new AsyncExceptionHandshake(e));
1157 Handshake::execute(&iaeh, target);
1158 }
1159
1160 #if INCLUDE_JVMTI
1161 void JavaThread::set_is_in_VTMS_transition(bool val) {
1162 _is_in_VTMS_transition = val;
1163 }
1164
1165 #ifdef ASSERT
1166 void JavaThread::set_is_VTMS_transition_disabler(bool val) {
1167 _is_VTMS_transition_disabler = val;
1168 }
1169 #endif
1170 #endif
1171
1172 // External suspension mechanism.
1173 //
1174 // Guarantees on return (for a valid target thread):
1175 // - Target thread will not execute any new bytecode.
1176 // - Target thread will not enter any new monitors.
1177 //
1178 bool JavaThread::java_suspend() {
1179 #if INCLUDE_JVMTI
1180 // Suspending a JavaThread in VTMS transition or disabling VTMS transitions can cause deadlocks.
1181 assert(!is_in_VTMS_transition(), "no suspend allowed in VTMS transition");
1505 void JavaThread::print_thread_state_on(outputStream *st) const {
1506 st->print_cr(" JavaThread state: %s", _get_thread_state_name(_thread_state));
1507 }
1508
1509 // Called by Threads::print() for VM_PrintThreads operation
1510 void JavaThread::print_on(outputStream *st, bool print_extended_info) const {
1511 st->print_raw("\"");
1512 st->print_raw(name());
1513 st->print_raw("\" ");
1514 oop thread_oop = threadObj();
1515 if (thread_oop != nullptr) {
1516 st->print("#" INT64_FORMAT " [%ld] ", (int64_t)java_lang_Thread::thread_id(thread_oop), (long) osthread()->thread_id());
1517 if (java_lang_Thread::is_daemon(thread_oop)) st->print("daemon ");
1518 st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
1519 }
1520 Thread::print_on(st, print_extended_info);
1521 // print guess for valid stack memory region (assume 4K pages); helps lock debugging
1522 st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
1523 if (thread_oop != nullptr) {
1524 if (is_vthread_mounted()) {
1525 oop vt = vthread();
1526 assert(vt != nullptr, "");
1527 st->print_cr(" Carrying virtual thread #" INT64_FORMAT, (int64_t)java_lang_Thread::thread_id(vt));
1528 } else {
1529 st->print_cr(" java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
1530 }
1531 }
1532 #ifndef PRODUCT
1533 _safepoint_state->print_on(st);
1534 #endif // PRODUCT
1535 if (is_Compiler_thread()) {
1536 CompileTask *task = ((CompilerThread*)this)->task();
1537 if (task != nullptr) {
1538 st->print(" Compiling: ");
1539 task->print(st, nullptr, true, false);
1540 } else {
1541 st->print(" No compile task");
1542 }
1543 st->cr();
1544 }
1545 }
1546
1547 void JavaThread::print() const { print_on(tty); }
1691 assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
1692 assert(NoPriority <= prio && prio <= MaxPriority, "sanity check");
1693 // Link Java Thread object <-> C++ Thread
1694
1695 // Get the C++ thread object (an oop) from the JNI handle (a jthread)
1696 // and put it into a new Handle. The Handle "thread_oop" can then
1697 // be used to pass the C++ thread object to other methods.
1698
1699 // Set the Java level thread object (jthread) field of the
1700 // new thread (a JavaThread *) to C++ thread object using the
1701 // "thread_oop" handle.
1702
1703 // Set the thread field (a JavaThread *) of the
1704 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
1705
1706 Handle thread_oop(Thread::current(),
1707 JNIHandles::resolve_non_null(jni_thread));
1708 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
1709 "must be initialized");
1710 set_threadOopHandles(thread_oop());
1711
1712 if (prio == NoPriority) {
1713 prio = java_lang_Thread::priority(thread_oop());
1714 assert(prio != NoPriority, "A valid priority should be present");
1715 }
1716
1717 // Push the Java priority down to the native thread; needs Threads_lock
1718 Thread::set_priority(this, prio);
1719
1720 // Add the new thread to the Threads list and set it in motion.
1721 // We must have threads lock in order to call Threads::add.
1722 // It is crucial that we do not block before the thread is
1723 // added to the Threads list for if a GC happens, then the java_thread oop
1724 // will not be visited by GC.
1725 Threads::add(this);
1726 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
1727 // on a ThreadsList. We don't want to wait for the release when the
1728 // Theads_lock is dropped somewhere in the caller since the JavaThread*
1729 // is already visible to JVM/TI via the ThreadsList.
1730 java_lang_Thread::release_set_thread(thread_oop(), this);
1986 // Slow-path increment of the held monitor counts. JNI locking is always
1987 // this slow-path.
1988 void JavaThread::inc_held_monitor_count(intx i, bool jni) {
1989 #ifdef SUPPORT_MONITOR_COUNT
1990 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
1991 _held_monitor_count += i;
1992 if (jni) {
1993 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
1994 _jni_monitor_count += i;
1995 }
1996 assert(_held_monitor_count >= _jni_monitor_count, "Monitor count discrepancy detected - held count "
1997 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
1998 #endif
1999 }
2000
2001 // Slow-path decrement of the held monitor counts. JNI unlocking is always
2002 // this slow-path.
2003 void JavaThread::dec_held_monitor_count(intx i, bool jni) {
2004 #ifdef SUPPORT_MONITOR_COUNT
2005 _held_monitor_count -= i;
2006 assert(_held_monitor_count >= 0, "Must always be greater than 0: " INTX_FORMAT, _held_monitor_count);
2007 if (jni) {
2008 _jni_monitor_count -= i;
2009 assert(_jni_monitor_count >= 0, "Must always be greater than 0: " INTX_FORMAT, _jni_monitor_count);
2010 }
2011 // When a thread is detaching with still owned JNI monitors, the logic that releases
2012 // the monitors doesn't know to set the "jni" flag and so the counts can get out of sync.
2013 // So we skip this assert if the thread is exiting. Once all monitors are unlocked the
2014 // JNI count is directly set to zero.
2015 assert(_held_monitor_count >= _jni_monitor_count || is_exiting(), "Monitor count discrepancy detected - held count "
2016 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2017 #endif
2018 }
2019
2020 frame JavaThread::vthread_last_frame() {
2021 assert (is_vthread_mounted(), "Virtual thread not mounted");
2022 return last_frame();
2023 }
2024
2025 frame JavaThread::carrier_last_frame(RegisterMap* reg_map) {
2026 const ContinuationEntry* entry = vthread_continuation();
2027 guarantee (entry != nullptr, "Not a carrier thread");
2028 frame f = entry->to_frame();
2029 if (reg_map->process_frames()) {
2179 // bound to the given java.lang.Thread instance.
2180 // The Threads_lock is held for the duration.
2181 void JavaThread::start_internal_daemon(JavaThread* current, JavaThread* target,
2182 Handle thread_oop, ThreadPriority prio) {
2183
2184 assert(target->osthread() != nullptr, "target thread is not properly initialized");
2185
2186 MutexLocker mu(current, Threads_lock);
2187
2188 // Initialize the fields of the thread_oop first.
2189 if (prio != NoPriority) {
2190 java_lang_Thread::set_priority(thread_oop(), prio);
2191 // Note: we don't call os::set_priority here. Possibly we should,
2192 // else all threads should call it themselves when they first run.
2193 }
2194
2195 java_lang_Thread::set_daemon(thread_oop());
2196
2197 // Now bind the thread_oop to the target JavaThread.
2198 target->set_threadOopHandles(thread_oop());
2199
2200 Threads::add(target); // target is now visible for safepoint/handshake
2201 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
2202 // on a ThreadsList. We don't want to wait for the release when the
2203 // Theads_lock is dropped when the 'mu' destructor is run since the
2204 // JavaThread* is already visible to JVM/TI via the ThreadsList.
2205
2206 assert(java_lang_Thread::thread(thread_oop()) == nullptr, "must not be alive");
2207 java_lang_Thread::release_set_thread(thread_oop(), target); // isAlive == true now
2208 Thread::start(target);
2209 }
2210
2211 void JavaThread::vm_exit_on_osthread_failure(JavaThread* thread) {
2212 // At this point it may be possible that no osthread was created for the
2213 // JavaThread due to lack of resources. However, since this must work
2214 // for critical system threads just check and abort if this fails.
2215 if (thread->osthread() == nullptr) {
2216 // This isn't really an OOM condition, but historically this is what
2217 // we report.
2218 vm_exit_during_initialization("java.lang.OutOfMemoryError",
|
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/handshake.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/javaThread.inline.hpp"
72 #include "runtime/jniHandles.inline.hpp"
73 #include "runtime/lockStack.inline.hpp"
74 #include "runtime/mutexLocker.hpp"
75 #include "runtime/orderAccess.hpp"
76 #include "runtime/os.inline.hpp"
77 #include "runtime/osThread.hpp"
78 #include "runtime/safepoint.hpp"
79 #include "runtime/safepointMechanism.inline.hpp"
80 #include "runtime/safepointVerifiers.hpp"
81 #include "runtime/serviceThread.hpp"
82 #include "runtime/stackFrameStream.inline.hpp"
83 #include "runtime/stackWatermarkSet.hpp"
84 #include "runtime/synchronizer.hpp"
85 #include "runtime/threadCritical.hpp"
86 #include "runtime/threadIdentifier.hpp"
87 #include "runtime/threadSMR.inline.hpp"
88 #include "runtime/threadStatisticalInfo.hpp"
89 #include "runtime/threadWXSetters.inline.hpp"
90 #include "runtime/timer.hpp"
91 #include "runtime/timerTrace.hpp"
92 #include "runtime/vframe.inline.hpp"
93 #include "runtime/vframeArray.hpp"
94 #include "runtime/vframe_hp.hpp"
95 #include "runtime/vmThread.hpp"
96 #include "runtime/vmOperations.hpp"
97 #include "services/threadService.hpp"
98 #include "utilities/copy.hpp"
99 #include "utilities/defaultStream.hpp"
100 #include "utilities/dtrace.hpp"
101 #include "utilities/events.hpp"
102 #include "utilities/macros.hpp"
103 #include "utilities/preserveException.hpp"
104 #include "utilities/spinYield.hpp"
105 #include "utilities/vmError.hpp"
106 #if INCLUDE_JVMCI
219 // before the java.lang.Thread instance has been created.
220 if (vthread_oop != nullptr) {
221 java_lang_Thread::clear_scopedValueBindings(vthread_oop);
222 }
223 }
224
225 void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name,
226 bool daemon, TRAPS) {
227 assert(thread_group.not_null(), "thread group should be specified");
228 assert(threadObj() == nullptr, "should only create Java thread object once");
229
230 InstanceKlass* ik = vmClasses::Thread_klass();
231 assert(ik->is_initialized(), "must be");
232 instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
233
234 // We are called from jni_AttachCurrentThread/jni_AttachCurrentThreadAsDaemon.
235 // We cannot use JavaCalls::construct_new_instance because the java.lang.Thread
236 // constructor calls Thread.current(), which must be set here.
237 java_lang_Thread::set_thread(thread_oop(), this);
238 set_threadOopHandles(thread_oop());
239 // Set the lock_id to the next thread_id temporarily while initialization runs.
240 set_lock_id(ThreadIdentifier::next());
241
242 JavaValue result(T_VOID);
243 if (thread_name != nullptr) {
244 Handle name = java_lang_String::create_from_str(thread_name, CHECK);
245 // Thread gets assigned specified name and null target
246 JavaCalls::call_special(&result,
247 thread_oop,
248 ik,
249 vmSymbols::object_initializer_name(),
250 vmSymbols::threadgroup_string_void_signature(),
251 thread_group,
252 name,
253 CHECK);
254 } else {
255 // Thread gets assigned name "Thread-nnn" and null target
256 // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
257 JavaCalls::call_special(&result,
258 thread_oop,
259 ik,
260 vmSymbols::object_initializer_name(),
261 vmSymbols::threadgroup_runnable_void_signature(),
262 thread_group,
263 Handle(),
264 CHECK);
265 }
266 // Update the lock_id with the tid value.
267 set_lock_id(java_lang_Thread::thread_id(thread_oop()));
268
269 os::set_priority(this, NormPriority);
270
271 if (daemon) {
272 java_lang_Thread::set_daemon(thread_oop());
273 }
274 }
275
276 // ======= JavaThread ========
277
278 #if INCLUDE_JVMCI
279
280 jlong* JavaThread::_jvmci_old_thread_counters;
281
282 static bool jvmci_counters_include(JavaThread* thread) {
283 return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
284 }
285
286 void JavaThread::collect_counters(jlong* array, int length) {
287 assert(length == JVMCICounterSize, "wrong value");
288 for (int i = 0; i < length; i++) {
418 JavaThread::JavaThread(MEMFLAGS flags) :
419 Thread(flags),
420 // Initialize fields
421 _on_thread_list(false),
422 DEBUG_ONLY(_java_call_counter(0) COMMA)
423 _entry_point(nullptr),
424 _deopt_mark(nullptr),
425 _deopt_nmethod(nullptr),
426 _vframe_array_head(nullptr),
427 _vframe_array_last(nullptr),
428 _jvmti_deferred_updates(nullptr),
429 _callee_target(nullptr),
430 _vm_result(nullptr),
431 _vm_result_2(nullptr),
432
433 _current_pending_monitor(nullptr),
434 _current_pending_monitor_is_from_java(true),
435 _current_waiting_monitor(nullptr),
436 _active_handles(nullptr),
437 _free_handle_block(nullptr),
438 _lock_id(0),
439 _on_monitorenter(false),
440
441 _suspend_flags(0),
442
443 _thread_state(_thread_new),
444 _saved_exception_pc(nullptr),
445 #ifdef ASSERT
446 _no_safepoint_count(0),
447 _visited_for_critical_count(false),
448 #endif
449
450 _terminated(_not_terminated),
451 _in_deopt_handler(0),
452 _doing_unsafe_access(false),
453 _do_not_unlock_if_synchronized(false),
454 #if INCLUDE_JVMTI
455 _carrier_thread_suspended(false),
456 _is_in_VTMS_transition(false),
457 _is_in_tmp_VTMS_transition(false),
458 _is_disable_suspend(false),
459 _VTMS_transition_mark(false),
460 _pending_jvmti_unmount_event(false),
461 _contended_entered_monitor(nullptr),
462 #ifdef ASSERT
463 _is_VTMS_transition_disabler(false),
464 #endif
465 #endif
466 _jni_attach_state(_not_attaching_via_jni),
467 _is_in_internal_oome_mark(false),
468 #if INCLUDE_JVMCI
469 _pending_deoptimization(-1),
470 _pending_monitorenter(false),
471 _pending_transfer_to_interpreter(false),
472 _pending_failed_speculation(0),
473 _jvmci{nullptr},
474 _libjvmci_runtime(nullptr),
475 _jvmci_counters(nullptr),
476 _jvmci_reserved0(0),
477 _jvmci_reserved1(0),
478 _jvmci_reserved_oop0(nullptr),
479 _live_nmethod(nullptr),
480 #endif // INCLUDE_JVMCI
481
482 _exception_oop(oop()),
483 _exception_pc(0),
484 _exception_handler_pc(0),
485 _is_method_handle_return(0),
486
487 _jni_active_critical(0),
488 _pending_jni_exception_check_fn(nullptr),
489 _depth_first_number(0),
490
491 // JVMTI PopFrame support
492 _popframe_condition(popframe_inactive),
493 _frames_to_pop_failed_realloc(0),
494
495 _cont_entry(nullptr),
496 _cont_fastpath(0),
497 _cont_fastpath_thread_state(1),
498 _held_monitor_count(0),
499 _jni_monitor_count(0),
500 _preempting(false),
501 _preemption_cancelled(false),
502 _pending_interrupted_exception(false),
503 _preempt_alternate_return(nullptr),
504 DEBUG_ONLY(_obj_locker_count(0) COMMA)
505
506 _handshake(this),
507
508 _popframe_preserved_args(nullptr),
509 _popframe_preserved_args_size(0),
510
511 _jvmti_thread_state(nullptr),
512 _interp_only_mode(0),
513 _should_post_on_exceptions_flag(JNI_FALSE),
514 _thread_stat(new ThreadStatistics()),
515
516 _parker(),
517
518 _class_to_be_initialized(nullptr),
519
520 _SleepEvent(ParkEvent::Allocate(this)),
521
522 _lock_stack(this) {
523 set_jni_functions(jni_functions());
524
928 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
929 held_monitor_count(), jni_monitor_count());
930 // All in-use monitors, including JNI-locked ones, should have been released above.
931 assert(held_monitor_count() == 0, "Failed to unlock " INTX_FORMAT " object monitors",
932 held_monitor_count());
933 } else {
934 // Check for monitor counts being out of sync.
935 assert(held_monitor_count() == jni_monitor_count(),
936 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
937 held_monitor_count(), jni_monitor_count());
938 // It is possible that a terminating thread failed to unlock monitors it locked
939 // via JNI so we don't assert the count is zero.
940 }
941
942 if (CheckJNICalls && jni_monitor_count() > 0) {
943 // We would like a fatal here, but due to we never checked this before there
944 // is a lot of tests which breaks, even with an error log.
945 log_debug(jni)("JavaThread %s (tid: " UINTX_FORMAT ") with Objects still locked by JNI MonitorEnter.",
946 exit_type == JavaThread::normal_exit ? "exiting" : "detaching", os::current_thread_id());
947 }
948 assert(obj_locker_count() == 0, "expected 0 but found: " INTX_FORMAT, obj_locker_count());
949
950 // These things needs to be done while we are still a Java Thread. Make sure that thread
951 // is in a consistent state, in case GC happens
952 JFR_ONLY(Jfr::on_thread_exit(this);)
953
954 if (active_handles() != nullptr) {
955 JNIHandleBlock* block = active_handles();
956 set_active_handles(nullptr);
957 JNIHandleBlock::release_block(block);
958 }
959
960 if (free_handle_block() != nullptr) {
961 JNIHandleBlock* block = free_handle_block();
962 set_free_handle_block(nullptr);
963 JNIHandleBlock::release_block(block);
964 }
965
966 // These have to be removed while this is still a valid thread.
967 _stack_overflow_state.remove_stack_guard_pages();
968
1158 HandshakeClosure("InstallAsyncException"), _aeh(aeh) {}
1159 ~InstallAsyncExceptionHandshake() {
1160 // If InstallAsyncExceptionHandshake was never executed we need to clean up _aeh.
1161 delete _aeh;
1162 }
1163 void do_thread(Thread* thr) {
1164 JavaThread* target = JavaThread::cast(thr);
1165 target->install_async_exception(_aeh);
1166 _aeh = nullptr;
1167 }
1168 };
1169
1170 void JavaThread::send_async_exception(JavaThread* target, oop java_throwable) {
1171 OopHandle e(Universe::vm_global(), java_throwable);
1172 InstallAsyncExceptionHandshake iaeh(new AsyncExceptionHandshake(e));
1173 Handshake::execute(&iaeh, target);
1174 }
1175
1176 #if INCLUDE_JVMTI
1177 void JavaThread::set_is_in_VTMS_transition(bool val) {
1178 assert(is_in_VTMS_transition() != val, "already %s transition", val ? "inside" : "outside");
1179 _is_in_VTMS_transition = val;
1180 }
1181
1182 #ifdef ASSERT
1183 void JavaThread::set_is_VTMS_transition_disabler(bool val) {
1184 _is_VTMS_transition_disabler = val;
1185 }
1186 #endif
1187 #endif
1188
1189 // External suspension mechanism.
1190 //
1191 // Guarantees on return (for a valid target thread):
1192 // - Target thread will not execute any new bytecode.
1193 // - Target thread will not enter any new monitors.
1194 //
1195 bool JavaThread::java_suspend() {
1196 #if INCLUDE_JVMTI
1197 // Suspending a JavaThread in VTMS transition or disabling VTMS transitions can cause deadlocks.
1198 assert(!is_in_VTMS_transition(), "no suspend allowed in VTMS transition");
1522 void JavaThread::print_thread_state_on(outputStream *st) const {
1523 st->print_cr(" JavaThread state: %s", _get_thread_state_name(_thread_state));
1524 }
1525
1526 // Called by Threads::print() for VM_PrintThreads operation
1527 void JavaThread::print_on(outputStream *st, bool print_extended_info) const {
1528 st->print_raw("\"");
1529 st->print_raw(name());
1530 st->print_raw("\" ");
1531 oop thread_oop = threadObj();
1532 if (thread_oop != nullptr) {
1533 st->print("#" INT64_FORMAT " [%ld] ", (int64_t)java_lang_Thread::thread_id(thread_oop), (long) osthread()->thread_id());
1534 if (java_lang_Thread::is_daemon(thread_oop)) st->print("daemon ");
1535 st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
1536 }
1537 Thread::print_on(st, print_extended_info);
1538 // print guess for valid stack memory region (assume 4K pages); helps lock debugging
1539 st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
1540 if (thread_oop != nullptr) {
1541 if (is_vthread_mounted()) {
1542 // _lock_id is the thread ID of the mounted virtual thread
1543 st->print_cr(" Carrying virtual thread #" INT64_FORMAT, lock_id());
1544 } else {
1545 st->print_cr(" java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
1546 }
1547 }
1548 #ifndef PRODUCT
1549 _safepoint_state->print_on(st);
1550 #endif // PRODUCT
1551 if (is_Compiler_thread()) {
1552 CompileTask *task = ((CompilerThread*)this)->task();
1553 if (task != nullptr) {
1554 st->print(" Compiling: ");
1555 task->print(st, nullptr, true, false);
1556 } else {
1557 st->print(" No compile task");
1558 }
1559 st->cr();
1560 }
1561 }
1562
1563 void JavaThread::print() const { print_on(tty); }
1707 assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
1708 assert(NoPriority <= prio && prio <= MaxPriority, "sanity check");
1709 // Link Java Thread object <-> C++ Thread
1710
1711 // Get the C++ thread object (an oop) from the JNI handle (a jthread)
1712 // and put it into a new Handle. The Handle "thread_oop" can then
1713 // be used to pass the C++ thread object to other methods.
1714
1715 // Set the Java level thread object (jthread) field of the
1716 // new thread (a JavaThread *) to C++ thread object using the
1717 // "thread_oop" handle.
1718
1719 // Set the thread field (a JavaThread *) of the
1720 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
1721
1722 Handle thread_oop(Thread::current(),
1723 JNIHandles::resolve_non_null(jni_thread));
1724 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
1725 "must be initialized");
1726 set_threadOopHandles(thread_oop());
1727 set_lock_id(java_lang_Thread::thread_id(thread_oop()));
1728
1729 if (prio == NoPriority) {
1730 prio = java_lang_Thread::priority(thread_oop());
1731 assert(prio != NoPriority, "A valid priority should be present");
1732 }
1733
1734 // Push the Java priority down to the native thread; needs Threads_lock
1735 Thread::set_priority(this, prio);
1736
1737 // Add the new thread to the Threads list and set it in motion.
1738 // We must have threads lock in order to call Threads::add.
1739 // It is crucial that we do not block before the thread is
1740 // added to the Threads list for if a GC happens, then the java_thread oop
1741 // will not be visited by GC.
1742 Threads::add(this);
1743 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
1744 // on a ThreadsList. We don't want to wait for the release when the
1745 // Theads_lock is dropped somewhere in the caller since the JavaThread*
1746 // is already visible to JVM/TI via the ThreadsList.
1747 java_lang_Thread::release_set_thread(thread_oop(), this);
2003 // Slow-path increment of the held monitor counts. JNI locking is always
2004 // this slow-path.
2005 void JavaThread::inc_held_monitor_count(intx i, bool jni) {
2006 #ifdef SUPPORT_MONITOR_COUNT
2007 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
2008 _held_monitor_count += i;
2009 if (jni) {
2010 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
2011 _jni_monitor_count += i;
2012 }
2013 assert(_held_monitor_count >= _jni_monitor_count, "Monitor count discrepancy detected - held count "
2014 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2015 #endif
2016 }
2017
2018 // Slow-path decrement of the held monitor counts. JNI unlocking is always
2019 // this slow-path.
2020 void JavaThread::dec_held_monitor_count(intx i, bool jni) {
2021 #ifdef SUPPORT_MONITOR_COUNT
2022 _held_monitor_count -= i;
2023 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
2024 if (jni) {
2025 _jni_monitor_count -= i;
2026 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
2027 }
2028 // When a thread is detaching with still owned JNI monitors, the logic that releases
2029 // the monitors doesn't know to set the "jni" flag and so the counts can get out of sync.
2030 // So we skip this assert if the thread is exiting. Once all monitors are unlocked the
2031 // JNI count is directly set to zero.
2032 assert(_held_monitor_count >= _jni_monitor_count || is_exiting(), "Monitor count discrepancy detected - held count "
2033 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2034 #endif
2035 }
2036
2037 frame JavaThread::vthread_last_frame() {
2038 assert (is_vthread_mounted(), "Virtual thread not mounted");
2039 return last_frame();
2040 }
2041
2042 frame JavaThread::carrier_last_frame(RegisterMap* reg_map) {
2043 const ContinuationEntry* entry = vthread_continuation();
2044 guarantee (entry != nullptr, "Not a carrier thread");
2045 frame f = entry->to_frame();
2046 if (reg_map->process_frames()) {
2196 // bound to the given java.lang.Thread instance.
2197 // The Threads_lock is held for the duration.
2198 void JavaThread::start_internal_daemon(JavaThread* current, JavaThread* target,
2199 Handle thread_oop, ThreadPriority prio) {
2200
2201 assert(target->osthread() != nullptr, "target thread is not properly initialized");
2202
2203 MutexLocker mu(current, Threads_lock);
2204
2205 // Initialize the fields of the thread_oop first.
2206 if (prio != NoPriority) {
2207 java_lang_Thread::set_priority(thread_oop(), prio);
2208 // Note: we don't call os::set_priority here. Possibly we should,
2209 // else all threads should call it themselves when they first run.
2210 }
2211
2212 java_lang_Thread::set_daemon(thread_oop());
2213
2214 // Now bind the thread_oop to the target JavaThread.
2215 target->set_threadOopHandles(thread_oop());
2216 target->set_lock_id(java_lang_Thread::thread_id(thread_oop()));
2217
2218 Threads::add(target); // target is now visible for safepoint/handshake
2219 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
2220 // on a ThreadsList. We don't want to wait for the release when the
2221 // Theads_lock is dropped when the 'mu' destructor is run since the
2222 // JavaThread* is already visible to JVM/TI via the ThreadsList.
2223
2224 assert(java_lang_Thread::thread(thread_oop()) == nullptr, "must not be alive");
2225 java_lang_Thread::release_set_thread(thread_oop(), target); // isAlive == true now
2226 Thread::start(target);
2227 }
2228
2229 void JavaThread::vm_exit_on_osthread_failure(JavaThread* thread) {
2230 // At this point it may be possible that no osthread was created for the
2231 // JavaThread due to lack of resources. However, since this must work
2232 // for critical system threads just check and abort if this fails.
2233 if (thread->osthread() == nullptr) {
2234 // This isn't really an OOM condition, but historically this is what
2235 // we report.
2236 vm_exit_during_initialization("java.lang.OutOfMemoryError",
|