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