< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




 100 #include "runtime/vframe.inline.hpp"
 101 #include "runtime/vframeArray.hpp"
 102 #include "runtime/vframe_hp.hpp"
 103 #include "runtime/vmThread.hpp"
 104 #include "runtime/vmOperations.hpp"
 105 #include "runtime/vm_version.hpp"
 106 #include "services/attachListener.hpp"
 107 #include "services/management.hpp"
 108 #include "services/memTracker.hpp"
 109 #include "services/threadService.hpp"
 110 #include "utilities/align.hpp"
 111 #include "utilities/copy.hpp"
 112 #include "utilities/defaultStream.hpp"
 113 #include "utilities/dtrace.hpp"
 114 #include "utilities/events.hpp"
 115 #include "utilities/macros.hpp"
 116 #include "utilities/preserveException.hpp"
 117 #include "utilities/singleWriterSynchronizer.hpp"
 118 #include "utilities/vmError.hpp"
 119 #if INCLUDE_JVMCI
 120 #include "jvmci/jvmci.hpp"
 121 #include "jvmci/jvmciEnv.hpp"

 122 #endif
 123 #ifdef COMPILER1
 124 #include "c1/c1_Compiler.hpp"
 125 #endif
 126 #ifdef COMPILER2
 127 #include "opto/c2compiler.hpp"
 128 #include "opto/idealGraphPrinter.hpp"
 129 #endif
 130 #if INCLUDE_RTM_OPT
 131 #include "runtime/rtmLocking.hpp"
 132 #endif
 133 #if INCLUDE_JFR
 134 #include "jfr/jfr.hpp"
 135 #endif
 136 
 137 // Initialization after module runtime initialization
 138 void universe_post_module_init();  // must happen after call_initPhase2
 139 
 140 #ifdef DTRACE_ENABLED
 141 


 700       *bits |= 0x00004000;
 701 
 702       // Once the thread leaves thread_in_native_trans for another
 703       // thread state, we break out of this retry loop. We shouldn't
 704       // need this flag to prevent us from getting back here, but
 705       // sometimes paranoia is good.
 706       did_trans_retry = true;
 707 
 708       // We wait for the thread to transition to a more usable state.
 709       for (int i = 1; i <= SuspendRetryCount; i++) {
 710         // We used to do an "os::yield_all(i)" call here with the intention
 711         // that yielding would increase on each retry. However, the parameter
 712         // is ignored on Linux which means the yield didn't scale up. Waiting
 713         // on the SR_lock below provides a much more predictable scale up for
 714         // the delay. It also provides a simple/direct point to check for any
 715         // safepoint requests from the VMThread
 716 
 717         // temporarily drops SR_lock while doing wait with safepoint check
 718         // (if we're a JavaThread - the WatcherThread can also call this)
 719         // and increase delay with each retry
 720         if (Thread::current()->is_Java_thread()) {
 721           SR_lock()->wait(i * delay);
 722         } else {
 723           SR_lock()->wait_without_safepoint_check(i * delay);
 724         }
 725 
 726         // check the actual thread state instead of what we saved above
 727         if (thread_state() != _thread_in_native_trans) {
 728           // the thread has transitioned to another thread state so
 729           // try all the checks (except this one) one more time.
 730           do_trans_retry = true;
 731           break;
 732         }
 733       } // end retry loop
 734 
 735 
 736     }
 737   } while (do_trans_retry);
 738 
 739   *bits |= 0x00000010;
 740   return false;
 741 }
 742 
 743 // Wait for an external suspend request to complete (or be cancelled).
 744 // Returns true if the thread is externally suspended and false otherwise.
 745 //
 746 bool JavaThread::wait_for_ext_suspend_completion(int retries, int delay,
 747                                                  uint32_t *bits) {
 748   TraceSuspendDebugBits tsdb(this, true /* is_wait */,
 749                              false /* !called_by_wait */, bits);
 750 
 751   // local flag copies to minimize SR_lock hold time
 752   bool is_suspended;
 753   bool pending;
 754   uint32_t reset_bits;
 755 
 756   // set a marker so is_ext_suspend_completed() knows we are the caller
 757   *bits |= 0x00010000;
 758 
 759   // We use reset_bits to reinitialize the bits value at the top of
 760   // each retry loop. This allows the caller to make use of any
 761   // unused bits for their own marking purposes.
 762   reset_bits = *bits;
 763 
 764   {
 765     MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
 766     is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
 767                                             delay, bits);
 768     pending = is_external_suspend();
 769   }
 770   // must release SR_lock to allow suspension to complete
 771 
 772   if (!pending) {
 773     // A cancelled suspend request is the only false return from
 774     // is_ext_suspend_completed() that keeps us from entering the
 775     // retry loop.
 776     *bits |= 0x00020000;
 777     return false;
 778   }
 779 
 780   if (is_suspended) {
 781     *bits |= 0x00040000;
 782     return true;
 783   }
 784 
 785   for (int i = 1; i <= retries; i++) {
 786     *bits = reset_bits;  // reinit to only track last retry
 787 
 788     // We used to do an "os::yield_all(i)" call here with the intention
 789     // that yielding would increase on each retry. However, the parameter
 790     // is ignored on Linux which means the yield didn't scale up. Waiting
 791     // on the SR_lock below provides a much more predictable scale up for
 792     // the delay. It also provides a simple/direct point to check for any
 793     // safepoint requests from the VMThread
 794 
 795     {
 796       Thread* t = Thread::current();
 797       MonitorLocker ml(SR_lock(),
 798                        t->is_Java_thread() ? Mutex::_safepoint_check_flag : Mutex::_no_safepoint_check_flag);
 799       // wait with safepoint check (if we're a JavaThread - the WatcherThread
 800       // can also call this)  and increase delay with each retry
 801       ml.wait(i * delay);
 802 
 803       is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
 804                                               delay, bits);
 805 
 806       // It is possible for the external suspend request to be cancelled
 807       // (by a resume) before the actual suspend operation is completed.
 808       // Refresh our local copy to see if we still need to wait.
 809       pending = is_external_suspend();
 810     }
 811 
 812     if (!pending) {
 813       // A cancelled suspend request is the only false return from
 814       // is_ext_suspend_completed() that keeps us from staying in the
 815       // retry loop.
 816       *bits |= 0x00080000;
 817       return false;
 818     }
 819 
 820     if (is_suspended) {
 821       *bits |= 0x00100000;


 936     st->print("elapsed=%.2fs ",
 937               _statistical_info.getElapsedTime() / 1000.0
 938               );
 939     if (is_Java_thread() && (PrintExtendedThreadInfo || print_extended_info)) {
 940       size_t allocated_bytes = (size_t) const_cast<Thread*>(this)->cooked_allocated_bytes();
 941       st->print("allocated=" SIZE_FORMAT "%s ",
 942                 byte_size_in_proper_unit(allocated_bytes),
 943                 proper_unit_for_byte_size(allocated_bytes)
 944                 );
 945       st->print("defined_classes=" INT64_FORMAT " ", _statistical_info.getDefineClassCount());
 946     }
 947 
 948     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
 949     osthread()->print_on(st);
 950   }
 951   ThreadsSMRSupport::print_info_on(this, st);
 952   st->print(" ");
 953   debug_only(if (WizardMode) print_owned_locks_on(st);)
 954 }
 955 
 956 void Thread::print() const { print_on(tty); }
 957 
 958 // Thread::print_on_error() is called by fatal error handler. Don't use
 959 // any lock or allocate memory.
 960 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
 961   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
 962 
 963   if (is_VM_thread())                 { st->print("VMThread"); }
 964   else if (is_GC_task_thread())       { st->print("GCTaskThread"); }
 965   else if (is_Watcher_thread())       { st->print("WatcherThread"); }
 966   else if (is_ConcurrentGC_thread())  { st->print("ConcurrentGCThread"); }
 967   else                                { st->print("Thread"); }
 968 
 969   if (is_Named_thread()) {
 970     st->print(" \"%s\"", name());
 971   }
 972 
 973   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
 974             p2i(stack_end()), p2i(stack_base()));
 975 
 976   if (osthread()) {
 977     st->print(" [id=%d]", osthread()->thread_id());


1279   _protect_enter(_the_list._protect.enter()),
1280   _current(OrderAccess::load_acquire(&_the_list._head))
1281 {}
1282 
1283 NonJavaThread::Iterator::~Iterator() {
1284   _the_list._protect.exit(_protect_enter);
1285 }
1286 
1287 void NonJavaThread::Iterator::step() {
1288   assert(!end(), "precondition");
1289   _current = OrderAccess::load_acquire(&_current->_next);
1290 }
1291 
1292 NonJavaThread::NonJavaThread() : Thread(), _next(NULL) {
1293   assert(BarrierSet::barrier_set() != NULL, "NonJavaThread created too soon!");
1294 }
1295 
1296 NonJavaThread::~NonJavaThread() { }
1297 
1298 void NonJavaThread::add_to_the_list() {
1299   MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
1300   // Initialize BarrierSet-related data before adding to list.
1301   BarrierSet::barrier_set()->on_thread_attach(this);
1302   OrderAccess::release_store(&_next, _the_list._head);
1303   OrderAccess::release_store(&_the_list._head, this);
1304 }
1305 
1306 void NonJavaThread::remove_from_the_list() {
1307   {
1308     MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
1309     // Cleanup BarrierSet-related data before removing from list.
1310     BarrierSet::barrier_set()->on_thread_detach(this);
1311     NonJavaThread* volatile* p = &_the_list._head;
1312     for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) {
1313       if (t == this) {
1314         *p = _next;
1315         break;
1316       }
1317     }
1318   }
1319   // Wait for any in-progress iterators.  Concurrent synchronize is not
1320   // allowed, so do it while holding a dedicated lock.  Outside and distinct
1321   // from NJTList_lock in case an iteration attempts to lock it.
1322   MutexLocker ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag);
1323   _the_list._protect.synchronize();
1324   _next = NULL;                 // Safe to drop the link now.
1325 }
1326 
1327 void NonJavaThread::pre_run() {
1328   add_to_the_list();
1329 
1330   // This is slightly odd in that NamedThread is a subclass, but
1331   // in fact name() is defined in Thread
1332   assert(this->name() != NULL, "thread name was not set before it was started");
1333   this->set_native_thread_name(this->name());
1334 }
1335 
1336 void NonJavaThread::post_run() {
1337   JFR_ONLY(Jfr::on_thread_exit(this);)
1338   remove_from_the_list();
1339   // Ensure thread-local-storage is cleared before termination.
1340   Thread::clear_thread_current();
1341 }
1342 


1387   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1388   if (os::create_thread(this, os::watcher_thread)) {
1389     _watcher_thread = this;
1390 
1391     // Set the watcher thread to the highest OS priority which should not be
1392     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1393     // is created. The only normal thread using this priority is the reference
1394     // handler thread, which runs for very short intervals only.
1395     // If the VMThread's priority is not lower than the WatcherThread profiling
1396     // will be inaccurate.
1397     os::set_priority(this, MaxPriority);
1398     if (!DisableStartThread) {
1399       os::start_thread(this);
1400     }
1401   }
1402 }
1403 
1404 int WatcherThread::sleep() const {
1405   // The WatcherThread does not participate in the safepoint protocol
1406   // for the PeriodicTask_lock because it is not a JavaThread.
1407   MonitorLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1408 
1409   if (_should_terminate) {
1410     // check for termination before we do any housekeeping or wait
1411     return 0;  // we did not sleep.
1412   }
1413 
1414   // remaining will be zero if there are no tasks,
1415   // causing the WatcherThread to sleep until a task is
1416   // enrolled
1417   int remaining = PeriodicTask::time_to_wait();
1418   int time_slept = 0;
1419 
1420   // we expect this to timeout - we only ever get unparked when
1421   // we should terminate or when a new task has been enrolled
1422   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1423 
1424   jlong time_before_loop = os::javaTimeNanos();
1425 
1426   while (true) {
1427     bool timedout = ml.wait(remaining);

1428     jlong now = os::javaTimeNanos();
1429 
1430     if (remaining == 0) {
1431       // if we didn't have any tasks we could have waited for a long time
1432       // consider the time_slept zero and reset time_before_loop
1433       time_slept = 0;
1434       time_before_loop = now;
1435     } else {
1436       // need to recalculate since we might have new tasks in _tasks
1437       time_slept = (int) ((now - time_before_loop) / 1000000);
1438     }
1439 
1440     // Change to task list or spurious wakeup of some kind
1441     if (timedout || _should_terminate) {
1442       break;
1443     }
1444 
1445     remaining = PeriodicTask::time_to_wait();
1446     if (remaining == 0) {
1447       // Last task was just disenrolled so loop around and wait until


1495           err.print_raw_cr("# [ timer expired, abort... ]");
1496           // skip atexit/vm_exit/vm_abort hooks
1497           os::die();
1498         }
1499 
1500         // Wait a second, then recheck for timeout.
1501         os::naked_short_sleep(999);
1502       }
1503     }
1504 
1505     if (_should_terminate) {
1506       // check for termination before posting the next tick
1507       break;
1508     }
1509 
1510     PeriodicTask::real_time_tick(time_waited);
1511   }
1512 
1513   // Signal that it is terminated
1514   {
1515     MutexLocker mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1516     _watcher_thread = NULL;
1517     Terminator_lock->notify_all();
1518   }
1519 }
1520 
1521 void WatcherThread::start() {
1522   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1523 
1524   if (watcher_thread() == NULL && _startable) {
1525     _should_terminate = false;
1526     // Create the single instance of WatcherThread
1527     new WatcherThread();
1528   }
1529 }
1530 
1531 void WatcherThread::make_startable() {
1532   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1533   _startable = true;
1534 }
1535 
1536 void WatcherThread::stop() {
1537   {
1538     // Follow normal safepoint aware lock enter protocol since the
1539     // WatcherThread is stopped by another JavaThread.
1540     MutexLocker ml(PeriodicTask_lock);
1541     _should_terminate = true;
1542 
1543     WatcherThread* watcher = watcher_thread();
1544     if (watcher != NULL) {
1545       // unpark the WatcherThread so it can see that it should terminate
1546       watcher->unpark();
1547     }
1548   }
1549 
1550   MonitorLocker mu(Terminator_lock);
1551 
1552   while (watcher_thread() != NULL) {
1553     // This wait should make safepoint checks, wait without a timeout,
1554     // and wait as a suspend-equivalent condition.
1555     mu.wait(0, Mutex::_as_suspend_equivalent_flag);

1556   }
1557 }
1558 
1559 void WatcherThread::unpark() {
1560   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1561   PeriodicTask_lock->notify();
1562 }
1563 
1564 void WatcherThread::print_on(outputStream* st) const {
1565   st->print("\"%s\" ", name());
1566   Thread::print_on(st);
1567   st->cr();
1568 }
1569 
1570 // ======= JavaThread ========
1571 
1572 #if INCLUDE_JVMCI
1573 
1574 jlong* JavaThread::_jvmci_old_thread_counters;
1575 
1576 bool jvmci_counters_include(JavaThread* thread) {
1577   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1578 }
1579 
1580 void JavaThread::collect_counters(jlong* array, int length) {
1581   assert(length == JVMCICounterSize, "wrong value");
1582   for (int i = 0; i < length; i++) {
1583     array[i] = _jvmci_old_thread_counters[i];
1584   }
1585   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
1586     if (jvmci_counters_include(tp)) {
1587       for (int i = 0; i < length; i++) {
1588         array[i] += tp->_jvmci_counters[i];


1589       }
1590     }
1591   }
1592 }
1593 
1594 #endif // INCLUDE_JVMCI
1595 
1596 // A JavaThread is a normal Java thread
1597 
1598 void JavaThread::initialize() {
1599   // Initialize fields
1600 
1601   set_saved_exception_pc(NULL);
1602   set_threadObj(NULL);
1603   _anchor.clear();
1604   set_entry_point(NULL);
1605   set_jni_functions(jni_functions());
1606   set_callee_target(NULL);
1607   set_vm_result(NULL);
1608   set_vm_result_2(NULL);
1609   set_vframe_array_head(NULL);
1610   set_vframe_array_last(NULL);
1611   set_deferred_locals(NULL);
1612   set_deopt_mark(NULL);
1613   set_deopt_compiled_method(NULL);
1614   clear_must_deopt_id();
1615   set_monitor_chunks(NULL);

1616   _on_thread_list = false;
1617   set_thread_state(_thread_new);
1618   _terminated = _not_terminated;
1619   _array_for_gc = NULL;
1620   _suspend_equivalent = false;
1621   _in_deopt_handler = 0;
1622   _doing_unsafe_access = false;
1623   _stack_guard_state = stack_guard_unused;
1624 #if INCLUDE_JVMCI
1625   _pending_monitorenter = false;
1626   _pending_deoptimization = -1;
1627   _pending_failed_speculation = 0;
1628   _pending_transfer_to_interpreter = false;

1629   _in_retryable_allocation = false;
1630   _jvmci._alternate_call_target = NULL;
1631   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1632   if (JVMCICounterSize > 0) {
1633     _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1634     memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1635   } else {
1636     _jvmci_counters = NULL;
1637   }
1638 #endif // INCLUDE_JVMCI
1639   _reserved_stack_activation = NULL;  // stack base not known yet
1640   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1641   _exception_pc  = 0;
1642   _exception_handler_pc = 0;
1643   _is_method_handle_return = 0;
1644   _jvmti_thread_state= NULL;
1645   _should_post_on_exceptions_flag = JNI_FALSE;
1646   _interp_only_mode    = 0;
1647   _special_runtime_exit_condition = _no_async_condition;
1648   _pending_async_exception = NULL;


1821 // The main routine called by a new Java thread. This isn't overridden
1822 // by subclasses, instead different subclasses define a different "entry_point"
1823 // which defines the actual logic for that kind of thread.
1824 void JavaThread::run() {
1825   // initialize thread-local alloc buffer related fields
1826   this->initialize_tlab();
1827 
1828   // Used to test validity of stack trace backs.
1829   // This can't be moved into pre_run() else we invalidate
1830   // the requirement that thread_main_inner is lower on
1831   // the stack. Consequently all the initialization logic
1832   // stays here in run() rather than pre_run().
1833   this->record_base_of_stack_pointer();
1834 
1835   this->create_stack_guard_pages();
1836 
1837   this->cache_global_variables();
1838 
1839   // Thread is now sufficiently initialized to be handled by the safepoint code as being
1840   // in the VM. Change thread state from _thread_new to _thread_in_vm
1841   ThreadStateTransition::transition(this, _thread_new, _thread_in_vm);
1842   // Before a thread is on the threads list it is always safe, so after leaving the
1843   // _thread_new we should emit a instruction barrier. The distance to modified code
1844   // from here is probably far enough, but this is consistent and safe.
1845   OrderAccess::cross_modify_fence();
1846 
1847   assert(JavaThread::current() == this, "sanity check");
1848   assert(!Thread::current()->owns_locks(), "sanity check");
1849 
1850   DTRACE_THREAD_PROBE(start, this);
1851 
1852   // This operation might block. We call that after all safepoint checks for a new thread has
1853   // been completed.
1854   this->set_active_handles(JNIHandleBlock::allocate_block());
1855 
1856   if (JvmtiExport::should_post_thread_life()) {
1857     JvmtiExport::post_thread_start(this);
1858 
1859   }
1860 
1861   // We call another function to do the rest so we are sure that the stack addresses used


1974         JavaValue result(T_VOID);
1975         Klass* thread_klass = SystemDictionary::Thread_klass();
1976         JavaCalls::call_virtual(&result,
1977                                 threadObj, thread_klass,
1978                                 vmSymbols::exit_method_name(),
1979                                 vmSymbols::void_method_signature(),
1980                                 THREAD);
1981         CLEAR_PENDING_EXCEPTION;
1982       }
1983     }
1984     // notify JVMTI
1985     if (JvmtiExport::should_post_thread_life()) {
1986       JvmtiExport::post_thread_end(this);
1987     }
1988 
1989     // We have notified the agents that we are exiting, before we go on,
1990     // we must check for a pending external suspend request and honor it
1991     // in order to not surprise the thread that made the suspend request.
1992     while (true) {
1993       {
1994         MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1995         if (!is_external_suspend()) {
1996           set_terminated(_thread_exiting);
1997           ThreadService::current_thread_exiting(this, is_daemon(threadObj()));
1998           break;
1999         }
2000         // Implied else:
2001         // Things get a little tricky here. We have a pending external
2002         // suspend request, but we are holding the SR_lock so we
2003         // can't just self-suspend. So we temporarily drop the lock
2004         // and then self-suspend.
2005       }
2006 
2007       ThreadBlockInVM tbivm(this);
2008       java_suspend_self();
2009 
2010       // We're done with this suspend request, but we have to loop around
2011       // and check again. Eventually we will get SR_lock without a pending
2012       // external suspend request and will be able to mark ourselves as
2013       // exiting.
2014     }


2343 }
2344 
2345 // External suspension mechanism.
2346 //
2347 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2348 // to any VM_locks and it is at a transition
2349 // Self-suspension will happen on the transition out of the vm.
2350 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2351 //
2352 // Guarantees on return:
2353 //   + Target thread will not execute any new bytecode (that's why we need to
2354 //     force a safepoint)
2355 //   + Target thread will not enter any new monitors
2356 //
2357 void JavaThread::java_suspend() {
2358   ThreadsListHandle tlh;
2359   if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2360     return;
2361   }
2362 
2363   { MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2364     if (!is_external_suspend()) {
2365       // a racing resume has cancelled us; bail out now
2366       return;
2367     }
2368 
2369     // suspend is done
2370     uint32_t debug_bits = 0;
2371     // Warning: is_ext_suspend_completed() may temporarily drop the
2372     // SR_lock to allow the thread to reach a stable thread state if
2373     // it is currently in a transient thread state.
2374     if (is_ext_suspend_completed(false /* !called_by_wait */,
2375                                  SuspendRetryDelay, &debug_bits)) {
2376       return;
2377     }
2378   }
2379 
2380   if (Thread::current() == this) {
2381     // Safely self-suspend.
2382     // If we don't do this explicitly it will implicitly happen
2383     // before we transition back to Java, and on some other thread-state


2402 //
2403 // Note: DO NOT call java_suspend_self() when you just want to block current
2404 //       thread. java_suspend_self() is the second stage of cooperative
2405 //       suspension for external suspend requests and should only be used
2406 //       to complete an external suspend request.
2407 //
2408 int JavaThread::java_suspend_self() {
2409   assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()");
2410   int ret = 0;
2411 
2412   // we are in the process of exiting so don't suspend
2413   if (is_exiting()) {
2414     clear_external_suspend();
2415     return ret;
2416   }
2417 
2418   assert(_anchor.walkable() ||
2419          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
2420          "must have walkable stack");
2421 
2422   MonitorLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2423 
2424   assert(!this->is_ext_suspended(),
2425          "a thread trying to self-suspend should not already be suspended");
2426 
2427   if (this->is_suspend_equivalent()) {
2428     // If we are self-suspending as a result of the lifting of a
2429     // suspend equivalent condition, then the suspend_equivalent
2430     // flag is not cleared until we set the ext_suspended flag so
2431     // that wait_for_ext_suspend_completion() returns consistent
2432     // results.
2433     this->clear_suspend_equivalent();
2434   }
2435 
2436   // A racing resume may have cancelled us before we grabbed SR_lock
2437   // above. Or another external suspend request could be waiting for us
2438   // by the time we return from SR_lock()->wait(). The thread
2439   // that requested the suspension may already be trying to walk our
2440   // stack and if we return now, we can change the stack out from under
2441   // it. This would be a "bad thing (TM)" and cause the stack walker
2442   // to crash. We stay self-suspended until there are no more pending
2443   // external suspend requests.
2444   while (is_external_suspend()) {
2445     ret++;
2446     this->set_ext_suspended();
2447 
2448     // _ext_suspended flag is cleared by java_resume()
2449     while (is_ext_suspended()) {
2450       ml.wait();
2451     }
2452   }
2453   return ret;
2454 }
2455 
2456 // Helper routine to set up the correct thread state before calling java_suspend_self.
2457 // This is called when regular thread-state transition helpers can't be used because
2458 // we can be in various states, in particular _thread_in_native_trans.
2459 // Because this thread is external suspended the safepoint code will count it as at
2460 // a safepoint, regardless of what its actual current thread-state is. But
2461 // is_ext_suspend_completed() may be waiting to see a thread transition from
2462 // _thread_in_native_trans to _thread_blocked. So we set the thread state directly
2463 // to _thread_blocked. The problem with setting thread state directly is that a
2464 // safepoint could happen just after java_suspend_self() returns after being resumed,
2465 // and the VM thread will see the _thread_blocked state. So we must check for a safepoint
2466 // after restoring the state to make sure we won't leave while a safepoint is in progress.
2467 // However, not all initial-states are allowed when performing a safepoint check, as we
2468 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
2469 // only _thread_in_native is possible when executing this code (based on our two callers).
2470 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
2471 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
2472 // and so we don't need the explicit safepoint check.
2473 
2474 void JavaThread::java_suspend_self_with_safepoint_check() {
2475   assert(this == Thread::current(), "invariant");
2476   JavaThreadState state = thread_state();
2477   set_thread_state(_thread_blocked);
2478   java_suspend_self();
2479   set_thread_state_fence(state);
2480   // Since we are not using a regular thread-state transition helper here,
2481   // we must manually emit the instruction barrier after leaving a safe state.
2482   OrderAccess::cross_modify_fence();

2483   if (state != _thread_in_native) {
2484     SafepointMechanism::block_if_requested(this);
2485   }
2486 }
2487 
2488 #ifdef ASSERT
2489 // Verify the JavaThread has not yet been published in the Threads::list, and
2490 // hence doesn't need protection from concurrent access at this stage.
2491 void JavaThread::verify_not_published() {
2492   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2493   // since an unpublished JavaThread doesn't participate in the
2494   // Thread-SMR protocol for keeping a ThreadsList alive.
2495   assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2496 }
2497 #endif
2498 
2499 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2500 // progress or when _suspend_flags is non-zero.
2501 // Current thread needs to self-suspend if there is a suspend request and/or
2502 // block if a safepoint is in progress.


2504 // Note only the ThreadInVMfromNative transition can call this function
2505 // directly and when thread state is _thread_in_native_trans
2506 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2507   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2508 
2509   JavaThread *curJT = JavaThread::current();
2510   bool do_self_suspend = thread->is_external_suspend();
2511 
2512   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2513 
2514   // If JNIEnv proxies are allowed, don't self-suspend if the target
2515   // thread is not the current thread. In older versions of jdbx, jdbx
2516   // threads could call into the VM with another thread's JNIEnv so we
2517   // can be here operating on behalf of a suspended thread (4432884).
2518   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
2519     thread->java_suspend_self_with_safepoint_check();
2520   } else {
2521     SafepointMechanism::block_if_requested(curJT);
2522   }
2523 















2524   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2525 }
2526 
2527 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2528 // progress or when _suspend_flags is non-zero.
2529 // Current thread needs to self-suspend if there is a suspend request and/or
2530 // block if a safepoint is in progress.
2531 // Also check for pending async exception (not including unsafe access error).
2532 // Note only the native==>VM/Java barriers can call this function and when
2533 // thread state is _thread_in_native_trans.
2534 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2535   check_safepoint_and_suspend_for_native_trans(thread);
2536 
2537   if (thread->has_async_exception()) {
2538     // We are in _thread_in_native_trans state, don't handle unsafe
2539     // access error since that may block.
2540     thread->check_and_handle_async_exceptions(false);
2541   }
2542 }
2543 


2557   if (thread->do_critical_native_unlock()) {
2558     ThreadInVMfromJavaNoAsyncException tiv(thread);
2559     GCLocker::unlock_critical(thread);
2560     thread->clear_critical_native_unlock();
2561   }
2562 }
2563 
2564 // We need to guarantee the Threads_lock here, since resumes are not
2565 // allowed during safepoint synchronization
2566 // Can only resume from an external suspension
2567 void JavaThread::java_resume() {
2568   assert_locked_or_safepoint(Threads_lock);
2569 
2570   // Sanity check: thread is gone, has started exiting or the thread
2571   // was not externally suspended.
2572   ThreadsListHandle tlh;
2573   if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) {
2574     return;
2575   }
2576 
2577   MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2578 
2579   clear_external_suspend();
2580 
2581   if (is_ext_suspended()) {
2582     clear_ext_suspended();
2583     SR_lock()->notify_all();
2584   }
2585 }
2586 
2587 size_t JavaThread::_stack_red_zone_size = 0;
2588 size_t JavaThread::_stack_yellow_zone_size = 0;
2589 size_t JavaThread::_stack_reserved_zone_size = 0;
2590 size_t JavaThread::_stack_shadow_zone_size = 0;
2591 
2592 void JavaThread::create_stack_guard_pages() {
2593   if (!os::uses_stack_guard_pages() ||
2594       _stack_guard_state != stack_guard_unused ||
2595       (DisablePrimordialThreadGuardPages && os::is_primordial_thread())) {
2596       log_info(os, thread)("Stack guard page creation for thread "
2597                            UINTX_FORMAT " disabled", os::current_thread_id());


2815   if (DebugDeoptimization && deopt) {
2816     tty->print_cr("[AFTER Deoptimization]");
2817     trace_frames();
2818   }
2819 }
2820 
2821 
2822 // Make zombies
2823 void JavaThread::make_zombies() {
2824   for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
2825     if (fst.current()->can_be_deoptimized()) {
2826       // it is a Java nmethod
2827       nmethod* nm = CodeCache::find_nmethod(fst.current()->pc());
2828       nm->make_not_entrant();
2829     }
2830   }
2831 }
2832 #endif // PRODUCT
2833 
2834 
2835 void JavaThread::deoptimize_marked_methods(bool in_handshake) {
2836   if (!has_last_Java_frame()) return;
2837   // BiasedLocking needs an updated RegisterMap for the revoke monitors pass
2838   StackFrameStream fst(this, UseBiasedLocking);
2839   for (; !fst.is_done(); fst.next()) {
2840     if (fst.current()->should_be_deoptimized()) {
2841       Deoptimization::deoptimize(this, *fst.current(), fst.register_map(), in_handshake);
2842     }
2843   }
2844 }
2845 

2846 // If the caller is a NamedThread, then remember, in the current scope,
2847 // the given JavaThread in its _processed_thread field.
2848 class RememberProcessedThread: public StackObj {
2849   NamedThread* _cur_thr;
2850  public:
2851   RememberProcessedThread(JavaThread* jthr) {
2852     Thread* thread = Thread::current();
2853     if (thread->is_Named_thread()) {
2854       _cur_thr = (NamedThread *)thread;
2855       _cur_thr->set_processed_thread(jthr);
2856     } else {
2857       _cur_thr = NULL;
2858     }
2859   }
2860 
2861   ~RememberProcessedThread() {
2862     if (_cur_thr) {
2863       _cur_thr->set_processed_thread(NULL);
2864     }
2865   }


3009   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
3010   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
3011   if (thread_oop != NULL) {
3012     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
3013   }
3014 #ifndef PRODUCT
3015   _safepoint_state->print_on(st);
3016 #endif // PRODUCT
3017   if (is_Compiler_thread()) {
3018     CompileTask *task = ((CompilerThread*)this)->task();
3019     if (task != NULL) {
3020       st->print("   Compiling: ");
3021       task->print(st, NULL, true, false);
3022     } else {
3023       st->print("   No compile task");
3024     }
3025     st->cr();
3026   }
3027 }
3028 
3029 void JavaThread::print() const { print_on(tty); }
3030 
3031 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
3032   st->print("%s", get_thread_name_string(buf, buflen));
3033 }
3034 
3035 // Called by fatal error handler. The difference between this and
3036 // JavaThread::print() is that we can't grab lock or allocate memory.
3037 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
3038   st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
3039   oop thread_obj = threadObj();
3040   if (thread_obj != NULL) {
3041     if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
3042   }
3043   st->print(" [");
3044   st->print("%s", _get_thread_state_name(_thread_state));
3045   if (osthread()) {
3046     st->print(", id=%d", osthread()->thread_id());
3047   }
3048   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
3049             p2i(stack_end()), p2i(stack_base()));
3050   st->print("]");


3175 
3176   if (prio == NoPriority) {
3177     prio = java_lang_Thread::priority(thread_oop());
3178     assert(prio != NoPriority, "A valid priority should be present");
3179   }
3180 
3181   // Push the Java priority down to the native thread; needs Threads_lock
3182   Thread::set_priority(this, prio);
3183 
3184   // Add the new thread to the Threads list and set it in motion.
3185   // We must have threads lock in order to call Threads::add.
3186   // It is crucial that we do not block before the thread is
3187   // added to the Threads list for if a GC happens, then the java_thread oop
3188   // will not be visited by GC.
3189   Threads::add(this);
3190 }
3191 
3192 oop JavaThread::current_park_blocker() {
3193   // Support for JSR-166 locks
3194   oop thread_oop = threadObj();
3195   if (thread_oop != NULL) {

3196     return java_lang_Thread::park_blocker(thread_oop);
3197   }
3198   return NULL;
3199 }
3200 
3201 
3202 void JavaThread::print_stack_on(outputStream* st) {
3203   if (!has_last_Java_frame()) return;
3204   ResourceMark rm;
3205   HandleMark   hm;
3206 
3207   RegisterMap reg_map(this);
3208   vframe* start_vf = last_java_vframe(&reg_map);
3209   int count = 0;
3210   for (vframe* f = start_vf; f != NULL; f = f->sender()) {
3211     if (f->is_java_frame()) {
3212       javaVFrame* jvf = javaVFrame::cast(f);
3213       java_lang_Throwable::print_stack_element(st, jvf->method(), jvf->bci());
3214 
3215       // Print out lock information


3438     // process it here to make sure it isn't unloaded in the middle of
3439     // a scan.
3440     cf->do_code_blob(_scanned_compiled_method);
3441   }
3442 }
3443 
3444 
3445 // ======= Threads ========
3446 
3447 // The Threads class links together all active threads, and provides
3448 // operations over all threads. It is protected by the Threads_lock,
3449 // which is also used in other global contexts like safepointing.
3450 // ThreadsListHandles are used to safely perform operations on one
3451 // or more threads without the risk of the thread exiting during the
3452 // operation.
3453 //
3454 // Note: The Threads_lock is currently more widely used than we
3455 // would like. We are actively migrating Threads_lock uses to other
3456 // mechanisms in order to reduce Threads_lock contention.
3457 

3458 int         Threads::_number_of_threads = 0;
3459 int         Threads::_number_of_non_daemon_threads = 0;
3460 int         Threads::_return_code = 0;
3461 uintx       Threads::_thread_claim_token = 1; // Never zero.
3462 size_t      JavaThread::_stack_size_at_create = 0;
3463 
3464 #ifdef ASSERT
3465 bool        Threads::_vm_complete = false;
3466 #endif
3467 
3468 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3469   Prefetch::read((void*)addr, prefetch_interval);
3470   return *addr;
3471 }
3472 
3473 // Possibly the ugliest for loop the world has seen. C++ does not allow
3474 // multiple types in the declaration section of the for loop. In this case
3475 // we are only dealing with pointers and hence can cast them. It looks ugly
3476 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3477 #define DO_JAVA_THREADS(LIST, X)                                                                                          \


3595 
3596   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3597     create_vm_init_libraries();
3598   }
3599 
3600   initialize_class(vmSymbols::java_lang_String(), CHECK);
3601 
3602   // Inject CompactStrings value after the static initializers for String ran.
3603   java_lang_String::set_compact_strings(CompactStrings);
3604 
3605   // Initialize java_lang.System (needed before creating the thread)
3606   initialize_class(vmSymbols::java_lang_System(), CHECK);
3607   // The VM creates & returns objects of this class. Make sure it's initialized.
3608   initialize_class(vmSymbols::java_lang_Class(), CHECK);
3609   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3610   Handle thread_group = create_initial_thread_group(CHECK);
3611   Universe::set_main_thread_group(thread_group());
3612   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3613   oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
3614   main_thread->set_threadObj(thread_object);
3615 
3616   // Set thread status to running since main thread has
3617   // been started and running.
3618   java_lang_Thread::set_thread_status(thread_object,
3619                                       java_lang_Thread::RUNNABLE);
3620 
3621   // The VM creates objects of this class.
3622   initialize_class(vmSymbols::java_lang_Module(), CHECK);
3623 
3624 #ifdef ASSERT
3625   InstanceKlass *k = SystemDictionary::UnsafeConstants_klass();
3626   assert(k->is_not_initialized(), "UnsafeConstants should not already be initialized");
3627 #endif
3628 
3629   // initialize the hardware-specific constants needed by Unsafe
3630   initialize_class(vmSymbols::jdk_internal_misc_UnsafeConstants(), CHECK);
3631   jdk_internal_misc_UnsafeConstants::set_unsafe_constants();
3632 
3633   // The VM preresolves methods to these classes. Make sure that they get initialized
3634   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
3635   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
3636 
3637   // Phase 1 of the system initialization in the library, java.lang.System class initialization
3638   call_initPhase1(CHECK);
3639 
3640   // get the Java runtime name after java.lang.System is initialized
3641   JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
3642   JDK_Version::set_runtime_version(get_java_runtime_version(THREAD));
3643 
3644   // an instance of OutOfMemory exception has been allocated earlier
3645   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
3646   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
3647   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
3648   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
3649   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
3650   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
3651   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
3652   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);


3744   SafepointMechanism::initialize();
3745 
3746   jint adjust_after_os_result = Arguments::adjust_after_os();
3747   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3748 
3749   // Initialize output stream logging
3750   ostream_init_log();
3751 
3752   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3753   // Must be before create_vm_init_agents()
3754   if (Arguments::init_libraries_at_startup()) {
3755     convert_vm_init_libraries_to_agents();
3756   }
3757 
3758   // Launch -agentlib/-agentpath and converted -Xrun agents
3759   if (Arguments::init_agents_at_startup()) {
3760     create_vm_init_agents();
3761   }
3762 
3763   // Initialize Threads state

3764   _number_of_threads = 0;
3765   _number_of_non_daemon_threads = 0;
3766 
3767   // Initialize global data structures and create system classes in heap
3768   vm_init_globals();
3769 
3770 #if INCLUDE_JVMCI
3771   if (JVMCICounterSize > 0) {
3772     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3773     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3774   } else {
3775     JavaThread::_jvmci_old_thread_counters = NULL;
3776   }
3777 #endif // INCLUDE_JVMCI
3778 
3779   // Attach the main thread to this os thread
3780   JavaThread* main_thread = new JavaThread();
3781   main_thread->set_thread_state(_thread_in_vm);
3782   main_thread->initialize_thread_current();
3783   // must do this before set_active_handles


3820   }
3821 
3822   // Any JVMTI raw monitors entered in onload will transition into
3823   // real raw monitor. VM is setup enough here for raw monitor enter.
3824   JvmtiExport::transition_pending_onload_raw_monitors();
3825 
3826   // Create the VMThread
3827   { TraceTime timer("Start VMThread", TRACETIME_LOG(Info, startuptime));
3828 
3829   VMThread::create();
3830     Thread* vmthread = VMThread::vm_thread();
3831 
3832     if (!os::create_thread(vmthread, os::vm_thread)) {
3833       vm_exit_during_initialization("Cannot create VM thread. "
3834                                     "Out of system resources.");
3835     }
3836 
3837     // Wait for the VM thread to become ready, and VMThread::run to initialize
3838     // Monitors can have spurious returns, must always check another state flag
3839     {
3840       MonitorLocker ml(Notify_lock);
3841       os::start_thread(vmthread);
3842       while (vmthread->active_handles() == NULL) {
3843         ml.wait();
3844       }
3845     }
3846   }
3847 
3848   assert(Universe::is_fully_initialized(), "not initialized");
3849   if (VerifyDuringStartup) {
3850     // Make sure we're starting with a clean slate.
3851     VM_Verify verify_op;
3852     VMThread::execute(&verify_op);
3853   }
3854 
3855   // We need this to update the java.vm.info property in case any flags used
3856   // to initially define it have been changed. This is needed for both CDS and
3857   // AOT, since UseSharedSpaces and UseAOT may be changed after java.vm.info
3858   // is initially computed. See Abstract_VM_Version::vm_info_string().
3859   // This update must happen before we initialize the java classes, but
3860   // after any initialization logic that might modify the flags.
3861   Arguments::update_vm_info_property(VM_Version::vm_info_string());
3862 
3863   Thread* THREAD = Thread::current();


3901     }
3902   }
3903 
3904   // Launch -Xrun agents
3905   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3906   // back-end can launch with -Xdebug -Xrunjdwp.
3907   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3908     create_vm_init_libraries();
3909   }
3910 
3911   if (CleanChunkPoolAsync) {
3912     Chunk::start_chunk_pool_cleaner_task();
3913   }
3914 
3915   // initialize compiler(s)
3916 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
3917 #if INCLUDE_JVMCI
3918   bool force_JVMCI_intialization = false;
3919   if (EnableJVMCI) {
3920     // Initialize JVMCI eagerly when it is explicitly requested.
3921     // Or when JVMCILibDumpJNIConfig or JVMCIPrintProperties is enabled.
3922     force_JVMCI_intialization = EagerJVMCI || JVMCIPrintProperties || JVMCILibDumpJNIConfig;


3923 
3924     if (!force_JVMCI_intialization) {
3925       // 8145270: Force initialization of JVMCI runtime otherwise requests for blocking
3926       // compilations via JVMCI will not actually block until JVMCI is initialized.
3927       force_JVMCI_intialization = UseJVMCICompiler && (!UseInterpreter || !BackgroundCompilation);
3928     }
3929   }
3930 #endif
3931   CompileBroker::compilation_init_phase1(CHECK_JNI_ERR);
3932   // Postpone completion of compiler initialization to after JVMCI
3933   // is initialized to avoid timeouts of blocking compilations.
3934   if (JVMCI_ONLY(!force_JVMCI_intialization) NOT_JVMCI(true)) {
3935     CompileBroker::compilation_init_phase2();
3936   }
3937 #endif
3938 
3939   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3940   // It is done after compilers are initialized, because otherwise compilations of
3941   // signature polymorphic MH intrinsics can be missed
3942   // (see SystemDictionary::find_method_handle_intrinsic).
3943   initialize_jsr292_core_classes(CHECK_JNI_ERR);
3944 
3945   // This will initialize the module system.  Only java.base classes can be
3946   // loaded until phase 2 completes
3947   call_initPhase2(CHECK_JNI_ERR);
3948 
3949   // Always call even when there are not JVMTI environments yet, since environments
3950   // may be attached late and JVMTI must track phases of VM execution
3951   JvmtiExport::enter_start_phase();
3952 
3953   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
3954   JvmtiExport::post_vm_start();
3955 
3956   // Final system initialization including security manager and system class loader
3957   call_initPhase3(CHECK_JNI_ERR);
3958 
3959   // cache the system and platform class loaders
3960   SystemDictionary::compute_java_loaders(CHECK_JNI_ERR);
3961 
3962 #if INCLUDE_CDS
3963   // capture the module path info from the ModuleEntryTable
3964   ClassLoader::initialize_module_path(THREAD);


3965 #endif
3966 
3967 #if INCLUDE_JVMCI
3968   if (force_JVMCI_intialization) {
3969     JVMCI::initialize_compiler(CHECK_JNI_ERR);
3970     CompileBroker::compilation_init_phase2();
3971   }
3972 #endif
3973 
3974   // Always call even when there are not JVMTI environments yet, since environments
3975   // may be attached late and JVMTI must track phases of VM execution
3976   JvmtiExport::enter_live_phase();
3977 
3978   // Make perfmemory accessible
3979   PerfMemory::set_accessible(true);
3980 
3981   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3982   JvmtiExport::post_vm_initialized();
3983 
3984   JFR_ONLY(Jfr::on_vm_start();)
3985 
3986 #if INCLUDE_MANAGEMENT
3987   Management::initialize(THREAD);
3988 
3989   if (HAS_PENDING_EXCEPTION) {
3990     // management agent fails to start possibly due to
3991     // configuration problem and is responsible for printing
3992     // stack trace if appropriate. Simply exit VM.
3993     vm_exit(1);
3994   }
3995 #endif // INCLUDE_MANAGEMENT
3996 
3997   if (MemProfiling)                   MemProfiler::engage();
3998   StatSampler::engage();
3999   if (CheckJNICalls)                  JniPeriodicChecker::engage();
4000 
4001   BiasedLocking::init();
4002 
4003 #if INCLUDE_RTM_OPT
4004   RTMLockingCounters::init();
4005 #endif
4006 
4007   call_postVMInitHook(THREAD);
4008   // The Java side of PostVMInitHook.run must deal with all
4009   // exceptions and provide means of diagnosis.
4010   if (HAS_PENDING_EXCEPTION) {
4011     CLEAR_PENDING_EXCEPTION;


4012   }
4013 
4014   {
4015     MutexLocker ml(PeriodicTask_lock);
4016     // Make sure the WatcherThread can be started by WatcherThread::start()
4017     // or by dynamic enrollment.
4018     WatcherThread::make_startable();
4019     // Start up the WatcherThread if there are any periodic tasks
4020     // NOTE:  All PeriodicTasks should be registered by now. If they
4021     //   aren't, late joiners might appear to start slowly (we might
4022     //   take a while to process their first tick).
4023     if (PeriodicTask::num_tasks() > 0) {
4024       WatcherThread::start();
4025     }
4026   }
4027 
4028   create_vm_timer.end();
4029 #ifdef ASSERT
4030   _vm_complete = true;
4031 #endif


4146         Arguments::convert_library_to_agent(agent);
4147       } else {
4148         vm_exit_during_initialization("Could not find JVM_OnLoad or Agent_OnLoad function in the library", agent->name());
4149       }
4150     }
4151   }
4152 }
4153 
4154 // Create agents for -agentlib:  -agentpath:  and converted -Xrun
4155 // Invokes Agent_OnLoad
4156 // Called very early -- before JavaThreads exist
4157 void Threads::create_vm_init_agents() {
4158   extern struct JavaVM_ main_vm;
4159   AgentLibrary* agent;
4160 
4161   JvmtiExport::enter_onload_phase();
4162 
4163   for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
4164     // CDS dumping does not support native JVMTI agent.
4165     // CDS dumping supports Java agent if the AllowArchivingWithJavaAgent diagnostic option is specified.
4166     if (DumpSharedSpaces || DynamicDumpSharedSpaces) {
4167       if(!agent->is_instrument_lib()) {
4168         vm_exit_during_cds_dumping("CDS dumping does not support native JVMTI agent, name", agent->name());
4169       } else if (!AllowArchivingWithJavaAgent) {
4170         vm_exit_during_cds_dumping(
4171           "Must enable AllowArchivingWithJavaAgent in order to run Java agent during CDS dumping");
4172       }
4173     }
4174 
4175     OnLoadEntry_t  on_load_entry = lookup_agent_on_load(agent);
4176 
4177     if (on_load_entry != NULL) {
4178       // Invoke the Agent_OnLoad function
4179       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4180       if (err != JNI_OK) {
4181         vm_exit_during_initialization("agent library failed to init", agent->name());
4182       }
4183     } else {
4184       vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name());
4185     }
4186   }


4247 
4248   // We could get here with a pending exception, if so clear it now.
4249   if (this->has_pending_exception()) {
4250     this->clear_pending_exception();
4251   }
4252 
4253   EXCEPTION_MARK;
4254   Klass* shutdown_klass =
4255     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4256                                       THREAD);
4257   if (shutdown_klass != NULL) {
4258     // SystemDictionary::resolve_or_null will return null if there was
4259     // an exception.  If we cannot load the Shutdown class, just don't
4260     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4261     // won't be run.  Note that if a shutdown hook was registered,
4262     // the Shutdown class would have already been loaded
4263     // (Runtime.addShutdownHook will load it).
4264     JavaValue result(T_VOID);
4265     JavaCalls::call_static(&result,
4266                            shutdown_klass,
4267                            vmSymbols::shutdown_name(),
4268                            vmSymbols::void_method_signature(),
4269                            THREAD);
4270   }
4271   CLEAR_PENDING_EXCEPTION;
4272 }
4273 
4274 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4275 // the program falls off the end of main(). Another VM exit path is through
4276 // vm_exit() when the program calls System.exit() to return a value or when
4277 // there is a serious error in VM. The two shutdown paths are not exactly
4278 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4279 // and VM_Exit op at VM level.
4280 //
4281 // Shutdown sequence:
4282 //   + Shutdown native memory tracking if it is on
4283 //   + Wait until we are the last non-daemon thread to execute
4284 //     <-- every thing is still working at this moment -->
4285 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
4286 //        shutdown hooks
4287 //   + Call before_exit(), prepare for VM exit


4295 //      > remove this thread from Threads list
4296 //     <-- no more Java code from this thread after this point -->
4297 //   + Stop VM thread, it will bring the remaining VM to a safepoint and stop
4298 //     the compiler threads at safepoint
4299 //     <-- do not use anything that could get blocked by Safepoint -->
4300 //   + Disable tracing at JNI/JVM barriers
4301 //   + Set _vm_exited flag for threads that are still running native code
4302 //   + Call exit_globals()
4303 //      > deletes tty
4304 //      > deletes PerfMemory resources
4305 //   + Delete this thread
4306 //   + Return to caller
4307 
4308 bool Threads::destroy_vm() {
4309   JavaThread* thread = JavaThread::current();
4310 
4311 #ifdef ASSERT
4312   _vm_complete = false;
4313 #endif
4314   // Wait until we are the last non-daemon thread to execute
4315   { MonitorLocker nu(Threads_lock);
4316     while (Threads::number_of_non_daemon_threads() > 1)
4317       // This wait should make safepoint checks, wait without a timeout,
4318       // and wait as a suspend-equivalent condition.
4319       nu.wait(0, Mutex::_as_suspend_equivalent_flag);

4320   }
4321 
4322   EventShutdown e;
4323   if (e.should_commit()) {
4324     e.set_reason("No remaining non-daemon Java threads");
4325     e.commit();
4326   }
4327 
4328   // Hang forever on exit if we are reporting an error.
4329   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
4330     os::infinite_sleep();
4331   }
4332   os::wait_for_keypress_at_exit();
4333 
4334   // run Java level shutdown hooks
4335   thread->invoke_shutdown_hooks();
4336 
4337   before_exit(thread);
4338 
4339   thread->exit(true);
4340 
4341   // Stop VM thread.
4342   {
4343     // 4945125 The vm thread comes to a safepoint during exit.
4344     // GC vm_operations can get caught at the safepoint, and the
4345     // heap is unparseable if they are caught. Grab the Heap_lock
4346     // to prevent this. The GC vm_operations will not be able to
4347     // queue until after the vm thread is dead. After this point,
4348     // we'll never emerge out of the safepoint before the VM exits.
4349 
4350     MutexLocker ml(Heap_lock, Mutex::_no_safepoint_check_flag);
4351 
4352     VMThread::wait_for_vm_thread_exit();
4353     assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4354     VMThread::destroy();
4355   }
4356 
4357   // Now, all Java threads are gone except daemon threads. Daemon threads
4358   // running Java code or in VM are stopped by the Safepoint. However,
4359   // daemon threads executing native code are still running.  But they
4360   // will be stopped at native=>Java/VM barriers. Note that we can't
4361   // simply kill or suspend them, as it is inherently deadlock-prone.
4362 
4363   VM_Exit::set_vm_exited();
4364 
4365   // Clean up ideal graph printers after the VMThread has started
4366   // the final safepoint which will block all the Compiler threads.
4367   // Note that this Thread has already logically exited so the
4368   // clean_up() function's use of a JavaThreadIteratorWithHandle
4369   // would be a problem except set_vm_exited() has remembered the
4370   // shutdown thread which is granted a policy exception.


4402 }
4403 
4404 
4405 jboolean Threads::is_supported_jni_version(jint version) {
4406   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4407   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4408   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4409   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4410   if (version == JNI_VERSION_9) return JNI_TRUE;
4411   if (version == JNI_VERSION_10) return JNI_TRUE;
4412   return JNI_FALSE;
4413 }
4414 
4415 
4416 void Threads::add(JavaThread* p, bool force_daemon) {
4417   // The threads lock must be owned at this point
4418   assert(Threads_lock->owned_by_self(), "must have threads lock");
4419 
4420   BarrierSet::barrier_set()->on_thread_attach(p);
4421 



4422   // Once a JavaThread is added to the Threads list, smr_delete() has
4423   // to be used to delete it. Otherwise we can just delete it directly.
4424   p->set_on_thread_list();
4425 
4426   _number_of_threads++;
4427   oop threadObj = p->threadObj();
4428   bool daemon = true;
4429   // Bootstrapping problem: threadObj can be null for initial
4430   // JavaThread (or for threads attached via JNI)
4431   if ((!force_daemon) && !is_daemon((threadObj))) {
4432     _number_of_non_daemon_threads++;
4433     daemon = false;
4434   }
4435 
4436   ThreadService::add_thread(p, daemon);
4437 
4438   // Maintain fast thread list
4439   ThreadsSMRSupport::add_thread(p);
4440 
4441   // Possible GC point.
4442   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4443 }
4444 
4445 void Threads::remove(JavaThread* p, bool is_daemon) {
4446 
4447   // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread.
4448   ObjectSynchronizer::omFlush(p);
4449 
4450   // Extra scope needed for Thread_lock, so we can check
4451   // that we do not remove thread without safepoint code notice
4452   { MonitorLocker ml(Threads_lock);
4453 
4454     assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
4455 
4456     // Maintain fast thread list
4457     ThreadsSMRSupport::remove_thread(p);
4458 














4459     _number_of_threads--;
4460     if (!is_daemon) {
4461       _number_of_non_daemon_threads--;
4462 
4463       // Only one thread left, do a notify on the Threads_lock so a thread waiting
4464       // on destroy_vm will wake up.
4465       if (number_of_non_daemon_threads() == 1) {
4466         ml.notify_all();
4467       }
4468     }
4469     ThreadService::remove_thread(p, is_daemon);
4470 
4471     // Make sure that safepoint code disregard this thread. This is needed since
4472     // the thread might mess around with locks after this point. This can cause it
4473     // to do callbacks into the safepoint code. However, the safepoint code is not aware
4474     // of this thread since it is removed from the queue.
4475     p->set_terminated_value();
4476   } // unlock Threads_lock
4477 
4478   // Since Events::log uses a lock, we grab it outside the Threads_lock
4479   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4480 }
4481 
4482 // Operations on the Threads list for GC.  These are not explicitly locked,
4483 // but the garbage collector must provide a safe context for them to run.
4484 // In particular, these things should never be called when the Threads_lock
4485 // is held by some other thread. (Note: the Safepoint abstraction also
4486 // uses the Threads_lock to guarantee this property. It also makes sure that


4559 void Threads::metadata_do(MetadataClosure* f) {
4560   ALL_JAVA_THREADS(p) {
4561     p->metadata_do(f);
4562   }
4563 }
4564 
4565 class ThreadHandlesClosure : public ThreadClosure {
4566   void (*_f)(Metadata*);
4567  public:
4568   ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
4569   virtual void do_thread(Thread* thread) {
4570     thread->metadata_handles_do(_f);
4571   }
4572 };
4573 
4574 void Threads::metadata_handles_do(void f(Metadata*)) {
4575   // Only walk the Handles in Thread.
4576   ThreadHandlesClosure handles_closure(f);
4577   threads_do(&handles_closure);
4578 }







4579 
4580 // Get count Java threads that are waiting to enter the specified monitor.
4581 GrowableArray<JavaThread*>* Threads::get_pending_threads(ThreadsList * t_list,
4582                                                          int count,
4583                                                          address monitor) {
4584   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
4585 
4586   int i = 0;
4587   DO_JAVA_THREADS(t_list, p) {
4588     if (!p->can_call_java()) continue;
4589 
4590     address pending = (address)p->current_pending_monitor();
4591     if (pending == monitor) {             // found a match
4592       if (i < count) result->append(p);   // save the first count matches
4593       i++;
4594     }
4595   }
4596 
4597   return result;
4598 }




 100 #include "runtime/vframe.inline.hpp"
 101 #include "runtime/vframeArray.hpp"
 102 #include "runtime/vframe_hp.hpp"
 103 #include "runtime/vmThread.hpp"
 104 #include "runtime/vmOperations.hpp"
 105 #include "runtime/vm_version.hpp"
 106 #include "services/attachListener.hpp"
 107 #include "services/management.hpp"
 108 #include "services/memTracker.hpp"
 109 #include "services/threadService.hpp"
 110 #include "utilities/align.hpp"
 111 #include "utilities/copy.hpp"
 112 #include "utilities/defaultStream.hpp"
 113 #include "utilities/dtrace.hpp"
 114 #include "utilities/events.hpp"
 115 #include "utilities/macros.hpp"
 116 #include "utilities/preserveException.hpp"
 117 #include "utilities/singleWriterSynchronizer.hpp"
 118 #include "utilities/vmError.hpp"
 119 #if INCLUDE_JVMCI
 120 #include "jvmci/jvmciCompiler.hpp"
 121 #include "jvmci/jvmciRuntime.hpp"
 122 #include "logging/logHandle.hpp"
 123 #endif
 124 #ifdef COMPILER1
 125 #include "c1/c1_Compiler.hpp"
 126 #endif
 127 #ifdef COMPILER2
 128 #include "opto/c2compiler.hpp"
 129 #include "opto/idealGraphPrinter.hpp"
 130 #endif
 131 #if INCLUDE_RTM_OPT
 132 #include "runtime/rtmLocking.hpp"
 133 #endif
 134 #if INCLUDE_JFR
 135 #include "jfr/jfr.hpp"
 136 #endif
 137 
 138 // Initialization after module runtime initialization
 139 void universe_post_module_init();  // must happen after call_initPhase2
 140 
 141 #ifdef DTRACE_ENABLED
 142 


 701       *bits |= 0x00004000;
 702 
 703       // Once the thread leaves thread_in_native_trans for another
 704       // thread state, we break out of this retry loop. We shouldn't
 705       // need this flag to prevent us from getting back here, but
 706       // sometimes paranoia is good.
 707       did_trans_retry = true;
 708 
 709       // We wait for the thread to transition to a more usable state.
 710       for (int i = 1; i <= SuspendRetryCount; i++) {
 711         // We used to do an "os::yield_all(i)" call here with the intention
 712         // that yielding would increase on each retry. However, the parameter
 713         // is ignored on Linux which means the yield didn't scale up. Waiting
 714         // on the SR_lock below provides a much more predictable scale up for
 715         // the delay. It also provides a simple/direct point to check for any
 716         // safepoint requests from the VMThread
 717 
 718         // temporarily drops SR_lock while doing wait with safepoint check
 719         // (if we're a JavaThread - the WatcherThread can also call this)
 720         // and increase delay with each retry
 721         SR_lock()->wait(!Thread::current()->is_Java_thread(), i * delay);




 722 
 723         // check the actual thread state instead of what we saved above
 724         if (thread_state() != _thread_in_native_trans) {
 725           // the thread has transitioned to another thread state so
 726           // try all the checks (except this one) one more time.
 727           do_trans_retry = true;
 728           break;
 729         }
 730       } // end retry loop
 731 
 732 
 733     }
 734   } while (do_trans_retry);
 735 
 736   *bits |= 0x00000010;
 737   return false;
 738 }
 739 
 740 // Wait for an external suspend request to complete (or be cancelled).
 741 // Returns true if the thread is externally suspended and false otherwise.
 742 //
 743 bool JavaThread::wait_for_ext_suspend_completion(int retries, int delay,
 744                                                  uint32_t *bits) {
 745   TraceSuspendDebugBits tsdb(this, true /* is_wait */,
 746                              false /* !called_by_wait */, bits);
 747 
 748   // local flag copies to minimize SR_lock hold time
 749   bool is_suspended;
 750   bool pending;
 751   uint32_t reset_bits;
 752 
 753   // set a marker so is_ext_suspend_completed() knows we are the caller
 754   *bits |= 0x00010000;
 755 
 756   // We use reset_bits to reinitialize the bits value at the top of
 757   // each retry loop. This allows the caller to make use of any
 758   // unused bits for their own marking purposes.
 759   reset_bits = *bits;
 760 
 761   {
 762     MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
 763     is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
 764                                             delay, bits);
 765     pending = is_external_suspend();
 766   }
 767   // must release SR_lock to allow suspension to complete
 768 
 769   if (!pending) {
 770     // A cancelled suspend request is the only false return from
 771     // is_ext_suspend_completed() that keeps us from entering the
 772     // retry loop.
 773     *bits |= 0x00020000;
 774     return false;
 775   }
 776 
 777   if (is_suspended) {
 778     *bits |= 0x00040000;
 779     return true;
 780   }
 781 
 782   for (int i = 1; i <= retries; i++) {
 783     *bits = reset_bits;  // reinit to only track last retry
 784 
 785     // We used to do an "os::yield_all(i)" call here with the intention
 786     // that yielding would increase on each retry. However, the parameter
 787     // is ignored on Linux which means the yield didn't scale up. Waiting
 788     // on the SR_lock below provides a much more predictable scale up for
 789     // the delay. It also provides a simple/direct point to check for any
 790     // safepoint requests from the VMThread
 791 
 792     {
 793       MutexLocker ml(SR_lock());


 794       // wait with safepoint check (if we're a JavaThread - the WatcherThread
 795       // can also call this)  and increase delay with each retry
 796       SR_lock()->wait(!Thread::current()->is_Java_thread(), i * delay);
 797 
 798       is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
 799                                               delay, bits);
 800 
 801       // It is possible for the external suspend request to be cancelled
 802       // (by a resume) before the actual suspend operation is completed.
 803       // Refresh our local copy to see if we still need to wait.
 804       pending = is_external_suspend();
 805     }
 806 
 807     if (!pending) {
 808       // A cancelled suspend request is the only false return from
 809       // is_ext_suspend_completed() that keeps us from staying in the
 810       // retry loop.
 811       *bits |= 0x00080000;
 812       return false;
 813     }
 814 
 815     if (is_suspended) {
 816       *bits |= 0x00100000;


 931     st->print("elapsed=%.2fs ",
 932               _statistical_info.getElapsedTime() / 1000.0
 933               );
 934     if (is_Java_thread() && (PrintExtendedThreadInfo || print_extended_info)) {
 935       size_t allocated_bytes = (size_t) const_cast<Thread*>(this)->cooked_allocated_bytes();
 936       st->print("allocated=" SIZE_FORMAT "%s ",
 937                 byte_size_in_proper_unit(allocated_bytes),
 938                 proper_unit_for_byte_size(allocated_bytes)
 939                 );
 940       st->print("defined_classes=" INT64_FORMAT " ", _statistical_info.getDefineClassCount());
 941     }
 942 
 943     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
 944     osthread()->print_on(st);
 945   }
 946   ThreadsSMRSupport::print_info_on(this, st);
 947   st->print(" ");
 948   debug_only(if (WizardMode) print_owned_locks_on(st);)
 949 }
 950 


 951 // Thread::print_on_error() is called by fatal error handler. Don't use
 952 // any lock or allocate memory.
 953 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
 954   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
 955 
 956   if (is_VM_thread())                 { st->print("VMThread"); }
 957   else if (is_GC_task_thread())       { st->print("GCTaskThread"); }
 958   else if (is_Watcher_thread())       { st->print("WatcherThread"); }
 959   else if (is_ConcurrentGC_thread())  { st->print("ConcurrentGCThread"); }
 960   else                                { st->print("Thread"); }
 961 
 962   if (is_Named_thread()) {
 963     st->print(" \"%s\"", name());
 964   }
 965 
 966   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
 967             p2i(stack_end()), p2i(stack_base()));
 968 
 969   if (osthread()) {
 970     st->print(" [id=%d]", osthread()->thread_id());


1272   _protect_enter(_the_list._protect.enter()),
1273   _current(OrderAccess::load_acquire(&_the_list._head))
1274 {}
1275 
1276 NonJavaThread::Iterator::~Iterator() {
1277   _the_list._protect.exit(_protect_enter);
1278 }
1279 
1280 void NonJavaThread::Iterator::step() {
1281   assert(!end(), "precondition");
1282   _current = OrderAccess::load_acquire(&_current->_next);
1283 }
1284 
1285 NonJavaThread::NonJavaThread() : Thread(), _next(NULL) {
1286   assert(BarrierSet::barrier_set() != NULL, "NonJavaThread created too soon!");
1287 }
1288 
1289 NonJavaThread::~NonJavaThread() { }
1290 
1291 void NonJavaThread::add_to_the_list() {
1292   MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
1293   // Initialize BarrierSet-related data before adding to list.
1294   BarrierSet::barrier_set()->on_thread_attach(this);
1295   OrderAccess::release_store(&_next, _the_list._head);
1296   OrderAccess::release_store(&_the_list._head, this);
1297 }
1298 
1299 void NonJavaThread::remove_from_the_list() {
1300   {
1301     MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
1302     // Cleanup BarrierSet-related data before removing from list.
1303     BarrierSet::barrier_set()->on_thread_detach(this);
1304     NonJavaThread* volatile* p = &_the_list._head;
1305     for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) {
1306       if (t == this) {
1307         *p = _next;
1308         break;
1309       }
1310     }
1311   }
1312   // Wait for any in-progress iterators.  Concurrent synchronize is not
1313   // allowed, so do it while holding a dedicated lock.  Outside and distinct
1314   // from NJTList_lock in case an iteration attempts to lock it.
1315   MutexLockerEx ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag);
1316   _the_list._protect.synchronize();
1317   _next = NULL;                 // Safe to drop the link now.
1318 }
1319 
1320 void NonJavaThread::pre_run() {
1321   add_to_the_list();
1322 
1323   // This is slightly odd in that NamedThread is a subclass, but
1324   // in fact name() is defined in Thread
1325   assert(this->name() != NULL, "thread name was not set before it was started");
1326   this->set_native_thread_name(this->name());
1327 }
1328 
1329 void NonJavaThread::post_run() {
1330   JFR_ONLY(Jfr::on_thread_exit(this);)
1331   remove_from_the_list();
1332   // Ensure thread-local-storage is cleared before termination.
1333   Thread::clear_thread_current();
1334 }
1335 


1380   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1381   if (os::create_thread(this, os::watcher_thread)) {
1382     _watcher_thread = this;
1383 
1384     // Set the watcher thread to the highest OS priority which should not be
1385     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1386     // is created. The only normal thread using this priority is the reference
1387     // handler thread, which runs for very short intervals only.
1388     // If the VMThread's priority is not lower than the WatcherThread profiling
1389     // will be inaccurate.
1390     os::set_priority(this, MaxPriority);
1391     if (!DisableStartThread) {
1392       os::start_thread(this);
1393     }
1394   }
1395 }
1396 
1397 int WatcherThread::sleep() const {
1398   // The WatcherThread does not participate in the safepoint protocol
1399   // for the PeriodicTask_lock because it is not a JavaThread.
1400   MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1401 
1402   if (_should_terminate) {
1403     // check for termination before we do any housekeeping or wait
1404     return 0;  // we did not sleep.
1405   }
1406 
1407   // remaining will be zero if there are no tasks,
1408   // causing the WatcherThread to sleep until a task is
1409   // enrolled
1410   int remaining = PeriodicTask::time_to_wait();
1411   int time_slept = 0;
1412 
1413   // we expect this to timeout - we only ever get unparked when
1414   // we should terminate or when a new task has been enrolled
1415   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1416 
1417   jlong time_before_loop = os::javaTimeNanos();
1418 
1419   while (true) {
1420     bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag,
1421                                             remaining);
1422     jlong now = os::javaTimeNanos();
1423 
1424     if (remaining == 0) {
1425       // if we didn't have any tasks we could have waited for a long time
1426       // consider the time_slept zero and reset time_before_loop
1427       time_slept = 0;
1428       time_before_loop = now;
1429     } else {
1430       // need to recalculate since we might have new tasks in _tasks
1431       time_slept = (int) ((now - time_before_loop) / 1000000);
1432     }
1433 
1434     // Change to task list or spurious wakeup of some kind
1435     if (timedout || _should_terminate) {
1436       break;
1437     }
1438 
1439     remaining = PeriodicTask::time_to_wait();
1440     if (remaining == 0) {
1441       // Last task was just disenrolled so loop around and wait until


1489           err.print_raw_cr("# [ timer expired, abort... ]");
1490           // skip atexit/vm_exit/vm_abort hooks
1491           os::die();
1492         }
1493 
1494         // Wait a second, then recheck for timeout.
1495         os::naked_short_sleep(999);
1496       }
1497     }
1498 
1499     if (_should_terminate) {
1500       // check for termination before posting the next tick
1501       break;
1502     }
1503 
1504     PeriodicTask::real_time_tick(time_waited);
1505   }
1506 
1507   // Signal that it is terminated
1508   {
1509     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1510     _watcher_thread = NULL;
1511     Terminator_lock->notify_all();
1512   }
1513 }
1514 
1515 void WatcherThread::start() {
1516   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1517 
1518   if (watcher_thread() == NULL && _startable) {
1519     _should_terminate = false;
1520     // Create the single instance of WatcherThread
1521     new WatcherThread();
1522   }
1523 }
1524 
1525 void WatcherThread::make_startable() {
1526   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1527   _startable = true;
1528 }
1529 
1530 void WatcherThread::stop() {
1531   {
1532     // Follow normal safepoint aware lock enter protocol since the
1533     // WatcherThread is stopped by another JavaThread.
1534     MutexLocker ml(PeriodicTask_lock);
1535     _should_terminate = true;
1536 
1537     WatcherThread* watcher = watcher_thread();
1538     if (watcher != NULL) {
1539       // unpark the WatcherThread so it can see that it should terminate
1540       watcher->unpark();
1541     }
1542   }
1543 
1544   MutexLocker mu(Terminator_lock);
1545 
1546   while (watcher_thread() != NULL) {
1547     // This wait should make safepoint checks, wait without a timeout,
1548     // and wait as a suspend-equivalent condition.
1549     Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
1550                           Mutex::_as_suspend_equivalent_flag);
1551   }
1552 }
1553 
1554 void WatcherThread::unpark() {
1555   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1556   PeriodicTask_lock->notify();
1557 }
1558 
1559 void WatcherThread::print_on(outputStream* st) const {
1560   st->print("\"%s\" ", name());
1561   Thread::print_on(st);
1562   st->cr();
1563 }
1564 
1565 // ======= JavaThread ========
1566 
1567 #if INCLUDE_JVMCI
1568 
1569 jlong* JavaThread::_jvmci_old_thread_counters;
1570 
1571 bool jvmci_counters_include(JavaThread* thread) {
1572   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1573 }
1574 
1575 void JavaThread::collect_counters(typeArrayOop array) {
1576   if (JVMCICounterSize > 0) {
1577     JavaThreadIteratorWithHandle jtiwh;
1578     for (int i = 0; i < array->length(); i++) {
1579       array->long_at_put(i, _jvmci_old_thread_counters[i]);
1580     }
1581     for (; JavaThread *tp = jtiwh.next(); ) {
1582       if (jvmci_counters_include(tp)) {
1583         for (int i = 0; i < array->length(); i++) {
1584           array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
1585         }
1586       }
1587     }
1588   }
1589 }
1590 
1591 #endif // INCLUDE_JVMCI
1592 
1593 // A JavaThread is a normal Java thread
1594 
1595 void JavaThread::initialize() {
1596   // Initialize fields
1597 
1598   set_saved_exception_pc(NULL);
1599   set_threadObj(NULL);
1600   _anchor.clear();
1601   set_entry_point(NULL);
1602   set_jni_functions(jni_functions());
1603   set_callee_target(NULL);
1604   set_vm_result(NULL);
1605   set_vm_result_2(NULL);
1606   set_vframe_array_head(NULL);
1607   set_vframe_array_last(NULL);
1608   set_deferred_locals(NULL);
1609   set_deopt_mark(NULL);
1610   set_deopt_compiled_method(NULL);
1611   clear_must_deopt_id();
1612   set_monitor_chunks(NULL);
1613   set_next(NULL);
1614   _on_thread_list = false;
1615   set_thread_state(_thread_new);
1616   _terminated = _not_terminated;
1617   _array_for_gc = NULL;
1618   _suspend_equivalent = false;
1619   _in_deopt_handler = 0;
1620   _doing_unsafe_access = false;
1621   _stack_guard_state = stack_guard_unused;
1622 #if INCLUDE_JVMCI
1623   _pending_monitorenter = false;
1624   _pending_deoptimization = -1;
1625   _pending_failed_speculation = 0;
1626   _pending_transfer_to_interpreter = false;
1627   _adjusting_comp_level = false;
1628   _in_retryable_allocation = false;
1629   _jvmci._alternate_call_target = NULL;
1630   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1631   if (JVMCICounterSize > 0) {
1632     _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1633     memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1634   } else {
1635     _jvmci_counters = NULL;
1636   }
1637 #endif // INCLUDE_JVMCI
1638   _reserved_stack_activation = NULL;  // stack base not known yet
1639   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1640   _exception_pc  = 0;
1641   _exception_handler_pc = 0;
1642   _is_method_handle_return = 0;
1643   _jvmti_thread_state= NULL;
1644   _should_post_on_exceptions_flag = JNI_FALSE;
1645   _interp_only_mode    = 0;
1646   _special_runtime_exit_condition = _no_async_condition;
1647   _pending_async_exception = NULL;


1820 // The main routine called by a new Java thread. This isn't overridden
1821 // by subclasses, instead different subclasses define a different "entry_point"
1822 // which defines the actual logic for that kind of thread.
1823 void JavaThread::run() {
1824   // initialize thread-local alloc buffer related fields
1825   this->initialize_tlab();
1826 
1827   // Used to test validity of stack trace backs.
1828   // This can't be moved into pre_run() else we invalidate
1829   // the requirement that thread_main_inner is lower on
1830   // the stack. Consequently all the initialization logic
1831   // stays here in run() rather than pre_run().
1832   this->record_base_of_stack_pointer();
1833 
1834   this->create_stack_guard_pages();
1835 
1836   this->cache_global_variables();
1837 
1838   // Thread is now sufficiently initialized to be handled by the safepoint code as being
1839   // in the VM. Change thread state from _thread_new to _thread_in_vm
1840   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1841   // Before a thread is on the threads list it is always safe, so after leaving the
1842   // _thread_new we should emit a instruction barrier. The distance to modified code
1843   // from here is probably far enough, but this is consistent and safe.
1844   OrderAccess::cross_modify_fence();
1845 
1846   assert(JavaThread::current() == this, "sanity check");
1847   assert(!Thread::current()->owns_locks(), "sanity check");
1848 
1849   DTRACE_THREAD_PROBE(start, this);
1850 
1851   // This operation might block. We call that after all safepoint checks for a new thread has
1852   // been completed.
1853   this->set_active_handles(JNIHandleBlock::allocate_block());
1854 
1855   if (JvmtiExport::should_post_thread_life()) {
1856     JvmtiExport::post_thread_start(this);
1857 
1858   }
1859 
1860   // We call another function to do the rest so we are sure that the stack addresses used


1973         JavaValue result(T_VOID);
1974         Klass* thread_klass = SystemDictionary::Thread_klass();
1975         JavaCalls::call_virtual(&result,
1976                                 threadObj, thread_klass,
1977                                 vmSymbols::exit_method_name(),
1978                                 vmSymbols::void_method_signature(),
1979                                 THREAD);
1980         CLEAR_PENDING_EXCEPTION;
1981       }
1982     }
1983     // notify JVMTI
1984     if (JvmtiExport::should_post_thread_life()) {
1985       JvmtiExport::post_thread_end(this);
1986     }
1987 
1988     // We have notified the agents that we are exiting, before we go on,
1989     // we must check for a pending external suspend request and honor it
1990     // in order to not surprise the thread that made the suspend request.
1991     while (true) {
1992       {
1993         MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1994         if (!is_external_suspend()) {
1995           set_terminated(_thread_exiting);
1996           ThreadService::current_thread_exiting(this, is_daemon(threadObj()));
1997           break;
1998         }
1999         // Implied else:
2000         // Things get a little tricky here. We have a pending external
2001         // suspend request, but we are holding the SR_lock so we
2002         // can't just self-suspend. So we temporarily drop the lock
2003         // and then self-suspend.
2004       }
2005 
2006       ThreadBlockInVM tbivm(this);
2007       java_suspend_self();
2008 
2009       // We're done with this suspend request, but we have to loop around
2010       // and check again. Eventually we will get SR_lock without a pending
2011       // external suspend request and will be able to mark ourselves as
2012       // exiting.
2013     }


2342 }
2343 
2344 // External suspension mechanism.
2345 //
2346 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2347 // to any VM_locks and it is at a transition
2348 // Self-suspension will happen on the transition out of the vm.
2349 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2350 //
2351 // Guarantees on return:
2352 //   + Target thread will not execute any new bytecode (that's why we need to
2353 //     force a safepoint)
2354 //   + Target thread will not enter any new monitors
2355 //
2356 void JavaThread::java_suspend() {
2357   ThreadsListHandle tlh;
2358   if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2359     return;
2360   }
2361 
2362   { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2363     if (!is_external_suspend()) {
2364       // a racing resume has cancelled us; bail out now
2365       return;
2366     }
2367 
2368     // suspend is done
2369     uint32_t debug_bits = 0;
2370     // Warning: is_ext_suspend_completed() may temporarily drop the
2371     // SR_lock to allow the thread to reach a stable thread state if
2372     // it is currently in a transient thread state.
2373     if (is_ext_suspend_completed(false /* !called_by_wait */,
2374                                  SuspendRetryDelay, &debug_bits)) {
2375       return;
2376     }
2377   }
2378 
2379   if (Thread::current() == this) {
2380     // Safely self-suspend.
2381     // If we don't do this explicitly it will implicitly happen
2382     // before we transition back to Java, and on some other thread-state


2401 //
2402 // Note: DO NOT call java_suspend_self() when you just want to block current
2403 //       thread. java_suspend_self() is the second stage of cooperative
2404 //       suspension for external suspend requests and should only be used
2405 //       to complete an external suspend request.
2406 //
2407 int JavaThread::java_suspend_self() {
2408   assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()");
2409   int ret = 0;
2410 
2411   // we are in the process of exiting so don't suspend
2412   if (is_exiting()) {
2413     clear_external_suspend();
2414     return ret;
2415   }
2416 
2417   assert(_anchor.walkable() ||
2418          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
2419          "must have walkable stack");
2420 
2421   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2422 
2423   assert(!this->is_ext_suspended(),
2424          "a thread trying to self-suspend should not already be suspended");
2425 
2426   if (this->is_suspend_equivalent()) {
2427     // If we are self-suspending as a result of the lifting of a
2428     // suspend equivalent condition, then the suspend_equivalent
2429     // flag is not cleared until we set the ext_suspended flag so
2430     // that wait_for_ext_suspend_completion() returns consistent
2431     // results.
2432     this->clear_suspend_equivalent();
2433   }
2434 
2435   // A racing resume may have cancelled us before we grabbed SR_lock
2436   // above. Or another external suspend request could be waiting for us
2437   // by the time we return from SR_lock()->wait(). The thread
2438   // that requested the suspension may already be trying to walk our
2439   // stack and if we return now, we can change the stack out from under
2440   // it. This would be a "bad thing (TM)" and cause the stack walker
2441   // to crash. We stay self-suspended until there are no more pending
2442   // external suspend requests.
2443   while (is_external_suspend()) {
2444     ret++;
2445     this->set_ext_suspended();
2446 
2447     // _ext_suspended flag is cleared by java_resume()
2448     while (is_ext_suspended()) {
2449       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2450     }
2451   }
2452   return ret;
2453 }
2454 
2455 // Helper routine to set up the correct thread state before calling java_suspend_self.
2456 // This is called when regular thread-state transition helpers can't be used because
2457 // we can be in various states, in particular _thread_in_native_trans.
2458 // Because this thread is external suspended the safepoint code will count it as at
2459 // a safepoint, regardless of what its actual current thread-state is. But
2460 // is_ext_suspend_completed() may be waiting to see a thread transition from
2461 // _thread_in_native_trans to _thread_blocked. So we set the thread state directly
2462 // to _thread_blocked. The problem with setting thread state directly is that a
2463 // safepoint could happen just after java_suspend_self() returns after being resumed,
2464 // and the VM thread will see the _thread_blocked state. So we must check for a safepoint
2465 // after restoring the state to make sure we won't leave while a safepoint is in progress.
2466 // However, not all initial-states are allowed when performing a safepoint check, as we
2467 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
2468 // only _thread_in_native is possible when executing this code (based on our two callers).
2469 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
2470 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
2471 // and so we don't need the explicit safepoint check.
2472 
2473 void JavaThread::java_suspend_self_with_safepoint_check() {
2474   assert(this == Thread::current(), "invariant");
2475   JavaThreadState state = thread_state();
2476   set_thread_state(_thread_blocked);
2477   java_suspend_self();
2478   set_thread_state(state);
2479   // Since we are not using a regular thread-state transition helper here,
2480   // we must manually emit the instruction barrier after leaving a safe state.
2481   OrderAccess::cross_modify_fence();
2482   InterfaceSupport::serialize_thread_state_with_handler(this);
2483   if (state != _thread_in_native) {
2484     SafepointMechanism::block_if_requested(this);
2485   }
2486 }
2487 
2488 #ifdef ASSERT
2489 // Verify the JavaThread has not yet been published in the Threads::list, and
2490 // hence doesn't need protection from concurrent access at this stage.
2491 void JavaThread::verify_not_published() {
2492   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2493   // since an unpublished JavaThread doesn't participate in the
2494   // Thread-SMR protocol for keeping a ThreadsList alive.
2495   assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2496 }
2497 #endif
2498 
2499 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2500 // progress or when _suspend_flags is non-zero.
2501 // Current thread needs to self-suspend if there is a suspend request and/or
2502 // block if a safepoint is in progress.


2504 // Note only the ThreadInVMfromNative transition can call this function
2505 // directly and when thread state is _thread_in_native_trans
2506 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2507   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2508 
2509   JavaThread *curJT = JavaThread::current();
2510   bool do_self_suspend = thread->is_external_suspend();
2511 
2512   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2513 
2514   // If JNIEnv proxies are allowed, don't self-suspend if the target
2515   // thread is not the current thread. In older versions of jdbx, jdbx
2516   // threads could call into the VM with another thread's JNIEnv so we
2517   // can be here operating on behalf of a suspended thread (4432884).
2518   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
2519     thread->java_suspend_self_with_safepoint_check();
2520   } else {
2521     SafepointMechanism::block_if_requested(curJT);
2522   }
2523 
2524   if (thread->is_deopt_suspend()) {
2525     thread->clear_deopt_suspend();
2526     RegisterMap map(thread, false);
2527     frame f = thread->last_frame();
2528     while (f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2529       f = f.sender(&map);
2530     }
2531     if (f.id() == thread->must_deopt_id()) {
2532       thread->clear_must_deopt_id();
2533       f.deoptimize(thread);
2534     } else {
2535       fatal("missed deoptimization!");
2536     }
2537   }
2538 
2539   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2540 }
2541 
2542 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2543 // progress or when _suspend_flags is non-zero.
2544 // Current thread needs to self-suspend if there is a suspend request and/or
2545 // block if a safepoint is in progress.
2546 // Also check for pending async exception (not including unsafe access error).
2547 // Note only the native==>VM/Java barriers can call this function and when
2548 // thread state is _thread_in_native_trans.
2549 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2550   check_safepoint_and_suspend_for_native_trans(thread);
2551 
2552   if (thread->has_async_exception()) {
2553     // We are in _thread_in_native_trans state, don't handle unsafe
2554     // access error since that may block.
2555     thread->check_and_handle_async_exceptions(false);
2556   }
2557 }
2558 


2572   if (thread->do_critical_native_unlock()) {
2573     ThreadInVMfromJavaNoAsyncException tiv(thread);
2574     GCLocker::unlock_critical(thread);
2575     thread->clear_critical_native_unlock();
2576   }
2577 }
2578 
2579 // We need to guarantee the Threads_lock here, since resumes are not
2580 // allowed during safepoint synchronization
2581 // Can only resume from an external suspension
2582 void JavaThread::java_resume() {
2583   assert_locked_or_safepoint(Threads_lock);
2584 
2585   // Sanity check: thread is gone, has started exiting or the thread
2586   // was not externally suspended.
2587   ThreadsListHandle tlh;
2588   if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) {
2589     return;
2590   }
2591 
2592   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2593 
2594   clear_external_suspend();
2595 
2596   if (is_ext_suspended()) {
2597     clear_ext_suspended();
2598     SR_lock()->notify_all();
2599   }
2600 }
2601 
2602 size_t JavaThread::_stack_red_zone_size = 0;
2603 size_t JavaThread::_stack_yellow_zone_size = 0;
2604 size_t JavaThread::_stack_reserved_zone_size = 0;
2605 size_t JavaThread::_stack_shadow_zone_size = 0;
2606 
2607 void JavaThread::create_stack_guard_pages() {
2608   if (!os::uses_stack_guard_pages() ||
2609       _stack_guard_state != stack_guard_unused ||
2610       (DisablePrimordialThreadGuardPages && os::is_primordial_thread())) {
2611       log_info(os, thread)("Stack guard page creation for thread "
2612                            UINTX_FORMAT " disabled", os::current_thread_id());


2830   if (DebugDeoptimization && deopt) {
2831     tty->print_cr("[AFTER Deoptimization]");
2832     trace_frames();
2833   }
2834 }
2835 
2836 
2837 // Make zombies
2838 void JavaThread::make_zombies() {
2839   for (StackFrameStream fst(this); !fst.is_done(); fst.next()) {
2840     if (fst.current()->can_be_deoptimized()) {
2841       // it is a Java nmethod
2842       nmethod* nm = CodeCache::find_nmethod(fst.current()->pc());
2843       nm->make_not_entrant();
2844     }
2845   }
2846 }
2847 #endif // PRODUCT
2848 
2849 
2850 void JavaThread::deoptimized_wrt_marked_nmethods() {
2851   if (!has_last_Java_frame()) return;
2852   // BiasedLocking needs an updated RegisterMap for the revoke monitors pass
2853   StackFrameStream fst(this, UseBiasedLocking);
2854   for (; !fst.is_done(); fst.next()) {
2855     if (fst.current()->should_be_deoptimized()) {
2856       Deoptimization::deoptimize(this, *fst.current(), fst.register_map());
2857     }
2858   }
2859 }
2860 
2861 
2862 // If the caller is a NamedThread, then remember, in the current scope,
2863 // the given JavaThread in its _processed_thread field.
2864 class RememberProcessedThread: public StackObj {
2865   NamedThread* _cur_thr;
2866  public:
2867   RememberProcessedThread(JavaThread* jthr) {
2868     Thread* thread = Thread::current();
2869     if (thread->is_Named_thread()) {
2870       _cur_thr = (NamedThread *)thread;
2871       _cur_thr->set_processed_thread(jthr);
2872     } else {
2873       _cur_thr = NULL;
2874     }
2875   }
2876 
2877   ~RememberProcessedThread() {
2878     if (_cur_thr) {
2879       _cur_thr->set_processed_thread(NULL);
2880     }
2881   }


3025   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
3026   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
3027   if (thread_oop != NULL) {
3028     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
3029   }
3030 #ifndef PRODUCT
3031   _safepoint_state->print_on(st);
3032 #endif // PRODUCT
3033   if (is_Compiler_thread()) {
3034     CompileTask *task = ((CompilerThread*)this)->task();
3035     if (task != NULL) {
3036       st->print("   Compiling: ");
3037       task->print(st, NULL, true, false);
3038     } else {
3039       st->print("   No compile task");
3040     }
3041     st->cr();
3042   }
3043 }
3044 


3045 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
3046   st->print("%s", get_thread_name_string(buf, buflen));
3047 }
3048 
3049 // Called by fatal error handler. The difference between this and
3050 // JavaThread::print() is that we can't grab lock or allocate memory.
3051 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
3052   st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
3053   oop thread_obj = threadObj();
3054   if (thread_obj != NULL) {
3055     if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
3056   }
3057   st->print(" [");
3058   st->print("%s", _get_thread_state_name(_thread_state));
3059   if (osthread()) {
3060     st->print(", id=%d", osthread()->thread_id());
3061   }
3062   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
3063             p2i(stack_end()), p2i(stack_base()));
3064   st->print("]");


3189 
3190   if (prio == NoPriority) {
3191     prio = java_lang_Thread::priority(thread_oop());
3192     assert(prio != NoPriority, "A valid priority should be present");
3193   }
3194 
3195   // Push the Java priority down to the native thread; needs Threads_lock
3196   Thread::set_priority(this, prio);
3197 
3198   // Add the new thread to the Threads list and set it in motion.
3199   // We must have threads lock in order to call Threads::add.
3200   // It is crucial that we do not block before the thread is
3201   // added to the Threads list for if a GC happens, then the java_thread oop
3202   // will not be visited by GC.
3203   Threads::add(this);
3204 }
3205 
3206 oop JavaThread::current_park_blocker() {
3207   // Support for JSR-166 locks
3208   oop thread_oop = threadObj();
3209   if (thread_oop != NULL &&
3210       JDK_Version::current().supports_thread_park_blocker()) {
3211     return java_lang_Thread::park_blocker(thread_oop);
3212   }
3213   return NULL;
3214 }
3215 
3216 
3217 void JavaThread::print_stack_on(outputStream* st) {
3218   if (!has_last_Java_frame()) return;
3219   ResourceMark rm;
3220   HandleMark   hm;
3221 
3222   RegisterMap reg_map(this);
3223   vframe* start_vf = last_java_vframe(&reg_map);
3224   int count = 0;
3225   for (vframe* f = start_vf; f != NULL; f = f->sender()) {
3226     if (f->is_java_frame()) {
3227       javaVFrame* jvf = javaVFrame::cast(f);
3228       java_lang_Throwable::print_stack_element(st, jvf->method(), jvf->bci());
3229 
3230       // Print out lock information


3453     // process it here to make sure it isn't unloaded in the middle of
3454     // a scan.
3455     cf->do_code_blob(_scanned_compiled_method);
3456   }
3457 }
3458 
3459 
3460 // ======= Threads ========
3461 
3462 // The Threads class links together all active threads, and provides
3463 // operations over all threads. It is protected by the Threads_lock,
3464 // which is also used in other global contexts like safepointing.
3465 // ThreadsListHandles are used to safely perform operations on one
3466 // or more threads without the risk of the thread exiting during the
3467 // operation.
3468 //
3469 // Note: The Threads_lock is currently more widely used than we
3470 // would like. We are actively migrating Threads_lock uses to other
3471 // mechanisms in order to reduce Threads_lock contention.
3472 
3473 JavaThread* Threads::_thread_list = NULL;
3474 int         Threads::_number_of_threads = 0;
3475 int         Threads::_number_of_non_daemon_threads = 0;
3476 int         Threads::_return_code = 0;
3477 uintx       Threads::_thread_claim_token = 1; // Never zero.
3478 size_t      JavaThread::_stack_size_at_create = 0;
3479 
3480 #ifdef ASSERT
3481 bool        Threads::_vm_complete = false;
3482 #endif
3483 
3484 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3485   Prefetch::read((void*)addr, prefetch_interval);
3486   return *addr;
3487 }
3488 
3489 // Possibly the ugliest for loop the world has seen. C++ does not allow
3490 // multiple types in the declaration section of the for loop. In this case
3491 // we are only dealing with pointers and hence can cast them. It looks ugly
3492 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3493 #define DO_JAVA_THREADS(LIST, X)                                                                                          \


3611 
3612   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3613     create_vm_init_libraries();
3614   }
3615 
3616   initialize_class(vmSymbols::java_lang_String(), CHECK);
3617 
3618   // Inject CompactStrings value after the static initializers for String ran.
3619   java_lang_String::set_compact_strings(CompactStrings);
3620 
3621   // Initialize java_lang.System (needed before creating the thread)
3622   initialize_class(vmSymbols::java_lang_System(), CHECK);
3623   // The VM creates & returns objects of this class. Make sure it's initialized.
3624   initialize_class(vmSymbols::java_lang_Class(), CHECK);
3625   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3626   Handle thread_group = create_initial_thread_group(CHECK);
3627   Universe::set_main_thread_group(thread_group());
3628   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
3629   oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
3630   main_thread->set_threadObj(thread_object);

3631   // Set thread status to running since main thread has
3632   // been started and running.
3633   java_lang_Thread::set_thread_status(thread_object,
3634                                       java_lang_Thread::RUNNABLE);
3635 
3636   // The VM creates objects of this class.
3637   initialize_class(vmSymbols::java_lang_Module(), CHECK);
3638 









3639   // The VM preresolves methods to these classes. Make sure that they get initialized
3640   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
3641   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
3642 
3643   // Phase 1 of the system initialization in the library, java.lang.System class initialization
3644   call_initPhase1(CHECK);
3645 
3646   // get the Java runtime name after java.lang.System is initialized
3647   JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
3648   JDK_Version::set_runtime_version(get_java_runtime_version(THREAD));
3649 
3650   // an instance of OutOfMemory exception has been allocated earlier
3651   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
3652   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
3653   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
3654   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
3655   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
3656   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
3657   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
3658   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);


3750   SafepointMechanism::initialize();
3751 
3752   jint adjust_after_os_result = Arguments::adjust_after_os();
3753   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3754 
3755   // Initialize output stream logging
3756   ostream_init_log();
3757 
3758   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3759   // Must be before create_vm_init_agents()
3760   if (Arguments::init_libraries_at_startup()) {
3761     convert_vm_init_libraries_to_agents();
3762   }
3763 
3764   // Launch -agentlib/-agentpath and converted -Xrun agents
3765   if (Arguments::init_agents_at_startup()) {
3766     create_vm_init_agents();
3767   }
3768 
3769   // Initialize Threads state
3770   _thread_list = NULL;
3771   _number_of_threads = 0;
3772   _number_of_non_daemon_threads = 0;
3773 
3774   // Initialize global data structures and create system classes in heap
3775   vm_init_globals();
3776 
3777 #if INCLUDE_JVMCI
3778   if (JVMCICounterSize > 0) {
3779     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3780     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3781   } else {
3782     JavaThread::_jvmci_old_thread_counters = NULL;
3783   }
3784 #endif // INCLUDE_JVMCI
3785 
3786   // Attach the main thread to this os thread
3787   JavaThread* main_thread = new JavaThread();
3788   main_thread->set_thread_state(_thread_in_vm);
3789   main_thread->initialize_thread_current();
3790   // must do this before set_active_handles


3827   }
3828 
3829   // Any JVMTI raw monitors entered in onload will transition into
3830   // real raw monitor. VM is setup enough here for raw monitor enter.
3831   JvmtiExport::transition_pending_onload_raw_monitors();
3832 
3833   // Create the VMThread
3834   { TraceTime timer("Start VMThread", TRACETIME_LOG(Info, startuptime));
3835 
3836   VMThread::create();
3837     Thread* vmthread = VMThread::vm_thread();
3838 
3839     if (!os::create_thread(vmthread, os::vm_thread)) {
3840       vm_exit_during_initialization("Cannot create VM thread. "
3841                                     "Out of system resources.");
3842     }
3843 
3844     // Wait for the VM thread to become ready, and VMThread::run to initialize
3845     // Monitors can have spurious returns, must always check another state flag
3846     {
3847       MutexLocker ml(Notify_lock);
3848       os::start_thread(vmthread);
3849       while (vmthread->active_handles() == NULL) {
3850         Notify_lock->wait();
3851       }
3852     }
3853   }
3854 
3855   assert(Universe::is_fully_initialized(), "not initialized");
3856   if (VerifyDuringStartup) {
3857     // Make sure we're starting with a clean slate.
3858     VM_Verify verify_op;
3859     VMThread::execute(&verify_op);
3860   }
3861 
3862   // We need this to update the java.vm.info property in case any flags used
3863   // to initially define it have been changed. This is needed for both CDS and
3864   // AOT, since UseSharedSpaces and UseAOT may be changed after java.vm.info
3865   // is initially computed. See Abstract_VM_Version::vm_info_string().
3866   // This update must happen before we initialize the java classes, but
3867   // after any initialization logic that might modify the flags.
3868   Arguments::update_vm_info_property(VM_Version::vm_info_string());
3869 
3870   Thread* THREAD = Thread::current();


3908     }
3909   }
3910 
3911   // Launch -Xrun agents
3912   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3913   // back-end can launch with -Xdebug -Xrunjdwp.
3914   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3915     create_vm_init_libraries();
3916   }
3917 
3918   if (CleanChunkPoolAsync) {
3919     Chunk::start_chunk_pool_cleaner_task();
3920   }
3921 
3922   // initialize compiler(s)
3923 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
3924 #if INCLUDE_JVMCI
3925   bool force_JVMCI_intialization = false;
3926   if (EnableJVMCI) {
3927     // Initialize JVMCI eagerly when it is explicitly requested.
3928     // Or when JVMCIPrintProperties is enabled.
3929     // The JVMCI Java initialization code will read this flag and
3930     // do the printing if it's set.
3931     force_JVMCI_intialization = EagerJVMCI || JVMCIPrintProperties;
3932 
3933     if (!force_JVMCI_intialization) {
3934       // 8145270: Force initialization of JVMCI runtime otherwise requests for blocking
3935       // compilations via JVMCI will not actually block until JVMCI is initialized.
3936       force_JVMCI_intialization = UseJVMCICompiler && (!UseInterpreter || !BackgroundCompilation);
3937     }
3938   }
3939 #endif
3940   CompileBroker::compilation_init_phase1(CHECK_JNI_ERR);
3941   // Postpone completion of compiler initialization to after JVMCI
3942   // is initialized to avoid timeouts of blocking compilations.
3943   if (JVMCI_ONLY(!force_JVMCI_intialization) NOT_JVMCI(true)) {
3944     CompileBroker::compilation_init_phase2();
3945   }
3946 #endif
3947 
3948   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3949   // It is done after compilers are initialized, because otherwise compilations of
3950   // signature polymorphic MH intrinsics can be missed
3951   // (see SystemDictionary::find_method_handle_intrinsic).
3952   initialize_jsr292_core_classes(CHECK_JNI_ERR);
3953 
3954   // This will initialize the module system.  Only java.base classes can be
3955   // loaded until phase 2 completes
3956   call_initPhase2(CHECK_JNI_ERR);
3957 
3958   // Always call even when there are not JVMTI environments yet, since environments
3959   // may be attached late and JVMTI must track phases of VM execution
3960   JvmtiExport::enter_start_phase();
3961 
3962   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
3963   JvmtiExport::post_vm_start();
3964 
3965   // Final system initialization including security manager and system class loader
3966   call_initPhase3(CHECK_JNI_ERR);
3967 
3968   // cache the system and platform class loaders
3969   SystemDictionary::compute_java_loaders(CHECK_JNI_ERR);
3970 
3971 #if INCLUDE_CDS
3972   if (DumpSharedSpaces) {
3973     // capture the module path info from the ModuleEntryTable
3974     ClassLoader::initialize_module_path(THREAD);
3975   }
3976 #endif
3977 
3978 #if INCLUDE_JVMCI
3979   if (force_JVMCI_intialization) {
3980     JVMCIRuntime::force_initialization(CHECK_JNI_ERR);
3981     CompileBroker::compilation_init_phase2();
3982   }
3983 #endif
3984 
3985   // Always call even when there are not JVMTI environments yet, since environments
3986   // may be attached late and JVMTI must track phases of VM execution
3987   JvmtiExport::enter_live_phase();
3988 
3989   // Make perfmemory accessible
3990   PerfMemory::set_accessible(true);
3991 
3992   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3993   JvmtiExport::post_vm_initialized();
3994 
3995   JFR_ONLY(Jfr::on_vm_start();)
3996 
3997 #if INCLUDE_MANAGEMENT
3998   Management::initialize(THREAD);
3999 
4000   if (HAS_PENDING_EXCEPTION) {
4001     // management agent fails to start possibly due to
4002     // configuration problem and is responsible for printing
4003     // stack trace if appropriate. Simply exit VM.
4004     vm_exit(1);
4005   }
4006 #endif // INCLUDE_MANAGEMENT
4007 
4008   if (MemProfiling)                   MemProfiler::engage();
4009   StatSampler::engage();
4010   if (CheckJNICalls)                  JniPeriodicChecker::engage();
4011 
4012   BiasedLocking::init();
4013 
4014 #if INCLUDE_RTM_OPT
4015   RTMLockingCounters::init();
4016 #endif
4017 
4018   if (JDK_Version::current().post_vm_init_hook_enabled()) {
4019     call_postVMInitHook(THREAD);
4020     // The Java side of PostVMInitHook.run must deal with all
4021     // exceptions and provide means of diagnosis.
4022     if (HAS_PENDING_EXCEPTION) {
4023       CLEAR_PENDING_EXCEPTION;
4024     }
4025   }
4026 
4027   {
4028     MutexLocker ml(PeriodicTask_lock);
4029     // Make sure the WatcherThread can be started by WatcherThread::start()
4030     // or by dynamic enrollment.
4031     WatcherThread::make_startable();
4032     // Start up the WatcherThread if there are any periodic tasks
4033     // NOTE:  All PeriodicTasks should be registered by now. If they
4034     //   aren't, late joiners might appear to start slowly (we might
4035     //   take a while to process their first tick).
4036     if (PeriodicTask::num_tasks() > 0) {
4037       WatcherThread::start();
4038     }
4039   }
4040 
4041   create_vm_timer.end();
4042 #ifdef ASSERT
4043   _vm_complete = true;
4044 #endif


4159         Arguments::convert_library_to_agent(agent);
4160       } else {
4161         vm_exit_during_initialization("Could not find JVM_OnLoad or Agent_OnLoad function in the library", agent->name());
4162       }
4163     }
4164   }
4165 }
4166 
4167 // Create agents for -agentlib:  -agentpath:  and converted -Xrun
4168 // Invokes Agent_OnLoad
4169 // Called very early -- before JavaThreads exist
4170 void Threads::create_vm_init_agents() {
4171   extern struct JavaVM_ main_vm;
4172   AgentLibrary* agent;
4173 
4174   JvmtiExport::enter_onload_phase();
4175 
4176   for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
4177     // CDS dumping does not support native JVMTI agent.
4178     // CDS dumping supports Java agent if the AllowArchivingWithJavaAgent diagnostic option is specified.
4179     if (DumpSharedSpaces) {
4180       if(!agent->is_instrument_lib()) {
4181         vm_exit_during_cds_dumping("CDS dumping does not support native JVMTI agent, name", agent->name());
4182       } else if (!AllowArchivingWithJavaAgent) {
4183         vm_exit_during_cds_dumping(
4184           "Must enable AllowArchivingWithJavaAgent in order to run Java agent during CDS dumping");
4185       }
4186     }
4187 
4188     OnLoadEntry_t  on_load_entry = lookup_agent_on_load(agent);
4189 
4190     if (on_load_entry != NULL) {
4191       // Invoke the Agent_OnLoad function
4192       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4193       if (err != JNI_OK) {
4194         vm_exit_during_initialization("agent library failed to init", agent->name());
4195       }
4196     } else {
4197       vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name());
4198     }
4199   }


4260 
4261   // We could get here with a pending exception, if so clear it now.
4262   if (this->has_pending_exception()) {
4263     this->clear_pending_exception();
4264   }
4265 
4266   EXCEPTION_MARK;
4267   Klass* shutdown_klass =
4268     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4269                                       THREAD);
4270   if (shutdown_klass != NULL) {
4271     // SystemDictionary::resolve_or_null will return null if there was
4272     // an exception.  If we cannot load the Shutdown class, just don't
4273     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4274     // won't be run.  Note that if a shutdown hook was registered,
4275     // the Shutdown class would have already been loaded
4276     // (Runtime.addShutdownHook will load it).
4277     JavaValue result(T_VOID);
4278     JavaCalls::call_static(&result,
4279                            shutdown_klass,
4280                            vmSymbols::shutdown_method_name(),
4281                            vmSymbols::void_method_signature(),
4282                            THREAD);
4283   }
4284   CLEAR_PENDING_EXCEPTION;
4285 }
4286 
4287 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4288 // the program falls off the end of main(). Another VM exit path is through
4289 // vm_exit() when the program calls System.exit() to return a value or when
4290 // there is a serious error in VM. The two shutdown paths are not exactly
4291 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4292 // and VM_Exit op at VM level.
4293 //
4294 // Shutdown sequence:
4295 //   + Shutdown native memory tracking if it is on
4296 //   + Wait until we are the last non-daemon thread to execute
4297 //     <-- every thing is still working at this moment -->
4298 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
4299 //        shutdown hooks
4300 //   + Call before_exit(), prepare for VM exit


4308 //      > remove this thread from Threads list
4309 //     <-- no more Java code from this thread after this point -->
4310 //   + Stop VM thread, it will bring the remaining VM to a safepoint and stop
4311 //     the compiler threads at safepoint
4312 //     <-- do not use anything that could get blocked by Safepoint -->
4313 //   + Disable tracing at JNI/JVM barriers
4314 //   + Set _vm_exited flag for threads that are still running native code
4315 //   + Call exit_globals()
4316 //      > deletes tty
4317 //      > deletes PerfMemory resources
4318 //   + Delete this thread
4319 //   + Return to caller
4320 
4321 bool Threads::destroy_vm() {
4322   JavaThread* thread = JavaThread::current();
4323 
4324 #ifdef ASSERT
4325   _vm_complete = false;
4326 #endif
4327   // Wait until we are the last non-daemon thread to execute
4328   { MutexLocker nu(Threads_lock);
4329     while (Threads::number_of_non_daemon_threads() > 1)
4330       // This wait should make safepoint checks, wait without a timeout,
4331       // and wait as a suspend-equivalent condition.
4332       Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
4333                          Mutex::_as_suspend_equivalent_flag);
4334   }
4335 
4336   EventShutdown e;
4337   if (e.should_commit()) {
4338     e.set_reason("No remaining non-daemon Java threads");
4339     e.commit();
4340   }
4341 
4342   // Hang forever on exit if we are reporting an error.
4343   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
4344     os::infinite_sleep();
4345   }
4346   os::wait_for_keypress_at_exit();
4347 
4348   // run Java level shutdown hooks
4349   thread->invoke_shutdown_hooks();
4350 
4351   before_exit(thread);
4352 
4353   thread->exit(true);
4354 
4355   // Stop VM thread.
4356   {
4357     // 4945125 The vm thread comes to a safepoint during exit.
4358     // GC vm_operations can get caught at the safepoint, and the
4359     // heap is unparseable if they are caught. Grab the Heap_lock
4360     // to prevent this. The GC vm_operations will not be able to
4361     // queue until after the vm thread is dead. After this point,
4362     // we'll never emerge out of the safepoint before the VM exits.
4363 
4364     MutexLockerEx ml(Heap_lock, Mutex::_no_safepoint_check_flag);
4365 
4366     VMThread::wait_for_vm_thread_exit();
4367     assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4368     VMThread::destroy();
4369   }
4370 
4371   // Now, all Java threads are gone except daemon threads. Daemon threads
4372   // running Java code or in VM are stopped by the Safepoint. However,
4373   // daemon threads executing native code are still running.  But they
4374   // will be stopped at native=>Java/VM barriers. Note that we can't
4375   // simply kill or suspend them, as it is inherently deadlock-prone.
4376 
4377   VM_Exit::set_vm_exited();
4378 
4379   // Clean up ideal graph printers after the VMThread has started
4380   // the final safepoint which will block all the Compiler threads.
4381   // Note that this Thread has already logically exited so the
4382   // clean_up() function's use of a JavaThreadIteratorWithHandle
4383   // would be a problem except set_vm_exited() has remembered the
4384   // shutdown thread which is granted a policy exception.


4416 }
4417 
4418 
4419 jboolean Threads::is_supported_jni_version(jint version) {
4420   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4421   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4422   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4423   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4424   if (version == JNI_VERSION_9) return JNI_TRUE;
4425   if (version == JNI_VERSION_10) return JNI_TRUE;
4426   return JNI_FALSE;
4427 }
4428 
4429 
4430 void Threads::add(JavaThread* p, bool force_daemon) {
4431   // The threads lock must be owned at this point
4432   assert(Threads_lock->owned_by_self(), "must have threads lock");
4433 
4434   BarrierSet::barrier_set()->on_thread_attach(p);
4435 
4436   p->set_next(_thread_list);
4437   _thread_list = p;
4438 
4439   // Once a JavaThread is added to the Threads list, smr_delete() has
4440   // to be used to delete it. Otherwise we can just delete it directly.
4441   p->set_on_thread_list();
4442 
4443   _number_of_threads++;
4444   oop threadObj = p->threadObj();
4445   bool daemon = true;
4446   // Bootstrapping problem: threadObj can be null for initial
4447   // JavaThread (or for threads attached via JNI)
4448   if ((!force_daemon) && !is_daemon((threadObj))) {
4449     _number_of_non_daemon_threads++;
4450     daemon = false;
4451   }
4452 
4453   ThreadService::add_thread(p, daemon);
4454 
4455   // Maintain fast thread list
4456   ThreadsSMRSupport::add_thread(p);
4457 
4458   // Possible GC point.
4459   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4460 }
4461 
4462 void Threads::remove(JavaThread* p, bool is_daemon) {
4463 
4464   // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread.
4465   ObjectSynchronizer::omFlush(p);
4466 
4467   // Extra scope needed for Thread_lock, so we can check
4468   // that we do not remove thread without safepoint code notice
4469   { MutexLocker ml(Threads_lock);
4470 
4471     assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
4472 
4473     // Maintain fast thread list
4474     ThreadsSMRSupport::remove_thread(p);
4475 
4476     JavaThread* current = _thread_list;
4477     JavaThread* prev    = NULL;
4478 
4479     while (current != p) {
4480       prev    = current;
4481       current = current->next();
4482     }
4483 
4484     if (prev) {
4485       prev->set_next(current->next());
4486     } else {
4487       _thread_list = p->next();
4488     }
4489 
4490     _number_of_threads--;
4491     if (!is_daemon) {
4492       _number_of_non_daemon_threads--;
4493 
4494       // Only one thread left, do a notify on the Threads_lock so a thread waiting
4495       // on destroy_vm will wake up.
4496       if (number_of_non_daemon_threads() == 1) {
4497         Threads_lock->notify_all();
4498       }
4499     }
4500     ThreadService::remove_thread(p, is_daemon);
4501 
4502     // Make sure that safepoint code disregard this thread. This is needed since
4503     // the thread might mess around with locks after this point. This can cause it
4504     // to do callbacks into the safepoint code. However, the safepoint code is not aware
4505     // of this thread since it is removed from the queue.
4506     p->set_terminated_value();
4507   } // unlock Threads_lock
4508 
4509   // Since Events::log uses a lock, we grab it outside the Threads_lock
4510   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4511 }
4512 
4513 // Operations on the Threads list for GC.  These are not explicitly locked,
4514 // but the garbage collector must provide a safe context for them to run.
4515 // In particular, these things should never be called when the Threads_lock
4516 // is held by some other thread. (Note: the Safepoint abstraction also
4517 // uses the Threads_lock to guarantee this property. It also makes sure that


4590 void Threads::metadata_do(MetadataClosure* f) {
4591   ALL_JAVA_THREADS(p) {
4592     p->metadata_do(f);
4593   }
4594 }
4595 
4596 class ThreadHandlesClosure : public ThreadClosure {
4597   void (*_f)(Metadata*);
4598  public:
4599   ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
4600   virtual void do_thread(Thread* thread) {
4601     thread->metadata_handles_do(_f);
4602   }
4603 };
4604 
4605 void Threads::metadata_handles_do(void f(Metadata*)) {
4606   // Only walk the Handles in Thread.
4607   ThreadHandlesClosure handles_closure(f);
4608   threads_do(&handles_closure);
4609 }
4610 
4611 void Threads::deoptimized_wrt_marked_nmethods() {
4612   ALL_JAVA_THREADS(p) {
4613     p->deoptimized_wrt_marked_nmethods();
4614   }
4615 }
4616 
4617 
4618 // Get count Java threads that are waiting to enter the specified monitor.
4619 GrowableArray<JavaThread*>* Threads::get_pending_threads(ThreadsList * t_list,
4620                                                          int count,
4621                                                          address monitor) {
4622   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
4623 
4624   int i = 0;
4625   DO_JAVA_THREADS(t_list, p) {
4626     if (!p->can_call_java()) continue;
4627 
4628     address pending = (address)p->current_pending_monitor();
4629     if (pending == monitor) {             // found a match
4630       if (i < count) result->append(p);   // save the first count matches
4631       i++;
4632     }
4633   }
4634 
4635   return result;
4636 }


< prev index next >