< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page
rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed
rev 47863 : imported patch 10.08.open.rebase_20171114.rehn
rev 47864 : imported patch 10.09.open.TLH_hang_fix.rehn
rev 47865 : dholmes CR: Fix indents, trailing spaces and various typos. Add descriptions for the '_cnt', '_max' and '_times" fields, add impl notes to document the type choices.
rev 47866 : robinw CR: Fix some inefficient code, update some comments, fix some indents, and add some 'const' specifiers.


  54 #include "oops/verifyOopClosure.hpp"
  55 #include "prims/jvm_misc.hpp"
  56 #include "prims/jvmtiExport.hpp"
  57 #include "prims/jvmtiThreadState.hpp"
  58 #include "prims/privilegedStack.hpp"
  59 #include "runtime/arguments.hpp"
  60 #include "runtime/atomic.hpp"
  61 #include "runtime/biasedLocking.hpp"
  62 #include "runtime/commandLineFlagConstraintList.hpp"
  63 #include "runtime/commandLineFlagWriteableList.hpp"
  64 #include "runtime/commandLineFlagRangeList.hpp"
  65 #include "runtime/deoptimization.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/globals.hpp"
  68 #include "runtime/handshake.hpp"
  69 #include "runtime/init.hpp"
  70 #include "runtime/interfaceSupport.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/jniPeriodicChecker.hpp"
  74 #include "runtime/timerTrace.hpp"
  75 #include "runtime/memprofiler.hpp"
  76 #include "runtime/mutexLocker.hpp"
  77 #include "runtime/objectMonitor.hpp"
  78 #include "runtime/orderAccess.inline.hpp"
  79 #include "runtime/osThread.hpp"

  80 #include "runtime/safepoint.hpp"
  81 #include "runtime/safepointMechanism.inline.hpp"
  82 #include "runtime/sharedRuntime.hpp"
  83 #include "runtime/statSampler.hpp"
  84 #include "runtime/stubRoutines.hpp"
  85 #include "runtime/sweeper.hpp"
  86 #include "runtime/task.hpp"
  87 #include "runtime/thread.inline.hpp"
  88 #include "runtime/threadCritical.hpp"



  89 #include "runtime/vframe.hpp"
  90 #include "runtime/vframeArray.hpp"
  91 #include "runtime/vframe_hp.hpp"
  92 #include "runtime/vmThread.hpp"
  93 #include "runtime/vm_operations.hpp"
  94 #include "runtime/vm_version.hpp"
  95 #include "services/attachListener.hpp"
  96 #include "services/management.hpp"
  97 #include "services/memTracker.hpp"
  98 #include "services/threadService.hpp"
  99 #include "trace/traceMacros.hpp"
 100 #include "trace/tracing.hpp"
 101 #include "utilities/align.hpp"
 102 #include "utilities/defaultStream.hpp"
 103 #include "utilities/dtrace.hpp"
 104 #include "utilities/events.hpp"
 105 #include "utilities/macros.hpp"
 106 #include "utilities/preserveException.hpp"

 107 #include "utilities/vmError.hpp"
 108 #if INCLUDE_ALL_GCS
 109 #include "gc/cms/concurrentMarkSweepThread.hpp"
 110 #include "gc/g1/concurrentMarkThread.inline.hpp"
 111 #include "gc/parallel/pcTasks.hpp"
 112 #endif // INCLUDE_ALL_GCS
 113 #if INCLUDE_JVMCI
 114 #include "jvmci/jvmciCompiler.hpp"
 115 #include "jvmci/jvmciRuntime.hpp"
 116 #include "logging/logHandle.hpp"
 117 #endif
 118 #ifdef COMPILER1
 119 #include "c1/c1_Compiler.hpp"
 120 #endif
 121 #ifdef COMPILER2
 122 #include "opto/c2compiler.hpp"
 123 #include "opto/idealGraphPrinter.hpp"
 124 #endif
 125 #if INCLUDE_RTM_OPT
 126 #include "runtime/rtmLocking.hpp"


 178                                                          AllocFailStrategy::RETURN_NULL);
 179     void* aligned_addr     = align_up(real_malloc_addr, alignment);
 180     assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
 181            ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
 182            "JavaThread alignment code overflowed allocated storage");
 183     if (aligned_addr != real_malloc_addr) {
 184       log_info(biasedlocking)("Aligned thread " INTPTR_FORMAT " to " INTPTR_FORMAT,
 185                               p2i(real_malloc_addr),
 186                               p2i(aligned_addr));
 187     }
 188     ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
 189     return aligned_addr;
 190   } else {
 191     return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
 192                        : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 193   }
 194 }
 195 
 196 void Thread::operator delete(void* p) {
 197   if (UseBiasedLocking) {
 198     void* real_malloc_addr = ((Thread*) p)->_real_malloc_address;
 199     FreeHeap(real_malloc_addr);
 200   } else {
 201     FreeHeap(p);
 202   }
 203 }
 204 







 205 
 206 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 207 // JavaThread
 208 
 209 
 210 Thread::Thread() {
 211   // stack and get_thread
 212   set_stack_base(NULL);
 213   set_stack_size(0);
 214   set_self_raw_id(0);
 215   set_lgrp_id(-1);
 216   DEBUG_ONLY(clear_suspendible_thread();)
 217 
 218   // allocated data structures
 219   set_osthread(NULL);
 220   set_resource_area(new (mtThread)ResourceArea());
 221   DEBUG_ONLY(_current_resource_mark = NULL;)
 222   set_handle_area(new (mtThread) HandleArea(NULL));
 223   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
 224   set_active_handles(NULL);
 225   set_free_handle_block(NULL);
 226   set_last_handle_mark(NULL);
 227 
 228   // This initial value ==> never claimed.
 229   _oops_do_parity = 0;



 230 
 231   // the handle mark links itself to last_handle_mark
 232   new HandleMark(this);
 233 
 234   // plain initialization
 235   debug_only(_owned_locks = NULL;)
 236   debug_only(_allow_allocation_count = 0;)
 237   NOT_PRODUCT(_allow_safepoint_count = 0;)
 238   NOT_PRODUCT(_skip_gcalot = false;)
 239   _jvmti_env_iteration_count = 0;
 240   set_allocated_bytes(0);
 241   _vm_operation_started_count = 0;
 242   _vm_operation_completed_count = 0;
 243   _current_pending_monitor = NULL;
 244   _current_pending_monitor_is_from_java = true;
 245   _current_waiting_monitor = NULL;
 246   _num_nested_signal = 0;
 247   omFreeList = NULL;
 248   omFreeCount = 0;
 249   omFreeProvision = 32;


 381   _SR_lock = NULL;
 382 
 383   // osthread() can be NULL, if creation of thread failed.
 384   if (osthread() != NULL) os::free_thread(osthread());
 385 
 386   // clear Thread::current if thread is deleting itself.
 387   // Needed to ensure JNI correctly detects non-attached threads.
 388   if (this == Thread::current()) {
 389     clear_thread_current();
 390   }
 391 
 392   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 393 }
 394 
 395 // NOTE: dummy function for assertion purpose.
 396 void Thread::run() {
 397   ShouldNotReachHere();
 398 }
 399 
 400 #ifdef ASSERT
 401 // Private method to check for dangling thread pointer
 402 void check_for_dangling_thread_pointer(Thread *thread) {
 403   assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),






 404          "possibility of dangling Thread pointer");
 405 }
 406 #endif
 407 
 408 ThreadPriority Thread::get_priority(const Thread* const thread) {
 409   ThreadPriority priority;
 410   // Can return an error!
 411   (void)os::get_priority(thread, priority);
 412   assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
 413   return priority;
 414 }
 415 
 416 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
 417   debug_only(check_for_dangling_thread_pointer(thread);)
 418   // Can return an error!
 419   (void)os::set_priority(thread, priority);
 420 }
 421 
 422 
 423 void Thread::start(Thread* thread) {


 715 
 716     if (!pending) {
 717       // A cancelled suspend request is the only false return from
 718       // is_ext_suspend_completed() that keeps us from staying in the
 719       // retry loop.
 720       *bits |= 0x00080000;
 721       return false;
 722     }
 723 
 724     if (is_suspended) {
 725       *bits |= 0x00100000;
 726       return true;
 727     }
 728   } // end retry loop
 729 
 730   // thread did not suspend after all our retries
 731   *bits |= 0x00200000;
 732   return false;
 733 }
 734 































 735 #ifndef PRODUCT
 736 void JavaThread::record_jump(address target, address instr, const char* file,
 737                              int line) {
 738 
 739   // This should not need to be atomic as the only way for simultaneous
 740   // updates is via interrupts. Even then this should be rare or non-existent
 741   // and we don't care that much anyway.
 742 
 743   int index = _jmp_ring_index;
 744   _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
 745   _jmp_ring[index]._target = (intptr_t) target;
 746   _jmp_ring[index]._instruction = (intptr_t) instr;
 747   _jmp_ring[index]._file = file;
 748   _jmp_ring[index]._line = line;
 749 }
 750 #endif // PRODUCT
 751 
 752 void Thread::interrupt(Thread* thread) {
 753   debug_only(check_for_dangling_thread_pointer(thread);)
 754   os::interrupt(thread);


 793 void Thread::metadata_handles_do(void f(Metadata*)) {
 794   // Only walk the Handles in Thread.
 795   if (metadata_handles() != NULL) {
 796     for (int i = 0; i< metadata_handles()->length(); i++) {
 797       f(metadata_handles()->at(i));
 798     }
 799   }
 800 }
 801 
 802 void Thread::print_on(outputStream* st) const {
 803   // get_priority assumes osthread initialized
 804   if (osthread() != NULL) {
 805     int os_prio;
 806     if (os::get_native_priority(this, &os_prio) == OS_OK) {
 807       st->print("os_prio=%d ", os_prio);
 808     }
 809     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
 810     ext().print_on(st);
 811     osthread()->print_on(st);
 812   }







 813   debug_only(if (WizardMode) print_owned_locks_on(st);)
 814 }
 815 

















 816 // Thread::print_on_error() is called by fatal error handler. Don't use
 817 // any lock or allocate memory.
 818 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
 819   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
 820 
 821   if (is_VM_thread())                 { st->print("VMThread"); }
 822   else if (is_GC_task_thread())       { st->print("GCTaskThread"); }
 823   else if (is_Watcher_thread())       { st->print("WatcherThread"); }
 824   else if (is_ConcurrentGC_thread())  { st->print("ConcurrentGCThread"); }
 825   else                                { st->print("Thread"); }
 826 
 827   if (is_Named_thread()) {
 828     st->print(" \"%s\"", name());
 829   }
 830 
 831   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
 832             p2i(stack_end()), p2i(stack_base()));
 833 
 834   if (osthread()) {
 835     st->print(" [id=%d]", osthread()->thread_id());
 836   }







 837 }
 838 
 839 void Thread::print_value_on(outputStream* st) const {
 840   if (is_Named_thread()) {
 841     st->print(" \"%s\" ", name());
 842   }
 843   st->print(INTPTR_FORMAT, p2i(this));   // print address
 844 }
 845 
 846 #ifdef ASSERT
 847 void Thread::print_owned_locks_on(outputStream* st) const {
 848   Monitor *cur = _owned_locks;
 849   if (cur == NULL) {
 850     st->print(" (no locks) ");
 851   } else {
 852     st->print_cr(" Locks owned:");
 853     while (cur) {
 854       cur->print_on(st);
 855       cur = cur->next();
 856     }
 857   }
 858 }
 859 
 860 static int ref_use_count  = 0;
 861 
 862 bool Thread::owns_locks_but_compiled_lock() const {
 863   for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 864     if (cur != Compile_lock) return true;
 865   }
 866   return false;
 867 }
 868 
 869 
 870 #endif
 871 
 872 #ifndef PRODUCT
 873 
 874 // The flag: potential_vm_operation notifies if this particular safepoint state could potential
 875 // invoke the vm-thread (i.e., and oop allocation). In that case, we also have to make sure that
 876 // no threads which allow_vm_block's are held
 877 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
 878   // Check if current thread is allowed to block at a safepoint
 879   if (!(_allow_safepoint_count == 0)) {
 880     fatal("Possible safepoint reached by thread that does not allow it");
 881   }
 882   if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
 883     fatal("LEAF method calling lock?");
 884   }
 885 
 886 #ifdef ASSERT
 887   if (potential_vm_operation && is_Java_thread()
 888       && !Universe::is_bootstrapping()) {
 889     // Make sure we do not hold any locks that the VM thread also uses.
 890     // This could potentially lead to deadlocks
 891     for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 892       // Threads_lock is special, since the safepoint synchronization will not start before this is
 893       // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
 894       // since it is used to transfer control between JavaThreads and the VMThread
 895       // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!


1381 
1382 void WatcherThread::print_on(outputStream* st) const {
1383   st->print("\"%s\" ", name());
1384   Thread::print_on(st);
1385   st->cr();
1386 }
1387 
1388 // ======= JavaThread ========
1389 
1390 #if INCLUDE_JVMCI
1391 
1392 jlong* JavaThread::_jvmci_old_thread_counters;
1393 
1394 bool jvmci_counters_include(JavaThread* thread) {
1395   oop threadObj = thread->threadObj();
1396   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1397 }
1398 
1399 void JavaThread::collect_counters(typeArrayOop array) {
1400   if (JVMCICounterSize > 0) {


1401     MutexLocker tl(Threads_lock);

1402     for (int i = 0; i < array->length(); i++) {
1403       array->long_at_put(i, _jvmci_old_thread_counters[i]);
1404     }
1405     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
1406       if (jvmci_counters_include(tp)) {
1407         for (int i = 0; i < array->length(); i++) {
1408           array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
1409         }
1410       }
1411     }
1412   }
1413 }
1414 
1415 #endif // INCLUDE_JVMCI
1416 
1417 // A JavaThread is a normal Java thread
1418 
1419 void JavaThread::initialize() {
1420   // Initialize fields
1421 
1422   set_saved_exception_pc(NULL);
1423   set_threadObj(NULL);
1424   _anchor.clear();
1425   set_entry_point(NULL);
1426   set_jni_functions(jni_functions());
1427   set_callee_target(NULL);
1428   set_vm_result(NULL);
1429   set_vm_result_2(NULL);
1430   set_vframe_array_head(NULL);
1431   set_vframe_array_last(NULL);
1432   set_deferred_locals(NULL);
1433   set_deopt_mark(NULL);
1434   set_deopt_compiled_method(NULL);
1435   clear_must_deopt_id();
1436   set_monitor_chunks(NULL);
1437   set_next(NULL);

1438   set_thread_state(_thread_new);
1439   _terminated = _not_terminated;
1440   _privileged_stack_top = NULL;
1441   _array_for_gc = NULL;
1442   _suspend_equivalent = false;
1443   _in_deopt_handler = 0;
1444   _doing_unsafe_access = false;
1445   _stack_guard_state = stack_guard_unused;
1446 #if INCLUDE_JVMCI
1447   _pending_monitorenter = false;
1448   _pending_deoptimization = -1;
1449   _pending_failed_speculation = NULL;
1450   _pending_transfer_to_interpreter = false;
1451   _adjusting_comp_level = false;
1452   _jvmci._alternate_call_target = NULL;
1453   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1454   if (JVMCICounterSize > 0) {
1455     _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1456     memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1457   } else {


1698 void JavaThread::thread_main_inner() {
1699   assert(JavaThread::current() == this, "sanity check");
1700   assert(this->threadObj() != NULL, "just checking");
1701 
1702   // Execute thread entry point unless this thread has a pending exception
1703   // or has been stopped before starting.
1704   // Note: Due to JVM_StopThread we can have pending exceptions already!
1705   if (!this->has_pending_exception() &&
1706       !java_lang_Thread::is_stillborn(this->threadObj())) {
1707     {
1708       ResourceMark rm(this);
1709       this->set_native_thread_name(this->get_thread_name());
1710     }
1711     HandleMark hm(this);
1712     this->entry_point()(this, this);
1713   }
1714 
1715   DTRACE_THREAD_PROBE(stop, this);
1716 
1717   this->exit(false);
1718   delete this;
1719 }
1720 
1721 
1722 static void ensure_join(JavaThread* thread) {
1723   // We do not need to grap the Threads_lock, since we are operating on ourself.
1724   Handle threadObj(thread, thread->threadObj());
1725   assert(threadObj.not_null(), "java thread object must exist");
1726   ObjectLocker lock(threadObj, thread);
1727   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1728   thread->clear_pending_exception();
1729   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1730   java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1731   // Clear the native thread instance - this makes isAlive return false and allows the join()
1732   // to complete once we've done the notify_all below
1733   java_lang_Thread::set_thread(threadObj(), NULL);
1734   lock.notify_all(thread);
1735   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1736   thread->clear_pending_exception();
1737 }
1738 
1739 
1740 // For any new cleanup additions, please check to see if they need to be applied to
1741 // cleanup_failed_attach_current_thread as well.
1742 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1743   assert(this == JavaThread::current(), "thread consistency check");
1744 









1745   HandleMark hm(this);
1746   Handle uncaught_exception(this, this->pending_exception());
1747   this->clear_pending_exception();
1748   Handle threadObj(this, this->threadObj());
1749   assert(threadObj.not_null(), "Java thread object should be created");
1750 
1751   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1752   {
1753     EXCEPTION_MARK;
1754 
1755     CLEAR_PENDING_EXCEPTION;
1756   }
1757   if (!destroy_vm) {
1758     if (uncaught_exception.not_null()) {
1759       EXCEPTION_MARK;
1760       // Call method Thread.dispatchUncaughtException().
1761       Klass* thread_klass = SystemDictionary::Thread_klass();
1762       JavaValue result(T_VOID);
1763       JavaCalls::call_virtual(&result,
1764                               threadObj, thread_klass,


1824         // Implied else:
1825         // Things get a little tricky here. We have a pending external
1826         // suspend request, but we are holding the SR_lock so we
1827         // can't just self-suspend. So we temporarily drop the lock
1828         // and then self-suspend.
1829       }
1830 
1831       ThreadBlockInVM tbivm(this);
1832       java_suspend_self();
1833 
1834       // We're done with this suspend request, but we have to loop around
1835       // and check again. Eventually we will get SR_lock without a pending
1836       // external suspend request and will be able to mark ourselves as
1837       // exiting.
1838     }
1839     // no more external suspends are allowed at this point
1840   } else {
1841     // before_exit() has already posted JVMTI THREAD_END events
1842   }
1843 




1844   // Notify waiters on thread object. This has to be done after exit() is called
1845   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1846   // group should have the destroyed bit set before waiters are notified).
1847   ensure_join(this);
1848   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1849 




1850   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1851   // held by this thread must be released. The spec does not distinguish
1852   // between JNI-acquired and regular Java monitors. We can only see
1853   // regular Java monitors here if monitor enter-exit matching is broken.
1854   //
1855   // Optionally release any monitors for regular JavaThread exits. This
1856   // is provided as a work around for any bugs in monitor enter-exit
1857   // matching. This can be expensive so it is not enabled by default.
1858   //
1859   // ensure_join() ignores IllegalThreadStateExceptions, and so does
1860   // ObjectSynchronizer::release_monitors_owned_by_thread().
1861   if (exit_type == jni_detach || ObjectMonitor::Knob_ExitRelease) {
1862     // Sanity check even though JNI DetachCurrentThread() would have
1863     // returned JNI_ERR if there was a Java frame. JavaThread exit
1864     // should be done executing Java code by the time we get here.
1865     assert(!this->has_last_Java_frame(),
1866            "should not have a Java frame when detaching or exiting");
1867     ObjectSynchronizer::release_monitors_owned_by_thread(this);
1868     assert(!this->has_pending_exception(), "release_monitors should have cleared");
1869   }


1897 
1898   // We must flush any deferred card marks before removing a thread from
1899   // the list of active threads.
1900   Universe::heap()->flush_deferred_store_barrier(this);
1901   assert(deferred_card_mark().is_empty(), "Should have been flushed");
1902 
1903 #if INCLUDE_ALL_GCS
1904   // We must flush the G1-related buffers before removing a thread
1905   // from the list of active threads. We must do this after any deferred
1906   // card marks have been flushed (above) so that any entries that are
1907   // added to the thread's dirty card queue as a result are not lost.
1908   if (UseG1GC) {
1909     flush_barrier_queues();
1910   }
1911 #endif // INCLUDE_ALL_GCS
1912 
1913   log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
1914     exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
1915     os::current_thread_id());
1916 




1917   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1918   Threads::remove(this);
1919 
1920   // If someone set a handshake on us just as we entered exit path, we simple cancel it.
1921   if (ThreadLocalHandshakes) {
1922     cancel_handshake();










1923   }
1924 }
1925 
1926 #if INCLUDE_ALL_GCS
1927 // Flush G1-related queues.
1928 void JavaThread::flush_barrier_queues() {
1929   satb_mark_queue().flush();
1930   dirty_card_queue().flush();
1931 }
1932 
1933 void JavaThread::initialize_queues() {
1934   assert(!SafepointSynchronize::is_at_safepoint(),
1935          "we should not be at a safepoint");
1936 
1937   SATBMarkQueue& satb_queue = satb_mark_queue();
1938   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1939   // The SATB queue should have been constructed with its active
1940   // field set to false.
1941   assert(!satb_queue.is_active(), "SATB queue should not be active");
1942   assert(satb_queue.is_empty(), "SATB queue should be empty");


1963   if (free_handle_block() != NULL) {
1964     JNIHandleBlock* block = free_handle_block();
1965     set_free_handle_block(NULL);
1966     JNIHandleBlock::release_block(block);
1967   }
1968 
1969   // These have to be removed while this is still a valid thread.
1970   remove_stack_guard_pages();
1971 
1972   if (UseTLAB) {
1973     tlab().make_parsable(true);  // retire TLAB, if any
1974   }
1975 
1976 #if INCLUDE_ALL_GCS
1977   if (UseG1GC) {
1978     flush_barrier_queues();
1979   }
1980 #endif // INCLUDE_ALL_GCS
1981 
1982   Threads::remove(this);
1983   delete this;
1984 }
1985 
1986 
1987 
1988 
1989 JavaThread* JavaThread::active() {
1990   Thread* thread = Thread::current();
1991   if (thread->is_Java_thread()) {
1992     return (JavaThread*) thread;
1993   } else {
1994     assert(thread->is_VM_thread(), "this must be a vm thread");
1995     VM_Operation* op = ((VMThread*) thread)->vm_operation();
1996     JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1997     assert(ret->is_Java_thread(), "must be a Java thread");
1998     return ret;
1999   }
2000 }
2001 
2002 bool JavaThread::is_lock_owned(address adr) const {
2003   if (Thread::is_lock_owned(adr)) return true;


2218   }
2219 
2220 
2221   // Interrupt thread so it will wake up from a potential wait()
2222   Thread::interrupt(this);
2223 }
2224 
2225 // External suspension mechanism.
2226 //
2227 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2228 // to any VM_locks and it is at a transition
2229 // Self-suspension will happen on the transition out of the vm.
2230 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2231 //
2232 // Guarantees on return:
2233 //   + Target thread will not execute any new bytecode (that's why we need to
2234 //     force a safepoint)
2235 //   + Target thread will not enter any new monitors
2236 //
2237 void JavaThread::java_suspend() {
2238   { MutexLocker mu(Threads_lock);
2239     if (!Threads::includes(this) || is_exiting() || this->threadObj() == NULL) {
2240       return;
2241     }
2242   }
2243 
2244   { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2245     if (!is_external_suspend()) {
2246       // a racing resume has cancelled us; bail out now
2247       return;
2248     }
2249 
2250     // suspend is done
2251     uint32_t debug_bits = 0;
2252     // Warning: is_ext_suspend_completed() may temporarily drop the
2253     // SR_lock to allow the thread to reach a stable thread state if
2254     // it is currently in a transient thread state.
2255     if (is_ext_suspend_completed(false /* !called_by_wait */,
2256                                  SuspendRetryDelay, &debug_bits)) {
2257       return;
2258     }
2259   }
2260 
2261   VM_ThreadSuspend vm_suspend;
2262   VMThread::execute(&vm_suspend);


2310   // it. This would be a "bad thing (TM)" and cause the stack walker
2311   // to crash. We stay self-suspended until there are no more pending
2312   // external suspend requests.
2313   while (is_external_suspend()) {
2314     ret++;
2315     this->set_ext_suspended();
2316 
2317     // _ext_suspended flag is cleared by java_resume()
2318     while (is_ext_suspended()) {
2319       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2320     }
2321   }
2322 
2323   return ret;
2324 }
2325 
2326 #ifdef ASSERT
2327 // verify the JavaThread has not yet been published in the Threads::list, and
2328 // hence doesn't need protection from concurrent access at this stage
2329 void JavaThread::verify_not_published() {
2330   if (!Threads_lock->owned_by_self()) {
2331     MutexLockerEx ml(Threads_lock,  Mutex::_no_safepoint_check_flag);
2332     assert(!Threads::includes(this),
2333            "java thread shouldn't have been published yet!");
2334   } else {
2335     assert(!Threads::includes(this),
2336            "java thread shouldn't have been published yet!");
2337   }
2338 }
2339 #endif
2340 
2341 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2342 // progress or when _suspend_flags is non-zero.
2343 // Current thread needs to self-suspend if there is a suspend request and/or
2344 // block if a safepoint is in progress.
2345 // Async exception ISN'T checked.
2346 // Note only the ThreadInVMfromNative transition can call this function
2347 // directly and when thread state is _thread_in_native_trans
2348 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2349   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2350 
2351   JavaThread *curJT = JavaThread::current();
2352   bool do_self_suspend = thread->is_external_suspend();
2353 
2354   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2355 
2356   // If JNIEnv proxies are allowed, don't self-suspend if the target
2357   // thread is not the current thread. In older versions of jdbx, jdbx


2434   check_special_condition_for_native_trans(thread);
2435 
2436   // Finish the transition
2437   thread->set_thread_state(_thread_in_Java);
2438 
2439   if (thread->do_critical_native_unlock()) {
2440     ThreadInVMfromJavaNoAsyncException tiv(thread);
2441     GCLocker::unlock_critical(thread);
2442     thread->clear_critical_native_unlock();
2443   }
2444 }
2445 
2446 // We need to guarantee the Threads_lock here, since resumes are not
2447 // allowed during safepoint synchronization
2448 // Can only resume from an external suspension
2449 void JavaThread::java_resume() {
2450   assert_locked_or_safepoint(Threads_lock);
2451 
2452   // Sanity check: thread is gone, has started exiting or the thread
2453   // was not externally suspended.
2454   if (!Threads::includes(this) || is_exiting() || !is_external_suspend()) {

2455     return;
2456   }
2457 
2458   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2459 
2460   clear_external_suspend();
2461 
2462   if (is_ext_suspended()) {
2463     clear_ext_suspended();
2464     SR_lock()->notify_all();
2465   }
2466 }
2467 
2468 size_t JavaThread::_stack_red_zone_size = 0;
2469 size_t JavaThread::_stack_yellow_zone_size = 0;
2470 size_t JavaThread::_stack_reserved_zone_size = 0;
2471 size_t JavaThread::_stack_shadow_zone_size = 0;
2472 
2473 void JavaThread::create_stack_guard_pages() {
2474   if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }


2902 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
2903   st->print("%s", get_thread_name_string(buf, buflen));
2904 }
2905 
2906 // Called by fatal error handler. The difference between this and
2907 // JavaThread::print() is that we can't grab lock or allocate memory.
2908 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
2909   st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
2910   oop thread_obj = threadObj();
2911   if (thread_obj != NULL) {
2912     if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
2913   }
2914   st->print(" [");
2915   st->print("%s", _get_thread_state_name(_thread_state));
2916   if (osthread()) {
2917     st->print(", id=%d", osthread()->thread_id());
2918   }
2919   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
2920             p2i(stack_end()), p2i(stack_base()));
2921   st->print("]");







2922   return;
2923 }
2924 
2925 // Verification
2926 
2927 static void frame_verify(frame* f, const RegisterMap *map) { f->verify(map); }
2928 
2929 void JavaThread::verify() {
2930   // Verify oops in the thread.
2931   oops_do(&VerifyOopClosure::verify_oop, NULL);
2932 
2933   // Verify the stack frames.
2934   frames_do(frame_verify);
2935 }
2936 
2937 // CR 6300358 (sub-CR 2137150)
2938 // Most callers of this method assume that it can't return NULL but a
2939 // thread may not have a name whilst it is in the process of attaching to
2940 // the VM - see CR 6412693, and there are places where a JavaThread can be
2941 // seen prior to having it's threadObj set (eg JNI attaching threads and


3295     // process it here to make sure it isn't unloaded in the middle of
3296     // a scan.
3297     cf->do_code_blob(_scanned_compiled_method);
3298   }
3299 }
3300 
3301 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3302   JavaThread::nmethods_do(cf);
3303   if (_scanned_compiled_method != NULL && cf != NULL) {
3304     // Safepoints can occur when the sweeper is scanning an nmethod so
3305     // process it here to make sure it isn't unloaded in the middle of
3306     // a scan.
3307     cf->do_code_blob(_scanned_compiled_method);
3308   }
3309 }
3310 
3311 
3312 // ======= Threads ========
3313 
3314 // The Threads class links together all active threads, and provides
3315 // operations over all threads.  It is protected by its own Mutex
3316 // lock, which is also used in other contexts to protect thread
3317 // operations from having the thread being operated on from exiting
3318 // and going away unexpectedly (e.g., safepoint synchronization)





3319 
3320 JavaThread* Threads::_thread_list = NULL;
3321 int         Threads::_number_of_threads = 0;
3322 int         Threads::_number_of_non_daemon_threads = 0;
3323 int         Threads::_return_code = 0;
3324 int         Threads::_thread_claim_parity = 0;
3325 size_t      JavaThread::_stack_size_at_create = 0;

























































































3326 #ifdef ASSERT
3327 bool        Threads::_vm_complete = false;
3328 #endif
3329 



















3330 // All JavaThreads
3331 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next())
3332 
3333 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3334 void Threads::threads_do(ThreadClosure* tc) {
3335   assert_locked_or_safepoint(Threads_lock);
3336   // ALL_JAVA_THREADS iterates through all JavaThreads
3337   ALL_JAVA_THREADS(p) {
3338     tc->do_thread(p);
3339   }
3340   // Someday we could have a table or list of all non-JavaThreads.
3341   // For now, just manually iterate through them.
3342   tc->do_thread(VMThread::vm_thread());
3343   Universe::heap()->gc_threads_do(tc);
3344   WatcherThread *wt = WatcherThread::watcher_thread();
3345   // Strictly speaking, the following NULL check isn't sufficient to make sure
3346   // the data for WatcherThread is still valid upon being examined. However,
3347   // considering that WatchThread terminates when the VM is on the way to
3348   // exit at safepoint, the chance of the above is extremely small. The right
3349   // way to prevent termination of WatcherThread would be to acquire
3350   // Terminator_lock, but we can't do that without violating the lock rank
3351   // checking in some cases.


3412   if (result.get_jint() != JNI_OK) {
3413     vm_exit_during_initialization(); // no message or exception
3414   }
3415 
3416   universe_post_module_init();
3417 }
3418 
3419 // Phase 3. final setup - set security manager, system class loader and TCCL
3420 //
3421 //     This will instantiate and set the security manager, set the system class
3422 //     loader as well as the thread context class loader.  The security manager
3423 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
3424 //     other modules or the application's classpath.
3425 static void call_initPhase3(TRAPS) {
3426   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3427   JavaValue result(T_VOID);
3428   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3429                                          vmSymbols::void_method_signature(), CHECK);
3430 }
3431 
















































































































































































































3432 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3433   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3434 
3435   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3436     create_vm_init_libraries();
3437   }
3438 
3439   initialize_class(vmSymbols::java_lang_String(), CHECK);
3440 
3441   // Inject CompactStrings value after the static initializers for String ran.
3442   java_lang_String::set_compact_strings(CompactStrings);
3443 
3444   // Initialize java_lang.System (needed before creating the thread)
3445   initialize_class(vmSymbols::java_lang_System(), CHECK);
3446   // The VM creates & returns objects of this class. Make sure it's initialized.
3447   initialize_class(vmSymbols::java_lang_Class(), CHECK);
3448   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3449   Handle thread_group = create_initial_thread_group(CHECK);
3450   Universe::set_main_thread_group(thread_group());
3451   initialize_class(vmSymbols::java_lang_Thread(), CHECK);


3593 #if INCLUDE_JVMCI
3594   if (JVMCICounterSize > 0) {
3595     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3596     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3597   } else {
3598     JavaThread::_jvmci_old_thread_counters = NULL;
3599   }
3600 #endif // INCLUDE_JVMCI
3601 
3602   // Attach the main thread to this os thread
3603   JavaThread* main_thread = new JavaThread();
3604   main_thread->set_thread_state(_thread_in_vm);
3605   main_thread->initialize_thread_current();
3606   // must do this before set_active_handles
3607   main_thread->record_stack_base_and_size();
3608   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3609 
3610   if (!main_thread->set_as_starting_thread()) {
3611     vm_shutdown_during_initialization(
3612                                       "Failed necessary internal allocation. Out of swap space");
3613     delete main_thread;
3614     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3615     return JNI_ENOMEM;
3616   }
3617 
3618   // Enable guard page *after* os::create_main_thread(), otherwise it would
3619   // crash Linux VM, see notes in os_linux.cpp.
3620   main_thread->create_stack_guard_pages();
3621 
3622   // Initialize Java-Level synchronization subsystem
3623   ObjectMonitor::Initialize();
3624 
3625   // Initialize global modules
3626   jint status = init_globals();
3627   if (status != JNI_OK) {
3628     delete main_thread;
3629     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3630     return status;
3631   }
3632 
3633   if (TRACE_INITIALIZE() != JNI_OK) {
3634     vm_exit_during_initialization("Failed to initialize tracing backend");
3635   }
3636 
3637   // Should be done after the heap is fully created
3638   main_thread->cache_global_variables();
3639 
3640   HandleMark hm;
3641 
3642   { MutexLocker mu(Threads_lock);
3643     Threads::add(main_thread);
3644   }
3645 
3646   // Any JVMTI raw monitors entered in onload will transition into
3647   // real raw monitor. VM is setup enough here for raw monitor enter.
3648   JvmtiExport::transition_pending_onload_raw_monitors();


4014   AgentLibrary* agent;
4015 
4016   for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
4017     OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
4018 
4019     if (on_load_entry != NULL) {
4020       // Invoke the JVM_OnLoad function
4021       JavaThread* thread = JavaThread::current();
4022       ThreadToNativeFromVM ttn(thread);
4023       HandleMark hm(thread);
4024       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4025       if (err != JNI_OK) {
4026         vm_exit_during_initialization("-Xrun library failed to init", agent->name());
4027       }
4028     } else {
4029       vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
4030     }
4031   }
4032 }
4033 
4034 JavaThread* Threads::find_java_thread_from_java_tid(jlong java_tid) {
4035   assert(Threads_lock->owned_by_self(), "Must hold Threads_lock");
4036 
4037   JavaThread* java_thread = NULL;
4038   // Sequential search for now.  Need to do better optimization later.
4039   for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) {
4040     oop tobj = thread->threadObj();
4041     if (!thread->is_exiting() &&
4042         tobj != NULL &&
4043         java_tid == java_lang_Thread::thread_id(tobj)) {
4044       java_thread = thread;
4045       break;
4046     }
4047   }
4048   return java_thread;
4049 }
4050 
4051 
4052 // Last thread running calls java.lang.Shutdown.shutdown()
4053 void JavaThread::invoke_shutdown_hooks() {
4054   HandleMark hm(this);
4055 
4056   // We could get here with a pending exception, if so clear it now.
4057   if (this->has_pending_exception()) {
4058     this->clear_pending_exception();
4059   }
4060 
4061   EXCEPTION_MARK;
4062   Klass* shutdown_klass =
4063     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4064                                       THREAD);
4065   if (shutdown_klass != NULL) {
4066     // SystemDictionary::resolve_or_null will return null if there was
4067     // an exception.  If we cannot load the Shutdown class, just don't
4068     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4069     // and finalizers (if runFinalizersOnExit is set) won't be run.
4070     // Note that if a shutdown hook was registered or runFinalizersOnExit
4071     // was called, the Shutdown class would have already been loaded


4156     VMThread::wait_for_vm_thread_exit();
4157     assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4158     VMThread::destroy();
4159   }
4160 
4161   // clean up ideal graph printers
4162 #if defined(COMPILER2) && !defined(PRODUCT)
4163   IdealGraphPrinter::clean_up();
4164 #endif
4165 
4166   // Now, all Java threads are gone except daemon threads. Daemon threads
4167   // running Java code or in VM are stopped by the Safepoint. However,
4168   // daemon threads executing native code are still running.  But they
4169   // will be stopped at native=>Java/VM barriers. Note that we can't
4170   // simply kill or suspend them, as it is inherently deadlock-prone.
4171 
4172   VM_Exit::set_vm_exited();
4173 
4174   notify_vm_shutdown();
4175 





4176   delete thread;
4177 
4178 #if INCLUDE_JVMCI
4179   if (JVMCICounterSize > 0) {
4180     FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
4181   }
4182 #endif
4183 
4184   // exit_globals() will delete tty
4185   exit_globals();
4186 
4187   LogConfiguration::finalize();
4188 
4189   return true;
4190 }
4191 
4192 
4193 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4194   if (version == JNI_VERSION_1_1) return JNI_TRUE;
4195   return is_supported_jni_version(version);
4196 }
4197 
4198 
4199 jboolean Threads::is_supported_jni_version(jint version) {
4200   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4201   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4202   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4203   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4204   if (version == JNI_VERSION_9) return JNI_TRUE;
4205   if (version == JNI_VERSION_10) return JNI_TRUE;
4206   return JNI_FALSE;
4207 }
4208 










4209 




































































































































































































































































































































































































































































































4210 void Threads::add(JavaThread* p, bool force_daemon) {
4211   // The threads lock must be owned at this point
4212   assert_locked_or_safepoint(Threads_lock);
4213 
4214   // See the comment for this method in thread.hpp for its purpose and
4215   // why it is called here.
4216   p->initialize_queues();
4217   p->set_next(_thread_list);
4218   _thread_list = p;





4219   _number_of_threads++;
4220   oop threadObj = p->threadObj();
4221   bool daemon = true;
4222   // Bootstrapping problem: threadObj can be null for initial
4223   // JavaThread (or for threads attached via JNI)
4224   if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4225     _number_of_non_daemon_threads++;
4226     daemon = false;
4227   }
4228 
4229   ThreadService::add_thread(p, daemon);
4230 














4231   // Possible GC point.
4232   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4233 }
4234 
4235 void Threads::remove(JavaThread* p) {
4236 
4237   // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4238   ObjectSynchronizer::omFlush(p);
4239 
4240   // Extra scope needed for Thread_lock, so we can check
4241   // that we do not remove thread without safepoint code notice
4242   { MutexLocker ml(Threads_lock);
4243 
4244     assert(includes(p), "p must be present");
4245 













4246     JavaThread* current = _thread_list;
4247     JavaThread* prev    = NULL;
4248 
4249     while (current != p) {
4250       prev    = current;
4251       current = current->next();
4252     }
4253 
4254     if (prev) {
4255       prev->set_next(current->next());
4256     } else {
4257       _thread_list = p->next();
4258     }

4259     _number_of_threads--;
4260     oop threadObj = p->threadObj();
4261     bool daemon = true;
4262     if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
4263       _number_of_non_daemon_threads--;
4264       daemon = false;
4265 
4266       // Only one thread left, do a notify on the Threads_lock so a thread waiting
4267       // on destroy_vm will wake up.
4268       if (number_of_non_daemon_threads() == 1) {
4269         Threads_lock->notify_all();
4270       }
4271     }
4272     ThreadService::remove_thread(p, daemon);
4273 
4274     // Make sure that safepoint code disregard this thread. This is needed since
4275     // the thread might mess around with locks after this point. This can cause it
4276     // to do callbacks into the safepoint code. However, the safepoint code is not aware
4277     // of this thread since it is removed from the queue.
4278     p->set_terminated_value();
4279   } // unlock Threads_lock
4280 
4281   // Since Events::log uses a lock, we grab it outside the Threads_lock
4282   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4283 }
4284 
4285 // Threads_lock must be held when this is called (or must be called during a safepoint)
4286 bool Threads::includes(JavaThread* p) {
4287   assert(Threads_lock->is_locked(), "sanity check");
4288   ALL_JAVA_THREADS(q) {
4289     if (q == p) {
4290       return true;
4291     }
4292   }
4293   return false;
4294 }
4295 
4296 // Operations on the Threads list for GC.  These are not explicitly locked,
4297 // but the garbage collector must provide a safe context for them to run.
4298 // In particular, these things should never be called when the Threads_lock
4299 // is held by some other thread. (Note: the Safepoint abstraction also
4300 // uses the Threads_lock to guarantee this property. It also makes sure that
4301 // all threads gets blocked when exiting or starting).
4302 
4303 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
4304   ALL_JAVA_THREADS(p) {
4305     p->oops_do(f, cf);
4306   }
4307   VMThread::vm_thread()->oops_do(f, cf);
4308 }
4309 
4310 void Threads::change_thread_claim_parity() {
4311   // Set the new claim parity.
4312   assert(_thread_claim_parity >= 0 && _thread_claim_parity <= 2,
4313          "Not in range.");
4314   _thread_claim_parity++;
4315   if (_thread_claim_parity == 3) _thread_claim_parity = 1;


4388   ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
4389   virtual void do_thread(Thread* thread) {
4390     thread->metadata_handles_do(_f);
4391   }
4392 };
4393 
4394 void Threads::metadata_handles_do(void f(Metadata*)) {
4395   // Only walk the Handles in Thread.
4396   ThreadHandlesClosure handles_closure(f);
4397   threads_do(&handles_closure);
4398 }
4399 
4400 void Threads::deoptimized_wrt_marked_nmethods() {
4401   ALL_JAVA_THREADS(p) {
4402     p->deoptimized_wrt_marked_nmethods();
4403   }
4404 }
4405 
4406 
4407 // Get count Java threads that are waiting to enter the specified monitor.
4408 GrowableArray<JavaThread*>* Threads::get_pending_threads(int count,
4409                                                          address monitor,
4410                                                          bool doLock) {
4411   assert(doLock || SafepointSynchronize::is_at_safepoint(),
4412          "must grab Threads_lock or be at safepoint");
4413   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
4414 
4415   int i = 0;
4416   {
4417     MutexLockerEx ml(doLock ? Threads_lock : NULL);
4418     ALL_JAVA_THREADS(p) {
4419       if (!p->can_call_java()) continue;
4420 
4421       address pending = (address)p->current_pending_monitor();
4422       if (pending == monitor) {             // found a match
4423         if (i < count) result->append(p);   // save the first count matches
4424         i++;
4425       }
4426     }
4427   }
4428   return result;
4429 }
4430 
4431 
4432 JavaThread *Threads::owning_thread_from_monitor_owner(address owner,
4433                                                       bool doLock) {
4434   assert(doLock ||
4435          Threads_lock->owned_by_self() ||
4436          SafepointSynchronize::is_at_safepoint(),
4437          "must grab Threads_lock or be at safepoint");
4438 
4439   // NULL owner means not locked so we can skip the search
4440   if (owner == NULL) return NULL;
4441 
4442   {
4443     MutexLockerEx ml(doLock ? Threads_lock : NULL);
4444     ALL_JAVA_THREADS(p) {
4445       // first, see if owner is the address of a Java thread
4446       if (owner == (address)p) return p;
4447     }
4448   }
4449   // Cannot assert on lack of success here since this function may be
4450   // used by code that is trying to report useful problem information
4451   // like deadlock detection.
4452   if (UseHeavyMonitors) return NULL;
4453 
4454   // If we didn't find a matching Java thread and we didn't force use of
4455   // heavyweight monitors, then the owner is the stack address of the
4456   // Lock Word in the owning Java thread's stack.
4457   //
4458   JavaThread* the_owner = NULL;
4459   {
4460     MutexLockerEx ml(doLock ? Threads_lock : NULL);
4461     ALL_JAVA_THREADS(q) {
4462       if (q->is_lock_owned(owner)) {
4463         the_owner = q;
4464         break;
4465       }
4466     }
4467   }
4468   // cannot assert on lack of success here; see above comment
4469   return the_owner;
4470 }
4471 
4472 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
4473 void Threads::print_on(outputStream* st, bool print_stacks,
4474                        bool internal_format, bool print_concurrent_locks) {
4475   char buf[32];
4476   st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
4477 
4478   st->print_cr("Full thread dump %s (%s %s):",
4479                Abstract_VM_Version::vm_name(),
4480                Abstract_VM_Version::vm_release(),
4481                Abstract_VM_Version::vm_info_string());
4482   st->cr();
4483 
4484 #if INCLUDE_SERVICES
4485   // Dump concurrent locks
4486   ConcurrentLocksDump concurrent_locks;
4487   if (print_concurrent_locks) {
4488     concurrent_locks.dump_at_safepoint();
4489   }
4490 #endif // INCLUDE_SERVICES
4491 



4492   ALL_JAVA_THREADS(p) {
4493     ResourceMark rm;
4494     p->print_on(st);
4495     if (print_stacks) {
4496       if (internal_format) {
4497         p->trace_stack();
4498       } else {
4499         p->print_stack_on(st);
4500       }
4501     }
4502     st->cr();
4503 #if INCLUDE_SERVICES
4504     if (print_concurrent_locks) {
4505       concurrent_locks.print_locks_on(p, st);
4506     }
4507 #endif // INCLUDE_SERVICES
4508   }
4509 
4510   VMThread::vm_thread()->print_on(st);
4511   st->cr();
4512   Universe::heap()->print_gc_threads_on(st);
4513   WatcherThread* wt = WatcherThread::watcher_thread();
4514   if (wt != NULL) {
4515     wt->print_on(st);
4516     st->cr();
4517   }

4518   st->flush();
4519 }
4520 































































































4521 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
4522                              int buflen, bool* found_current) {
4523   if (this_thread != NULL) {
4524     bool is_current = (current == this_thread);
4525     *found_current = *found_current || is_current;
4526     st->print("%s", is_current ? "=>" : "  ");
4527 
4528     st->print(PTR_FORMAT, p2i(this_thread));
4529     st->print(" ");
4530     this_thread->print_on_error(st, buf, buflen);
4531     st->cr();
4532   }
4533 }
4534 
4535 class PrintOnErrorClosure : public ThreadClosure {
4536   outputStream* _st;
4537   Thread* _current;
4538   char* _buf;
4539   int _buflen;
4540   bool* _found_current;
4541  public:
4542   PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
4543                       int buflen, bool* found_current) :
4544    _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
4545 
4546   virtual void do_thread(Thread* thread) {
4547     Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
4548   }
4549 };
4550 
4551 // Threads::print_on_error() is called by fatal error handler. It's possible
4552 // that VM is not at safepoint and/or current thread is inside signal handler.
4553 // Don't print stack trace, as the stack may not be walkable. Don't allocate
4554 // memory (even in resource area), it might deadlock the error handler.
4555 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
4556                              int buflen) {



4557   bool found_current = false;
4558   st->print_cr("Java Threads: ( => current thread )");
4559   ALL_JAVA_THREADS(thread) {
4560     print_on_error(thread, st, current, buf, buflen, &found_current);
4561   }
4562   st->cr();
4563 
4564   st->print_cr("Other Threads:");
4565   print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
4566   print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
4567 
4568   PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
4569   Universe::heap()->gc_threads_do(&print_closure);
4570 
4571   if (!found_current) {
4572     st->cr();
4573     st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
4574     current->print_on_error(st, buf, buflen);
4575     st->cr();
4576   }
4577   st->cr();

4578   st->print_cr("Threads with active compile tasks:");
4579   print_threads_compiling(st, buf, buflen);
4580 }
4581 
4582 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen) {
4583   ALL_JAVA_THREADS(thread) {
4584     if (thread->is_Compiler_thread()) {
4585       CompilerThread* ct = (CompilerThread*) thread;
4586       if (ct->task() != NULL) {
4587         thread->print_name_on_error(st, buf, buflen);
4588         ct->task()->print(st, NULL, true, true);
4589       }
4590     }
4591   }
4592 }
4593 
4594 
4595 // Internal SpinLock and Mutex
4596 // Based on ParkEvent
4597 




  54 #include "oops/verifyOopClosure.hpp"
  55 #include "prims/jvm_misc.hpp"
  56 #include "prims/jvmtiExport.hpp"
  57 #include "prims/jvmtiThreadState.hpp"
  58 #include "prims/privilegedStack.hpp"
  59 #include "runtime/arguments.hpp"
  60 #include "runtime/atomic.hpp"
  61 #include "runtime/biasedLocking.hpp"
  62 #include "runtime/commandLineFlagConstraintList.hpp"
  63 #include "runtime/commandLineFlagWriteableList.hpp"
  64 #include "runtime/commandLineFlagRangeList.hpp"
  65 #include "runtime/deoptimization.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/globals.hpp"
  68 #include "runtime/handshake.hpp"
  69 #include "runtime/init.hpp"
  70 #include "runtime/interfaceSupport.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/jniPeriodicChecker.hpp"

  74 #include "runtime/memprofiler.hpp"
  75 #include "runtime/mutexLocker.hpp"
  76 #include "runtime/objectMonitor.hpp"
  77 #include "runtime/orderAccess.inline.hpp"
  78 #include "runtime/osThread.hpp"
  79 #include "runtime/prefetch.inline.hpp"
  80 #include "runtime/safepoint.hpp"
  81 #include "runtime/safepointMechanism.inline.hpp"
  82 #include "runtime/sharedRuntime.hpp"
  83 #include "runtime/statSampler.hpp"
  84 #include "runtime/stubRoutines.hpp"
  85 #include "runtime/sweeper.hpp"
  86 #include "runtime/task.hpp"
  87 #include "runtime/thread.inline.hpp"
  88 #include "runtime/threadCritical.hpp"
  89 #include "runtime/threadSMR.inline.hpp"
  90 #include "runtime/timer.hpp"
  91 #include "runtime/timerTrace.hpp"
  92 #include "runtime/vframe.hpp"
  93 #include "runtime/vframeArray.hpp"
  94 #include "runtime/vframe_hp.hpp"
  95 #include "runtime/vmThread.hpp"
  96 #include "runtime/vm_operations.hpp"
  97 #include "runtime/vm_version.hpp"
  98 #include "services/attachListener.hpp"
  99 #include "services/management.hpp"
 100 #include "services/memTracker.hpp"
 101 #include "services/threadService.hpp"
 102 #include "trace/traceMacros.hpp"
 103 #include "trace/tracing.hpp"
 104 #include "utilities/align.hpp"
 105 #include "utilities/defaultStream.hpp"
 106 #include "utilities/dtrace.hpp"
 107 #include "utilities/events.hpp"
 108 #include "utilities/macros.hpp"
 109 #include "utilities/preserveException.hpp"
 110 #include "utilities/resourceHash.hpp"
 111 #include "utilities/vmError.hpp"
 112 #if INCLUDE_ALL_GCS
 113 #include "gc/cms/concurrentMarkSweepThread.hpp"
 114 #include "gc/g1/concurrentMarkThread.inline.hpp"
 115 #include "gc/parallel/pcTasks.hpp"
 116 #endif // INCLUDE_ALL_GCS
 117 #if INCLUDE_JVMCI
 118 #include "jvmci/jvmciCompiler.hpp"
 119 #include "jvmci/jvmciRuntime.hpp"
 120 #include "logging/logHandle.hpp"
 121 #endif
 122 #ifdef COMPILER1
 123 #include "c1/c1_Compiler.hpp"
 124 #endif
 125 #ifdef COMPILER2
 126 #include "opto/c2compiler.hpp"
 127 #include "opto/idealGraphPrinter.hpp"
 128 #endif
 129 #if INCLUDE_RTM_OPT
 130 #include "runtime/rtmLocking.hpp"


 182                                                          AllocFailStrategy::RETURN_NULL);
 183     void* aligned_addr     = align_up(real_malloc_addr, alignment);
 184     assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
 185            ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
 186            "JavaThread alignment code overflowed allocated storage");
 187     if (aligned_addr != real_malloc_addr) {
 188       log_info(biasedlocking)("Aligned thread " INTPTR_FORMAT " to " INTPTR_FORMAT,
 189                               p2i(real_malloc_addr),
 190                               p2i(aligned_addr));
 191     }
 192     ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
 193     return aligned_addr;
 194   } else {
 195     return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
 196                        : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 197   }
 198 }
 199 
 200 void Thread::operator delete(void* p) {
 201   if (UseBiasedLocking) {
 202     FreeHeap(((Thread*) p)->_real_malloc_address);

 203   } else {
 204     FreeHeap(p);
 205   }
 206 }
 207 
 208 void JavaThread::smr_delete() {
 209   if (_on_thread_list) {
 210     Threads::smr_delete(this);
 211   } else {
 212     delete this;
 213   }
 214 }
 215 
 216 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 217 // JavaThread
 218 
 219 
 220 Thread::Thread() {
 221   // stack and get_thread
 222   set_stack_base(NULL);
 223   set_stack_size(0);
 224   set_self_raw_id(0);
 225   set_lgrp_id(-1);
 226   DEBUG_ONLY(clear_suspendible_thread();)
 227 
 228   // allocated data structures
 229   set_osthread(NULL);
 230   set_resource_area(new (mtThread)ResourceArea());
 231   DEBUG_ONLY(_current_resource_mark = NULL;)
 232   set_handle_area(new (mtThread) HandleArea(NULL));
 233   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, true));
 234   set_active_handles(NULL);
 235   set_free_handle_block(NULL);
 236   set_last_handle_mark(NULL);
 237 
 238   // This initial value ==> never claimed.
 239   _oops_do_parity = 0;
 240   _threads_hazard_ptr = NULL;
 241   _nested_threads_hazard_ptr = NULL;
 242   _nested_threads_hazard_ptr_cnt = 0;
 243 
 244   // the handle mark links itself to last_handle_mark
 245   new HandleMark(this);
 246 
 247   // plain initialization
 248   debug_only(_owned_locks = NULL;)
 249   debug_only(_allow_allocation_count = 0;)
 250   NOT_PRODUCT(_allow_safepoint_count = 0;)
 251   NOT_PRODUCT(_skip_gcalot = false;)
 252   _jvmti_env_iteration_count = 0;
 253   set_allocated_bytes(0);
 254   _vm_operation_started_count = 0;
 255   _vm_operation_completed_count = 0;
 256   _current_pending_monitor = NULL;
 257   _current_pending_monitor_is_from_java = true;
 258   _current_waiting_monitor = NULL;
 259   _num_nested_signal = 0;
 260   omFreeList = NULL;
 261   omFreeCount = 0;
 262   omFreeProvision = 32;


 394   _SR_lock = NULL;
 395 
 396   // osthread() can be NULL, if creation of thread failed.
 397   if (osthread() != NULL) os::free_thread(osthread());
 398 
 399   // clear Thread::current if thread is deleting itself.
 400   // Needed to ensure JNI correctly detects non-attached threads.
 401   if (this == Thread::current()) {
 402     clear_thread_current();
 403   }
 404 
 405   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 406 }
 407 
 408 // NOTE: dummy function for assertion purpose.
 409 void Thread::run() {
 410   ShouldNotReachHere();
 411 }
 412 
 413 #ifdef ASSERT
 414 // A JavaThread is considered "dangling" if it is not the current
 415 // thread, has been added the Threads list, the system is not at a
 416 // safepoint and the Thread is not "protected".
 417 //
 418 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
 419   assert(!thread->is_Java_thread() || Thread::current() == thread ||
 420          !((JavaThread *) thread)->on_thread_list() ||
 421          SafepointSynchronize::is_at_safepoint() ||
 422          Threads::is_a_protected_JavaThread_with_lock((JavaThread *) thread),
 423          "possibility of dangling Thread pointer");
 424 }
 425 #endif
 426 
 427 ThreadPriority Thread::get_priority(const Thread* const thread) {
 428   ThreadPriority priority;
 429   // Can return an error!
 430   (void)os::get_priority(thread, priority);
 431   assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
 432   return priority;
 433 }
 434 
 435 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
 436   debug_only(check_for_dangling_thread_pointer(thread);)
 437   // Can return an error!
 438   (void)os::set_priority(thread, priority);
 439 }
 440 
 441 
 442 void Thread::start(Thread* thread) {


 734 
 735     if (!pending) {
 736       // A cancelled suspend request is the only false return from
 737       // is_ext_suspend_completed() that keeps us from staying in the
 738       // retry loop.
 739       *bits |= 0x00080000;
 740       return false;
 741     }
 742 
 743     if (is_suspended) {
 744       *bits |= 0x00100000;
 745       return true;
 746     }
 747   } // end retry loop
 748 
 749   // thread did not suspend after all our retries
 750   *bits |= 0x00200000;
 751   return false;
 752 }
 753 
 754 // Called from API entry points which perform stack walking. If the
 755 // associated JavaThread is the current thread, then wait_for_suspend
 756 // is not used. Otherwise, it determines if we should wait for the
 757 // "other" thread to complete external suspension. (NOTE: in future
 758 // releases the suspension mechanism should be reimplemented so this
 759 // is not necessary.)
 760 //
 761 bool
 762 JavaThread::is_thread_fully_suspended(bool wait_for_suspend, uint32_t *bits) {
 763   if (this != JavaThread::current()) {
 764     // "other" threads require special handling.
 765     if (wait_for_suspend) {
 766       // We are allowed to wait for the external suspend to complete
 767       // so give the other thread a chance to get suspended.
 768       if (!wait_for_ext_suspend_completion(SuspendRetryCount,
 769                                            SuspendRetryDelay, bits)) {
 770         // Didn't make it so let the caller know.
 771         return false;
 772       }
 773     }
 774     // We aren't allowed to wait for the external suspend to complete
 775     // so if the other thread isn't externally suspended we need to
 776     // let the caller know.
 777     else if (!is_ext_suspend_completed_with_lock(bits)) {
 778       return false;
 779     }
 780   }
 781 
 782   return true;
 783 }
 784 
 785 #ifndef PRODUCT
 786 void JavaThread::record_jump(address target, address instr, const char* file,
 787                              int line) {
 788 
 789   // This should not need to be atomic as the only way for simultaneous
 790   // updates is via interrupts. Even then this should be rare or non-existent
 791   // and we don't care that much anyway.
 792 
 793   int index = _jmp_ring_index;
 794   _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
 795   _jmp_ring[index]._target = (intptr_t) target;
 796   _jmp_ring[index]._instruction = (intptr_t) instr;
 797   _jmp_ring[index]._file = file;
 798   _jmp_ring[index]._line = line;
 799 }
 800 #endif // PRODUCT
 801 
 802 void Thread::interrupt(Thread* thread) {
 803   debug_only(check_for_dangling_thread_pointer(thread);)
 804   os::interrupt(thread);


 843 void Thread::metadata_handles_do(void f(Metadata*)) {
 844   // Only walk the Handles in Thread.
 845   if (metadata_handles() != NULL) {
 846     for (int i = 0; i< metadata_handles()->length(); i++) {
 847       f(metadata_handles()->at(i));
 848     }
 849   }
 850 }
 851 
 852 void Thread::print_on(outputStream* st) const {
 853   // get_priority assumes osthread initialized
 854   if (osthread() != NULL) {
 855     int os_prio;
 856     if (os::get_native_priority(this, &os_prio) == OS_OK) {
 857       st->print("os_prio=%d ", os_prio);
 858     }
 859     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
 860     ext().print_on(st);
 861     osthread()->print_on(st);
 862   }
 863   if (_threads_hazard_ptr != NULL) {
 864     st->print("_threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
 865   }
 866   if (_nested_threads_hazard_ptr != NULL) {
 867     print_nested_threads_hazard_ptrs_on(st);
 868   }
 869   st->print(" ");
 870   debug_only(if (WizardMode) print_owned_locks_on(st);)
 871 }
 872 
 873 void Thread::print_nested_threads_hazard_ptrs_on(outputStream* st) const {
 874   assert(_nested_threads_hazard_ptr != NULL, "must be set to print");
 875 
 876   if (EnableThreadSMRStatistics) {
 877     st->print(", _nested_threads_hazard_ptr_cnt=%u", _nested_threads_hazard_ptr_cnt);
 878   }
 879   st->print(", _nested_threads_hazard_ptrs=");
 880   for (NestedThreadsList* node = _nested_threads_hazard_ptr; node != NULL;
 881        node = node->next()) {
 882     if (node != _nested_threads_hazard_ptr) {
 883       // First node does not need a comma-space separator.
 884       st->print(", ");
 885     }
 886     st->print(INTPTR_FORMAT, p2i(node->t_list()));
 887   }
 888 }
 889 
 890 // Thread::print_on_error() is called by fatal error handler. Don't use
 891 // any lock or allocate memory.
 892 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
 893   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
 894 
 895   if (is_VM_thread())                 { st->print("VMThread"); }
 896   else if (is_GC_task_thread())       { st->print("GCTaskThread"); }
 897   else if (is_Watcher_thread())       { st->print("WatcherThread"); }
 898   else if (is_ConcurrentGC_thread())  { st->print("ConcurrentGCThread"); }
 899   else                                { st->print("Thread"); }
 900 
 901   if (is_Named_thread()) {
 902     st->print(" \"%s\"", name());
 903   }
 904 
 905   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
 906             p2i(stack_end()), p2i(stack_base()));
 907 
 908   if (osthread()) {
 909     st->print(" [id=%d]", osthread()->thread_id());
 910   }
 911 
 912   if (_threads_hazard_ptr != NULL) {
 913     st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
 914   }
 915   if (_nested_threads_hazard_ptr != NULL) {
 916     print_nested_threads_hazard_ptrs_on(st);
 917   }
 918 }
 919 
 920 void Thread::print_value_on(outputStream* st) const {
 921   if (is_Named_thread()) {
 922     st->print(" \"%s\" ", name());
 923   }
 924   st->print(INTPTR_FORMAT, p2i(this));   // print address
 925 }
 926 
 927 #ifdef ASSERT
 928 void Thread::print_owned_locks_on(outputStream* st) const {
 929   Monitor *cur = _owned_locks;
 930   if (cur == NULL) {
 931     st->print(" (no locks) ");
 932   } else {
 933     st->print_cr(" Locks owned:");
 934     while (cur) {
 935       cur->print_on(st);
 936       cur = cur->next();
 937     }
 938   }
 939 }
 940 
 941 static int ref_use_count  = 0;
 942 
 943 bool Thread::owns_locks_but_compiled_lock() const {
 944   for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 945     if (cur != Compile_lock) return true;
 946   }
 947   return false;
 948 }
 949 
 950 
 951 #endif
 952 
 953 #ifndef PRODUCT
 954 
 955 // The flag: potential_vm_operation notifies if this particular safepoint state could potentially
 956 // invoke the vm-thread (e.g., an oop allocation). In that case, we also have to make sure that
 957 // no threads which allow_vm_block's are held
 958 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
 959   // Check if current thread is allowed to block at a safepoint
 960   if (!(_allow_safepoint_count == 0)) {
 961     fatal("Possible safepoint reached by thread that does not allow it");
 962   }
 963   if (is_Java_thread() && ((JavaThread*)this)->thread_state() != _thread_in_vm) {
 964     fatal("LEAF method calling lock?");
 965   }
 966 
 967 #ifdef ASSERT
 968   if (potential_vm_operation && is_Java_thread()
 969       && !Universe::is_bootstrapping()) {
 970     // Make sure we do not hold any locks that the VM thread also uses.
 971     // This could potentially lead to deadlocks
 972     for (Monitor *cur = _owned_locks; cur; cur = cur->next()) {
 973       // Threads_lock is special, since the safepoint synchronization will not start before this is
 974       // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
 975       // since it is used to transfer control between JavaThreads and the VMThread
 976       // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!


1462 
1463 void WatcherThread::print_on(outputStream* st) const {
1464   st->print("\"%s\" ", name());
1465   Thread::print_on(st);
1466   st->cr();
1467 }
1468 
1469 // ======= JavaThread ========
1470 
1471 #if INCLUDE_JVMCI
1472 
1473 jlong* JavaThread::_jvmci_old_thread_counters;
1474 
1475 bool jvmci_counters_include(JavaThread* thread) {
1476   oop threadObj = thread->threadObj();
1477   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
1478 }
1479 
1480 void JavaThread::collect_counters(typeArrayOop array) {
1481   if (JVMCICounterSize > 0) {
1482     // dcubed - Looks like the Threads_lock is for stable access
1483     // to _jvmci_old_thread_counters and _jvmci_counters.
1484     MutexLocker tl(Threads_lock);
1485     JavaThreadIteratorWithHandle jtiwh;
1486     for (int i = 0; i < array->length(); i++) {
1487       array->long_at_put(i, _jvmci_old_thread_counters[i]);
1488     }
1489     for (; JavaThread *tp = jtiwh.next(); ) {
1490       if (jvmci_counters_include(tp)) {
1491         for (int i = 0; i < array->length(); i++) {
1492           array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
1493         }
1494       }
1495     }
1496   }
1497 }
1498 
1499 #endif // INCLUDE_JVMCI
1500 
1501 // A JavaThread is a normal Java thread
1502 
1503 void JavaThread::initialize() {
1504   // Initialize fields
1505 
1506   set_saved_exception_pc(NULL);
1507   set_threadObj(NULL);
1508   _anchor.clear();
1509   set_entry_point(NULL);
1510   set_jni_functions(jni_functions());
1511   set_callee_target(NULL);
1512   set_vm_result(NULL);
1513   set_vm_result_2(NULL);
1514   set_vframe_array_head(NULL);
1515   set_vframe_array_last(NULL);
1516   set_deferred_locals(NULL);
1517   set_deopt_mark(NULL);
1518   set_deopt_compiled_method(NULL);
1519   clear_must_deopt_id();
1520   set_monitor_chunks(NULL);
1521   set_next(NULL);
1522   _on_thread_list = false;
1523   set_thread_state(_thread_new);
1524   _terminated = _not_terminated;
1525   _privileged_stack_top = NULL;
1526   _array_for_gc = NULL;
1527   _suspend_equivalent = false;
1528   _in_deopt_handler = 0;
1529   _doing_unsafe_access = false;
1530   _stack_guard_state = stack_guard_unused;
1531 #if INCLUDE_JVMCI
1532   _pending_monitorenter = false;
1533   _pending_deoptimization = -1;
1534   _pending_failed_speculation = NULL;
1535   _pending_transfer_to_interpreter = false;
1536   _adjusting_comp_level = false;
1537   _jvmci._alternate_call_target = NULL;
1538   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1539   if (JVMCICounterSize > 0) {
1540     _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
1541     memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
1542   } else {


1783 void JavaThread::thread_main_inner() {
1784   assert(JavaThread::current() == this, "sanity check");
1785   assert(this->threadObj() != NULL, "just checking");
1786 
1787   // Execute thread entry point unless this thread has a pending exception
1788   // or has been stopped before starting.
1789   // Note: Due to JVM_StopThread we can have pending exceptions already!
1790   if (!this->has_pending_exception() &&
1791       !java_lang_Thread::is_stillborn(this->threadObj())) {
1792     {
1793       ResourceMark rm(this);
1794       this->set_native_thread_name(this->get_thread_name());
1795     }
1796     HandleMark hm(this);
1797     this->entry_point()(this, this);
1798   }
1799 
1800   DTRACE_THREAD_PROBE(stop, this);
1801 
1802   this->exit(false);
1803   this->smr_delete();
1804 }
1805 
1806 
1807 static void ensure_join(JavaThread* thread) {
1808   // We do not need to grab the Threads_lock, since we are operating on ourself.
1809   Handle threadObj(thread, thread->threadObj());
1810   assert(threadObj.not_null(), "java thread object must exist");
1811   ObjectLocker lock(threadObj, thread);
1812   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1813   thread->clear_pending_exception();
1814   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1815   java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1816   // Clear the native thread instance - this makes isAlive return false and allows the join()
1817   // to complete once we've done the notify_all below
1818   java_lang_Thread::set_thread(threadObj(), NULL);
1819   lock.notify_all(thread);
1820   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1821   thread->clear_pending_exception();
1822 }
1823 
1824 
1825 // For any new cleanup additions, please check to see if they need to be applied to
1826 // cleanup_failed_attach_current_thread as well.
1827 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1828   assert(this == JavaThread::current(), "thread consistency check");
1829 
1830   elapsedTimer _timer_exit_phase1;
1831   elapsedTimer _timer_exit_phase2;
1832   elapsedTimer _timer_exit_phase3;
1833   elapsedTimer _timer_exit_phase4;
1834 
1835   if (log_is_enabled(Debug, os, thread, timer)) {
1836     _timer_exit_phase1.start();
1837   }
1838 
1839   HandleMark hm(this);
1840   Handle uncaught_exception(this, this->pending_exception());
1841   this->clear_pending_exception();
1842   Handle threadObj(this, this->threadObj());
1843   assert(threadObj.not_null(), "Java thread object should be created");
1844 
1845   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1846   {
1847     EXCEPTION_MARK;
1848 
1849     CLEAR_PENDING_EXCEPTION;
1850   }
1851   if (!destroy_vm) {
1852     if (uncaught_exception.not_null()) {
1853       EXCEPTION_MARK;
1854       // Call method Thread.dispatchUncaughtException().
1855       Klass* thread_klass = SystemDictionary::Thread_klass();
1856       JavaValue result(T_VOID);
1857       JavaCalls::call_virtual(&result,
1858                               threadObj, thread_klass,


1918         // Implied else:
1919         // Things get a little tricky here. We have a pending external
1920         // suspend request, but we are holding the SR_lock so we
1921         // can't just self-suspend. So we temporarily drop the lock
1922         // and then self-suspend.
1923       }
1924 
1925       ThreadBlockInVM tbivm(this);
1926       java_suspend_self();
1927 
1928       // We're done with this suspend request, but we have to loop around
1929       // and check again. Eventually we will get SR_lock without a pending
1930       // external suspend request and will be able to mark ourselves as
1931       // exiting.
1932     }
1933     // no more external suspends are allowed at this point
1934   } else {
1935     // before_exit() has already posted JVMTI THREAD_END events
1936   }
1937 
1938   if (log_is_enabled(Debug, os, thread, timer)) {
1939     _timer_exit_phase1.stop();
1940     _timer_exit_phase2.start();
1941   }
1942   // Notify waiters on thread object. This has to be done after exit() is called
1943   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1944   // group should have the destroyed bit set before waiters are notified).
1945   ensure_join(this);
1946   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1947 
1948   if (log_is_enabled(Debug, os, thread, timer)) {
1949     _timer_exit_phase2.stop();
1950     _timer_exit_phase3.start();
1951   }
1952   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1953   // held by this thread must be released. The spec does not distinguish
1954   // between JNI-acquired and regular Java monitors. We can only see
1955   // regular Java monitors here if monitor enter-exit matching is broken.
1956   //
1957   // Optionally release any monitors for regular JavaThread exits. This
1958   // is provided as a work around for any bugs in monitor enter-exit
1959   // matching. This can be expensive so it is not enabled by default.
1960   //
1961   // ensure_join() ignores IllegalThreadStateExceptions, and so does
1962   // ObjectSynchronizer::release_monitors_owned_by_thread().
1963   if (exit_type == jni_detach || ObjectMonitor::Knob_ExitRelease) {
1964     // Sanity check even though JNI DetachCurrentThread() would have
1965     // returned JNI_ERR if there was a Java frame. JavaThread exit
1966     // should be done executing Java code by the time we get here.
1967     assert(!this->has_last_Java_frame(),
1968            "should not have a Java frame when detaching or exiting");
1969     ObjectSynchronizer::release_monitors_owned_by_thread(this);
1970     assert(!this->has_pending_exception(), "release_monitors should have cleared");
1971   }


1999 
2000   // We must flush any deferred card marks before removing a thread from
2001   // the list of active threads.
2002   Universe::heap()->flush_deferred_store_barrier(this);
2003   assert(deferred_card_mark().is_empty(), "Should have been flushed");
2004 
2005 #if INCLUDE_ALL_GCS
2006   // We must flush the G1-related buffers before removing a thread
2007   // from the list of active threads. We must do this after any deferred
2008   // card marks have been flushed (above) so that any entries that are
2009   // added to the thread's dirty card queue as a result are not lost.
2010   if (UseG1GC) {
2011     flush_barrier_queues();
2012   }
2013 #endif // INCLUDE_ALL_GCS
2014 
2015   log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
2016     exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
2017     os::current_thread_id());
2018 
2019   if (log_is_enabled(Debug, os, thread, timer)) {
2020     _timer_exit_phase3.stop();
2021     _timer_exit_phase4.start();
2022   }
2023   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
2024   Threads::remove(this);
2025 
2026   if (log_is_enabled(Debug, os, thread, timer)) {
2027     _timer_exit_phase4.stop();
2028     ResourceMark rm(this);
2029     log_debug(os, thread, timer)("name='%s'"
2030                                  ", exit-phase1=" JLONG_FORMAT
2031                                  ", exit-phase2=" JLONG_FORMAT
2032                                  ", exit-phase3=" JLONG_FORMAT
2033                                  ", exit-phase4=" JLONG_FORMAT,
2034                                  get_thread_name(),
2035                                  _timer_exit_phase1.milliseconds(),
2036                                  _timer_exit_phase2.milliseconds(),
2037                                  _timer_exit_phase3.milliseconds(),
2038                                  _timer_exit_phase4.milliseconds());
2039   }
2040 }
2041 
2042 #if INCLUDE_ALL_GCS
2043 // Flush G1-related queues.
2044 void JavaThread::flush_barrier_queues() {
2045   satb_mark_queue().flush();
2046   dirty_card_queue().flush();
2047 }
2048 
2049 void JavaThread::initialize_queues() {
2050   assert(!SafepointSynchronize::is_at_safepoint(),
2051          "we should not be at a safepoint");
2052 
2053   SATBMarkQueue& satb_queue = satb_mark_queue();
2054   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
2055   // The SATB queue should have been constructed with its active
2056   // field set to false.
2057   assert(!satb_queue.is_active(), "SATB queue should not be active");
2058   assert(satb_queue.is_empty(), "SATB queue should be empty");


2079   if (free_handle_block() != NULL) {
2080     JNIHandleBlock* block = free_handle_block();
2081     set_free_handle_block(NULL);
2082     JNIHandleBlock::release_block(block);
2083   }
2084 
2085   // These have to be removed while this is still a valid thread.
2086   remove_stack_guard_pages();
2087 
2088   if (UseTLAB) {
2089     tlab().make_parsable(true);  // retire TLAB, if any
2090   }
2091 
2092 #if INCLUDE_ALL_GCS
2093   if (UseG1GC) {
2094     flush_barrier_queues();
2095   }
2096 #endif // INCLUDE_ALL_GCS
2097 
2098   Threads::remove(this);
2099   this->smr_delete();
2100 }
2101 
2102 
2103 
2104 
2105 JavaThread* JavaThread::active() {
2106   Thread* thread = Thread::current();
2107   if (thread->is_Java_thread()) {
2108     return (JavaThread*) thread;
2109   } else {
2110     assert(thread->is_VM_thread(), "this must be a vm thread");
2111     VM_Operation* op = ((VMThread*) thread)->vm_operation();
2112     JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
2113     assert(ret->is_Java_thread(), "must be a Java thread");
2114     return ret;
2115   }
2116 }
2117 
2118 bool JavaThread::is_lock_owned(address adr) const {
2119   if (Thread::is_lock_owned(adr)) return true;


2334   }
2335 
2336 
2337   // Interrupt thread so it will wake up from a potential wait()
2338   Thread::interrupt(this);
2339 }
2340 
2341 // External suspension mechanism.
2342 //
2343 // Tell the VM to suspend a thread when ever it knows that it does not hold on
2344 // to any VM_locks and it is at a transition
2345 // Self-suspension will happen on the transition out of the vm.
2346 // Catch "this" coming in from JNIEnv pointers when the thread has been freed
2347 //
2348 // Guarantees on return:
2349 //   + Target thread will not execute any new bytecode (that's why we need to
2350 //     force a safepoint)
2351 //   + Target thread will not enter any new monitors
2352 //
2353 void JavaThread::java_suspend() {
2354   ThreadsListHandle tlh;
2355   if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) {
2356     return;
2357   }

2358 
2359   { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2360     if (!is_external_suspend()) {
2361       // a racing resume has cancelled us; bail out now
2362       return;
2363     }
2364 
2365     // suspend is done
2366     uint32_t debug_bits = 0;
2367     // Warning: is_ext_suspend_completed() may temporarily drop the
2368     // SR_lock to allow the thread to reach a stable thread state if
2369     // it is currently in a transient thread state.
2370     if (is_ext_suspend_completed(false /* !called_by_wait */,
2371                                  SuspendRetryDelay, &debug_bits)) {
2372       return;
2373     }
2374   }
2375 
2376   VM_ThreadSuspend vm_suspend;
2377   VMThread::execute(&vm_suspend);


2425   // it. This would be a "bad thing (TM)" and cause the stack walker
2426   // to crash. We stay self-suspended until there are no more pending
2427   // external suspend requests.
2428   while (is_external_suspend()) {
2429     ret++;
2430     this->set_ext_suspended();
2431 
2432     // _ext_suspended flag is cleared by java_resume()
2433     while (is_ext_suspended()) {
2434       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2435     }
2436   }
2437 
2438   return ret;
2439 }
2440 
2441 #ifdef ASSERT
2442 // verify the JavaThread has not yet been published in the Threads::list, and
2443 // hence doesn't need protection from concurrent access at this stage
2444 void JavaThread::verify_not_published() {
2445   ThreadsListHandle tlh;
2446   assert(!tlh.includes(this), "JavaThread shouldn't have been published yet!");






2447 }
2448 #endif
2449 
2450 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2451 // progress or when _suspend_flags is non-zero.
2452 // Current thread needs to self-suspend if there is a suspend request and/or
2453 // block if a safepoint is in progress.
2454 // Async exception ISN'T checked.
2455 // Note only the ThreadInVMfromNative transition can call this function
2456 // directly and when thread state is _thread_in_native_trans
2457 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2458   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2459 
2460   JavaThread *curJT = JavaThread::current();
2461   bool do_self_suspend = thread->is_external_suspend();
2462 
2463   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2464 
2465   // If JNIEnv proxies are allowed, don't self-suspend if the target
2466   // thread is not the current thread. In older versions of jdbx, jdbx


2543   check_special_condition_for_native_trans(thread);
2544 
2545   // Finish the transition
2546   thread->set_thread_state(_thread_in_Java);
2547 
2548   if (thread->do_critical_native_unlock()) {
2549     ThreadInVMfromJavaNoAsyncException tiv(thread);
2550     GCLocker::unlock_critical(thread);
2551     thread->clear_critical_native_unlock();
2552   }
2553 }
2554 
2555 // We need to guarantee the Threads_lock here, since resumes are not
2556 // allowed during safepoint synchronization
2557 // Can only resume from an external suspension
2558 void JavaThread::java_resume() {
2559   assert_locked_or_safepoint(Threads_lock);
2560 
2561   // Sanity check: thread is gone, has started exiting or the thread
2562   // was not externally suspended.
2563   ThreadsListHandle tlh;
2564   if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) {
2565     return;
2566   }
2567 
2568   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2569 
2570   clear_external_suspend();
2571 
2572   if (is_ext_suspended()) {
2573     clear_ext_suspended();
2574     SR_lock()->notify_all();
2575   }
2576 }
2577 
2578 size_t JavaThread::_stack_red_zone_size = 0;
2579 size_t JavaThread::_stack_yellow_zone_size = 0;
2580 size_t JavaThread::_stack_reserved_zone_size = 0;
2581 size_t JavaThread::_stack_shadow_zone_size = 0;
2582 
2583 void JavaThread::create_stack_guard_pages() {
2584   if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }


3012 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
3013   st->print("%s", get_thread_name_string(buf, buflen));
3014 }
3015 
3016 // Called by fatal error handler. The difference between this and
3017 // JavaThread::print() is that we can't grab lock or allocate memory.
3018 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
3019   st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
3020   oop thread_obj = threadObj();
3021   if (thread_obj != NULL) {
3022     if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
3023   }
3024   st->print(" [");
3025   st->print("%s", _get_thread_state_name(_thread_state));
3026   if (osthread()) {
3027     st->print(", id=%d", osthread()->thread_id());
3028   }
3029   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
3030             p2i(stack_end()), p2i(stack_base()));
3031   st->print("]");
3032 
3033   if (_threads_hazard_ptr != NULL) {
3034     st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_threads_hazard_ptr));
3035   }
3036   if (_nested_threads_hazard_ptr != NULL) {
3037     print_nested_threads_hazard_ptrs_on(st);
3038   }
3039   return;
3040 }
3041 
3042 // Verification
3043 
3044 static void frame_verify(frame* f, const RegisterMap *map) { f->verify(map); }
3045 
3046 void JavaThread::verify() {
3047   // Verify oops in the thread.
3048   oops_do(&VerifyOopClosure::verify_oop, NULL);
3049 
3050   // Verify the stack frames.
3051   frames_do(frame_verify);
3052 }
3053 
3054 // CR 6300358 (sub-CR 2137150)
3055 // Most callers of this method assume that it can't return NULL but a
3056 // thread may not have a name whilst it is in the process of attaching to
3057 // the VM - see CR 6412693, and there are places where a JavaThread can be
3058 // seen prior to having it's threadObj set (eg JNI attaching threads and


3412     // process it here to make sure it isn't unloaded in the middle of
3413     // a scan.
3414     cf->do_code_blob(_scanned_compiled_method);
3415   }
3416 }
3417 
3418 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3419   JavaThread::nmethods_do(cf);
3420   if (_scanned_compiled_method != NULL && cf != NULL) {
3421     // Safepoints can occur when the sweeper is scanning an nmethod so
3422     // process it here to make sure it isn't unloaded in the middle of
3423     // a scan.
3424     cf->do_code_blob(_scanned_compiled_method);
3425   }
3426 }
3427 
3428 
3429 // ======= Threads ========
3430 
3431 // The Threads class links together all active threads, and provides
3432 // operations over all threads. It is protected by the Threads_lock,
3433 // which is also used in other global contexts like safepointing.
3434 // ThreadsListHandles are used to safely perform operations on one
3435 // or more threads without the risk of the thread exiting during the
3436 // operation.
3437 //
3438 // Note: The Threads_lock is currently more widely used than we
3439 // would like. We are actively migrating Threads_lock uses to other
3440 // mechanisms in order to reduce Threads_lock contention.
3441 
3442 JavaThread*           Threads::_thread_list = NULL;
3443 int                   Threads::_number_of_threads = 0;
3444 int                   Threads::_number_of_non_daemon_threads = 0;
3445 int                   Threads::_return_code = 0;
3446 int                   Threads::_thread_claim_parity = 0;
3447 size_t                JavaThread::_stack_size_at_create = 0;
3448 // Safe Memory Reclamation (SMR) support:
3449 Monitor*              Threads::_smr_delete_lock =
3450                           new Monitor(Monitor::special, "smr_delete_lock",
3451                                       false /* allow_vm_block */,
3452                                       Monitor::_safepoint_check_never);
3453 // The '_cnt', '_max' and '_times" fields are enabled via
3454 // -XX:+EnableThreadSMRStatistics:
3455 
3456 // # of parallel threads in _smr_delete_lock->wait().
3457 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
3458 // but there is no nice 16-bit _FORMAT support.
3459 uint                  Threads::_smr_delete_lock_wait_cnt = 0;
3460 
3461 // Max # of parallel threads in _smr_delete_lock->wait().
3462 // Impl note: See _smr_delete_lock_wait_cnt note.
3463 uint                  Threads::_smr_delete_lock_wait_max = 0;
3464 
3465 // Flag to indicate when an _smr_delete_lock->notify() is needed.
3466 // Impl note: See _smr_delete_lock_wait_cnt note.
3467 volatile uint         Threads::_smr_delete_notify = 0;
3468 
3469 // # of threads deleted over VM lifetime.
3470 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
3471 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3472 // isn't available everywhere (or is it?).
3473 volatile uint         Threads::_smr_deleted_thread_cnt = 0;
3474 
3475 // Max time in millis to delete a thread.
3476 // Impl note: 16-bit might be too small on an overloaded machine. Use
3477 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3478 // loop for correctness.
3479 volatile uint         Threads::_smr_deleted_thread_time_max = 0;
3480 
3481 // Cumulative time in millis to delete threads.
3482 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3483 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3484 // isn't available everywhere (or is it?).
3485 volatile uint         Threads::_smr_deleted_thread_times = 0;
3486 
3487 ThreadsList* volatile Threads::_smr_java_thread_list = new ThreadsList(0);
3488 
3489 // # of ThreadsLists allocated over VM lifetime.
3490 // Impl note: We allocate a new ThreadsList for every thread create and
3491 // every thread delete so we need a bigger type than the
3492 // _smr_deleted_thread_cnt field.
3493 uint64_t              Threads::_smr_java_thread_list_alloc_cnt = 1;
3494 
3495 // # of ThreadsLists freed over VM lifetime.
3496 // Impl note: See _smr_java_thread_list_alloc_cnt note.
3497 uint64_t              Threads::_smr_java_thread_list_free_cnt = 0;
3498 
3499 // Max size ThreadsList allocated.
3500 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
3501 uint                  Threads::_smr_java_thread_list_max = 0;
3502 
3503 // Max # of nested ThreadsLists for a thread.
3504 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
3505 // 16-bit, but there is no nice 16-bit _FORMAT support.
3506 uint                  Threads::_smr_nested_thread_list_max = 0;
3507 
3508 // # of ThreadsListHandles deleted over VM lifetime.
3509 // Impl note: Atomically incremented over VM lifetime so use unsigned for
3510 // more range. There will be fewer ThreadsListHandles than threads so
3511 // unsigned 32-bit should be fine.
3512 volatile uint         Threads::_smr_tlh_cnt = 0;
3513 
3514 // Max time in millis to delete a ThreadsListHandle.
3515 // Impl note: 16-bit might be too small on an overloaded machine. Use
3516 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3517 // loop for correctness.
3518 volatile uint         Threads::_smr_tlh_time_max = 0;
3519 
3520 // Cumulative time in millis to delete ThreadsListHandles.
3521 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3522 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3523 // isn't available everywhere (or is it?).
3524 volatile uint         Threads::_smr_tlh_times = 0;
3525 
3526 ThreadsList*          Threads::_smr_to_delete_list = NULL;
3527 
3528 // # of parallel ThreadsLists on the to-delete list.
3529 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
3530 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
3531 uint                  Threads::_smr_to_delete_list_cnt = 0;
3532 
3533 // Max # of parallel ThreadsLists on the to-delete list.
3534 // Impl note: See _smr_to_delete_list_cnt note.
3535 uint                  Threads::_smr_to_delete_list_max = 0;
3536 
3537 #ifdef ASSERT
3538 bool                  Threads::_vm_complete = false;
3539 #endif
3540 
3541 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3542   Prefetch::read((void*)addr, prefetch_interval);
3543   return *addr;
3544 }
3545 
3546 // Possibly the ugliest for loop the world has seen. C++ does not allow
3547 // multiple types in the declaration section of the for loop. In this case
3548 // we are only dealing with pointers and hence can cast them. It looks ugly
3549 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3550 #define DO_JAVA_THREADS(LIST, X)                                                                                          \
3551     for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes,                           \
3552              *MACRO_list = (JavaThread*)(LIST),                                                                           \
3553              **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(),  \
3554              **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(),                                     \
3555              *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval);                 \
3556          MACRO_current_p != MACRO_end;                                                                                    \
3557          MACRO_current_p++,                                                                                               \
3558              X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
3559 
3560 // All JavaThreads
3561 #define ALL_JAVA_THREADS(X) DO_JAVA_THREADS(get_smr_java_thread_list(), X)
3562 
3563 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3564 void Threads::threads_do(ThreadClosure* tc) {
3565   assert_locked_or_safepoint(Threads_lock);
3566   // ALL_JAVA_THREADS iterates through all JavaThreads
3567   ALL_JAVA_THREADS(p) {
3568     tc->do_thread(p);
3569   }
3570   // Someday we could have a table or list of all non-JavaThreads.
3571   // For now, just manually iterate through them.
3572   tc->do_thread(VMThread::vm_thread());
3573   Universe::heap()->gc_threads_do(tc);
3574   WatcherThread *wt = WatcherThread::watcher_thread();
3575   // Strictly speaking, the following NULL check isn't sufficient to make sure
3576   // the data for WatcherThread is still valid upon being examined. However,
3577   // considering that WatchThread terminates when the VM is on the way to
3578   // exit at safepoint, the chance of the above is extremely small. The right
3579   // way to prevent termination of WatcherThread would be to acquire
3580   // Terminator_lock, but we can't do that without violating the lock rank
3581   // checking in some cases.


3642   if (result.get_jint() != JNI_OK) {
3643     vm_exit_during_initialization(); // no message or exception
3644   }
3645 
3646   universe_post_module_init();
3647 }
3648 
3649 // Phase 3. final setup - set security manager, system class loader and TCCL
3650 //
3651 //     This will instantiate and set the security manager, set the system class
3652 //     loader as well as the thread context class loader.  The security manager
3653 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
3654 //     other modules or the application's classpath.
3655 static void call_initPhase3(TRAPS) {
3656   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3657   JavaValue result(T_VOID);
3658   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3659                                          vmSymbols::void_method_signature(), CHECK);
3660 }
3661 
3662 // Safe Memory Reclamation (SMR) support:
3663 //
3664 
3665 // Acquire a stable ThreadsList.
3666 //
3667 ThreadsList *Threads::acquire_stable_list(Thread *self, bool is_ThreadsListSetter) {
3668   assert(self != NULL, "sanity check");
3669   // acquire_stable_list_nested_path() will grab the Threads_lock
3670   // so let's make sure the ThreadsListHandle is in a safe place.
3671   // ThreadsListSetter cannot make this check on this code path.
3672   debug_only(if (!is_ThreadsListSetter && StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
3673 
3674   if (self->get_threads_hazard_ptr() == NULL) {
3675     // The typical case is first.
3676     return acquire_stable_list_fast_path(self);
3677   }
3678 
3679   // The nested case is rare.
3680   return acquire_stable_list_nested_path(self);
3681 }
3682 
3683 // Fast path (and lock free) way to acquire a stable ThreadsList.
3684 //
3685 ThreadsList *Threads::acquire_stable_list_fast_path(Thread *self) {
3686   assert(self != NULL, "sanity check");
3687   assert(self->get_threads_hazard_ptr() == NULL, "sanity check");
3688   assert(self->get_nested_threads_hazard_ptr() == NULL,
3689          "cannot have a nested hazard ptr with a NULL regular hazard ptr");
3690 
3691   ThreadsList* threads;
3692 
3693   // Stable recording of a hazard ptr for SMR. This code does not use
3694   // locks so its use of the _smr_java_thread_list & _threads_hazard_ptr
3695   // fields is racy relative to code that uses those fields with locks.
3696   // OrderAccess and Atomic functions are used to deal with those races.
3697   //
3698   while (true) {
3699     threads = get_smr_java_thread_list();
3700 
3701     // Publish a tagged hazard ptr to denote that the hazard ptr is not
3702     // yet verified as being stable. Due to the fence after the hazard
3703     // ptr write, it will be sequentially consistent w.r.t. the
3704     // sequentially consistent writes of the ThreadsList, even on
3705     // non-multiple copy atomic machines where stores can be observed
3706     // in different order from different observer threads.
3707     ThreadsList* unverified_threads = Thread::tag_hazard_ptr(threads);
3708     self->set_threads_hazard_ptr(unverified_threads);
3709 
3710     // If _smr_java_thread_list has changed, we have lost a race with
3711     // Threads::add() or Threads::remove() and have to try again.
3712     if (get_smr_java_thread_list() != threads) {
3713       continue;
3714     }
3715 
3716     // We try to remove the tag which will verify the hazard ptr as
3717     // being stable. This exchange can race with a scanning thread
3718     // which might invalidate the tagged hazard ptr to keep it from
3719     // being followed to access JavaThread ptrs. If we lose the race,
3720     // we simply retry. If we win the race, then the stable hazard
3721     // ptr is officially published.
3722     if (self->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
3723       break;
3724     }
3725   }
3726 
3727   // A stable hazard ptr has been published letting other threads know
3728   // that the ThreadsList and the JavaThreads reachable from this list
3729   // are protected and hence they should not be deleted until everyone
3730   // agrees it is safe to do so.
3731 
3732   return threads;
3733 }
3734 
3735 // Acquire a nested stable ThreadsList; this is rare so it uses
3736 // Threads_lock.
3737 //
3738 ThreadsList *Threads::acquire_stable_list_nested_path(Thread *self) {
3739   assert(self != NULL, "sanity check");
3740   assert(self->get_threads_hazard_ptr() != NULL,
3741          "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
3742 
3743   // The thread already has a hazard ptr (ThreadsList ref) so we need
3744   // to create a nested ThreadsListHandle with the current ThreadsList
3745   // since it might be different than our current hazard ptr. The need
3746   // for a nested ThreadsListHandle is rare so we do this while holding
3747   // the Threads_lock so we don't race with the scanning code; the code
3748   // is so much simpler this way.
3749 
3750   NestedThreadsList* node;
3751   {
3752     // Only grab the Threads_lock if we don't already own it.
3753     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
3754     node = new NestedThreadsList(get_smr_java_thread_list());
3755     // We insert at the front of the list to match up with the delete
3756     // in release_stable_list().
3757     node->set_next(self->get_nested_threads_hazard_ptr());
3758     self->set_nested_threads_hazard_ptr(node);
3759     if (EnableThreadSMRStatistics) {
3760       self->inc_nested_threads_hazard_ptr_cnt();
3761       if (self->nested_threads_hazard_ptr_cnt() > _smr_nested_thread_list_max) {
3762         _smr_nested_thread_list_max = self->nested_threads_hazard_ptr_cnt();
3763       }
3764     }
3765   }
3766   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::acquire_stable_list: add NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
3767 
3768   return node->t_list();
3769 }
3770 
3771 // Release a stable ThreadsList.
3772 //
3773 void Threads::release_stable_list(Thread *self) {
3774   assert(self != NULL, "sanity check");
3775   // release_stable_list_nested_path() will grab the Threads_lock
3776   // so let's make sure the ThreadsListHandle is in a safe place.
3777   debug_only(if (StrictSafepointChecks) self->check_for_valid_safepoint_state(/* potential_vm_operation */ false);)
3778 
3779   if (self->get_nested_threads_hazard_ptr() == NULL) {
3780     // The typical case is first.
3781     release_stable_list_fast_path(self);
3782     return;
3783   }
3784 
3785   // The nested case is rare.
3786   release_stable_list_nested_path(self);
3787 }
3788 
3789 // Fast path way to release a stable ThreadsList. The release portion
3790 // is lock-free, but the wake up portion is not.
3791 //
3792 void Threads::release_stable_list_fast_path(Thread *self) {
3793   assert(self != NULL, "sanity check");
3794   assert(self->get_threads_hazard_ptr() != NULL, "sanity check");
3795   assert(self->get_nested_threads_hazard_ptr() == NULL,
3796          "cannot have a nested hazard ptr when releasing a regular hazard ptr");
3797 
3798   // After releasing the hazard ptr, other threads may go ahead and
3799   // free up some memory temporarily used by a ThreadsList snapshot.
3800   self->set_threads_hazard_ptr(NULL);
3801 
3802   // We use double-check locking to reduce traffic on the system
3803   // wide smr_delete_lock.
3804   if (Threads::smr_delete_notify()) {
3805     // An exiting thread might be waiting in smr_delete(); we need to
3806     // check with smr_delete_lock to be sure.
3807     release_stable_list_wake_up((char *) "regular hazard ptr");
3808   }
3809 }
3810 
3811 // Release a nested stable ThreadsList; this is rare so it uses
3812 // Threads_lock.
3813 //
3814 void Threads::release_stable_list_nested_path(Thread *self) {
3815   assert(self != NULL, "sanity check");
3816   assert(self->get_nested_threads_hazard_ptr() != NULL, "sanity check");
3817   assert(self->get_threads_hazard_ptr() != NULL,
3818          "must have a regular hazard ptr to have nested hazard ptrs");
3819 
3820   // We have a nested ThreadsListHandle so we have to release it first.
3821   // The need for a nested ThreadsListHandle is rare so we do this while
3822   // holding the Threads_lock so we don't race with the scanning code;
3823   // the code is so much simpler this way.
3824 
3825   NestedThreadsList *node;
3826   {
3827     // Only grab the Threads_lock if we don't already own it.
3828     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
3829     // We remove from the front of the list to match up with the insert
3830     // in acquire_stable_list().
3831     node = self->get_nested_threads_hazard_ptr();
3832     self->set_nested_threads_hazard_ptr(node->next());
3833     if (EnableThreadSMRStatistics) {
3834       self->dec_nested_threads_hazard_ptr_cnt();
3835     }
3836   }
3837 
3838   // An exiting thread might be waiting in smr_delete(); we need to
3839   // check with smr_delete_lock to be sure.
3840   release_stable_list_wake_up((char *) "nested hazard ptr");
3841 
3842   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::release_stable_list: delete NestedThreadsList node containing ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(node->t_list()));
3843 
3844   delete node;
3845 }
3846 
3847 // Wake up portion of the release stable ThreadsList protocol;
3848 // uses the smr_delete_lock().
3849 //
3850 void Threads::release_stable_list_wake_up(char *log_str) {
3851   assert(log_str != NULL, "sanity check");
3852 
3853   // Note: smr_delete_lock is held in smr_delete() for the entire
3854   // hazard ptr search so that we do not lose this notify() if
3855   // the exiting thread has to wait. That code path also holds
3856   // Threads_lock (which was grabbed before smr_delete_lock) so that
3857   // threads_do() can be called. This means the system can't start a
3858   // safepoint which means this thread can't take too long to get to
3859   // a safepoint because of being blocked on smr_delete_lock.
3860   //
3861   MonitorLockerEx ml(Threads::smr_delete_lock(), Monitor::_no_safepoint_check_flag);
3862   if (Threads::smr_delete_notify()) {
3863     // Notify any exiting JavaThreads that are waiting in smr_delete()
3864     // that we've released a ThreadsList.
3865     ml.notify_all();
3866     log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::release_stable_list notified %s", os::current_thread_id(), log_str);
3867   }
3868 }
3869 
3870 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3871   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3872 
3873   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3874     create_vm_init_libraries();
3875   }
3876 
3877   initialize_class(vmSymbols::java_lang_String(), CHECK);
3878 
3879   // Inject CompactStrings value after the static initializers for String ran.
3880   java_lang_String::set_compact_strings(CompactStrings);
3881 
3882   // Initialize java_lang.System (needed before creating the thread)
3883   initialize_class(vmSymbols::java_lang_System(), CHECK);
3884   // The VM creates & returns objects of this class. Make sure it's initialized.
3885   initialize_class(vmSymbols::java_lang_Class(), CHECK);
3886   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
3887   Handle thread_group = create_initial_thread_group(CHECK);
3888   Universe::set_main_thread_group(thread_group());
3889   initialize_class(vmSymbols::java_lang_Thread(), CHECK);


4031 #if INCLUDE_JVMCI
4032   if (JVMCICounterSize > 0) {
4033     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
4034     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
4035   } else {
4036     JavaThread::_jvmci_old_thread_counters = NULL;
4037   }
4038 #endif // INCLUDE_JVMCI
4039 
4040   // Attach the main thread to this os thread
4041   JavaThread* main_thread = new JavaThread();
4042   main_thread->set_thread_state(_thread_in_vm);
4043   main_thread->initialize_thread_current();
4044   // must do this before set_active_handles
4045   main_thread->record_stack_base_and_size();
4046   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
4047 
4048   if (!main_thread->set_as_starting_thread()) {
4049     vm_shutdown_during_initialization(
4050                                       "Failed necessary internal allocation. Out of swap space");
4051     main_thread->smr_delete();
4052     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
4053     return JNI_ENOMEM;
4054   }
4055 
4056   // Enable guard page *after* os::create_main_thread(), otherwise it would
4057   // crash Linux VM, see notes in os_linux.cpp.
4058   main_thread->create_stack_guard_pages();
4059 
4060   // Initialize Java-Level synchronization subsystem
4061   ObjectMonitor::Initialize();
4062 
4063   // Initialize global modules
4064   jint status = init_globals();
4065   if (status != JNI_OK) {
4066     main_thread->smr_delete();
4067     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
4068     return status;
4069   }
4070 
4071   if (TRACE_INITIALIZE() != JNI_OK) {
4072     vm_exit_during_initialization("Failed to initialize tracing backend");
4073   }
4074 
4075   // Should be done after the heap is fully created
4076   main_thread->cache_global_variables();
4077 
4078   HandleMark hm;
4079 
4080   { MutexLocker mu(Threads_lock);
4081     Threads::add(main_thread);
4082   }
4083 
4084   // Any JVMTI raw monitors entered in onload will transition into
4085   // real raw monitor. VM is setup enough here for raw monitor enter.
4086   JvmtiExport::transition_pending_onload_raw_monitors();


4452   AgentLibrary* agent;
4453 
4454   for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
4455     OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
4456 
4457     if (on_load_entry != NULL) {
4458       // Invoke the JVM_OnLoad function
4459       JavaThread* thread = JavaThread::current();
4460       ThreadToNativeFromVM ttn(thread);
4461       HandleMark hm(thread);
4462       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
4463       if (err != JNI_OK) {
4464         vm_exit_during_initialization("-Xrun library failed to init", agent->name());
4465       }
4466     } else {
4467       vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
4468     }
4469   }
4470 }
4471 


4472 















4473 // Last thread running calls java.lang.Shutdown.shutdown()
4474 void JavaThread::invoke_shutdown_hooks() {
4475   HandleMark hm(this);
4476 
4477   // We could get here with a pending exception, if so clear it now.
4478   if (this->has_pending_exception()) {
4479     this->clear_pending_exception();
4480   }
4481 
4482   EXCEPTION_MARK;
4483   Klass* shutdown_klass =
4484     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4485                                       THREAD);
4486   if (shutdown_klass != NULL) {
4487     // SystemDictionary::resolve_or_null will return null if there was
4488     // an exception.  If we cannot load the Shutdown class, just don't
4489     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4490     // and finalizers (if runFinalizersOnExit is set) won't be run.
4491     // Note that if a shutdown hook was registered or runFinalizersOnExit
4492     // was called, the Shutdown class would have already been loaded


4577     VMThread::wait_for_vm_thread_exit();
4578     assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
4579     VMThread::destroy();
4580   }
4581 
4582   // clean up ideal graph printers
4583 #if defined(COMPILER2) && !defined(PRODUCT)
4584   IdealGraphPrinter::clean_up();
4585 #endif
4586 
4587   // Now, all Java threads are gone except daemon threads. Daemon threads
4588   // running Java code or in VM are stopped by the Safepoint. However,
4589   // daemon threads executing native code are still running.  But they
4590   // will be stopped at native=>Java/VM barriers. Note that we can't
4591   // simply kill or suspend them, as it is inherently deadlock-prone.
4592 
4593   VM_Exit::set_vm_exited();
4594 
4595   notify_vm_shutdown();
4596 
4597   // We are after VM_Exit::set_vm_exited() so we can't call
4598   // thread->smr_delete() or we will block on the Threads_lock.
4599   // Deleting the shutdown thread here is safe because another
4600   // JavaThread cannot have an active ThreadsListHandle for
4601   // this JavaThread.
4602   delete thread;
4603 
4604 #if INCLUDE_JVMCI
4605   if (JVMCICounterSize > 0) {
4606     FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
4607   }
4608 #endif
4609 
4610   // exit_globals() will delete tty
4611   exit_globals();
4612 
4613   LogConfiguration::finalize();
4614 
4615   return true;
4616 }
4617 
4618 
4619 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4620   if (version == JNI_VERSION_1_1) return JNI_TRUE;
4621   return is_supported_jni_version(version);
4622 }
4623 
4624 
4625 jboolean Threads::is_supported_jni_version(jint version) {
4626   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4627   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4628   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4629   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4630   if (version == JNI_VERSION_9) return JNI_TRUE;
4631   if (version == JNI_VERSION_10) return JNI_TRUE;
4632   return JNI_FALSE;
4633 }
4634 
4635 // Hash table of pointers found by a scan. Used for collecting hazard
4636 // pointers (ThreadsList references). Also used for collecting JavaThreads
4637 // that are indirectly referenced by hazard ptrs. An instance of this
4638 // class only contains one type of pointer.
4639 //
4640 class ThreadScanHashtable : public CHeapObj<mtThread> {
4641  private:
4642   static bool ptr_equals(void * const& s1, void * const& s2) {
4643     return s1 == s2;
4644   }
4645 
4646   static unsigned int ptr_hash(void * const& s1) {
4647     return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
4648   }
4649 
4650   int _table_size;
4651   // ResourceHashtable SIZE is specified at compile time so our
4652   // dynamic _table_size is unused for now; 1031 is the first prime
4653   // after 1024.
4654   typedef ResourceHashtable<void *, int, &ThreadScanHashtable::ptr_hash,
4655                             &ThreadScanHashtable::ptr_equals, 1031,
4656                             ResourceObj::C_HEAP, mtThread> PtrTable;
4657   PtrTable * _ptrs;
4658 
4659  public:
4660   // ResourceHashtable is passed to various functions and populated in
4661   // different places so we allocate it using C_HEAP to make it immune
4662   // from any ResourceMarks that happen to be in the code paths.
4663   ThreadScanHashtable(int table_size) : _table_size(table_size), _ptrs(new (ResourceObj::C_HEAP, mtThread) PtrTable()) {}
4664 
4665   ~ThreadScanHashtable() { delete _ptrs; }
4666 
4667   bool has_entry(void *pointer) {
4668     int *val_ptr = _ptrs->get(pointer);
4669     return val_ptr != NULL && *val_ptr == 1;
4670   }
4671 
4672   void add_entry(void *pointer) {
4673     _ptrs->put(pointer, 1);
4674   }
4675 };
4676 
4677 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
4678 // (ThreadsList references) into a hash table. This closure handles part 2
4679 // of the dance - adding all the JavaThreads referenced by the hazard
4680 // pointer (ThreadsList reference) to the hash table.
4681 //
4682 class AddThreadHazardPointerThreadClosure : public ThreadClosure {
4683  private:
4684   ThreadScanHashtable *_table;
4685 
4686  public:
4687   AddThreadHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
4688 
4689   virtual void do_thread(Thread *thread) {
4690     if (!_table->has_entry((void*)thread)) {
4691       // The same JavaThread might be on more than one ThreadsList or
4692       // more than one thread might be using the same ThreadsList. In
4693       // either case, we only need a single entry for a JavaThread.
4694       _table->add_entry((void*)thread);
4695     }
4696   }
4697 };
4698 
4699 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
4700 // (ThreadsList references) into a hash table. This closure handles part 1
4701 // of the dance - hazard ptr chain walking and dispatch to another
4702 // closure.
4703 //
4704 class ScanHazardPtrGatherProtectedThreadsClosure : public ThreadClosure {
4705  private:
4706   ThreadScanHashtable *_table;
4707  public:
4708   ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
4709 
4710   virtual void do_thread(Thread *thread) {
4711     assert_locked_or_safepoint(Threads_lock);
4712 
4713     if (thread == NULL) return;
4714 
4715     // This code races with Threads::acquire_stable_list() which is
4716     // lock-free so we have to handle some special situations.
4717     //
4718     ThreadsList *current_list = NULL;
4719     while (true) {
4720       current_list = thread->get_threads_hazard_ptr();
4721       // No hazard ptr so nothing more to do.
4722       if (current_list == NULL) {
4723         assert(thread->get_nested_threads_hazard_ptr() == NULL,
4724                "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4725         return;
4726       }
4727 
4728       // If the hazard ptr is verified as stable (since it is not tagged),
4729       // then it is safe to use.
4730       if (!Thread::is_hazard_ptr_tagged(current_list)) break;
4731 
4732       // The hazard ptr is tagged as not yet verified as being stable
4733       // so we are racing with acquire_stable_list(). This exchange
4734       // attempts to invalidate the hazard ptr. If we win the race,
4735       // then we can ignore this unstable hazard ptr and the other
4736       // thread will retry the attempt to publish a stable hazard ptr.
4737       // If we lose the race, then we retry our attempt to look at the
4738       // hazard ptr.
4739       if (thread->cmpxchg_threads_hazard_ptr(NULL, current_list) == current_list) return;
4740     }
4741 
4742     // The current JavaThread has a hazard ptr (ThreadsList reference)
4743     // which might be _smr_java_thread_list or it might be an older
4744     // ThreadsList that has been removed but not freed. In either case,
4745     // the hazard ptr is protecting all the JavaThreads on that
4746     // ThreadsList.
4747     AddThreadHazardPointerThreadClosure add_cl(_table);
4748     current_list->threads_do(&add_cl);
4749 
4750     // Any NestedThreadsLists are also protecting JavaThreads so
4751     // gather those also; the ThreadsLists may be different.
4752     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4753          node != NULL; node = node->next()) {
4754       node->t_list()->threads_do(&add_cl);
4755     }
4756   }
4757 };
4758 
4759 // Closure to print JavaThreads that have a hazard ptr (ThreadsList
4760 // reference) that contains an indirect reference to a specific JavaThread.
4761 //
4762 class ScanHazardPtrPrintMatchingThreadsClosure : public ThreadClosure {
4763  private:
4764   JavaThread *_thread;
4765  public:
4766   ScanHazardPtrPrintMatchingThreadsClosure(JavaThread *thread) : _thread(thread) {}
4767 
4768   virtual void do_thread(Thread *thread) {
4769     assert_locked_or_safepoint(Threads_lock);
4770 
4771     if (thread == NULL) return;
4772     ThreadsList *current_list = thread->get_threads_hazard_ptr();
4773     if (current_list == NULL) {
4774       assert(thread->get_nested_threads_hazard_ptr() == NULL,
4775              "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4776       return;
4777     }
4778     // If the hazard ptr is unverified, then ignore it.
4779     if (Thread::is_hazard_ptr_tagged(current_list)) return;
4780 
4781     // The current JavaThread has a hazard ptr (ThreadsList reference)
4782     // which might be _smr_java_thread_list or it might be an older
4783     // ThreadsList that has been removed but not freed. In either case,
4784     // the hazard ptr is protecting all the JavaThreads on that
4785     // ThreadsList, but we only care about matching a specific JavaThread.
4786     DO_JAVA_THREADS(current_list, p) {
4787       if (p == _thread) {
4788         log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread1=" INTPTR_FORMAT " has a hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
4789         break;
4790       }
4791     }
4792 
4793     // Any NestedThreadsLists are also protecting JavaThreads so
4794     // check those also; the ThreadsLists may be different.
4795     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4796          node != NULL; node = node->next()) {
4797       DO_JAVA_THREADS(node->t_list(), p) {
4798         if (p == _thread) {
4799           log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread1=" INTPTR_FORMAT " has a nested hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
4800           return;
4801         }
4802       }
4803     }
4804   }
4805 };
4806 
4807 // Return true if the specified JavaThread is protected by a hazard
4808 // pointer (ThreadsList reference). Otherwise, returns false.
4809 //
4810 bool Threads::is_a_protected_JavaThread(JavaThread *thread) {
4811   assert_locked_or_safepoint(Threads_lock);
4812 
4813   // Hash table size should be first power of two higher than twice
4814   // the length of the Threads list.
4815   int hash_table_size = MIN2(_number_of_threads, 32) << 1;
4816   hash_table_size--;
4817   hash_table_size |= hash_table_size >> 1;
4818   hash_table_size |= hash_table_size >> 2;
4819   hash_table_size |= hash_table_size >> 4;
4820   hash_table_size |= hash_table_size >> 8;
4821   hash_table_size |= hash_table_size >> 16;
4822   hash_table_size++;
4823 
4824   // Gather a hash table of the JavaThreads indirectly referenced by
4825   // hazard ptrs.
4826   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
4827   ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
4828   Threads::threads_do(&scan_cl);
4829 
4830   bool thread_is_protected = false;
4831   if (scan_table->has_entry((void*)thread)) {
4832     thread_is_protected = true;
4833   }
4834   delete scan_table;
4835   return thread_is_protected;
4836 }
4837 
4838 // Safely delete a JavaThread when it is no longer in use by a
4839 // ThreadsListHandle.
4840 //
4841 void Threads::smr_delete(JavaThread *thread) {
4842   assert(!Threads_lock->owned_by_self(), "sanity");
4843 
4844   bool has_logged_once = false;
4845   elapsedTimer timer;
4846   if (EnableThreadSMRStatistics) {
4847     timer.start();
4848   }
4849 
4850   while (true) {
4851     {
4852       // No safepoint check because this JavaThread is not on the
4853       // Threads list.
4854       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
4855       // Cannot use a MonitorLockerEx helper here because we have
4856       // to drop the Threads_lock first if we wait.
4857       Threads::smr_delete_lock()->lock_without_safepoint_check();
4858       // Set the smr_delete_notify flag after we grab smr_delete_lock
4859       // and before we scan hazard ptrs because we're doing
4860       // double-check locking in release_stable_list().
4861       Threads::set_smr_delete_notify();
4862 
4863       if (!is_a_protected_JavaThread(thread)) {
4864         // This is the common case.
4865         Threads::clear_smr_delete_notify();
4866         Threads::smr_delete_lock()->unlock();
4867         break;
4868       }
4869       if (!has_logged_once) {
4870         has_logged_once = true;
4871         log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
4872         if (log_is_enabled(Debug, os, thread)) {
4873           ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
4874           Threads::threads_do(&scan_cl);
4875         }
4876       }
4877     } // We have to drop the Threads_lock to wait or delete the thread
4878 
4879     if (EnableThreadSMRStatistics) {
4880       _smr_delete_lock_wait_cnt++;
4881       if (_smr_delete_lock_wait_cnt > _smr_delete_lock_wait_max) {
4882         _smr_delete_lock_wait_max = _smr_delete_lock_wait_cnt;
4883       }
4884     }
4885     // Wait for a release_stable_list() call before we check again. No
4886     // safepoint check, no timeout, and not as suspend equivalent flag
4887     // because this JavaThread is not on the Threads list.
4888     Threads::smr_delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
4889                                      !Mutex::_as_suspend_equivalent_flag);
4890     if (EnableThreadSMRStatistics) {
4891       _smr_delete_lock_wait_cnt--;
4892     }
4893 
4894     Threads::clear_smr_delete_notify();
4895     Threads::smr_delete_lock()->unlock();
4896     // Retry the whole scenario.
4897   }
4898 
4899   if (ThreadLocalHandshakes) {
4900     // The thread is about to be deleted so cancel any handshake.
4901     thread->cancel_handshake();
4902   }
4903 
4904   delete thread;
4905   if (EnableThreadSMRStatistics) {
4906     timer.stop();
4907     uint millis = (uint)timer.milliseconds();
4908     Threads::inc_smr_deleted_thread_cnt();
4909     Threads::add_smr_deleted_thread_times(millis);
4910     Threads::update_smr_deleted_thread_time_max(millis);
4911   }
4912 
4913   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
4914 }
4915 
4916 bool Threads::smr_delete_notify() {
4917   // Use load_acquire() in order to see any updates to _smr_delete_notify
4918   // earlier than when smr_delete_lock is grabbed.
4919   return (OrderAccess::load_acquire(&_smr_delete_notify) != 0);
4920 }
4921 
4922 // set_smr_delete_notify() and clear_smr_delete_notify() are called
4923 // under the protection of the smr_delete_lock, but we also use an
4924 // Atomic operation to ensure the memory update is seen earlier than
4925 // when the smr_delete_lock is dropped.
4926 //
4927 void Threads::set_smr_delete_notify() {
4928   Atomic::inc(&_smr_delete_notify);
4929 }
4930 
4931 void Threads::clear_smr_delete_notify() {
4932   Atomic::dec(&_smr_delete_notify);
4933 }
4934 
4935 // Closure to gather hazard ptrs (ThreadsList references) into a hash table.
4936 //
4937 class ScanHazardPtrGatherThreadsListClosure : public ThreadClosure {
4938  private:
4939   ThreadScanHashtable *_table;
4940  public:
4941   ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable *table) : _table(table) {}
4942 
4943   virtual void do_thread(Thread* thread) {
4944     assert_locked_or_safepoint(Threads_lock);
4945 
4946     if (thread == NULL) return;
4947     ThreadsList *threads = thread->get_threads_hazard_ptr();
4948     if (threads == NULL) {
4949       assert(thread->get_nested_threads_hazard_ptr() == NULL,
4950              "cannot have a nested hazard ptr with a NULL regular hazard ptr");
4951       return;
4952     }
4953     // In this closure we always ignore the tag that might mark this
4954     // hazard ptr as not yet verified. If we happen to catch an
4955     // unverified hazard ptr that is subsequently discarded (not
4956     // published), then the only side effect is that we might keep a
4957     // to-be-deleted ThreadsList alive a little longer.
4958     threads = Thread::untag_hazard_ptr(threads);
4959     if (!_table->has_entry((void*)threads)) {
4960       _table->add_entry((void*)threads);
4961     }
4962 
4963     // Any NestedThreadsLists are also protecting JavaThreads so
4964     // gather those also; the ThreadsLists may be different.
4965     for (NestedThreadsList* node = thread->get_nested_threads_hazard_ptr();
4966          node != NULL; node = node->next()) {
4967       threads = node->t_list();
4968       if (!_table->has_entry((void*)threads)) {
4969         _table->add_entry((void*)threads);
4970       }
4971     }
4972   }
4973 };
4974 
4975 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
4976 // The specified ThreadsList may not get deleted during this call if it
4977 // is still in-use (referenced by a hazard ptr). Other ThreadsLists
4978 // in the chain may get deleted by this call if they are no longer in-use.
4979 void Threads::smr_free_list(ThreadsList* threads) {
4980   assert_locked_or_safepoint(Threads_lock);
4981 
4982   threads->set_next_list(_smr_to_delete_list);
4983   _smr_to_delete_list = threads;
4984   if (EnableThreadSMRStatistics) {
4985     _smr_to_delete_list_cnt++;
4986     if (_smr_to_delete_list_cnt > _smr_to_delete_list_max) {
4987       _smr_to_delete_list_max = _smr_to_delete_list_cnt;
4988     }
4989   }
4990 
4991   // Hash table size should be first power of two higher than twice the length of the ThreadsList
4992   int hash_table_size = MIN2(_number_of_threads, 32) << 1;
4993   hash_table_size--;
4994   hash_table_size |= hash_table_size >> 1;
4995   hash_table_size |= hash_table_size >> 2;
4996   hash_table_size |= hash_table_size >> 4;
4997   hash_table_size |= hash_table_size >> 8;
4998   hash_table_size |= hash_table_size >> 16;
4999   hash_table_size++;
5000 
5001   // Gather a hash table of the current hazard ptrs:
5002   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
5003   ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
5004   Threads::threads_do(&scan_cl);
5005 
5006   // Walk through the linked list of pending freeable ThreadsLists
5007   // and free the ones that are not referenced from hazard ptrs.
5008   ThreadsList* current = _smr_to_delete_list;
5009   ThreadsList* prev = NULL;
5010   ThreadsList* next = NULL;
5011   bool threads_is_freed = false;
5012   while (current != NULL) {
5013     next = current->next_list();
5014     if (!scan_table->has_entry((void*)current)) {
5015       // This ThreadsList is not referenced by a hazard ptr.
5016       if (prev != NULL) {
5017         prev->set_next_list(next);
5018       }
5019       if (_smr_to_delete_list == current) {
5020         _smr_to_delete_list = next;
5021       }
5022 
5023       log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
5024       if (current == threads) threads_is_freed = true;
5025       delete current;
5026       if (EnableThreadSMRStatistics) {
5027         _smr_java_thread_list_free_cnt++;
5028         _smr_to_delete_list_cnt--;
5029       }
5030     } else {
5031       prev = current;
5032     }
5033     current = next;
5034   }
5035 
5036   if (!threads_is_freed) {
5037     // Only report "is not freed" on the original call to
5038     // smr_free_list() for this ThreadsList.
5039     log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_free_list: threads=" INTPTR_FORMAT " is not freed.", os::current_thread_id(), p2i(threads));
5040   }
5041 
5042   delete scan_table;
5043 }
5044 
5045 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a
5046 // new copy of the specified ThreadsList with the specified JavaThread
5047 // removed.
5048 ThreadsList *ThreadsList::remove_thread(ThreadsList* list, JavaThread* java_thread) {
5049   assert(list->_length > 0, "sanity");
5050 
5051   uint i = 0;
5052   DO_JAVA_THREADS(list, current) {
5053     if (current == java_thread) {
5054       break;
5055     }
5056     i++;
5057   }
5058   assert(i < list->_length, "did not find JavaThread on the list");
5059   const uint index = i;
5060   const uint new_length = list->_length - 1;
5061   const uint head_length = index;
5062   const uint tail_length = (new_length >= index) ? (new_length - index) : 0;
5063   ThreadsList *const new_list = new ThreadsList(new_length);
5064 
5065   if (head_length > 0) {
5066     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
5067   }
5068   if (tail_length > 0) {
5069     Copy::disjoint_words((HeapWord*)list->_threads + index + 1, (HeapWord*)new_list->_threads + index, tail_length);
5070   }
5071 
5072   return new_list;
5073 }
5074 
5075 // Add a JavaThread to a ThreadsList. The returned ThreadsList is a
5076 // new copy of the specified ThreadsList with the specified JavaThread
5077 // appended to the end.
5078 ThreadsList *ThreadsList::add_thread(ThreadsList *list, JavaThread *java_thread) {
5079   const uint index = list->_length;
5080   const uint new_length = index + 1;
5081   const uint head_length = index;
5082   ThreadsList *const new_list = new ThreadsList(new_length);
5083 
5084   if (head_length > 0) {
5085     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
5086   }
5087   *(JavaThread**)(new_list->_threads + index) = java_thread;
5088 
5089   return new_list;
5090 }
5091 
5092 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
5093   if (target == NULL) {
5094     return -1;
5095   }
5096   for (uint i = 0; i < length(); i++) {
5097     if (target == thread_at(i)) {
5098       return (int)i;
5099     }
5100   }
5101   return -1;
5102 }
5103 
5104 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
5105   DO_JAVA_THREADS(this, thread) {
5106     oop tobj = thread->threadObj();
5107     // Ignore the thread if it hasn't run yet, has exited
5108     // or is starting to exit.
5109     if (tobj != NULL && !thread->is_exiting() &&
5110         java_tid == java_lang_Thread::thread_id(tobj)) {
5111       // found a match
5112       return thread;
5113     }
5114   }
5115   return NULL;
5116 }
5117 
5118 bool ThreadsList::includes(const JavaThread * const p) const {
5119   if (p == NULL) {
5120     return false;
5121   }
5122   DO_JAVA_THREADS(this, q) {
5123     if (q == p) {
5124       return true;
5125     }
5126   }
5127   return false;
5128 }
5129 
5130 void Threads::add(JavaThread* p, bool force_daemon) {
5131   // The threads lock must be owned at this point
5132   assert_locked_or_safepoint(Threads_lock);
5133 
5134   // See the comment for this method in thread.hpp for its purpose and
5135   // why it is called here.
5136   p->initialize_queues();
5137   p->set_next(_thread_list);
5138   _thread_list = p;
5139 
5140   // Once a JavaThread is added to the Threads list, smr_delete() has
5141   // to be used to delete it. Otherwise we can just delete it directly.
5142   p->set_on_thread_list();
5143 
5144   _number_of_threads++;
5145   oop threadObj = p->threadObj();
5146   bool daemon = true;
5147   // Bootstrapping problem: threadObj can be null for initial
5148   // JavaThread (or for threads attached via JNI)
5149   if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
5150     _number_of_non_daemon_threads++;
5151     daemon = false;
5152   }
5153 
5154   ThreadService::add_thread(p, daemon);
5155 
5156   // Maintain fast thread list
5157   ThreadsList *new_list = ThreadsList::add_thread(get_smr_java_thread_list(), p);
5158   if (EnableThreadSMRStatistics) {
5159     _smr_java_thread_list_alloc_cnt++;
5160     if (new_list->length() > _smr_java_thread_list_max) {
5161       _smr_java_thread_list_max = new_list->length();
5162     }
5163   }
5164   // Initial _smr_java_thread_list will not generate a "Threads::add" mesg.
5165   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
5166 
5167   ThreadsList *old_list = xchg_smr_java_thread_list(new_list);
5168   smr_free_list(old_list);
5169 
5170   // Possible GC point.
5171   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
5172 }
5173 
5174 void Threads::remove(JavaThread* p) {
5175 
5176   // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
5177   ObjectSynchronizer::omFlush(p);
5178 
5179   // Extra scope needed for Thread_lock, so we can check
5180   // that we do not remove thread without safepoint code notice
5181   { MutexLocker ml(Threads_lock);
5182 
5183     assert(get_smr_java_thread_list()->includes(p), "p must be present");
5184 
5185     // Maintain fast thread list
5186     ThreadsList *new_list = ThreadsList::remove_thread(get_smr_java_thread_list(), p);
5187     if (EnableThreadSMRStatistics) {
5188       _smr_java_thread_list_alloc_cnt++;
5189       // This list is smaller so no need to check for a "longest" update.
5190     }
5191 
5192     // Final _smr_java_thread_list will not generate a "Threads::remove" mesg.
5193     log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
5194 
5195     ThreadsList *old_list = xchg_smr_java_thread_list(new_list);
5196     smr_free_list(old_list);
5197 
5198     JavaThread* current = _thread_list;
5199     JavaThread* prev    = NULL;
5200 
5201     while (current != p) {
5202       prev    = current;
5203       current = current->next();
5204     }
5205 
5206     if (prev) {
5207       prev->set_next(current->next());
5208     } else {
5209       _thread_list = p->next();
5210     }
5211 
5212     _number_of_threads--;
5213     oop threadObj = p->threadObj();
5214     bool daemon = true;
5215     if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
5216       _number_of_non_daemon_threads--;
5217       daemon = false;
5218 
5219       // Only one thread left, do a notify on the Threads_lock so a thread waiting
5220       // on destroy_vm will wake up.
5221       if (number_of_non_daemon_threads() == 1) {
5222         Threads_lock->notify_all();
5223       }
5224     }
5225     ThreadService::remove_thread(p, daemon);
5226 
5227     // Make sure that safepoint code disregard this thread. This is needed since
5228     // the thread might mess around with locks after this point. This can cause it
5229     // to do callbacks into the safepoint code. However, the safepoint code is not aware
5230     // of this thread since it is removed from the queue.
5231     p->set_terminated_value();
5232   } // unlock Threads_lock
5233 
5234   // Since Events::log uses a lock, we grab it outside the Threads_lock
5235   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
5236 }
5237 











5238 // Operations on the Threads list for GC.  These are not explicitly locked,
5239 // but the garbage collector must provide a safe context for them to run.
5240 // In particular, these things should never be called when the Threads_lock
5241 // is held by some other thread. (Note: the Safepoint abstraction also
5242 // uses the Threads_lock to guarantee this property. It also makes sure that
5243 // all threads gets blocked when exiting or starting).
5244 
5245 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
5246   ALL_JAVA_THREADS(p) {
5247     p->oops_do(f, cf);
5248   }
5249   VMThread::vm_thread()->oops_do(f, cf);
5250 }
5251 
5252 void Threads::change_thread_claim_parity() {
5253   // Set the new claim parity.
5254   assert(_thread_claim_parity >= 0 && _thread_claim_parity <= 2,
5255          "Not in range.");
5256   _thread_claim_parity++;
5257   if (_thread_claim_parity == 3) _thread_claim_parity = 1;


5330   ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
5331   virtual void do_thread(Thread* thread) {
5332     thread->metadata_handles_do(_f);
5333   }
5334 };
5335 
5336 void Threads::metadata_handles_do(void f(Metadata*)) {
5337   // Only walk the Handles in Thread.
5338   ThreadHandlesClosure handles_closure(f);
5339   threads_do(&handles_closure);
5340 }
5341 
5342 void Threads::deoptimized_wrt_marked_nmethods() {
5343   ALL_JAVA_THREADS(p) {
5344     p->deoptimized_wrt_marked_nmethods();
5345   }
5346 }
5347 
5348 
5349 // Get count Java threads that are waiting to enter the specified monitor.
5350 GrowableArray<JavaThread*>* Threads::get_pending_threads(ThreadsList * t_list,
5351                                                          int count,
5352                                                          address monitor) {


5353   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
5354 
5355   int i = 0;
5356   DO_JAVA_THREADS(t_list, p) {


5357     if (!p->can_call_java()) continue;
5358 
5359     address pending = (address)p->current_pending_monitor();
5360     if (pending == monitor) {             // found a match
5361       if (i < count) result->append(p);   // save the first count matches
5362       i++;
5363     }
5364   }
5365 
5366   return result;
5367 }
5368 
5369 
5370 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
5371                                                       address owner) {





5372   // NULL owner means not locked so we can skip the search
5373   if (owner == NULL) return NULL;
5374 
5375   DO_JAVA_THREADS(t_list, p) {


5376     // first, see if owner is the address of a Java thread
5377     if (owner == (address)p) return p;
5378   }
5379 
5380   // Cannot assert on lack of success here since this function may be
5381   // used by code that is trying to report useful problem information
5382   // like deadlock detection.
5383   if (UseHeavyMonitors) return NULL;
5384 
5385   // If we didn't find a matching Java thread and we didn't force use of
5386   // heavyweight monitors, then the owner is the stack address of the
5387   // Lock Word in the owning Java thread's stack.
5388   //
5389   JavaThread* the_owner = NULL;
5390   DO_JAVA_THREADS(t_list, q) {


5391     if (q->is_lock_owned(owner)) {
5392       the_owner = q;
5393       break;
5394     }
5395   }
5396 
5397   // cannot assert on lack of success here; see above comment
5398   return the_owner;
5399 }
5400 
5401 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
5402 void Threads::print_on(outputStream* st, bool print_stacks,
5403                        bool internal_format, bool print_concurrent_locks) {
5404   char buf[32];
5405   st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
5406 
5407   st->print_cr("Full thread dump %s (%s %s):",
5408                Abstract_VM_Version::vm_name(),
5409                Abstract_VM_Version::vm_release(),
5410                Abstract_VM_Version::vm_info_string());
5411   st->cr();
5412 
5413 #if INCLUDE_SERVICES
5414   // Dump concurrent locks
5415   ConcurrentLocksDump concurrent_locks;
5416   if (print_concurrent_locks) {
5417     concurrent_locks.dump_at_safepoint();
5418   }
5419 #endif // INCLUDE_SERVICES
5420 
5421   print_smr_info_on(st);
5422   st->cr();
5423 
5424   ALL_JAVA_THREADS(p) {
5425     ResourceMark rm;
5426     p->print_on(st);
5427     if (print_stacks) {
5428       if (internal_format) {
5429         p->trace_stack();
5430       } else {
5431         p->print_stack_on(st);
5432       }
5433     }
5434     st->cr();
5435 #if INCLUDE_SERVICES
5436     if (print_concurrent_locks) {
5437       concurrent_locks.print_locks_on(p, st);
5438     }
5439 #endif // INCLUDE_SERVICES
5440   }
5441 
5442   VMThread::vm_thread()->print_on(st);
5443   st->cr();
5444   Universe::heap()->print_gc_threads_on(st);
5445   WatcherThread* wt = WatcherThread::watcher_thread();
5446   if (wt != NULL) {
5447     wt->print_on(st);
5448     st->cr();
5449   }
5450 
5451   st->flush();
5452 }
5453 
5454 // Log Threads class SMR info.
5455 void Threads::log_smr_statistics() {
5456   LogTarget(Info, thread, smr) log;
5457   if (log.is_enabled()) {
5458     LogStream out(log);
5459     print_smr_info_on(&out);
5460   }
5461 }
5462 
5463 // Print Threads class SMR info.
5464 void Threads::print_smr_info_on(outputStream* st) {
5465   // Only grab the Threads_lock if we don't already own it
5466   // and if we are not reporting an error.
5467   MutexLockerEx ml((Threads_lock->owned_by_self() || VMError::is_error_reported()) ? NULL : Threads_lock);
5468 
5469   st->print_cr("Threads class SMR info:");
5470   st->print_cr("_smr_java_thread_list=" INTPTR_FORMAT ", length=%u, "
5471                "elements={", p2i(_smr_java_thread_list),
5472                _smr_java_thread_list->length());
5473   print_smr_info_elements_on(st, _smr_java_thread_list);
5474   st->print_cr("}");
5475   if (_smr_to_delete_list != NULL) {
5476     st->print_cr("_smr_to_delete_list=" INTPTR_FORMAT ", length=%u, "
5477                  "elements={", p2i(_smr_to_delete_list),
5478                  _smr_to_delete_list->length());
5479     print_smr_info_elements_on(st, _smr_to_delete_list);
5480     st->print_cr("}");
5481     for (ThreadsList *t_list = _smr_to_delete_list->next_list();
5482          t_list != NULL; t_list = t_list->next_list()) {
5483       st->print("next-> " INTPTR_FORMAT ", length=%u, "
5484                 "elements={", p2i(t_list), t_list->length());
5485       print_smr_info_elements_on(st, t_list);
5486       st->print_cr("}");
5487     }
5488   }
5489   if (!EnableThreadSMRStatistics) {
5490     return;
5491   }
5492   st->print_cr("_smr_java_thread_list_alloc_cnt=" UINT64_FORMAT ","
5493                "_smr_java_thread_list_free_cnt=" UINT64_FORMAT ","
5494                "_smr_java_thread_list_max=%u, "
5495                "_smr_nested_thread_list_max=%u",
5496                _smr_java_thread_list_alloc_cnt,
5497                _smr_java_thread_list_free_cnt,
5498                _smr_java_thread_list_max,
5499                _smr_nested_thread_list_max);
5500   if (_smr_tlh_cnt > 0) {
5501     st->print_cr("_smr_tlh_cnt=%u"
5502                  ", _smr_tlh_times=%u"
5503                  ", avg_smr_tlh_time=%0.2f"
5504                  ", _smr_tlh_time_max=%u",
5505                  _smr_tlh_cnt, _smr_tlh_times,
5506                  ((double) _smr_tlh_times / _smr_tlh_cnt),
5507                  _smr_tlh_time_max);
5508   }
5509   if (_smr_deleted_thread_cnt > 0) {
5510     st->print_cr("_smr_deleted_thread_cnt=%u"
5511                  ", _smr_deleted_thread_times=%u"
5512                  ", avg_smr_deleted_thread_time=%0.2f"
5513                  ", _smr_deleted_thread_time_max=%u",
5514                  _smr_deleted_thread_cnt, _smr_deleted_thread_times,
5515                  ((double) _smr_deleted_thread_times / _smr_deleted_thread_cnt),
5516                  _smr_deleted_thread_time_max);
5517   }
5518   st->print_cr("_smr_delete_lock_wait_cnt=%u, _smr_delete_lock_wait_max=%u",
5519                _smr_delete_lock_wait_cnt, _smr_delete_lock_wait_max);
5520   st->print_cr("_smr_to_delete_list_cnt=%u, _smr_to_delete_list_max=%u",
5521                _smr_to_delete_list_cnt, _smr_to_delete_list_max);
5522 }
5523 
5524 // Print ThreadsList elements (4 per line).
5525 void Threads::print_smr_info_elements_on(outputStream* st,
5526                                          ThreadsList* t_list) {
5527   uint cnt = 0;
5528   JavaThreadIterator jti(t_list);
5529   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
5530     st->print(INTPTR_FORMAT, p2i(jt));
5531     if (cnt < t_list->length() - 1) {
5532       // Separate with comma or comma-space except for the last one.
5533       if (((cnt + 1) % 4) == 0) {
5534         // Four INTPTR_FORMAT fit on an 80 column line so end the
5535         // current line with just a comma.
5536         st->print_cr(",");
5537       } else {
5538         // Not the last one on the current line so use comma-space:
5539         st->print(", ");
5540       }
5541     } else {
5542       // Last one so just end the current line.
5543       st->cr();
5544     }
5545     cnt++;
5546   }
5547 }
5548 
5549 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
5550                              int buflen, bool* found_current) {
5551   if (this_thread != NULL) {
5552     bool is_current = (current == this_thread);
5553     *found_current = *found_current || is_current;
5554     st->print("%s", is_current ? "=>" : "  ");
5555 
5556     st->print(PTR_FORMAT, p2i(this_thread));
5557     st->print(" ");
5558     this_thread->print_on_error(st, buf, buflen);
5559     st->cr();
5560   }
5561 }
5562 
5563 class PrintOnErrorClosure : public ThreadClosure {
5564   outputStream* _st;
5565   Thread* _current;
5566   char* _buf;
5567   int _buflen;
5568   bool* _found_current;
5569  public:
5570   PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
5571                       int buflen, bool* found_current) :
5572    _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
5573 
5574   virtual void do_thread(Thread* thread) {
5575     Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
5576   }
5577 };
5578 
5579 // Threads::print_on_error() is called by fatal error handler. It's possible
5580 // that VM is not at safepoint and/or current thread is inside signal handler.
5581 // Don't print stack trace, as the stack may not be walkable. Don't allocate
5582 // memory (even in resource area), it might deadlock the error handler.
5583 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
5584                              int buflen) {
5585   print_smr_info_on(st);
5586   st->cr();
5587 
5588   bool found_current = false;
5589   st->print_cr("Java Threads: ( => current thread )");
5590   ALL_JAVA_THREADS(thread) {
5591     print_on_error(thread, st, current, buf, buflen, &found_current);
5592   }
5593   st->cr();
5594 
5595   st->print_cr("Other Threads:");
5596   print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
5597   print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
5598 
5599   PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
5600   Universe::heap()->gc_threads_do(&print_closure);
5601 
5602   if (!found_current) {
5603     st->cr();
5604     st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
5605     current->print_on_error(st, buf, buflen);
5606     st->cr();
5607   }
5608   st->cr();
5609 
5610   st->print_cr("Threads with active compile tasks:");
5611   print_threads_compiling(st, buf, buflen);
5612 }
5613 
5614 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen) {
5615   ALL_JAVA_THREADS(thread) {
5616     if (thread->is_Compiler_thread()) {
5617       CompilerThread* ct = (CompilerThread*) thread;
5618       if (ct->task() != NULL) {
5619         thread->print_name_on_error(st, buf, buflen);
5620         ct->task()->print(st, NULL, true, true);
5621       }
5622     }
5623   }
5624 }
5625 
5626 
5627 // Internal SpinLock and Mutex
5628 // Based on ParkEvent
5629 


< prev index next >