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() :
413 // Initialize fields
414
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 _monitor_chunks(nullptr),
434
435 _suspend_flags(0),
436
437 _thread_state(_thread_new),
438 _saved_exception_pc(nullptr),
439 #ifdef ASSERT
440 _no_safepoint_count(0),
441 _visited_for_critical_count(false),
442 #endif
443
444 _terminated(_not_terminated),
445 _in_deopt_handler(0),
446 _doing_unsafe_access(false),
447 _do_not_unlock_if_synchronized(false),
448 #if INCLUDE_JVMTI
449 _carrier_thread_suspended(false),
450 _is_in_VTMS_transition(false),
451 _is_in_tmp_VTMS_transition(false),
471 #endif // INCLUDE_JVMCI
472
473 _exception_oop(oop()),
474 _exception_pc(0),
475 _exception_handler_pc(0),
476 _is_method_handle_return(0),
477
478 _jni_active_critical(0),
479 _pending_jni_exception_check_fn(nullptr),
480 _depth_first_number(0),
481
482 // JVMTI PopFrame support
483 _popframe_condition(popframe_inactive),
484 _frames_to_pop_failed_realloc(0),
485
486 _cont_entry(nullptr),
487 _cont_fastpath(0),
488 _cont_fastpath_thread_state(1),
489 _held_monitor_count(0),
490 _jni_monitor_count(0),
491
492 _handshake(this),
493
494 _popframe_preserved_args(nullptr),
495 _popframe_preserved_args_size(0),
496
497 _jvmti_thread_state(nullptr),
498 _interp_only_mode(0),
499 _should_post_on_exceptions_flag(JNI_FALSE),
500 _thread_stat(new ThreadStatistics()),
501
502 _parker(),
503
504 _class_to_be_initialized(nullptr),
505
506 _SleepEvent(ParkEvent::Allocate(this)),
507
508 _lock_stack(this) {
509 set_jni_functions(jni_functions());
510
916 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
917 held_monitor_count(), jni_monitor_count());
918 // All in-use monitors, including JNI-locked ones, should have been released above.
919 assert(held_monitor_count() == 0, "Failed to unlock " INTX_FORMAT " object monitors",
920 held_monitor_count());
921 } else {
922 // Check for monitor counts being out of sync.
923 assert(held_monitor_count() == jni_monitor_count(),
924 "held monitor count should be equal to jni: " INTX_FORMAT " != " INTX_FORMAT,
925 held_monitor_count(), jni_monitor_count());
926 // It is possible that a terminating thread failed to unlock monitors it locked
927 // via JNI so we don't assert the count is zero.
928 }
929
930 if (CheckJNICalls && jni_monitor_count() > 0) {
931 // We would like a fatal here, but due to we never checked this before there
932 // is a lot of tests which breaks, even with an error log.
933 log_debug(jni)("JavaThread %s (tid: " UINTX_FORMAT ") with Objects still locked by JNI MonitorEnter.",
934 exit_type == JavaThread::normal_exit ? "exiting" : "detaching", os::current_thread_id());
935 }
936
937 // These things needs to be done while we are still a Java Thread. Make sure that thread
938 // is in a consistent state, in case GC happens
939 JFR_ONLY(Jfr::on_thread_exit(this);)
940
941 if (active_handles() != nullptr) {
942 JNIHandleBlock* block = active_handles();
943 set_active_handles(nullptr);
944 JNIHandleBlock::release_block(block);
945 }
946
947 if (free_handle_block() != nullptr) {
948 JNIHandleBlock* block = free_handle_block();
949 set_free_handle_block(nullptr);
950 JNIHandleBlock::release_block(block);
951 }
952
953 // These have to be removed while this is still a valid thread.
954 _stack_overflow_state.remove_stack_guard_pages();
955
1167 HandshakeClosure("InstallAsyncException"), _aeh(aeh) {}
1168 ~InstallAsyncExceptionHandshake() {
1169 // If InstallAsyncExceptionHandshake was never executed we need to clean up _aeh.
1170 delete _aeh;
1171 }
1172 void do_thread(Thread* thr) {
1173 JavaThread* target = JavaThread::cast(thr);
1174 target->install_async_exception(_aeh);
1175 _aeh = nullptr;
1176 }
1177 };
1178
1179 void JavaThread::send_async_exception(JavaThread* target, oop java_throwable) {
1180 OopHandle e(Universe::vm_global(), java_throwable);
1181 InstallAsyncExceptionHandshake iaeh(new AsyncExceptionHandshake(e));
1182 Handshake::execute(&iaeh, target);
1183 }
1184
1185 #if INCLUDE_JVMTI
1186 void JavaThread::set_is_in_VTMS_transition(bool val) {
1187 _is_in_VTMS_transition = val;
1188 }
1189
1190 #ifdef ASSERT
1191 void JavaThread::set_is_VTMS_transition_disabler(bool val) {
1192 _is_VTMS_transition_disabler = val;
1193 }
1194 #endif
1195 #endif
1196
1197 // External suspension mechanism.
1198 //
1199 // Guarantees on return (for a valid target thread):
1200 // - Target thread will not execute any new bytecode.
1201 // - Target thread will not enter any new monitors.
1202 //
1203 bool JavaThread::java_suspend() {
1204 #if INCLUDE_JVMTI
1205 // Suspending a JavaThread in VTMS transition or disabling VTMS transitions can cause deadlocks.
1206 assert(!is_in_VTMS_transition(), "no suspend allowed in VTMS transition");
1723 assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
1724 assert(NoPriority <= prio && prio <= MaxPriority, "sanity check");
1725 // Link Java Thread object <-> C++ Thread
1726
1727 // Get the C++ thread object (an oop) from the JNI handle (a jthread)
1728 // and put it into a new Handle. The Handle "thread_oop" can then
1729 // be used to pass the C++ thread object to other methods.
1730
1731 // Set the Java level thread object (jthread) field of the
1732 // new thread (a JavaThread *) to C++ thread object using the
1733 // "thread_oop" handle.
1734
1735 // Set the thread field (a JavaThread *) of the
1736 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
1737
1738 Handle thread_oop(Thread::current(),
1739 JNIHandles::resolve_non_null(jni_thread));
1740 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
1741 "must be initialized");
1742 set_threadOopHandles(thread_oop());
1743
1744 if (prio == NoPriority) {
1745 prio = java_lang_Thread::priority(thread_oop());
1746 assert(prio != NoPriority, "A valid priority should be present");
1747 }
1748
1749 // Push the Java priority down to the native thread; needs Threads_lock
1750 Thread::set_priority(this, prio);
1751
1752 // Add the new thread to the Threads list and set it in motion.
1753 // We must have threads lock in order to call Threads::add.
1754 // It is crucial that we do not block before the thread is
1755 // added to the Threads list for if a GC happens, then the java_thread oop
1756 // will not be visited by GC.
1757 Threads::add(this);
1758 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
1759 // on a ThreadsList. We don't want to wait for the release when the
1760 // Theads_lock is dropped somewhere in the caller since the JavaThread*
1761 // is already visible to JVM/TI via the ThreadsList.
1762 java_lang_Thread::release_set_thread(thread_oop(), this);
2015 // Slow-path increment of the held monitor counts. JNI locking is always
2016 // this slow-path.
2017 void JavaThread::inc_held_monitor_count(intx i, bool jni) {
2018 #ifdef SUPPORT_MONITOR_COUNT
2019 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
2020 _held_monitor_count += i;
2021 if (jni) {
2022 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
2023 _jni_monitor_count += i;
2024 }
2025 assert(_held_monitor_count >= _jni_monitor_count, "Monitor count discrepancy detected - held count "
2026 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2027 #endif
2028 }
2029
2030 // Slow-path decrement of the held monitor counts. JNI unlocking is always
2031 // this slow-path.
2032 void JavaThread::dec_held_monitor_count(intx i, bool jni) {
2033 #ifdef SUPPORT_MONITOR_COUNT
2034 _held_monitor_count -= i;
2035 assert(_held_monitor_count >= 0, "Must always be greater than 0: " INTX_FORMAT, _held_monitor_count);
2036 if (jni) {
2037 _jni_monitor_count -= i;
2038 assert(_jni_monitor_count >= 0, "Must always be greater than 0: " INTX_FORMAT, _jni_monitor_count);
2039 }
2040 // When a thread is detaching with still owned JNI monitors, the logic that releases
2041 // the monitors doesn't know to set the "jni" flag and so the counts can get out of sync.
2042 // So we skip this assert if the thread is exiting. Once all monitors are unlocked the
2043 // JNI count is directly set to zero.
2044 assert(_held_monitor_count >= _jni_monitor_count || is_exiting(), "Monitor count discrepancy detected - held count "
2045 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2046 #endif
2047 }
2048
2049 frame JavaThread::vthread_last_frame() {
2050 assert (is_vthread_mounted(), "Virtual thread not mounted");
2051 return last_frame();
2052 }
2053
2054 frame JavaThread::carrier_last_frame(RegisterMap* reg_map) {
2055 const ContinuationEntry* entry = vthread_continuation();
2056 guarantee (entry != nullptr, "Not a carrier thread");
2057 frame f = entry->to_frame();
2058 if (reg_map->process_frames()) {
2208 // bound to the given java.lang.Thread instance.
2209 // The Threads_lock is held for the duration.
2210 void JavaThread::start_internal_daemon(JavaThread* current, JavaThread* target,
2211 Handle thread_oop, ThreadPriority prio) {
2212
2213 assert(target->osthread() != nullptr, "target thread is not properly initialized");
2214
2215 MutexLocker mu(current, Threads_lock);
2216
2217 // Initialize the fields of the thread_oop first.
2218 if (prio != NoPriority) {
2219 java_lang_Thread::set_priority(thread_oop(), prio);
2220 // Note: we don't call os::set_priority here. Possibly we should,
2221 // else all threads should call it themselves when they first run.
2222 }
2223
2224 java_lang_Thread::set_daemon(thread_oop());
2225
2226 // Now bind the thread_oop to the target JavaThread.
2227 target->set_threadOopHandles(thread_oop());
2228
2229 Threads::add(target); // target is now visible for safepoint/handshake
2230 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
2231 // on a ThreadsList. We don't want to wait for the release when the
2232 // Theads_lock is dropped when the 'mu' destructor is run since the
2233 // JavaThread* is already visible to JVM/TI via the ThreadsList.
2234
2235 assert(java_lang_Thread::thread(thread_oop()) == nullptr, "must not be alive");
2236 java_lang_Thread::release_set_thread(thread_oop(), target); // isAlive == true now
2237 Thread::start(target);
2238 }
2239
2240 void JavaThread::vm_exit_on_osthread_failure(JavaThread* thread) {
2241 // At this point it may be possible that no osthread was created for the
2242 // JavaThread due to lack of resources. However, since this must work
2243 // for critical system threads just check and abort if this fails.
2244 if (thread->osthread() == nullptr) {
2245 // This isn't really an OOM condition, but historically this is what
2246 // we report.
2247 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() :
419 // Initialize fields
420
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 _monitor_chunks(nullptr),
442
443 _suspend_flags(0),
444
445 _thread_state(_thread_new),
446 _saved_exception_pc(nullptr),
447 #ifdef ASSERT
448 _no_safepoint_count(0),
449 _visited_for_critical_count(false),
450 #endif
451
452 _terminated(_not_terminated),
453 _in_deopt_handler(0),
454 _doing_unsafe_access(false),
455 _do_not_unlock_if_synchronized(false),
456 #if INCLUDE_JVMTI
457 _carrier_thread_suspended(false),
458 _is_in_VTMS_transition(false),
459 _is_in_tmp_VTMS_transition(false),
479 #endif // INCLUDE_JVMCI
480
481 _exception_oop(oop()),
482 _exception_pc(0),
483 _exception_handler_pc(0),
484 _is_method_handle_return(0),
485
486 _jni_active_critical(0),
487 _pending_jni_exception_check_fn(nullptr),
488 _depth_first_number(0),
489
490 // JVMTI PopFrame support
491 _popframe_condition(popframe_inactive),
492 _frames_to_pop_failed_realloc(0),
493
494 _cont_entry(nullptr),
495 _cont_fastpath(0),
496 _cont_fastpath_thread_state(1),
497 _held_monitor_count(0),
498 _jni_monitor_count(0),
499 _preempting(false),
500 _preemption_cancelled(false),
501 _jvmti_unmount_event_pending(false),
502 DEBUG_ONLY(_obj_locker_count(0) COMMA)
503
504 _handshake(this),
505
506 _popframe_preserved_args(nullptr),
507 _popframe_preserved_args_size(0),
508
509 _jvmti_thread_state(nullptr),
510 _interp_only_mode(0),
511 _should_post_on_exceptions_flag(JNI_FALSE),
512 _thread_stat(new ThreadStatistics()),
513
514 _parker(),
515
516 _class_to_be_initialized(nullptr),
517
518 _SleepEvent(ParkEvent::Allocate(this)),
519
520 _lock_stack(this) {
521 set_jni_functions(jni_functions());
522
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
1180 HandshakeClosure("InstallAsyncException"), _aeh(aeh) {}
1181 ~InstallAsyncExceptionHandshake() {
1182 // If InstallAsyncExceptionHandshake was never executed we need to clean up _aeh.
1183 delete _aeh;
1184 }
1185 void do_thread(Thread* thr) {
1186 JavaThread* target = JavaThread::cast(thr);
1187 target->install_async_exception(_aeh);
1188 _aeh = nullptr;
1189 }
1190 };
1191
1192 void JavaThread::send_async_exception(JavaThread* target, oop java_throwable) {
1193 OopHandle e(Universe::vm_global(), java_throwable);
1194 InstallAsyncExceptionHandshake iaeh(new AsyncExceptionHandshake(e));
1195 Handshake::execute(&iaeh, target);
1196 }
1197
1198 #if INCLUDE_JVMTI
1199 void JavaThread::set_is_in_VTMS_transition(bool val) {
1200 assert(is_in_VTMS_transition() != val, "already %s transition", val ? "inside" : "outside");
1201 _is_in_VTMS_transition = val;
1202 }
1203
1204 #ifdef ASSERT
1205 void JavaThread::set_is_VTMS_transition_disabler(bool val) {
1206 _is_VTMS_transition_disabler = val;
1207 }
1208 #endif
1209 #endif
1210
1211 // External suspension mechanism.
1212 //
1213 // Guarantees on return (for a valid target thread):
1214 // - Target thread will not execute any new bytecode.
1215 // - Target thread will not enter any new monitors.
1216 //
1217 bool JavaThread::java_suspend() {
1218 #if INCLUDE_JVMTI
1219 // Suspending a JavaThread in VTMS transition or disabling VTMS transitions can cause deadlocks.
1220 assert(!is_in_VTMS_transition(), "no suspend allowed in VTMS transition");
1737 assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
1738 assert(NoPriority <= prio && prio <= MaxPriority, "sanity check");
1739 // Link Java Thread object <-> C++ Thread
1740
1741 // Get the C++ thread object (an oop) from the JNI handle (a jthread)
1742 // and put it into a new Handle. The Handle "thread_oop" can then
1743 // be used to pass the C++ thread object to other methods.
1744
1745 // Set the Java level thread object (jthread) field of the
1746 // new thread (a JavaThread *) to C++ thread object using the
1747 // "thread_oop" handle.
1748
1749 // Set the thread field (a JavaThread *) of the
1750 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
1751
1752 Handle thread_oop(Thread::current(),
1753 JNIHandles::resolve_non_null(jni_thread));
1754 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
1755 "must be initialized");
1756 set_threadOopHandles(thread_oop());
1757 set_lock_id(java_lang_Thread::thread_id(thread_oop()));
1758
1759 if (prio == NoPriority) {
1760 prio = java_lang_Thread::priority(thread_oop());
1761 assert(prio != NoPriority, "A valid priority should be present");
1762 }
1763
1764 // Push the Java priority down to the native thread; needs Threads_lock
1765 Thread::set_priority(this, prio);
1766
1767 // Add the new thread to the Threads list and set it in motion.
1768 // We must have threads lock in order to call Threads::add.
1769 // It is crucial that we do not block before the thread is
1770 // added to the Threads list for if a GC happens, then the java_thread oop
1771 // will not be visited by GC.
1772 Threads::add(this);
1773 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
1774 // on a ThreadsList. We don't want to wait for the release when the
1775 // Theads_lock is dropped somewhere in the caller since the JavaThread*
1776 // is already visible to JVM/TI via the ThreadsList.
1777 java_lang_Thread::release_set_thread(thread_oop(), this);
2030 // Slow-path increment of the held monitor counts. JNI locking is always
2031 // this slow-path.
2032 void JavaThread::inc_held_monitor_count(intx i, bool jni) {
2033 #ifdef SUPPORT_MONITOR_COUNT
2034 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
2035 _held_monitor_count += i;
2036 if (jni) {
2037 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
2038 _jni_monitor_count += i;
2039 }
2040 assert(_held_monitor_count >= _jni_monitor_count, "Monitor count discrepancy detected - held count "
2041 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2042 #endif
2043 }
2044
2045 // Slow-path decrement of the held monitor counts. JNI unlocking is always
2046 // this slow-path.
2047 void JavaThread::dec_held_monitor_count(intx i, bool jni) {
2048 #ifdef SUPPORT_MONITOR_COUNT
2049 _held_monitor_count -= i;
2050 assert(_held_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _held_monitor_count);
2051 if (jni) {
2052 _jni_monitor_count -= i;
2053 assert(_jni_monitor_count >= 0, "Must always be non-negative: " INTX_FORMAT, _jni_monitor_count);
2054 }
2055 // When a thread is detaching with still owned JNI monitors, the logic that releases
2056 // the monitors doesn't know to set the "jni" flag and so the counts can get out of sync.
2057 // So we skip this assert if the thread is exiting. Once all monitors are unlocked the
2058 // JNI count is directly set to zero.
2059 assert(_held_monitor_count >= _jni_monitor_count || is_exiting(), "Monitor count discrepancy detected - held count "
2060 INTX_FORMAT " is less than JNI count " INTX_FORMAT, _held_monitor_count, _jni_monitor_count);
2061 #endif
2062 }
2063
2064 frame JavaThread::vthread_last_frame() {
2065 assert (is_vthread_mounted(), "Virtual thread not mounted");
2066 return last_frame();
2067 }
2068
2069 frame JavaThread::carrier_last_frame(RegisterMap* reg_map) {
2070 const ContinuationEntry* entry = vthread_continuation();
2071 guarantee (entry != nullptr, "Not a carrier thread");
2072 frame f = entry->to_frame();
2073 if (reg_map->process_frames()) {
2223 // bound to the given java.lang.Thread instance.
2224 // The Threads_lock is held for the duration.
2225 void JavaThread::start_internal_daemon(JavaThread* current, JavaThread* target,
2226 Handle thread_oop, ThreadPriority prio) {
2227
2228 assert(target->osthread() != nullptr, "target thread is not properly initialized");
2229
2230 MutexLocker mu(current, Threads_lock);
2231
2232 // Initialize the fields of the thread_oop first.
2233 if (prio != NoPriority) {
2234 java_lang_Thread::set_priority(thread_oop(), prio);
2235 // Note: we don't call os::set_priority here. Possibly we should,
2236 // else all threads should call it themselves when they first run.
2237 }
2238
2239 java_lang_Thread::set_daemon(thread_oop());
2240
2241 // Now bind the thread_oop to the target JavaThread.
2242 target->set_threadOopHandles(thread_oop());
2243 target->set_lock_id(java_lang_Thread::thread_id(thread_oop()));
2244
2245 Threads::add(target); // target is now visible for safepoint/handshake
2246 // Publish the JavaThread* in java.lang.Thread after the JavaThread* is
2247 // on a ThreadsList. We don't want to wait for the release when the
2248 // Theads_lock is dropped when the 'mu' destructor is run since the
2249 // JavaThread* is already visible to JVM/TI via the ThreadsList.
2250
2251 assert(java_lang_Thread::thread(thread_oop()) == nullptr, "must not be alive");
2252 java_lang_Thread::release_set_thread(thread_oop(), target); // isAlive == true now
2253 Thread::start(target);
2254 }
2255
2256 void JavaThread::vm_exit_on_osthread_failure(JavaThread* thread) {
2257 // At this point it may be possible that no osthread was created for the
2258 // JavaThread due to lack of resources. However, since this must work
2259 // for critical system threads just check and abort if this fails.
2260 if (thread->osthread() == nullptr) {
2261 // This isn't really an OOM condition, but historically this is what
2262 // we report.
2263 vm_exit_during_initialization("java.lang.OutOfMemoryError",
|