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