149 assert(ik->is_initialized(), "must be");
150 instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
151
152 // Cannot use JavaCalls::construct_new_instance because the java.lang.Thread
153 // constructor calls Thread.current(), which must be set here for the
154 // initial thread.
155 java_lang_Thread::set_thread(thread_oop(), thread);
156 thread->set_threadOopHandles(thread_oop());
157
158 Handle string = java_lang_String::create_from_str("main", CHECK);
159
160 JavaValue result(T_VOID);
161 JavaCalls::call_special(&result, thread_oop,
162 ik,
163 vmSymbols::object_initializer_name(),
164 vmSymbols::threadgroup_string_void_signature(),
165 thread_group,
166 string,
167 CHECK);
168
169 // Set thread status to running since main thread has
170 // been started and running.
171 java_lang_Thread::set_thread_status(thread_oop(),
172 JavaThreadStatus::RUNNABLE);
173 }
174
175 // Extract version and vendor specific information from
176 // java.lang.VersionProps fields.
177 // Returned char* is allocated in the thread's resource area
178 // so must be copied for permanency.
179 static const char* get_java_version_info(InstanceKlass* ik,
180 Symbol* field_name) {
181 fieldDescriptor fd;
182 bool found = ik != nullptr &&
183 ik->find_local_field(field_name,
184 vmSymbols::string_signature(), &fd);
185 if (found) {
186 oop name_oop = ik->java_mirror()->obj_field(fd.offset());
187 if (name_oop == nullptr) {
188 return nullptr;
515 JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtJVMCI);
516 memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
517 } else {
518 JavaThread::_jvmci_old_thread_counters = nullptr;
519 }
520 #endif // INCLUDE_JVMCI
521
522 // Initialize OopStorage for threadObj
523 JavaThread::_thread_oop_storage = OopStorageSet::create_strong("Thread OopStorage", mtThread);
524
525 // Attach the main thread to this os thread
526 JavaThread* main_thread = new JavaThread();
527 main_thread->set_thread_state(_thread_in_vm);
528 main_thread->initialize_thread_current();
529 // must do this before set_active_handles
530 main_thread->record_stack_base_and_size();
531 main_thread->register_thread_stack_with_NMT();
532 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
533 MACOS_AARCH64_ONLY(main_thread->init_wx());
534
535 if (!main_thread->set_as_starting_thread()) {
536 vm_shutdown_during_initialization(
537 "Failed necessary internal allocation. Out of swap space");
538 main_thread->smr_delete();
539 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
540 return JNI_ENOMEM;
541 }
542
543 // Enable guard page *after* os::create_main_thread(), otherwise it would
544 // crash Linux VM, see notes in os_linux.cpp.
545 main_thread->stack_overflow_state()->create_stack_guard_pages();
546
547 // Initialize Java-Level synchronization subsystem
548 ObjectMonitor::Initialize();
549 ObjectSynchronizer::initialize();
550
551 // Initialize global modules
552 jint status = init_globals();
553 if (status != JNI_OK) {
554 main_thread->smr_delete();
564 // on_thread_attach. Should be before starting to build Java objects in
565 // init_globals2, which invokes barriers.
566 {
567 MutexLocker mu(Threads_lock);
568 Threads::add(main_thread);
569 }
570
571 status = init_globals2();
572 if (status != JNI_OK) {
573 Threads::remove(main_thread, false);
574 // It is possible that we managed to fully initialize Universe but have then
575 // failed by throwing an exception. In that case our caller JNI_CreateJavaVM
576 // will want to report it, so we can't delete the main thread.
577 if (!main_thread->has_pending_exception()) {
578 main_thread->smr_delete();
579 }
580 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
581 return status;
582 }
583
584 JFR_ONLY(Jfr::on_create_vm_1();)
585
586 // Should be done after the heap is fully created
587 main_thread->cache_global_variables();
588
589 // Any JVMTI raw monitors entered in onload will transition into
590 // real raw monitor. VM is setup enough here for raw monitor enter.
591 JvmtiExport::transition_pending_onload_raw_monitors();
592
593 // Create the VMThread
594 { TraceTime timer("Start VMThread", TRACETIME_LOG(Info, startuptime));
595
596 VMThread::create();
597 VMThread* vmthread = VMThread::vm_thread();
598
599 if (!os::create_thread(vmthread, os::vm_thread)) {
600 vm_exit_during_initialization("Cannot create VM thread. "
601 "Out of system resources.");
602 }
603
1209 continue;
1210 }
1211 // The first stage of async deflation does not affect any field
1212 // used by this comparison so the ObjectMonitor* is usable here.
1213 address pending = (address)p->current_pending_monitor();
1214 address waiting = (address)p->current_waiting_monitor();
1215 // do not include virtual threads to the list
1216 jint state = JvmtiEnvBase::get_thread_state(thread_oop, p);
1217 if (pending == monitor || (waiting == monitor &&
1218 (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER))
1219 ) { // found a match
1220 if (i < count) result->append(p); // save the first count matches
1221 i++;
1222 }
1223 }
1224
1225 return result;
1226 }
1227 #endif // INCLUDE_JVMTI
1228
1229 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
1230 address owner) {
1231 assert(LockingMode != LM_LIGHTWEIGHT, "Not with new lightweight locking");
1232 // null owner means not locked so we can skip the search
1233 if (owner == nullptr) return nullptr;
1234
1235 for (JavaThread* p : *t_list) {
1236 // first, see if owner is the address of a Java thread
1237 if (owner == (address)p) return p;
1238 }
1239
1240 // Cannot assert on lack of success here since this function may be
1241 // used by code that is trying to report useful problem information
1242 // like deadlock detection.
1243 if (LockingMode == LM_MONITOR) return nullptr;
1244
1245 // If we didn't find a matching Java thread and we didn't force use of
1246 // heavyweight monitors, then the owner is the stack address of the
1247 // Lock Word in the owning Java thread's stack.
1248 //
1249 JavaThread* the_owner = nullptr;
1250 for (JavaThread* q : *t_list) {
1251 if (q->is_lock_owned(owner)) {
1252 the_owner = q;
1253 break;
1254 }
1255 }
1256
1257 // cannot assert on lack of success here; see above comment
1258 return the_owner;
1259 }
1260
1261 JavaThread* Threads::owning_thread_from_object(ThreadsList * t_list, oop obj) {
1262 assert(LockingMode == LM_LIGHTWEIGHT, "Only with new lightweight locking");
1263 for (JavaThread* q : *t_list) {
1264 // Need to start processing before accessing oops in the thread.
1265 StackWatermark* watermark = StackWatermarkSet::get(q, StackWatermarkKind::gc);
1266 if (watermark != nullptr) {
1267 watermark->start_processing();
1268 }
1269
1270 if (q->lock_stack().contains(obj)) {
1271 return q;
1272 }
1273 }
1274 return nullptr;
1275 }
1276
1277 JavaThread* Threads::owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* monitor) {
1278 if (LockingMode == LM_LIGHTWEIGHT) {
1279 if (monitor->is_owner_anonymous()) {
1280 return owning_thread_from_object(t_list, monitor->object());
1281 } else {
1282 Thread* owner = reinterpret_cast<Thread*>(monitor->owner());
1283 assert(owner == nullptr || owner->is_Java_thread(), "only JavaThreads own monitors");
1284 return reinterpret_cast<JavaThread*>(owner);
1285 }
1286 } else {
1287 address owner = (address)monitor->owner();
1288 return owning_thread_from_monitor_owner(t_list, owner);
1289 }
1290 }
1291
1292 class PrintOnClosure : public ThreadClosure {
1293 private:
1294 outputStream* _st;
1295
1296 public:
1297 PrintOnClosure(outputStream* st) :
1298 _st(st) {}
1299
1300 virtual void do_thread(Thread* thread) {
1301 if (thread != nullptr) {
1302 thread->print_on(_st);
1303 _st->cr();
1304 }
1305 }
1306 };
1307
1308 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
1320
1321 #if INCLUDE_SERVICES
1322 // Dump concurrent locks
1323 ConcurrentLocksDump concurrent_locks;
1324 if (print_concurrent_locks) {
1325 concurrent_locks.dump_at_safepoint();
1326 }
1327 #endif // INCLUDE_SERVICES
1328
1329 ThreadsSMRSupport::print_info_on(st);
1330 st->cr();
1331
1332 ALL_JAVA_THREADS(p) {
1333 ResourceMark rm;
1334 p->print_on(st, print_extended_info);
1335 if (print_stacks) {
1336 if (internal_format) {
1337 p->trace_stack();
1338 } else {
1339 p->print_stack_on(st);
1340 const oop thread_oop = p->threadObj();
1341 if (thread_oop != nullptr) {
1342 if (p->is_vthread_mounted()) {
1343 const oop vt = p->vthread();
1344 assert(vt != nullptr, "vthread should not be null when vthread is mounted");
1345 // JavaThread._vthread can refer to the carrier thread. Print only if _vthread refers to a virtual thread.
1346 if (vt != thread_oop) {
1347 st->print_cr(" Mounted virtual thread #" INT64_FORMAT, (int64_t)java_lang_Thread::thread_id(vt));
1348 p->print_vthread_stack_on(st);
1349 }
1350 }
1351 }
1352 }
1353 }
1354 st->cr();
1355 #if INCLUDE_SERVICES
1356 if (print_concurrent_locks) {
1357 concurrent_locks.print_locks_on(p, st);
1358 }
1359 #endif // INCLUDE_SERVICES
1360 }
1361
1362 PrintOnClosure cl(st);
1363 non_java_threads_do(&cl);
1364
1365 st->flush();
1366 }
1367
1368 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
1369 int buflen, bool* found_current) {
1370 if (this_thread != nullptr) {
|
149 assert(ik->is_initialized(), "must be");
150 instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
151
152 // Cannot use JavaCalls::construct_new_instance because the java.lang.Thread
153 // constructor calls Thread.current(), which must be set here for the
154 // initial thread.
155 java_lang_Thread::set_thread(thread_oop(), thread);
156 thread->set_threadOopHandles(thread_oop());
157
158 Handle string = java_lang_String::create_from_str("main", CHECK);
159
160 JavaValue result(T_VOID);
161 JavaCalls::call_special(&result, thread_oop,
162 ik,
163 vmSymbols::object_initializer_name(),
164 vmSymbols::threadgroup_string_void_signature(),
165 thread_group,
166 string,
167 CHECK);
168
169 assert(thread->lock_id() == ThreadIdentifier::initial(), "invariant");
170
171 // Set thread status to running since main thread has
172 // been started and running.
173 java_lang_Thread::set_thread_status(thread_oop(),
174 JavaThreadStatus::RUNNABLE);
175 }
176
177 // Extract version and vendor specific information from
178 // java.lang.VersionProps fields.
179 // Returned char* is allocated in the thread's resource area
180 // so must be copied for permanency.
181 static const char* get_java_version_info(InstanceKlass* ik,
182 Symbol* field_name) {
183 fieldDescriptor fd;
184 bool found = ik != nullptr &&
185 ik->find_local_field(field_name,
186 vmSymbols::string_signature(), &fd);
187 if (found) {
188 oop name_oop = ik->java_mirror()->obj_field(fd.offset());
189 if (name_oop == nullptr) {
190 return nullptr;
517 JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtJVMCI);
518 memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
519 } else {
520 JavaThread::_jvmci_old_thread_counters = nullptr;
521 }
522 #endif // INCLUDE_JVMCI
523
524 // Initialize OopStorage for threadObj
525 JavaThread::_thread_oop_storage = OopStorageSet::create_strong("Thread OopStorage", mtThread);
526
527 // Attach the main thread to this os thread
528 JavaThread* main_thread = new JavaThread();
529 main_thread->set_thread_state(_thread_in_vm);
530 main_thread->initialize_thread_current();
531 // must do this before set_active_handles
532 main_thread->record_stack_base_and_size();
533 main_thread->register_thread_stack_with_NMT();
534 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
535 MACOS_AARCH64_ONLY(main_thread->init_wx());
536
537 // Set the lock_id now since we will run Java code before the Thread instance
538 // is even created. The same value will be assigned to the Thread instance on init.
539 main_thread->set_lock_id(ThreadIdentifier::next());
540 assert(main_thread->lock_id() == ThreadIdentifier::initial(), "invariant");
541
542 if (!main_thread->set_as_starting_thread()) {
543 vm_shutdown_during_initialization(
544 "Failed necessary internal allocation. Out of swap space");
545 main_thread->smr_delete();
546 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
547 return JNI_ENOMEM;
548 }
549
550 // Enable guard page *after* os::create_main_thread(), otherwise it would
551 // crash Linux VM, see notes in os_linux.cpp.
552 main_thread->stack_overflow_state()->create_stack_guard_pages();
553
554 // Initialize Java-Level synchronization subsystem
555 ObjectMonitor::Initialize();
556 ObjectSynchronizer::initialize();
557
558 // Initialize global modules
559 jint status = init_globals();
560 if (status != JNI_OK) {
561 main_thread->smr_delete();
571 // on_thread_attach. Should be before starting to build Java objects in
572 // init_globals2, which invokes barriers.
573 {
574 MutexLocker mu(Threads_lock);
575 Threads::add(main_thread);
576 }
577
578 status = init_globals2();
579 if (status != JNI_OK) {
580 Threads::remove(main_thread, false);
581 // It is possible that we managed to fully initialize Universe but have then
582 // failed by throwing an exception. In that case our caller JNI_CreateJavaVM
583 // will want to report it, so we can't delete the main thread.
584 if (!main_thread->has_pending_exception()) {
585 main_thread->smr_delete();
586 }
587 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
588 return status;
589 }
590
591 ObjectMonitor::Initialize2();
592
593 JFR_ONLY(Jfr::on_create_vm_1();)
594
595 // Should be done after the heap is fully created
596 main_thread->cache_global_variables();
597
598 // Any JVMTI raw monitors entered in onload will transition into
599 // real raw monitor. VM is setup enough here for raw monitor enter.
600 JvmtiExport::transition_pending_onload_raw_monitors();
601
602 // Create the VMThread
603 { TraceTime timer("Start VMThread", TRACETIME_LOG(Info, startuptime));
604
605 VMThread::create();
606 VMThread* vmthread = VMThread::vm_thread();
607
608 if (!os::create_thread(vmthread, os::vm_thread)) {
609 vm_exit_during_initialization("Cannot create VM thread. "
610 "Out of system resources.");
611 }
612
1218 continue;
1219 }
1220 // The first stage of async deflation does not affect any field
1221 // used by this comparison so the ObjectMonitor* is usable here.
1222 address pending = (address)p->current_pending_monitor();
1223 address waiting = (address)p->current_waiting_monitor();
1224 // do not include virtual threads to the list
1225 jint state = JvmtiEnvBase::get_thread_state(thread_oop, p);
1226 if (pending == monitor || (waiting == monitor &&
1227 (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER))
1228 ) { // found a match
1229 if (i < count) result->append(p); // save the first count matches
1230 i++;
1231 }
1232 }
1233
1234 return result;
1235 }
1236 #endif // INCLUDE_JVMTI
1237
1238 JavaThread *Threads::owning_thread_from_stacklock(ThreadsList * t_list, address basicLock) {
1239 assert(LockingMode == LM_LEGACY, "Not with new lightweight locking");
1240
1241 JavaThread* the_owner = nullptr;
1242 for (JavaThread* q : *t_list) {
1243 if (q->is_lock_owned(basicLock)) {
1244 the_owner = q;
1245 break;
1246 }
1247 }
1248 return the_owner;
1249 }
1250
1251 JavaThread* Threads::owning_thread_from_object(ThreadsList * t_list, oop obj) {
1252 assert(LockingMode == LM_LIGHTWEIGHT, "Only with new lightweight locking");
1253 for (JavaThread* q : *t_list) {
1254 // Need to start processing before accessing oops in the thread.
1255 StackWatermark* watermark = StackWatermarkSet::get(q, StackWatermarkKind::gc);
1256 if (watermark != nullptr) {
1257 watermark->start_processing();
1258 }
1259
1260 if (q->lock_stack().contains(obj)) {
1261 return q;
1262 }
1263 }
1264 return nullptr;
1265 }
1266
1267 JavaThread* Threads::owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* monitor) {
1268 if (monitor->has_anonymous_owner()) {
1269 if (LockingMode == LM_LIGHTWEIGHT) {
1270 return owning_thread_from_object(t_list, monitor->object());
1271 } else {
1272 assert(LockingMode == LM_LEGACY, "invariant");
1273 return owning_thread_from_stacklock(t_list, (address)monitor->stack_locker());
1274 }
1275 } else {
1276 JavaThread* the_owner = nullptr;
1277 for (JavaThread* q : *t_list) {
1278 if (monitor->has_owner(q)) {
1279 the_owner = q;
1280 break;
1281 }
1282 }
1283 return the_owner;
1284 }
1285 }
1286
1287 class PrintOnClosure : public ThreadClosure {
1288 private:
1289 outputStream* _st;
1290
1291 public:
1292 PrintOnClosure(outputStream* st) :
1293 _st(st) {}
1294
1295 virtual void do_thread(Thread* thread) {
1296 if (thread != nullptr) {
1297 thread->print_on(_st);
1298 _st->cr();
1299 }
1300 }
1301 };
1302
1303 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
1315
1316 #if INCLUDE_SERVICES
1317 // Dump concurrent locks
1318 ConcurrentLocksDump concurrent_locks;
1319 if (print_concurrent_locks) {
1320 concurrent_locks.dump_at_safepoint();
1321 }
1322 #endif // INCLUDE_SERVICES
1323
1324 ThreadsSMRSupport::print_info_on(st);
1325 st->cr();
1326
1327 ALL_JAVA_THREADS(p) {
1328 ResourceMark rm;
1329 p->print_on(st, print_extended_info);
1330 if (print_stacks) {
1331 if (internal_format) {
1332 p->trace_stack();
1333 } else {
1334 p->print_stack_on(st);
1335 if (p->is_vthread_mounted()) {
1336 st->print(" Mounted virtual thread #");
1337 // _lock_id is the thread ID of the mounted virtual thread. If it equals
1338 // the tid of the carrier we caught thread at the start of a temporary
1339 // transition in VirtualThread.switchToCarrierThread. Ignore that case.
1340 if (p->lock_id() != java_lang_Thread::thread_id(p->threadObj())) {
1341 st->print_cr(INT64_FORMAT, p->lock_id());
1342 } else {
1343 st->print_cr("%s", "(Unavailable)");
1344 }
1345 p->print_vthread_stack_on(st);
1346 }
1347 }
1348 }
1349 st->cr();
1350 #if INCLUDE_SERVICES
1351 if (print_concurrent_locks) {
1352 concurrent_locks.print_locks_on(p, st);
1353 }
1354 #endif // INCLUDE_SERVICES
1355 }
1356
1357 PrintOnClosure cl(st);
1358 non_java_threads_do(&cl);
1359
1360 st->flush();
1361 }
1362
1363 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
1364 int buflen, bool* found_current) {
1365 if (this_thread != nullptr) {
|