< prev index next >

src/hotspot/share/runtime/threads.cpp

Print this page

 150   assert(ik->is_initialized(), "must be");
 151   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
 152 
 153   // Cannot use JavaCalls::construct_new_instance because the java.lang.Thread
 154   // constructor calls Thread.current(), which must be set here for the
 155   // initial thread.
 156   java_lang_Thread::set_thread(thread_oop(), thread);
 157   thread->set_threadOopHandles(thread_oop());
 158 
 159   Handle string = java_lang_String::create_from_str("main", CHECK);
 160 
 161   JavaValue result(T_VOID);
 162   JavaCalls::call_special(&result, thread_oop,
 163                           ik,
 164                           vmSymbols::object_initializer_name(),
 165                           vmSymbols::threadgroup_string_void_signature(),
 166                           thread_group,
 167                           string,
 168                           CHECK);
 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   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 

1187                                                          address monitor) {
1188   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
1189 
1190   int i = 0;
1191   for (JavaThread* p : *t_list) {
1192     if (!p->can_call_java()) continue;
1193 
1194     // The first stage of async deflation does not affect any field
1195     // used by this comparison so the ObjectMonitor* is usable here.
1196     address pending = (address)p->current_pending_monitor();
1197     if (pending == monitor) {             // found a match
1198       if (i < count) result->append(p);   // save the first count matches
1199       i++;
1200     }
1201   }
1202 
1203   return result;
1204 }
1205 
1206 
1207 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
1208                                                       address owner) {
1209   assert(LockingMode != LM_LIGHTWEIGHT, "Not with new lightweight locking");
1210   // null owner means not locked so we can skip the search
1211   if (owner == nullptr) return nullptr;
1212 
1213   for (JavaThread* p : *t_list) {
1214     // first, see if owner is the address of a Java thread
1215     if (owner == (address)p) return p;
1216   }
1217 
1218   // Cannot assert on lack of success here since this function may be
1219   // used by code that is trying to report useful problem information
1220   // like deadlock detection.
1221   if (LockingMode == LM_MONITOR) return nullptr;
1222 
1223   // If we didn't find a matching Java thread and we didn't force use of
1224   // heavyweight monitors, then the owner is the stack address of the
1225   // Lock Word in the owning Java thread's stack.
1226   //
1227   JavaThread* the_owner = nullptr;
1228   for (JavaThread* q : *t_list) {
1229     if (q->is_lock_owned(owner)) {
1230       the_owner = q;
1231       break;
1232     }
1233   }
1234 
1235   // cannot assert on lack of success here; see above comment
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 (LockingMode == LM_LIGHTWEIGHT) {
1257     if (monitor->is_owner_anonymous()) {
1258       return owning_thread_from_object(t_list, monitor->object());
1259     } else {
1260       Thread* owner = reinterpret_cast<Thread*>(monitor->owner());
1261       assert(owner == nullptr || owner->is_Java_thread(), "only JavaThreads own monitors");
1262       return reinterpret_cast<JavaThread*>(owner);
1263     }
1264   } else {
1265     address owner = (address)monitor->owner();
1266     return owning_thread_from_monitor_owner(t_list, owner);







1267   }
1268 }
1269 
1270 class PrintOnClosure : public ThreadClosure {
1271 private:
1272   outputStream* _st;
1273 
1274 public:
1275   PrintOnClosure(outputStream* st) :
1276       _st(st) {}
1277 
1278   virtual void do_thread(Thread* thread) {
1279     if (thread != nullptr) {
1280       thread->print_on(_st);
1281       _st->cr();
1282     }
1283   }
1284 };
1285 
1286 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.

 150   assert(ik->is_initialized(), "must be");
 151   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
 152 
 153   // Cannot use JavaCalls::construct_new_instance because the java.lang.Thread
 154   // constructor calls Thread.current(), which must be set here for the
 155   // initial thread.
 156   java_lang_Thread::set_thread(thread_oop(), thread);
 157   thread->set_threadOopHandles(thread_oop());
 158 
 159   Handle string = java_lang_String::create_from_str("main", CHECK);
 160 
 161   JavaValue result(T_VOID);
 162   JavaCalls::call_special(&result, thread_oop,
 163                           ik,
 164                           vmSymbols::object_initializer_name(),
 165                           vmSymbols::threadgroup_string_void_signature(),
 166                           thread_group,
 167                           string,
 168                           CHECK);
 169 
 170   assert(thread->lock_id() == ThreadIdentifier::initial(), "invariant");
 171 
 172   // Set thread status to running since main thread has
 173   // been started and running.
 174   java_lang_Thread::set_thread_status(thread_oop(),
 175                                       JavaThreadStatus::RUNNABLE);
 176 }
 177 
 178 // Extract version and vendor specific information from
 179 // java.lang.VersionProps fields.
 180 // Returned char* is allocated in the thread's resource area
 181 // so must be copied for permanency.
 182 static const char* get_java_version_info(InstanceKlass* ik,
 183                                          Symbol* field_name) {
 184   fieldDescriptor fd;
 185   bool found = ik != nullptr &&
 186                ik->find_local_field(field_name,
 187                                     vmSymbols::string_signature(), &fd);
 188   if (found) {
 189     oop name_oop = ik->java_mirror()->obj_field(fd.offset());
 190     if (name_oop == nullptr) {
 191       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 

1196                                                          address monitor) {
1197   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
1198 
1199   int i = 0;
1200   for (JavaThread* p : *t_list) {
1201     if (!p->can_call_java()) continue;
1202 
1203     // The first stage of async deflation does not affect any field
1204     // used by this comparison so the ObjectMonitor* is usable here.
1205     address pending = (address)p->current_pending_monitor();
1206     if (pending == monitor) {             // found a match
1207       if (i < count) result->append(p);   // save the first count matches
1208       i++;
1209     }
1210   }
1211 
1212   return result;
1213 }
1214 
1215 
1216 JavaThread *Threads::owning_thread_from_stacklock(ThreadsList * t_list, address basicLock) {
1217   assert(LockingMode == LM_LEGACY, "Not with new lightweight locking");













1218 




1219   JavaThread* the_owner = nullptr;
1220   for (JavaThread* q : *t_list) {
1221     if (q->is_lock_owned(basicLock)) {
1222       the_owner = q;
1223       break;
1224     }
1225   }


1226   return the_owner;
1227 }
1228 
1229 JavaThread* Threads::owning_thread_from_object(ThreadsList * t_list, oop obj) {
1230   assert(LockingMode == LM_LIGHTWEIGHT, "Only with new lightweight locking");
1231   for (JavaThread* q : *t_list) {
1232     // Need to start processing before accessing oops in the thread.
1233     StackWatermark* watermark = StackWatermarkSet::get(q, StackWatermarkKind::gc);
1234     if (watermark != nullptr) {
1235       watermark->start_processing();
1236     }
1237 
1238     if (q->lock_stack().contains(obj)) {
1239       return q;
1240     }
1241   }
1242   return nullptr;
1243 }
1244 
1245 JavaThread* Threads::owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* monitor) {
1246   if (monitor->is_owner_anonymous()) {
1247     if (LockingMode == LM_LIGHTWEIGHT) {
1248       return owning_thread_from_object(t_list, monitor->object());
1249     } else {
1250       assert(LockingMode == LM_LEGACY, "invariant");
1251       return owning_thread_from_stacklock(t_list, (address)monitor->stack_locker());

1252     }
1253   } else {
1254     JavaThread* the_owner = nullptr;
1255     int64_t owner_tid = (int64_t)monitor->owner();
1256     for (JavaThread* q : *t_list) {
1257       if (monitor->is_owner(q)) {
1258         the_owner = q;
1259         break;
1260       }
1261     }
1262     return the_owner;
1263   }
1264 }
1265 
1266 class PrintOnClosure : public ThreadClosure {
1267 private:
1268   outputStream* _st;
1269 
1270 public:
1271   PrintOnClosure(outputStream* st) :
1272       _st(st) {}
1273 
1274   virtual void do_thread(Thread* thread) {
1275     if (thread != nullptr) {
1276       thread->print_on(_st);
1277       _st->cr();
1278     }
1279   }
1280 };
1281 
1282 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
< prev index next >