1 /*
   2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "jvm.h"
  28 #include "cds/dynamicArchive.hpp"
  29 #include "cds/metaspaceShared.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/javaClasses.hpp"
  32 #include "classfile/javaThreadStatus.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/vmClasses.hpp"
  35 #include "classfile/vmSymbols.hpp"
  36 #include "code/codeCache.hpp"
  37 #include "code/scopeDesc.hpp"
  38 #include "compiler/compileBroker.hpp"
  39 #include "compiler/compileTask.hpp"
  40 #include "compiler/compilerThread.hpp"
  41 #include "gc/shared/barrierSet.hpp"
  42 #include "gc/shared/collectedHeap.hpp"
  43 #include "gc/shared/gcId.hpp"
  44 #include "gc/shared/gcLocker.inline.hpp"
  45 #include "gc/shared/gcVMOperations.hpp"
  46 #include "gc/shared/oopStorage.hpp"
  47 #include "gc/shared/oopStorageSet.hpp"
  48 #include "gc/shared/stringdedup/stringDedup.hpp"
  49 #include "gc/shared/tlab_globals.hpp"
  50 #include "interpreter/interpreter.hpp"
  51 #include "interpreter/linkResolver.hpp"
  52 #include "interpreter/oopMapCache.hpp"
  53 #include "jfr/jfrEvents.hpp"
  54 #include "jvmtifiles/jvmtiEnv.hpp"
  55 #include "logging/log.hpp"
  56 #include "logging/logAsyncWriter.hpp"
  57 #include "logging/logConfiguration.hpp"
  58 #include "logging/logStream.hpp"
  59 #include "memory/allocation.inline.hpp"
  60 #include "memory/iterator.hpp"
  61 #include "memory/oopFactory.hpp"
  62 #include "memory/resourceArea.hpp"
  63 #include "memory/universe.hpp"
  64 #include "oops/access.inline.hpp"
  65 #include "oops/instanceKlass.hpp"
  66 #include "oops/klass.inline.hpp"
  67 #include "oops/objArrayOop.hpp"
  68 #include "oops/oop.inline.hpp"
  69 #include "oops/oopHandle.inline.hpp"
  70 #include "oops/symbol.hpp"
  71 #include "oops/typeArrayOop.inline.hpp"
  72 #include "oops/verifyOopClosure.hpp"
  73 #include "prims/jvm_misc.hpp"
  74 #include "prims/jvmtiDeferredUpdates.hpp"
  75 #include "prims/jvmtiExport.hpp"
  76 #include "prims/jvmtiThreadState.hpp"
  77 #include "runtime/arguments.hpp"
  78 #include "runtime/atomic.hpp"
  79 #include "runtime/biasedLocking.hpp"
  80 #include "runtime/fieldDescriptor.inline.hpp"
  81 #include "runtime/flags/jvmFlagLimit.hpp"
  82 #include "runtime/deoptimization.hpp"
  83 #include "runtime/frame.inline.hpp"
  84 #include "runtime/handles.inline.hpp"
  85 #include "runtime/handshake.hpp"
  86 #include "runtime/init.hpp"
  87 #include "runtime/interfaceSupport.inline.hpp"
  88 #include "runtime/java.hpp"
  89 #include "runtime/javaCalls.hpp"
  90 #include "runtime/jniHandles.inline.hpp"
  91 #include "runtime/jniPeriodicChecker.hpp"
  92 #include "runtime/lockStack.inline.hpp"
  93 #include "runtime/monitorDeflationThread.hpp"
  94 #include "runtime/mutexLocker.hpp"
  95 #include "runtime/nonJavaThread.hpp"
  96 #include "runtime/objectMonitor.inline.hpp"
  97 #include "runtime/orderAccess.hpp"
  98 #include "runtime/osThread.hpp"
  99 #include "runtime/prefetch.inline.hpp"
 100 #include "runtime/safepoint.hpp"
 101 #include "runtime/safepointMechanism.inline.hpp"
 102 #include "runtime/safepointVerifiers.hpp"
 103 #include "runtime/serviceThread.hpp"
 104 #include "runtime/sharedRuntime.hpp"
 105 #include "runtime/stackFrameStream.inline.hpp"
 106 #include "runtime/stackWatermarkSet.hpp"
 107 #include "runtime/statSampler.hpp"
 108 #include "runtime/task.hpp"
 109 #include "runtime/thread.inline.hpp"
 110 #include "runtime/threadCritical.hpp"
 111 #include "runtime/threadSMR.inline.hpp"
 112 #include "runtime/threadStatisticalInfo.hpp"
 113 #include "runtime/threadWXSetters.inline.hpp"
 114 #include "runtime/timer.hpp"
 115 #include "runtime/timerTrace.hpp"
 116 #include "runtime/vframe.inline.hpp"
 117 #include "runtime/vframeArray.hpp"
 118 #include "runtime/vframe_hp.hpp"
 119 #include "runtime/vmThread.hpp"
 120 #include "runtime/vmOperations.hpp"
 121 #include "runtime/vm_version.hpp"
 122 #include "services/attachListener.hpp"
 123 #include "services/management.hpp"
 124 #include "services/memTracker.hpp"
 125 #include "services/threadService.hpp"
 126 #include "utilities/align.hpp"
 127 #include "utilities/copy.hpp"
 128 #include "utilities/defaultStream.hpp"
 129 #include "utilities/dtrace.hpp"
 130 #include "utilities/events.hpp"
 131 #include "utilities/macros.hpp"
 132 #include "utilities/preserveException.hpp"
 133 #include "utilities/spinYield.hpp"
 134 #include "utilities/vmError.hpp"
 135 #if INCLUDE_JVMCI
 136 #include "jvmci/jvmci.hpp"
 137 #include "jvmci/jvmciEnv.hpp"
 138 #endif
 139 #ifdef COMPILER1
 140 #include "c1/c1_Compiler.hpp"
 141 #endif
 142 #ifdef COMPILER2
 143 #include "opto/c2compiler.hpp"
 144 #include "opto/idealGraphPrinter.hpp"
 145 #endif
 146 #if INCLUDE_RTM_OPT
 147 #include "runtime/rtmLocking.hpp"
 148 #endif
 149 #if INCLUDE_JFR
 150 #include "jfr/jfr.hpp"
 151 #endif
 152 #if INCLUDE_VM_STRUCTS
 153 #include "runtime/vmStructs.hpp"
 154 #endif
 155 
 156 // Initialization after module runtime initialization
 157 void universe_post_module_init();  // must happen after call_initPhase2
 158 
 159 #ifdef DTRACE_ENABLED
 160 
 161 // Only bother with this argument setup if dtrace is available
 162 
 163   #define HOTSPOT_THREAD_PROBE_start HOTSPOT_THREAD_START
 164   #define HOTSPOT_THREAD_PROBE_stop HOTSPOT_THREAD_STOP
 165 
 166   #define DTRACE_THREAD_PROBE(probe, javathread)                           \
 167     {                                                                      \
 168       ResourceMark rm(this);                                               \
 169       int len = 0;                                                         \
 170       const char* name = (javathread)->get_thread_name();                  \
 171       len = strlen(name);                                                  \
 172       HOTSPOT_THREAD_PROBE_##probe(/* probe = start, stop */               \
 173         (char *) name, len,                                                \
 174         java_lang_Thread::thread_id((javathread)->threadObj()),            \
 175         (uintptr_t) (javathread)->osthread()->thread_id(),                 \
 176         java_lang_Thread::is_daemon((javathread)->threadObj()));           \
 177     }
 178 
 179 #else //  ndef DTRACE_ENABLED
 180 
 181   #define DTRACE_THREAD_PROBE(probe, javathread)
 182 
 183 #endif // ndef DTRACE_ENABLED
 184 
 185 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 186 // Current thread is maintained as a thread-local variable
 187 THREAD_LOCAL Thread* Thread::_thr_current = NULL;
 188 #endif
 189 
 190 // ======= Thread ========
 191 // Support for forcing alignment of thread objects for biased locking
 192 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
 193   if (UseBiasedLocking) {
 194     const size_t alignment = markWord::biased_lock_alignment;
 195     size_t aligned_size = size + (alignment - sizeof(intptr_t));
 196     void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
 197                                           : AllocateHeap(aligned_size, flags, CURRENT_PC,
 198                                                          AllocFailStrategy::RETURN_NULL);
 199     void* aligned_addr     = align_up(real_malloc_addr, alignment);
 200     assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
 201            ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
 202            "JavaThread alignment code overflowed allocated storage");
 203     if (aligned_addr != real_malloc_addr) {
 204       log_info(biasedlocking)("Aligned thread " INTPTR_FORMAT " to " INTPTR_FORMAT,
 205                               p2i(real_malloc_addr),
 206                               p2i(aligned_addr));
 207     }
 208     ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
 209     return aligned_addr;
 210   } else {
 211     return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
 212                        : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 213   }
 214 }
 215 
 216 void Thread::operator delete(void* p) {
 217   if (UseBiasedLocking) {
 218     FreeHeap(((Thread*) p)->_real_malloc_address);
 219   } else {
 220     FreeHeap(p);
 221   }
 222 }
 223 
 224 void JavaThread::smr_delete() {
 225   if (_on_thread_list) {
 226     ThreadsSMRSupport::smr_delete(this);
 227   } else {
 228     delete this;
 229   }
 230 }
 231 
 232 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 233 // JavaThread
 234 
 235 DEBUG_ONLY(Thread* Thread::_starting_thread = NULL;)
 236 
 237 Thread::Thread() {
 238 
 239   DEBUG_ONLY(_run_state = PRE_CALL_RUN;)
 240 
 241   // stack and get_thread
 242   set_stack_base(NULL);
 243   set_stack_size(0);
 244   set_lgrp_id(-1);
 245   DEBUG_ONLY(clear_suspendible_thread();)
 246 
 247   // allocated data structures
 248   set_osthread(NULL);
 249   set_resource_area(new (mtThread)ResourceArea());
 250   DEBUG_ONLY(_current_resource_mark = NULL;)
 251   set_handle_area(new (mtThread) HandleArea(NULL));
 252   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, mtClass));
 253   set_active_handles(NULL);
 254   set_free_handle_block(NULL);
 255   set_last_handle_mark(NULL);
 256   DEBUG_ONLY(_missed_ic_stub_refill_verifier = NULL);
 257 
 258   // Initial value of zero ==> never claimed.
 259   _threads_do_token = 0;
 260   _threads_hazard_ptr = NULL;
 261   _threads_list_ptr = NULL;
 262   _nested_threads_hazard_ptr_cnt = 0;
 263   _rcu_counter = 0;
 264 
 265   // the handle mark links itself to last_handle_mark
 266   new HandleMark(this);
 267 
 268   // plain initialization
 269   debug_only(_owned_locks = NULL;)
 270   NOT_PRODUCT(_skip_gcalot = false;)
 271   _jvmti_env_iteration_count = 0;
 272   set_allocated_bytes(0);
 273   _current_pending_raw_monitor = NULL;
 274 
 275   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
 276   _hashStateX = os::random();
 277   _hashStateY = 842502087;
 278   _hashStateZ = 0x8767;    // (int)(3579807591LL & 0xffff) ;
 279   _hashStateW = 273326509;
 280 
 281   // Many of the following fields are effectively final - immutable
 282   // Note that nascent threads can't use the Native Monitor-Mutex
 283   // construct until the _MutexEvent is initialized ...
 284   // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
 285   // we might instead use a stack of ParkEvents that we could provision on-demand.
 286   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
 287   // and ::Release()
 288   _ParkEvent   = ParkEvent::Allocate(this);
 289 
 290 #ifdef CHECK_UNHANDLED_OOPS
 291   if (CheckUnhandledOops) {
 292     _unhandled_oops = new UnhandledOops(this);
 293   }
 294 #endif // CHECK_UNHANDLED_OOPS
 295 #ifdef ASSERT
 296   if (UseBiasedLocking) {
 297     assert(is_aligned(this, markWord::biased_lock_alignment), "forced alignment of thread object failed");
 298     assert(this == _real_malloc_address ||
 299            this == align_up(_real_malloc_address, markWord::biased_lock_alignment),
 300            "bug in forced alignment of thread objects");
 301   }
 302 #endif // ASSERT
 303 
 304   // Notify the barrier set that a thread is being created. The initial
 305   // thread is created before the barrier set is available.  The call to
 306   // BarrierSet::on_thread_create() for this thread is therefore deferred
 307   // to BarrierSet::set_barrier_set().
 308   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 309   if (barrier_set != NULL) {
 310     barrier_set->on_thread_create(this);
 311   } else {
 312     // Only the main thread should be created before the barrier set
 313     // and that happens just before Thread::current is set. No other thread
 314     // can attach as the VM is not created yet, so they can't execute this code.
 315     // If the main thread creates other threads before the barrier set that is an error.
 316     assert(Thread::current_or_null() == NULL, "creating thread before barrier set");
 317   }
 318 
 319   MACOS_AARCH64_ONLY(DEBUG_ONLY(_wx_init = false));
 320 }
 321 
 322 void Thread::initialize_tlab() {
 323   if (UseTLAB) {
 324     tlab().initialize();
 325   }
 326 }
 327 
 328 void Thread::initialize_thread_current() {
 329 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 330   assert(_thr_current == NULL, "Thread::current already initialized");
 331   _thr_current = this;
 332 #endif
 333   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 334   ThreadLocalStorage::set_thread(this);
 335   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 336 }
 337 
 338 void Thread::clear_thread_current() {
 339   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 340 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 341   _thr_current = NULL;
 342 #endif
 343   ThreadLocalStorage::set_thread(NULL);
 344 }
 345 
 346 void Thread::record_stack_base_and_size() {
 347   // Note: at this point, Thread object is not yet initialized. Do not rely on
 348   // any members being initialized. Do not rely on Thread::current() being set.
 349   // If possible, refrain from doing anything which may crash or assert since
 350   // quite probably those crash dumps will be useless.
 351   set_stack_base(os::current_stack_base());
 352   set_stack_size(os::current_stack_size());
 353 
 354   // Set stack limits after thread is initialized.
 355   if (is_Java_thread()) {
 356     as_Java_thread()->stack_overflow_state()->initialize(stack_base(), stack_end());
 357   }
 358 }
 359 
 360 #if INCLUDE_NMT
 361 void Thread::register_thread_stack_with_NMT() {
 362   MemTracker::record_thread_stack(stack_end(), stack_size());
 363 }
 364 
 365 void Thread::unregister_thread_stack_with_NMT() {
 366   MemTracker::release_thread_stack(stack_end(), stack_size());
 367 }
 368 #endif // INCLUDE_NMT
 369 
 370 void Thread::call_run() {
 371   DEBUG_ONLY(_run_state = CALL_RUN;)
 372 
 373   // At this point, Thread object should be fully initialized and
 374   // Thread::current() should be set.
 375 
 376   assert(Thread::current_or_null() != NULL, "current thread is unset");
 377   assert(Thread::current_or_null() == this, "current thread is wrong");
 378 
 379   // Perform common initialization actions
 380 
 381   MACOS_AARCH64_ONLY(this->init_wx());
 382 
 383   register_thread_stack_with_NMT();
 384 
 385   JFR_ONLY(Jfr::on_thread_start(this);)
 386 
 387   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
 388     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
 389     os::current_thread_id(), p2i(stack_end()),
 390     p2i(stack_base()), stack_size()/1024);
 391 
 392   // Perform <ChildClass> initialization actions
 393   DEBUG_ONLY(_run_state = PRE_RUN;)
 394   this->pre_run();
 395 
 396   // Invoke <ChildClass>::run()
 397   DEBUG_ONLY(_run_state = RUN;)
 398   this->run();
 399   // Returned from <ChildClass>::run(). Thread finished.
 400 
 401   // Perform common tear-down actions
 402 
 403   assert(Thread::current_or_null() != NULL, "current thread is unset");
 404   assert(Thread::current_or_null() == this, "current thread is wrong");
 405 
 406   // Perform <ChildClass> tear-down actions
 407   DEBUG_ONLY(_run_state = POST_RUN;)
 408   this->post_run();
 409 
 410   // Note: at this point the thread object may already have deleted itself,
 411   // so from here on do not dereference *this*. Not all thread types currently
 412   // delete themselves when they terminate. But no thread should ever be deleted
 413   // asynchronously with respect to its termination - that is what _run_state can
 414   // be used to check.
 415 
 416   assert(Thread::current_or_null() == NULL, "current thread still present");
 417 }
 418 
 419 Thread::~Thread() {
 420 
 421   // Attached threads will remain in PRE_CALL_RUN, as will threads that don't actually
 422   // get started due to errors etc. Any active thread should at least reach post_run
 423   // before it is deleted (usually in post_run()).
 424   assert(_run_state == PRE_CALL_RUN ||
 425          _run_state == POST_RUN, "Active Thread deleted before post_run(): "
 426          "_run_state=%d", (int)_run_state);
 427 
 428   // Notify the barrier set that a thread is being destroyed. Note that a barrier
 429   // set might not be available if we encountered errors during bootstrapping.
 430   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 431   if (barrier_set != NULL) {
 432     barrier_set->on_thread_destroy(this);
 433   }
 434 
 435   // deallocate data structures
 436   delete resource_area();
 437   // since the handle marks are using the handle area, we have to deallocated the root
 438   // handle mark before deallocating the thread's handle area,
 439   assert(last_handle_mark() != NULL, "check we have an element");
 440   delete last_handle_mark();
 441   assert(last_handle_mark() == NULL, "check we have reached the end");
 442 
 443   ParkEvent::Release(_ParkEvent);
 444   // Set to NULL as a termination indicator for has_terminated().
 445   Atomic::store(&_ParkEvent, (ParkEvent*)NULL);
 446 
 447   delete handle_area();
 448   delete metadata_handles();
 449 
 450   // osthread() can be NULL, if creation of thread failed.
 451   if (osthread() != NULL) os::free_thread(osthread());
 452 
 453   // Clear Thread::current if thread is deleting itself and it has not
 454   // already been done. This must be done before the memory is deallocated.
 455   // Needed to ensure JNI correctly detects non-attached threads.
 456   if (this == Thread::current_or_null()) {
 457     Thread::clear_thread_current();
 458   }
 459 
 460   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 461 }
 462 
 463 #ifdef ASSERT
 464 // A JavaThread is considered dangling if it not handshake-safe with respect to
 465 // the current thread, it is not on a ThreadsList, or not at safepoint.
 466 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
 467   assert(!thread->is_Java_thread() ||
 468          thread->as_Java_thread()->is_handshake_safe_for(Thread::current()) ||
 469          !thread->as_Java_thread()->on_thread_list() ||
 470          SafepointSynchronize::is_at_safepoint() ||
 471          ThreadsSMRSupport::is_a_protected_JavaThread_with_lock(thread->as_Java_thread()),
 472          "possibility of dangling Thread pointer");
 473 }
 474 #endif
 475 
 476 // Is the target JavaThread protected by the calling Thread
 477 // or by some other mechanism:
 478 bool Thread::is_JavaThread_protected(const JavaThread* p) {
 479   // Do the simplest check first:
 480   if (SafepointSynchronize::is_at_safepoint()) {
 481     // The target is protected since JavaThreads cannot exit
 482     // while we're at a safepoint.
 483     return true;
 484   }
 485 
 486   // If the target hasn't been started yet then it is trivially
 487   // "protected". We assume the caller is the thread that will do
 488   // the starting.
 489   if (p->osthread() == NULL || p->osthread()->get_state() <= INITIALIZED) {
 490     return true;
 491   }
 492 
 493   // Now make the simple checks based on who the caller is:
 494   Thread* current_thread = Thread::current();
 495   if (current_thread == p || Threads_lock->owner() == current_thread) {
 496     // Target JavaThread is self or calling thread owns the Threads_lock.
 497     // Second check is the same as Threads_lock->owner_is_self(),
 498     // but we already have the current thread so check directly.
 499     return true;
 500   }
 501 
 502   // Check the ThreadsLists associated with the calling thread (if any)
 503   // to see if one of them protects the target JavaThread:
 504   for (SafeThreadsListPtr* stlp = current_thread->_threads_list_ptr;
 505        stlp != NULL; stlp = stlp->previous()) {
 506     if (stlp->list()->includes(p)) {
 507       // The target JavaThread is protected by this ThreadsList:
 508       return true;
 509     }
 510   }
 511 
 512   // Use this debug code with -XX:+UseNewCode to diagnose locations that
 513   // are missing a ThreadsListHandle or other protection mechanism:
 514   // guarantee(!UseNewCode, "current_thread=" INTPTR_FORMAT " is not protecting p="
 515   //           INTPTR_FORMAT, p2i(current_thread), p2i(p));
 516 
 517   // Note: Since 'p' isn't protected by a TLH, the call to
 518   // p->is_handshake_safe_for() may crash, but we have debug bits so
 519   // we'll be able to figure out what protection mechanism is missing.
 520   assert(p->is_handshake_safe_for(current_thread), "JavaThread=" INTPTR_FORMAT
 521          " is not protected and not handshake safe.", p2i(p));
 522 
 523   // The target JavaThread is not protected so it is not safe to query:
 524   return false;
 525 }
 526 
 527 ThreadPriority Thread::get_priority(const Thread* const thread) {
 528   ThreadPriority priority;
 529   // Can return an error!
 530   (void)os::get_priority(thread, priority);
 531   assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
 532   return priority;
 533 }
 534 
 535 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
 536   debug_only(check_for_dangling_thread_pointer(thread);)
 537   // Can return an error!
 538   (void)os::set_priority(thread, priority);
 539 }
 540 
 541 
 542 void Thread::start(Thread* thread) {
 543   // Start is different from resume in that its safety is guaranteed by context or
 544   // being called from a Java method synchronized on the Thread object.
 545   if (thread->is_Java_thread()) {
 546     // Initialize the thread state to RUNNABLE before starting this thread.
 547     // Can not set it after the thread started because we do not know the
 548     // exact thread state at that time. It could be in MONITOR_WAIT or
 549     // in SLEEPING or some other state.
 550     java_lang_Thread::set_thread_status(thread->as_Java_thread()->threadObj(),
 551                                         JavaThreadStatus::RUNNABLE);
 552   }
 553   os::start_thread(thread);
 554 }
 555 
 556 // GC Support
 557 bool Thread::claim_par_threads_do(uintx claim_token) {
 558   uintx token = _threads_do_token;
 559   if (token != claim_token) {
 560     uintx res = Atomic::cmpxchg(&_threads_do_token, token, claim_token);
 561     if (res == token) {
 562       return true;
 563     }
 564     guarantee(res == claim_token, "invariant");
 565   }
 566   return false;
 567 }
 568 
 569 void Thread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) {
 570   if (active_handles() != NULL) {
 571     active_handles()->oops_do(f);
 572   }
 573   // Do oop for ThreadShadow
 574   f->do_oop((oop*)&_pending_exception);
 575   handle_area()->oops_do(f);
 576 }
 577 
 578 // If the caller is a NamedThread, then remember, in the current scope,
 579 // the given JavaThread in its _processed_thread field.
 580 class RememberProcessedThread: public StackObj {
 581   NamedThread* _cur_thr;
 582 public:
 583   RememberProcessedThread(Thread* thread) {
 584     Thread* self = Thread::current();
 585     if (self->is_Named_thread()) {
 586       _cur_thr = (NamedThread *)self;
 587       assert(_cur_thr->processed_thread() == NULL, "nesting not supported");
 588       _cur_thr->set_processed_thread(thread);
 589     } else {
 590       _cur_thr = NULL;
 591     }
 592   }
 593 
 594   ~RememberProcessedThread() {
 595     if (_cur_thr) {
 596       assert(_cur_thr->processed_thread() != NULL, "nesting not supported");
 597       _cur_thr->set_processed_thread(NULL);
 598     }
 599   }
 600 };
 601 
 602 void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
 603   // Record JavaThread to GC thread
 604   RememberProcessedThread rpt(this);
 605   oops_do_no_frames(f, cf);
 606   oops_do_frames(f, cf);
 607 }
 608 
 609 void Thread::metadata_handles_do(void f(Metadata*)) {
 610   // Only walk the Handles in Thread.
 611   if (metadata_handles() != NULL) {
 612     for (int i = 0; i< metadata_handles()->length(); i++) {
 613       f(metadata_handles()->at(i));
 614     }
 615   }
 616 }
 617 
 618 void Thread::print_on(outputStream* st, bool print_extended_info) const {
 619   // get_priority assumes osthread initialized
 620   if (osthread() != NULL) {
 621     int os_prio;
 622     if (os::get_native_priority(this, &os_prio) == OS_OK) {
 623       st->print("os_prio=%d ", os_prio);
 624     }
 625 
 626     st->print("cpu=%.2fms ",
 627               os::thread_cpu_time(const_cast<Thread*>(this), true) / 1000000.0
 628               );
 629     st->print("elapsed=%.2fs ",
 630               _statistical_info.getElapsedTime() / 1000.0
 631               );
 632     if (is_Java_thread() && (PrintExtendedThreadInfo || print_extended_info)) {
 633       size_t allocated_bytes = (size_t) const_cast<Thread*>(this)->cooked_allocated_bytes();
 634       st->print("allocated=" SIZE_FORMAT "%s ",
 635                 byte_size_in_proper_unit(allocated_bytes),
 636                 proper_unit_for_byte_size(allocated_bytes)
 637                 );
 638       st->print("defined_classes=" INT64_FORMAT " ", _statistical_info.getDefineClassCount());
 639     }
 640 
 641     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
 642     osthread()->print_on(st);
 643   }
 644   ThreadsSMRSupport::print_info_on(this, st);
 645   st->print(" ");
 646   debug_only(if (WizardMode) print_owned_locks_on(st);)
 647 }
 648 
 649 void Thread::print() const { print_on(tty); }
 650 
 651 // Thread::print_on_error() is called by fatal error handler. Don't use
 652 // any lock or allocate memory.
 653 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
 654   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
 655 
 656   if (is_VM_thread())                 { st->print("VMThread"); }
 657   else if (is_GC_task_thread())       { st->print("GCTaskThread"); }
 658   else if (is_Watcher_thread())       { st->print("WatcherThread"); }
 659   else if (is_ConcurrentGC_thread())  { st->print("ConcurrentGCThread"); }
 660   else                                { st->print("Thread"); }
 661 
 662   if (is_Named_thread()) {
 663     st->print(" \"%s\"", name());
 664   }
 665 
 666   OSThread* os_thr = osthread();
 667   if (os_thr != NULL) {
 668     if (os_thr->get_state() != ZOMBIE) {
 669       st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
 670                 p2i(stack_end()), p2i(stack_base()));
 671       st->print(" [id=%d]", osthread()->thread_id());
 672     } else {
 673       st->print(" terminated");
 674     }
 675   } else {
 676     st->print(" unknown state (no osThread)");
 677   }
 678   ThreadsSMRSupport::print_info_on(this, st);
 679 }
 680 
 681 void Thread::print_value_on(outputStream* st) const {
 682   if (is_Named_thread()) {
 683     st->print(" \"%s\" ", name());
 684   }
 685   st->print(INTPTR_FORMAT, p2i(this));   // print address
 686 }
 687 
 688 #ifdef ASSERT
 689 void Thread::print_owned_locks_on(outputStream* st) const {
 690   Mutex* cur = _owned_locks;
 691   if (cur == NULL) {
 692     st->print(" (no locks) ");
 693   } else {
 694     st->print_cr(" Locks owned:");
 695     while (cur) {
 696       cur->print_on(st);
 697       cur = cur->next();
 698     }
 699   }
 700 }
 701 #endif // ASSERT
 702 
 703 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
 704 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
 705 // used for compilation in the future. If that change is made, the need for these methods
 706 // should be revisited, and they should be removed if possible.
 707 
 708 bool Thread::is_lock_owned(address adr) const {
 709   assert(LockingMode != LM_LIGHTWEIGHT, "should not be called with new lightweight locking");
 710   return is_in_full_stack(adr);
 711 }
 712 
 713 bool Thread::set_as_starting_thread() {
 714   assert(_starting_thread == NULL, "already initialized: "
 715          "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
 716   // NOTE: this must be called inside the main thread.
 717   DEBUG_ONLY(_starting_thread = this;)
 718   return os::create_main_thread(this->as_Java_thread());
 719 }
 720 
 721 static void initialize_class(Symbol* class_name, TRAPS) {
 722   Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
 723   InstanceKlass::cast(klass)->initialize(CHECK);
 724 }
 725 
 726 
 727 // Creates the initial ThreadGroup
 728 static Handle create_initial_thread_group(TRAPS) {
 729   Handle system_instance = JavaCalls::construct_new_instance(
 730                             vmClasses::ThreadGroup_klass(),
 731                             vmSymbols::void_method_signature(),
 732                             CHECK_NH);
 733   Universe::set_system_thread_group(system_instance());
 734 
 735   Handle string = java_lang_String::create_from_str("main", CHECK_NH);
 736   Handle main_instance = JavaCalls::construct_new_instance(
 737                             vmClasses::ThreadGroup_klass(),
 738                             vmSymbols::threadgroup_string_void_signature(),
 739                             system_instance,
 740                             string,
 741                             CHECK_NH);
 742   return main_instance;
 743 }
 744 
 745 // Creates the initial Thread, and sets it to running.
 746 static void create_initial_thread(Handle thread_group, JavaThread* thread,
 747                                  TRAPS) {
 748   InstanceKlass* ik = vmClasses::Thread_klass();
 749   assert(ik->is_initialized(), "must be");
 750   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
 751 
 752   // Cannot use JavaCalls::construct_new_instance because the java.lang.Thread
 753   // constructor calls Thread.current(), which must be set here for the
 754   // initial thread.
 755   java_lang_Thread::set_thread(thread_oop(), thread);
 756   java_lang_Thread::set_priority(thread_oop(), NormPriority);
 757   thread->set_threadObj(thread_oop());
 758 
 759   Handle string = java_lang_String::create_from_str("main", CHECK);
 760 
 761   JavaValue result(T_VOID);
 762   JavaCalls::call_special(&result, thread_oop,
 763                           ik,
 764                           vmSymbols::object_initializer_name(),
 765                           vmSymbols::threadgroup_string_void_signature(),
 766                           thread_group,
 767                           string,
 768                           CHECK);
 769 
 770   // Set thread status to running since main thread has
 771   // been started and running.
 772   java_lang_Thread::set_thread_status(thread_oop(),
 773                                       JavaThreadStatus::RUNNABLE);
 774 }
 775 
 776 // Extract version and vendor specific information from
 777 // java.lang.VersionProps fields.
 778 // Returned char* is allocated in the thread's resource area
 779 // so must be copied for permanency.
 780 static const char* get_java_version_info(InstanceKlass* ik,
 781                                          Symbol* field_name) {
 782   fieldDescriptor fd;
 783   bool found = ik != NULL &&
 784                ik->find_local_field(field_name,
 785                                     vmSymbols::string_signature(), &fd);
 786   if (found) {
 787     oop name_oop = ik->java_mirror()->obj_field(fd.offset());
 788     if (name_oop == NULL) {
 789       return NULL;
 790     }
 791     const char* name = java_lang_String::as_utf8_string(name_oop);
 792     return name;
 793   } else {
 794     return NULL;
 795   }
 796 }
 797 
 798 // General purpose hook into Java code, run once when the VM is initialized.
 799 // The Java library method itself may be changed independently from the VM.
 800 static void call_postVMInitHook(TRAPS) {
 801   Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_vm_PostVMInitHook(), THREAD);
 802   if (klass != NULL) {
 803     JavaValue result(T_VOID);
 804     JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
 805                            vmSymbols::void_method_signature(),
 806                            CHECK);
 807   }
 808 }
 809 
 810 // Initialized by VMThread at vm_global_init
 811 static OopStorage* _thread_oop_storage = NULL;
 812 
 813 oop  JavaThread::threadObj() const    {
 814   return _threadObj.resolve();
 815 }
 816 
 817 void JavaThread::set_threadObj(oop p) {
 818   assert(_thread_oop_storage != NULL, "not yet initialized");
 819   _threadObj = OopHandle(_thread_oop_storage, p);
 820 }
 821 
 822 OopStorage* JavaThread::thread_oop_storage() {
 823   assert(_thread_oop_storage != NULL, "not yet initialized");
 824   return _thread_oop_storage;
 825 }
 826 
 827 void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name,
 828                                     bool daemon, TRAPS) {
 829   assert(thread_group.not_null(), "thread group should be specified");
 830   assert(threadObj() == NULL, "should only create Java thread object once");
 831 
 832   InstanceKlass* ik = vmClasses::Thread_klass();
 833   assert(ik->is_initialized(), "must be");
 834   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
 835 
 836   // We are called from jni_AttachCurrentThread/jni_AttachCurrentThreadAsDaemon.
 837   // We cannot use JavaCalls::construct_new_instance because the java.lang.Thread
 838   // constructor calls Thread.current(), which must be set here.
 839   java_lang_Thread::set_thread(thread_oop(), this);
 840   java_lang_Thread::set_priority(thread_oop(), NormPriority);
 841   set_threadObj(thread_oop());
 842 
 843   JavaValue result(T_VOID);
 844   if (thread_name != NULL) {
 845     Handle name = java_lang_String::create_from_str(thread_name, CHECK);
 846     // Thread gets assigned specified name and null target
 847     JavaCalls::call_special(&result,
 848                             thread_oop,
 849                             ik,
 850                             vmSymbols::object_initializer_name(),
 851                             vmSymbols::threadgroup_string_void_signature(),
 852                             thread_group,
 853                             name,
 854                             THREAD);
 855   } else {
 856     // Thread gets assigned name "Thread-nnn" and null target
 857     // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
 858     JavaCalls::call_special(&result,
 859                             thread_oop,
 860                             ik,
 861                             vmSymbols::object_initializer_name(),
 862                             vmSymbols::threadgroup_runnable_void_signature(),
 863                             thread_group,
 864                             Handle(),
 865                             THREAD);
 866   }
 867 
 868 
 869   if (daemon) {
 870     java_lang_Thread::set_daemon(thread_oop());
 871   }
 872 
 873   if (HAS_PENDING_EXCEPTION) {
 874     return;
 875   }
 876 
 877   Klass* group = vmClasses::ThreadGroup_klass();
 878   Handle threadObj(THREAD, this->threadObj());
 879 
 880   JavaCalls::call_special(&result,
 881                           thread_group,
 882                           group,
 883                           vmSymbols::add_method_name(),
 884                           vmSymbols::thread_void_signature(),
 885                           threadObj,          // Arg 1
 886                           THREAD);
 887 }
 888 
 889 // ======= JavaThread ========
 890 
 891 #if INCLUDE_JVMCI
 892 
 893 jlong* JavaThread::_jvmci_old_thread_counters;
 894 
 895 bool jvmci_counters_include(JavaThread* thread) {
 896   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
 897 }
 898 
 899 void JavaThread::collect_counters(jlong* array, int length) {
 900   assert(length == JVMCICounterSize, "wrong value");
 901   for (int i = 0; i < length; i++) {
 902     array[i] = _jvmci_old_thread_counters[i];
 903   }
 904   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 905     if (jvmci_counters_include(tp)) {
 906       for (int i = 0; i < length; i++) {
 907         array[i] += tp->_jvmci_counters[i];
 908       }
 909     }
 910   }
 911 }
 912 
 913 // Attempt to enlarge the array for per thread counters.
 914 jlong* resize_counters_array(jlong* old_counters, int current_size, int new_size) {
 915   jlong* new_counters = NEW_C_HEAP_ARRAY_RETURN_NULL(jlong, new_size, mtJVMCI);
 916   if (new_counters == NULL) {
 917     return NULL;
 918   }
 919   if (old_counters == NULL) {
 920     old_counters = new_counters;
 921     memset(old_counters, 0, sizeof(jlong) * new_size);
 922   } else {
 923     for (int i = 0; i < MIN2((int) current_size, new_size); i++) {
 924       new_counters[i] = old_counters[i];
 925     }
 926     if (new_size > current_size) {
 927       memset(new_counters + current_size, 0, sizeof(jlong) * (new_size - current_size));
 928     }
 929     FREE_C_HEAP_ARRAY(jlong, old_counters);
 930   }
 931   return new_counters;
 932 }
 933 
 934 // Attempt to enlarge the array for per thread counters.
 935 bool JavaThread::resize_counters(int current_size, int new_size) {
 936   jlong* new_counters = resize_counters_array(_jvmci_counters, current_size, new_size);
 937   if (new_counters == NULL) {
 938     return false;
 939   } else {
 940     _jvmci_counters = new_counters;
 941     return true;
 942   }
 943 }
 944 
 945 class VM_JVMCIResizeCounters : public VM_Operation {
 946  private:
 947   int _new_size;
 948   bool _failed;
 949 
 950  public:
 951   VM_JVMCIResizeCounters(int new_size) : _new_size(new_size), _failed(false) { }
 952   VMOp_Type type()                  const        { return VMOp_JVMCIResizeCounters; }
 953   bool allow_nested_vm_operations() const        { return true; }
 954   void doit() {
 955     // Resize the old thread counters array
 956     jlong* new_counters = resize_counters_array(JavaThread::_jvmci_old_thread_counters, JVMCICounterSize, _new_size);
 957     if (new_counters == NULL) {
 958       _failed = true;
 959       return;
 960     } else {
 961       JavaThread::_jvmci_old_thread_counters = new_counters;
 962     }
 963 
 964     // Now resize each threads array
 965     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 966       if (!tp->resize_counters(JVMCICounterSize, _new_size)) {
 967         _failed = true;
 968         break;
 969       }
 970     }
 971     if (!_failed) {
 972       JVMCICounterSize = _new_size;
 973     }
 974   }
 975 
 976   bool failed() { return _failed; }
 977 };
 978 
 979 bool JavaThread::resize_all_jvmci_counters(int new_size) {
 980   VM_JVMCIResizeCounters op(new_size);
 981   VMThread::execute(&op);
 982   return !op.failed();
 983 }
 984 
 985 #endif // INCLUDE_JVMCI
 986 
 987 #ifdef ASSERT
 988 // Checks safepoint allowed and clears unhandled oops at potential safepoints.
 989 void JavaThread::check_possible_safepoint() {
 990   if (_no_safepoint_count > 0) {
 991     print_owned_locks();
 992     assert(false, "Possible safepoint reached by thread that does not allow it");
 993   }
 994 #ifdef CHECK_UNHANDLED_OOPS
 995   // Clear unhandled oops in JavaThreads so we get a crash right away.
 996   clear_unhandled_oops();
 997 #endif // CHECK_UNHANDLED_OOPS
 998 }
 999 
1000 void JavaThread::check_for_valid_safepoint_state() {
1001   // Check NoSafepointVerifier, which is implied by locks taken that can be
1002   // shared with the VM thread.  This makes sure that no locks with allow_vm_block
1003   // are held.
1004   check_possible_safepoint();
1005 
1006   if (thread_state() != _thread_in_vm) {
1007     fatal("LEAF method calling lock?");
1008   }
1009 
1010   if (GCALotAtAllSafepoints) {
1011     // We could enter a safepoint here and thus have a gc
1012     InterfaceSupport::check_gc_alot();
1013   }
1014 }
1015 #endif // ASSERT
1016 
1017 // A JavaThread is a normal Java thread
1018 
1019 JavaThread::JavaThread() :
1020   // Initialize fields
1021 
1022   _in_asgct(false),
1023   _on_thread_list(false),
1024   DEBUG_ONLY(_java_call_counter(0) COMMA)
1025   _entry_point(nullptr),
1026   _deopt_mark(nullptr),
1027   _deopt_nmethod(nullptr),
1028   _vframe_array_head(nullptr),
1029   _vframe_array_last(nullptr),
1030   _jvmti_deferred_updates(nullptr),
1031   _callee_target(nullptr),
1032   _vm_result(nullptr),
1033   _vm_result_2(nullptr),
1034 
1035   _current_pending_monitor(NULL),
1036   _current_pending_monitor_is_from_java(true),
1037   _current_waiting_monitor(NULL),
1038   _Stalled(0),
1039 
1040   _monitor_chunks(nullptr),
1041 
1042   _suspend_flags(0),
1043   _async_exception_condition(_no_async_condition),
1044   _pending_async_exception(nullptr),
1045 
1046   _thread_state(_thread_new),
1047   _saved_exception_pc(nullptr),
1048 #ifdef ASSERT
1049   _no_safepoint_count(0),
1050   _visited_for_critical_count(false),
1051 #endif
1052 
1053   _terminated(_not_terminated),
1054   _in_deopt_handler(0),
1055   _doing_unsafe_access(false),
1056   _do_not_unlock_if_synchronized(false),
1057   _jni_attach_state(_not_attaching_via_jni),
1058 #if INCLUDE_JVMCI
1059   _pending_deoptimization(-1),
1060   _pending_monitorenter(false),
1061   _pending_transfer_to_interpreter(false),
1062   _in_retryable_allocation(false),
1063   _pending_failed_speculation(0),
1064   _jvmci{nullptr},
1065   _jvmci_counters(nullptr),
1066   _jvmci_reserved0(0),
1067   _jvmci_reserved1(0),
1068   _jvmci_reserved_oop0(nullptr),
1069 #endif // INCLUDE_JVMCI
1070 
1071   _exception_oop(oop()),
1072   _exception_pc(0),
1073   _exception_handler_pc(0),
1074   _is_method_handle_return(0),
1075 
1076   _jni_active_critical(0),
1077   _pending_jni_exception_check_fn(nullptr),
1078   _depth_first_number(0),
1079 
1080   // JVMTI PopFrame support
1081   _popframe_condition(popframe_inactive),
1082   _frames_to_pop_failed_realloc(0),
1083 
1084   _handshake(this),
1085 
1086   _popframe_preserved_args(nullptr),
1087   _popframe_preserved_args_size(0),
1088 
1089   _jvmti_thread_state(nullptr),
1090   _interp_only_mode(0),
1091   _should_post_on_exceptions_flag(JNI_FALSE),
1092   _thread_stat(new ThreadStatistics()),
1093 
1094   _parker(),
1095   _cached_monitor_info(nullptr),
1096 
1097   _class_to_be_initialized(nullptr),
1098 
1099   _SleepEvent(ParkEvent::Allocate(this)),
1100 
1101   _lock_stack(this) {
1102   set_jni_functions(jni_functions());
1103 
1104 #if INCLUDE_JVMCI
1105   assert(_jvmci._implicit_exception_pc == nullptr, "must be");
1106   if (JVMCICounterSize > 0) {
1107     resize_counters(0, (int) JVMCICounterSize);
1108   }
1109 #endif // INCLUDE_JVMCI
1110 
1111   // Setup safepoint state info for this thread
1112   ThreadSafepointState::create(this);
1113 
1114   SafepointMechanism::initialize_header(this);
1115 
1116   set_requires_cross_modify_fence(false);
1117 
1118   pd_initialize();
1119   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1120 }
1121 
1122 JavaThread::JavaThread(bool is_attaching_via_jni) : JavaThread() {
1123   if (is_attaching_via_jni) {
1124     _jni_attach_state = _attaching_via_jni;
1125   }
1126 }
1127 
1128 
1129 // interrupt support
1130 
1131 void JavaThread::interrupt() {
1132   // All callers should have 'this' thread protected by a
1133   // ThreadsListHandle so that it cannot terminate and deallocate
1134   // itself.
1135   debug_only(check_for_dangling_thread_pointer(this);)
1136 
1137   // For Windows _interrupt_event
1138   osthread()->set_interrupted(true);
1139 
1140   // For Thread.sleep
1141   _SleepEvent->unpark();
1142 
1143   // For JSR166 LockSupport.park
1144   parker()->unpark();
1145 
1146   // For ObjectMonitor and JvmtiRawMonitor
1147   _ParkEvent->unpark();
1148 }
1149 
1150 
1151 bool JavaThread::is_interrupted(bool clear_interrupted) {
1152   debug_only(check_for_dangling_thread_pointer(this);)
1153 
1154   if (_threadObj.peek() == NULL) {
1155     // If there is no j.l.Thread then it is impossible to have
1156     // been interrupted. We can find NULL during VM initialization
1157     // or when a JNI thread is still in the process of attaching.
1158     // In such cases this must be the current thread.
1159     assert(this == Thread::current(), "invariant");
1160     return false;
1161   }
1162 
1163   bool interrupted = java_lang_Thread::interrupted(threadObj());
1164 
1165   // NOTE that since there is no "lock" around the interrupt and
1166   // is_interrupted operations, there is the possibility that the
1167   // interrupted flag will be "false" but that the
1168   // low-level events will be in the signaled state. This is
1169   // intentional. The effect of this is that Object.wait() and
1170   // LockSupport.park() will appear to have a spurious wakeup, which
1171   // is allowed and not harmful, and the possibility is so rare that
1172   // it is not worth the added complexity to add yet another lock.
1173   // For the sleep event an explicit reset is performed on entry
1174   // to JavaThread::sleep, so there is no early return. It has also been
1175   // recommended not to put the interrupted flag into the "event"
1176   // structure because it hides the issue.
1177   // Also, because there is no lock, we must only clear the interrupt
1178   // state if we are going to report that we were interrupted; otherwise
1179   // an interrupt that happens just after we read the field would be lost.
1180   if (interrupted && clear_interrupted) {
1181     assert(this == Thread::current(), "only the current thread can clear");
1182     java_lang_Thread::set_interrupted(threadObj(), false);
1183     osthread()->set_interrupted(false);
1184   }
1185 
1186   return interrupted;
1187 }
1188 
1189 void JavaThread::block_if_vm_exited() {
1190   if (_terminated == _vm_exited) {
1191     // _vm_exited is set at safepoint, and Threads_lock is never released
1192     // we will block here forever.
1193     // Here we can be doing a jump from a safe state to an unsafe state without
1194     // proper transition, but it happens after the final safepoint has begun.
1195     set_thread_state(_thread_in_vm);
1196     Threads_lock->lock();
1197     ShouldNotReachHere();
1198   }
1199 }
1200 
1201 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) : JavaThread() {
1202   _jni_attach_state = _not_attaching_via_jni;
1203   set_entry_point(entry_point);
1204   // Create the native thread itself.
1205   // %note runtime_23
1206   os::ThreadType thr_type = os::java_thread;
1207   thr_type = entry_point == &CompilerThread::thread_entry ? os::compiler_thread :
1208                                                             os::java_thread;
1209   os::create_thread(this, thr_type, stack_sz);
1210   // The _osthread may be NULL here because we ran out of memory (too many threads active).
1211   // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1212   // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1213   // the exception consists of creating the exception object & initializing it, initialization
1214   // will leave the VM via a JavaCall and then all locks must be unlocked).
1215   //
1216   // The thread is still suspended when we reach here. Thread must be explicit started
1217   // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1218   // by calling Threads:add. The reason why this is not done here, is because the thread
1219   // object must be fully initialized (take a look at JVM_Start)
1220 }
1221 
1222 JavaThread::~JavaThread() {
1223 
1224   // Ask ServiceThread to release the threadObj OopHandle
1225   ServiceThread::add_oop_handle_release(_threadObj);
1226 
1227   // Return the sleep event to the free list
1228   ParkEvent::Release(_SleepEvent);
1229   _SleepEvent = NULL;
1230 
1231   // Free any remaining  previous UnrollBlock
1232   vframeArray* old_array = vframe_array_last();
1233 
1234   if (old_array != NULL) {
1235     Deoptimization::UnrollBlock* old_info = old_array->unroll_block();
1236     old_array->set_unroll_block(NULL);
1237     delete old_info;
1238     delete old_array;
1239   }
1240 
1241   JvmtiDeferredUpdates* updates = deferred_updates();
1242   if (updates != NULL) {
1243     // This can only happen if thread is destroyed before deoptimization occurs.
1244     assert(updates->count() > 0, "Updates holder not deleted");
1245     // free deferred updates.
1246     delete updates;
1247     set_deferred_updates(NULL);
1248   }
1249 
1250   // All Java related clean up happens in exit
1251   ThreadSafepointState::destroy(this);
1252   if (_thread_stat != NULL) delete _thread_stat;
1253 
1254 #if INCLUDE_JVMCI
1255   if (JVMCICounterSize > 0) {
1256     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1257   }
1258 #endif // INCLUDE_JVMCI
1259 }
1260 
1261 
1262 // First JavaThread specific code executed by a new Java thread.
1263 void JavaThread::pre_run() {
1264   // empty - see comments in run()
1265 }
1266 
1267 // The main routine called by a new Java thread. This isn't overridden
1268 // by subclasses, instead different subclasses define a different "entry_point"
1269 // which defines the actual logic for that kind of thread.
1270 void JavaThread::run() {
1271   // initialize thread-local alloc buffer related fields
1272   initialize_tlab();
1273 
1274   _stack_overflow_state.create_stack_guard_pages();
1275 
1276   cache_global_variables();
1277 
1278   // Thread is now sufficiently initialized to be handled by the safepoint code as being
1279   // in the VM. Change thread state from _thread_new to _thread_in_vm
1280   ThreadStateTransition::transition(this, _thread_new, _thread_in_vm);
1281   // Before a thread is on the threads list it is always safe, so after leaving the
1282   // _thread_new we should emit a instruction barrier. The distance to modified code
1283   // from here is probably far enough, but this is consistent and safe.
1284   OrderAccess::cross_modify_fence();
1285 
1286   assert(JavaThread::current() == this, "sanity check");
1287   assert(!Thread::current()->owns_locks(), "sanity check");
1288 
1289   DTRACE_THREAD_PROBE(start, this);
1290 
1291   // This operation might block. We call that after all safepoint checks for a new thread has
1292   // been completed.
1293   set_active_handles(JNIHandleBlock::allocate_block());
1294 
1295   if (JvmtiExport::should_post_thread_life()) {
1296     JvmtiExport::post_thread_start(this);
1297 
1298   }
1299 
1300   // We call another function to do the rest so we are sure that the stack addresses used
1301   // from there will be lower than the stack base just computed.
1302   thread_main_inner();
1303 }
1304 
1305 void JavaThread::thread_main_inner() {
1306   assert(JavaThread::current() == this, "sanity check");
1307   assert(_threadObj.peek() != NULL, "just checking");
1308 
1309   // Execute thread entry point unless this thread has a pending exception
1310   // or has been stopped before starting.
1311   // Note: Due to JVM_StopThread we can have pending exceptions already!
1312   if (!this->has_pending_exception() &&
1313       !java_lang_Thread::is_stillborn(this->threadObj())) {
1314     {
1315       ResourceMark rm(this);
1316       this->set_native_thread_name(this->get_thread_name());
1317     }
1318     HandleMark hm(this);
1319     this->entry_point()(this, this);
1320   }
1321 
1322   DTRACE_THREAD_PROBE(stop, this);
1323 
1324   // Cleanup is handled in post_run()
1325 }
1326 
1327 // Shared teardown for all JavaThreads
1328 void JavaThread::post_run() {
1329   this->exit(false);
1330   this->unregister_thread_stack_with_NMT();
1331   // Defer deletion to here to ensure 'this' is still referenceable in call_run
1332   // for any shared tear-down.
1333   this->smr_delete();
1334 }
1335 
1336 static void ensure_join(JavaThread* thread) {
1337   // We do not need to grab the Threads_lock, since we are operating on ourself.
1338   Handle threadObj(thread, thread->threadObj());
1339   assert(threadObj.not_null(), "java thread object must exist");
1340   ObjectLocker lock(threadObj, thread);
1341   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1342   thread->clear_pending_exception();
1343   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1344   java_lang_Thread::set_thread_status(threadObj(), JavaThreadStatus::TERMINATED);
1345   // Clear the native thread instance - this makes isAlive return false and allows the join()
1346   // to complete once we've done the notify_all below
1347   java_lang_Thread::set_thread(threadObj(), NULL);
1348   lock.notify_all(thread);
1349   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1350   thread->clear_pending_exception();
1351 }
1352 
1353 static bool is_daemon(oop threadObj) {
1354   return (threadObj != NULL && java_lang_Thread::is_daemon(threadObj));
1355 }
1356 
1357 // For any new cleanup additions, please check to see if they need to be applied to
1358 // cleanup_failed_attach_current_thread as well.
1359 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1360   assert(this == JavaThread::current(), "thread consistency check");
1361 
1362   elapsedTimer _timer_exit_phase1;
1363   elapsedTimer _timer_exit_phase2;
1364   elapsedTimer _timer_exit_phase3;
1365   elapsedTimer _timer_exit_phase4;
1366 
1367   if (log_is_enabled(Debug, os, thread, timer)) {
1368     _timer_exit_phase1.start();
1369   }
1370 
1371   HandleMark hm(this);
1372   Handle uncaught_exception(this, this->pending_exception());
1373   this->clear_pending_exception();
1374   Handle threadObj(this, this->threadObj());
1375   assert(threadObj.not_null(), "Java thread object should be created");
1376 
1377   if (!destroy_vm) {
1378     if (uncaught_exception.not_null()) {
1379       EXCEPTION_MARK;
1380       // Call method Thread.dispatchUncaughtException().
1381       Klass* thread_klass = vmClasses::Thread_klass();
1382       JavaValue result(T_VOID);
1383       JavaCalls::call_virtual(&result,
1384                               threadObj, thread_klass,
1385                               vmSymbols::dispatchUncaughtException_name(),
1386                               vmSymbols::throwable_void_signature(),
1387                               uncaught_exception,
1388                               THREAD);
1389       if (HAS_PENDING_EXCEPTION) {
1390         ResourceMark rm(this);
1391         jio_fprintf(defaultStream::error_stream(),
1392                     "\nException: %s thrown from the UncaughtExceptionHandler"
1393                     " in thread \"%s\"\n",
1394                     pending_exception()->klass()->external_name(),
1395                     get_thread_name());
1396         CLEAR_PENDING_EXCEPTION;
1397       }
1398     }
1399 
1400     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1401     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1402     // is deprecated anyhow.
1403     if (!is_Compiler_thread()) {
1404       int count = 3;
1405       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1406         EXCEPTION_MARK;
1407         JavaValue result(T_VOID);
1408         Klass* thread_klass = vmClasses::Thread_klass();
1409         JavaCalls::call_virtual(&result,
1410                                 threadObj, thread_klass,
1411                                 vmSymbols::exit_method_name(),
1412                                 vmSymbols::void_method_signature(),
1413                                 THREAD);
1414         CLEAR_PENDING_EXCEPTION;
1415       }
1416     }
1417     // notify JVMTI
1418     if (JvmtiExport::should_post_thread_life()) {
1419       JvmtiExport::post_thread_end(this);
1420     }
1421 
1422     // The careful dance between thread suspension and exit is handled here.
1423     // Since we are in thread_in_vm state and suspension is done with handshakes,
1424     // we can just put in the exiting state and it will be correctly handled.
1425     set_terminated(_thread_exiting);
1426 
1427     ThreadService::current_thread_exiting(this, is_daemon(threadObj()));
1428   } else {
1429     assert(!is_terminated() && !is_exiting(), "must not be exiting");
1430     // before_exit() has already posted JVMTI THREAD_END events
1431   }
1432 
1433   if (log_is_enabled(Debug, os, thread, timer)) {
1434     _timer_exit_phase1.stop();
1435     _timer_exit_phase2.start();
1436   }
1437 
1438   // Capture daemon status before the thread is marked as terminated.
1439   bool daemon = is_daemon(threadObj());
1440 
1441   // Notify waiters on thread object. This has to be done after exit() is called
1442   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1443   // group should have the destroyed bit set before waiters are notified).
1444   ensure_join(this);
1445   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1446 
1447   if (log_is_enabled(Debug, os, thread, timer)) {
1448     _timer_exit_phase2.stop();
1449     _timer_exit_phase3.start();
1450   }
1451   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1452   // held by this thread must be released. The spec does not distinguish
1453   // between JNI-acquired and regular Java monitors. We can only see
1454   // regular Java monitors here if monitor enter-exit matching is broken.
1455   //
1456   // ensure_join() ignores IllegalThreadStateExceptions, and so does
1457   // ObjectSynchronizer::release_monitors_owned_by_thread().
1458   if (exit_type == jni_detach) {
1459     // Sanity check even though JNI DetachCurrentThread() would have
1460     // returned JNI_ERR if there was a Java frame. JavaThread exit
1461     // should be done executing Java code by the time we get here.
1462     assert(!this->has_last_Java_frame(),
1463            "should not have a Java frame when detaching or exiting");
1464     ObjectSynchronizer::release_monitors_owned_by_thread(this);
1465     assert(!this->has_pending_exception(), "release_monitors should have cleared");
1466   }
1467 
1468   // These things needs to be done while we are still a Java Thread. Make sure that thread
1469   // is in a consistent state, in case GC happens
1470   JFR_ONLY(Jfr::on_thread_exit(this);)
1471 
1472   if (active_handles() != NULL) {
1473     JNIHandleBlock* block = active_handles();
1474     set_active_handles(NULL);
1475     JNIHandleBlock::release_block(block);
1476   }
1477 
1478   if (free_handle_block() != NULL) {
1479     JNIHandleBlock* block = free_handle_block();
1480     set_free_handle_block(NULL);
1481     JNIHandleBlock::release_block(block);
1482   }
1483 
1484   // These have to be removed while this is still a valid thread.
1485   _stack_overflow_state.remove_stack_guard_pages();
1486 
1487   if (UseTLAB) {
1488     tlab().retire();
1489   }
1490 
1491   if (JvmtiEnv::environments_might_exist()) {
1492     JvmtiExport::cleanup_thread(this);
1493   }
1494 
1495   // We need to cache the thread name for logging purposes below as once
1496   // we have called on_thread_detach this thread must not access any oops.
1497   char* thread_name = NULL;
1498   if (log_is_enabled(Debug, os, thread, timer)) {
1499     ResourceMark rm(this);
1500     thread_name = os::strdup(get_thread_name());
1501   }
1502 
1503   log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").",
1504     exit_type == JavaThread::normal_exit ? "exiting" : "detaching",
1505     os::current_thread_id());
1506 
1507   if (log_is_enabled(Debug, os, thread, timer)) {
1508     _timer_exit_phase3.stop();
1509     _timer_exit_phase4.start();
1510   }
1511 
1512 #if INCLUDE_JVMCI
1513   if (JVMCICounterSize > 0) {
1514     if (jvmci_counters_include(this)) {
1515       for (int i = 0; i < JVMCICounterSize; i++) {
1516         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1517       }
1518     }
1519   }
1520 #endif // INCLUDE_JVMCI
1521 
1522   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1523   Threads::remove(this, daemon);
1524 
1525   if (log_is_enabled(Debug, os, thread, timer)) {
1526     _timer_exit_phase4.stop();
1527     log_debug(os, thread, timer)("name='%s'"
1528                                  ", exit-phase1=" JLONG_FORMAT
1529                                  ", exit-phase2=" JLONG_FORMAT
1530                                  ", exit-phase3=" JLONG_FORMAT
1531                                  ", exit-phase4=" JLONG_FORMAT,
1532                                  thread_name,
1533                                  _timer_exit_phase1.milliseconds(),
1534                                  _timer_exit_phase2.milliseconds(),
1535                                  _timer_exit_phase3.milliseconds(),
1536                                  _timer_exit_phase4.milliseconds());
1537     os::free(thread_name);
1538   }
1539 }
1540 
1541 void JavaThread::cleanup_failed_attach_current_thread(bool is_daemon) {
1542   if (active_handles() != NULL) {
1543     JNIHandleBlock* block = active_handles();
1544     set_active_handles(NULL);
1545     JNIHandleBlock::release_block(block);
1546   }
1547 
1548   if (free_handle_block() != NULL) {
1549     JNIHandleBlock* block = free_handle_block();
1550     set_free_handle_block(NULL);
1551     JNIHandleBlock::release_block(block);
1552   }
1553 
1554   // These have to be removed while this is still a valid thread.
1555   _stack_overflow_state.remove_stack_guard_pages();
1556 
1557   if (UseTLAB) {
1558     tlab().retire();
1559   }
1560 
1561   Threads::remove(this, is_daemon);
1562   this->smr_delete();
1563 }
1564 
1565 JavaThread* JavaThread::active() {
1566   Thread* thread = Thread::current();
1567   if (thread->is_Java_thread()) {
1568     return thread->as_Java_thread();
1569   } else {
1570     assert(thread->is_VM_thread(), "this must be a vm thread");
1571     VM_Operation* op = ((VMThread*) thread)->vm_operation();
1572     JavaThread *ret = op == NULL ? NULL : op->calling_thread()->as_Java_thread();
1573     return ret;
1574   }
1575 }
1576 
1577 bool JavaThread::is_lock_owned(address adr) const {
1578   assert(LockingMode != LM_LIGHTWEIGHT, "should not be called with new lightweight locking");
1579  if (Thread::is_lock_owned(adr)) return true;
1580 
1581   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
1582     if (chunk->contains(adr)) return true;
1583   }
1584 
1585   return false;
1586 }
1587 
1588 oop JavaThread::exception_oop() const {
1589   return Atomic::load(&_exception_oop);
1590 }
1591 
1592 void JavaThread::set_exception_oop(oop o) {
1593   Atomic::store(&_exception_oop, o);
1594 }
1595 
1596 void JavaThread::add_monitor_chunk(MonitorChunk* chunk) {
1597   chunk->set_next(monitor_chunks());
1598   set_monitor_chunks(chunk);
1599 }
1600 
1601 void JavaThread::remove_monitor_chunk(MonitorChunk* chunk) {
1602   guarantee(monitor_chunks() != NULL, "must be non empty");
1603   if (monitor_chunks() == chunk) {
1604     set_monitor_chunks(chunk->next());
1605   } else {
1606     MonitorChunk* prev = monitor_chunks();
1607     while (prev->next() != chunk) prev = prev->next();
1608     prev->set_next(chunk->next());
1609   }
1610 }
1611 
1612 
1613 // Asynchronous exceptions support
1614 //
1615 // Note: this function shouldn't block if it's called in
1616 // _thread_in_native_trans state (such as from
1617 // check_special_condition_for_native_trans()).
1618 void JavaThread::check_and_handle_async_exceptions() {
1619   if (has_last_Java_frame() && has_async_exception_condition()) {
1620     // If we are at a polling page safepoint (not a poll return)
1621     // then we must defer async exception because live registers
1622     // will be clobbered by the exception path. Poll return is
1623     // ok because the call we a returning from already collides
1624     // with exception handling registers and so there is no issue.
1625     // (The exception handling path kills call result registers but
1626     //  this is ok since the exception kills the result anyway).
1627 
1628     if (is_at_poll_safepoint()) {
1629       // if the code we are returning to has deoptimized we must defer
1630       // the exception otherwise live registers get clobbered on the
1631       // exception path before deoptimization is able to retrieve them.
1632       //
1633       RegisterMap map(this, false);
1634       frame caller_fr = last_frame().sender(&map);
1635       assert(caller_fr.is_compiled_frame(), "what?");
1636       if (caller_fr.is_deoptimized_frame()) {
1637         log_info(exceptions)("deferred async exception at compiled safepoint");
1638         return;
1639       }
1640     }
1641   }
1642 
1643   AsyncExceptionCondition condition = clear_async_exception_condition();
1644   if (condition == _no_async_condition) {
1645     // Conditions have changed since has_special_runtime_exit_condition()
1646     // was called:
1647     // - if we were here only because of an external suspend request,
1648     //   then that was taken care of above (or cancelled) so we are done
1649     // - if we were here because of another async request, then it has
1650     //   been cleared between the has_special_runtime_exit_condition()
1651     //   and now so again we are done
1652     return;
1653   }
1654 
1655   // Check for pending async. exception
1656   if (_pending_async_exception != NULL) {
1657     // Only overwrite an already pending exception, if it is not a threadDeath.
1658     if (!has_pending_exception() || !pending_exception()->is_a(vmClasses::ThreadDeath_klass())) {
1659 
1660       // We cannot call Exceptions::_throw(...) here because we cannot block
1661       set_pending_exception(_pending_async_exception, __FILE__, __LINE__);
1662 
1663       LogTarget(Info, exceptions) lt;
1664       if (lt.is_enabled()) {
1665         ResourceMark rm;
1666         LogStream ls(lt);
1667         ls.print("Async. exception installed at runtime exit (" INTPTR_FORMAT ")", p2i(this));
1668           if (has_last_Java_frame()) {
1669             frame f = last_frame();
1670            ls.print(" (pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " )", p2i(f.pc()), p2i(f.sp()));
1671           }
1672         ls.print_cr(" of type: %s", _pending_async_exception->klass()->external_name());
1673       }
1674       _pending_async_exception = NULL;
1675       // Clear condition from _suspend_flags since we have finished processing it.
1676       clear_suspend_flag(_has_async_exception);
1677     }
1678   }
1679 
1680   if (condition == _async_unsafe_access_error && !has_pending_exception()) {
1681     // We may be at method entry which requires we save the do-not-unlock flag.
1682     UnlockFlagSaver fs(this);
1683     switch (thread_state()) {
1684     case _thread_in_vm: {
1685       JavaThread* THREAD = this;
1686       Exceptions::throw_unsafe_access_internal_error(THREAD, __FILE__, __LINE__, "a fault occurred in an unsafe memory access operation");
1687       return;
1688     }
1689     case _thread_in_native: {
1690       ThreadInVMfromNative tiv(this);
1691       JavaThread* THREAD = this;
1692       Exceptions::throw_unsafe_access_internal_error(THREAD, __FILE__, __LINE__, "a fault occurred in an unsafe memory access operation");
1693       return;
1694     }
1695     case _thread_in_Java: {
1696       ThreadInVMfromJava tiv(this);
1697       JavaThread* THREAD = this;
1698       Exceptions::throw_unsafe_access_internal_error(THREAD, __FILE__, __LINE__, "a fault occurred in a recent unsafe memory access operation in compiled Java code");
1699       return;
1700     }
1701     default:
1702       ShouldNotReachHere();
1703     }
1704   }
1705 
1706   assert(has_pending_exception(), "must have handled the async condition if no exception");
1707 }
1708 
1709 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
1710 
1711   if (is_obj_deopt_suspend()) {
1712     frame_anchor()->make_walkable();
1713     wait_for_object_deoptimization();
1714   }
1715 
1716   // We might be here for reasons in addition to the self-suspend request
1717   // so check for other async requests.
1718   if (check_asyncs) {
1719     check_and_handle_async_exceptions();
1720   }
1721 
1722   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
1723 }
1724 
1725 class InstallAsyncExceptionClosure : public HandshakeClosure {
1726   Handle _throwable; // The Throwable thrown at the target Thread
1727 public:
1728   InstallAsyncExceptionClosure(Handle throwable) : HandshakeClosure("InstallAsyncException"), _throwable(throwable) {}
1729 
1730   void do_thread(Thread* thr) {
1731     JavaThread* target = thr->as_Java_thread();
1732     // Note that this now allows multiple ThreadDeath exceptions to be
1733     // thrown at a thread.
1734     // The target thread has run and has not exited yet.
1735     target->send_thread_stop(_throwable());
1736   }
1737 };
1738 
1739 void JavaThread::send_async_exception(oop java_thread, oop java_throwable) {
1740   Handle throwable(Thread::current(), java_throwable);
1741   JavaThread* target = java_lang_Thread::thread(java_thread);
1742   InstallAsyncExceptionClosure vm_stop(throwable);
1743   Handshake::execute(&vm_stop, target);
1744 }
1745 
1746 void JavaThread::send_thread_stop(oop java_throwable)  {
1747   ResourceMark rm;
1748   assert(is_handshake_safe_for(Thread::current()),
1749          "should be self or handshakee");
1750 
1751   // Do not throw asynchronous exceptions against the compiler thread
1752   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
1753   if (!can_call_java()) return;
1754 
1755   {
1756     // Actually throw the Throwable against the target Thread - however
1757     // only if there is no thread death exception installed already.
1758     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(vmClasses::ThreadDeath_klass())) {
1759       // If the topmost frame is a runtime stub, then we are calling into
1760       // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
1761       // must deoptimize the caller before continuing, as the compiled  exception handler table
1762       // may not be valid
1763       if (has_last_Java_frame()) {
1764         frame f = last_frame();
1765         if (f.is_runtime_frame() || f.is_safepoint_blob_frame()) {
1766           RegisterMap reg_map(this, false);
1767           frame compiled_frame = f.sender(&reg_map);
1768           if (!StressCompiledExceptionHandlers && compiled_frame.can_be_deoptimized()) {
1769             Deoptimization::deoptimize(this, compiled_frame);
1770           }
1771         }
1772       }
1773 
1774       // Set async. pending exception in thread.
1775       set_pending_async_exception(java_throwable);
1776 
1777       if (log_is_enabled(Info, exceptions)) {
1778          ResourceMark rm;
1779         log_info(exceptions)("Pending Async. exception installed of type: %s",
1780                              InstanceKlass::cast(_pending_async_exception->klass())->external_name());
1781       }
1782       // for AbortVMOnException flag
1783       Exceptions::debug_check_abort(_pending_async_exception->klass()->external_name());
1784     }
1785   }
1786 
1787 
1788   // Interrupt thread so it will wake up from a potential wait()/sleep()/park()
1789   java_lang_Thread::set_interrupted(threadObj(), true);
1790   this->interrupt();
1791 }
1792 
1793 
1794 // External suspension mechanism.
1795 //
1796 // Guarantees on return (for a valid target thread):
1797 //   - Target thread will not execute any new bytecode.
1798 //   - Target thread will not enter any new monitors.
1799 //
1800 bool JavaThread::java_suspend() {
1801   ThreadsListHandle tlh;
1802   if (!tlh.includes(this)) {
1803     log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " not on ThreadsList, no suspension", p2i(this));
1804     return false;
1805   }
1806   return this->handshake_state()->suspend();
1807 }
1808 
1809 bool JavaThread::java_resume() {
1810   ThreadsListHandle tlh;
1811   if (!tlh.includes(this)) {
1812     log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " not on ThreadsList, nothing to resume", p2i(this));
1813     return false;
1814   }
1815   return this->handshake_state()->resume();
1816 }
1817 
1818 // Wait for another thread to perform object reallocation and relocking on behalf of
1819 // this thread.
1820 // Raw thread state transition to _thread_blocked and back again to the original
1821 // state before returning are performed. The current thread is required to
1822 // change to _thread_blocked in order to be seen to be safepoint/handshake safe
1823 // whilst suspended and only after becoming handshake safe, the other thread can
1824 // complete the handshake used to synchronize with this thread and then perform
1825 // the reallocation and relocking. We cannot use the thread state transition
1826 // helpers because we arrive here in various states and also because the helpers
1827 // indirectly call this method.  After leaving _thread_blocked we have to check
1828 // for safepoint/handshake, except if _thread_in_native. The thread is safe
1829 // without blocking then. Allowed states are enumerated in
1830 // SafepointSynchronize::block(). See also EscapeBarrier::sync_and_suspend_*()
1831 
1832 void JavaThread::wait_for_object_deoptimization() {
1833   assert(!has_last_Java_frame() || frame_anchor()->walkable(), "should have walkable stack");
1834   assert(this == Thread::current(), "invariant");
1835   JavaThreadState state = thread_state();
1836 
1837   bool spin_wait = os::is_MP();
1838   do {
1839     set_thread_state(_thread_blocked);
1840     // Wait for object deoptimization if requested.
1841     if (spin_wait) {
1842       // A single deoptimization is typically very short. Microbenchmarks
1843       // showed 5% better performance when spinning.
1844       const uint spin_limit = 10 * SpinYield::default_spin_limit;
1845       SpinYield spin(spin_limit);
1846       for (uint i = 0; is_obj_deopt_suspend() && i < spin_limit; i++) {
1847         spin.wait();
1848       }
1849       // Spin just once
1850       spin_wait = false;
1851     } else {
1852       MonitorLocker ml(this, EscapeBarrier_lock, Monitor::_no_safepoint_check_flag);
1853       if (is_obj_deopt_suspend()) {
1854         ml.wait();
1855       }
1856     }
1857     // The current thread could have been suspended again. We have to check for
1858     // suspend after restoring the saved state. Without this the current thread
1859     // might return to _thread_in_Java and execute bytecode.
1860     set_thread_state_fence(state);
1861 
1862     if (state != _thread_in_native) {
1863       SafepointMechanism::process_if_requested(this);
1864     }
1865     // A handshake for obj. deoptimization suspend could have been processed so
1866     // we must check after processing.
1867   } while (is_obj_deopt_suspend());
1868 }
1869 
1870 #ifdef ASSERT
1871 // Verify the JavaThread has not yet been published in the Threads::list, and
1872 // hence doesn't need protection from concurrent access at this stage.
1873 void JavaThread::verify_not_published() {
1874   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
1875   // since an unpublished JavaThread doesn't participate in the
1876   // Thread-SMR protocol for keeping a ThreadsList alive.
1877   assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
1878 }
1879 #endif
1880 
1881 // Slow path when the native==>Java barriers detect a safepoint/handshake is
1882 // pending, when _suspend_flags is non-zero or when we need to process a stack
1883 // watermark. Also check for pending async exceptions (except unsafe access error).
1884 // Note only the native==>Java barriers can call this function when thread state
1885 // is _thread_in_native_trans.
1886 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
1887   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
1888   assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->Java transition");
1889 
1890   // Enable WXWrite: called directly from interpreter native wrapper.
1891   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
1892 
1893   SafepointMechanism::process_if_requested_with_exit_check(thread, false /* check asyncs */);
1894 
1895   // After returning from native, it could be that the stack frames are not
1896   // yet safe to use. We catch such situations in the subsequent stack watermark
1897   // barrier, which will trap unsafe stack frames.
1898   StackWatermarkSet::before_unwind(thread);
1899 
1900   if (thread->has_async_exception_condition(false /* check unsafe access error */)) {
1901     // We are in _thread_in_native_trans state, don't handle unsafe
1902     // access error since that may block.
1903     thread->check_and_handle_async_exceptions();
1904   }
1905 }
1906 
1907 #ifndef PRODUCT
1908 // Deoptimization
1909 // Function for testing deoptimization
1910 void JavaThread::deoptimize() {
1911   StackFrameStream fst(this, false /* update */, true /* process_frames */);
1912   bool deopt = false;           // Dump stack only if a deopt actually happens.
1913   bool only_at = strlen(DeoptimizeOnlyAt) > 0;
1914   // Iterate over all frames in the thread and deoptimize
1915   for (; !fst.is_done(); fst.next()) {
1916     if (fst.current()->can_be_deoptimized()) {
1917 
1918       if (only_at) {
1919         // Deoptimize only at particular bcis.  DeoptimizeOnlyAt
1920         // consists of comma or carriage return separated numbers so
1921         // search for the current bci in that string.
1922         address pc = fst.current()->pc();
1923         nmethod* nm =  (nmethod*) fst.current()->cb();
1924         ScopeDesc* sd = nm->scope_desc_at(pc);
1925         char buffer[8];
1926         jio_snprintf(buffer, sizeof(buffer), "%d", sd->bci());
1927         size_t len = strlen(buffer);
1928         const char * found = strstr(DeoptimizeOnlyAt, buffer);
1929         while (found != NULL) {
1930           if ((found[len] == ',' || found[len] == '\n' || found[len] == '\0') &&
1931               (found == DeoptimizeOnlyAt || found[-1] == ',' || found[-1] == '\n')) {
1932             // Check that the bci found is bracketed by terminators.
1933             break;
1934           }
1935           found = strstr(found + 1, buffer);
1936         }
1937         if (!found) {
1938           continue;
1939         }
1940       }
1941 
1942       if (DebugDeoptimization && !deopt) {
1943         deopt = true; // One-time only print before deopt
1944         tty->print_cr("[BEFORE Deoptimization]");
1945         trace_frames();
1946         trace_stack();
1947       }
1948       Deoptimization::deoptimize(this, *fst.current());
1949     }
1950   }
1951 
1952   if (DebugDeoptimization && deopt) {
1953     tty->print_cr("[AFTER Deoptimization]");
1954     trace_frames();
1955   }
1956 }
1957 
1958 
1959 // Make zombies
1960 void JavaThread::make_zombies() {
1961   for (StackFrameStream fst(this, true /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
1962     if (fst.current()->can_be_deoptimized()) {
1963       // it is a Java nmethod
1964       nmethod* nm = CodeCache::find_nmethod(fst.current()->pc());
1965       nm->make_not_entrant();
1966     }
1967   }
1968 }
1969 #endif // PRODUCT
1970 
1971 
1972 void JavaThread::deoptimize_marked_methods() {
1973   if (!has_last_Java_frame()) return;
1974   StackFrameStream fst(this, false /* update */, true /* process_frames */);
1975   for (; !fst.is_done(); fst.next()) {
1976     if (fst.current()->should_be_deoptimized()) {
1977       Deoptimization::deoptimize(this, *fst.current());
1978     }
1979   }
1980 }
1981 
1982 #ifdef ASSERT
1983 void JavaThread::verify_frame_info() {
1984   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
1985          (has_last_Java_frame() && java_call_counter() > 0),
1986          "unexpected frame info: has_last_frame=%s, java_call_counter=%d",
1987          has_last_Java_frame() ? "true" : "false", java_call_counter());
1988 }
1989 #endif
1990 
1991 void JavaThread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) {
1992   // Verify that the deferred card marks have been flushed.
1993   assert(deferred_card_mark().is_empty(), "Should be empty during GC");
1994 
1995   // Traverse the GCHandles
1996   Thread::oops_do_no_frames(f, cf);
1997 
1998   DEBUG_ONLY(verify_frame_info();)
1999 
2000   if (has_last_Java_frame()) {
2001     // Traverse the monitor chunks
2002     for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
2003       chunk->oops_do(f);
2004     }
2005   }
2006 
2007   assert(vframe_array_head() == NULL, "deopt in progress at a safepoint!");
2008   // If we have deferred set_locals there might be oops waiting to be
2009   // written
2010   GrowableArray<jvmtiDeferredLocalVariableSet*>* list = JvmtiDeferredUpdates::deferred_locals(this);
2011   if (list != NULL) {
2012     for (int i = 0; i < list->length(); i++) {
2013       list->at(i)->oops_do(f);
2014     }
2015   }
2016 
2017   // Traverse instance variables at the end since the GC may be moving things
2018   // around using this function
2019   f->do_oop((oop*) &_vm_result);
2020   f->do_oop((oop*) &_exception_oop);
2021   f->do_oop((oop*) &_pending_async_exception);
2022 #if INCLUDE_JVMCI
2023   f->do_oop((oop*) &_jvmci_reserved_oop0);
2024 #endif
2025 
2026   if (jvmti_thread_state() != NULL) {
2027     jvmti_thread_state()->oops_do(f, cf);
2028   }
2029 
2030   if (LockingMode == LM_LIGHTWEIGHT) {
2031     lock_stack().oops_do(f);
2032   }
2033 }
2034 
2035 void JavaThread::oops_do_frames(OopClosure* f, CodeBlobClosure* cf) {
2036   if (!has_last_Java_frame()) {
2037     return;
2038   }
2039   // Finish any pending lazy GC activity for the frames
2040   StackWatermarkSet::finish_processing(this, NULL /* context */, StackWatermarkKind::gc);
2041   // Traverse the execution stack
2042   for (StackFrameStream fst(this, true /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
2043     fst.current()->oops_do(f, cf, fst.register_map());
2044   }
2045 }
2046 
2047 #ifdef ASSERT
2048 void JavaThread::verify_states_for_handshake() {
2049   // This checks that the thread has a correct frame state during a handshake.
2050   verify_frame_info();
2051 }
2052 #endif
2053 
2054 void JavaThread::nmethods_do(CodeBlobClosure* cf) {
2055   DEBUG_ONLY(verify_frame_info();)
2056 
2057   if (has_last_Java_frame()) {
2058     // Traverse the execution stack
2059     for (StackFrameStream fst(this, true /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
2060       fst.current()->nmethods_do(cf);
2061     }
2062   }
2063 
2064   if (jvmti_thread_state() != NULL) {
2065     jvmti_thread_state()->nmethods_do(cf);
2066   }
2067 }
2068 
2069 void JavaThread::metadata_do(MetadataClosure* f) {
2070   if (has_last_Java_frame()) {
2071     // Traverse the execution stack to call f() on the methods in the stack
2072     for (StackFrameStream fst(this, true /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
2073       fst.current()->metadata_do(f);
2074     }
2075   } else if (is_Compiler_thread()) {
2076     // need to walk ciMetadata in current compile tasks to keep alive.
2077     CompilerThread* ct = (CompilerThread*)this;
2078     if (ct->env() != NULL) {
2079       ct->env()->metadata_do(f);
2080     }
2081     CompileTask* task = ct->task();
2082     if (task != NULL) {
2083       task->metadata_do(f);
2084     }
2085   }
2086 }
2087 
2088 // Printing
2089 const char* _get_thread_state_name(JavaThreadState _thread_state) {
2090   switch (_thread_state) {
2091   case _thread_uninitialized:     return "_thread_uninitialized";
2092   case _thread_new:               return "_thread_new";
2093   case _thread_new_trans:         return "_thread_new_trans";
2094   case _thread_in_native:         return "_thread_in_native";
2095   case _thread_in_native_trans:   return "_thread_in_native_trans";
2096   case _thread_in_vm:             return "_thread_in_vm";
2097   case _thread_in_vm_trans:       return "_thread_in_vm_trans";
2098   case _thread_in_Java:           return "_thread_in_Java";
2099   case _thread_in_Java_trans:     return "_thread_in_Java_trans";
2100   case _thread_blocked:           return "_thread_blocked";
2101   case _thread_blocked_trans:     return "_thread_blocked_trans";
2102   default:                        return "unknown thread state";
2103   }
2104 }
2105 
2106 #ifndef PRODUCT
2107 void JavaThread::print_thread_state_on(outputStream *st) const {
2108   st->print_cr("   JavaThread state: %s", _get_thread_state_name(_thread_state));
2109 };
2110 #endif // PRODUCT
2111 
2112 // Called by Threads::print() for VM_PrintThreads operation
2113 void JavaThread::print_on(outputStream *st, bool print_extended_info) const {
2114   st->print_raw("\"");
2115   st->print_raw(get_thread_name());
2116   st->print_raw("\" ");
2117   oop thread_oop = threadObj();
2118   if (thread_oop != NULL) {
2119     st->print("#" INT64_FORMAT " ", (int64_t)java_lang_Thread::thread_id(thread_oop));
2120     if (java_lang_Thread::is_daemon(thread_oop))  st->print("daemon ");
2121     st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
2122   }
2123   Thread::print_on(st, print_extended_info);
2124   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
2125   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
2126   if (thread_oop != NULL) {
2127     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
2128   }
2129 #ifndef PRODUCT
2130   _safepoint_state->print_on(st);
2131 #endif // PRODUCT
2132   if (is_Compiler_thread()) {
2133     CompileTask *task = ((CompilerThread*)this)->task();
2134     if (task != NULL) {
2135       st->print("   Compiling: ");
2136       task->print(st, NULL, true, false);
2137     } else {
2138       st->print("   No compile task");
2139     }
2140     st->cr();
2141   }
2142 }
2143 
2144 void JavaThread::print() const { print_on(tty); }
2145 
2146 void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) const {
2147   st->print("%s", get_thread_name_string(buf, buflen));
2148 }
2149 
2150 // Called by fatal error handler. The difference between this and
2151 // JavaThread::print() is that we can't grab lock or allocate memory.
2152 void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
2153   st->print("JavaThread \"%s\"", get_thread_name_string(buf, buflen));
2154   oop thread_obj = threadObj();
2155   if (thread_obj != NULL) {
2156     if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
2157   }
2158   st->print(" [");
2159   st->print("%s", _get_thread_state_name(_thread_state));
2160   if (osthread()) {
2161     st->print(", id=%d", osthread()->thread_id());
2162   }
2163   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
2164             p2i(stack_end()), p2i(stack_base()));
2165   st->print("]");
2166 
2167   ThreadsSMRSupport::print_info_on(this, st);
2168   return;
2169 }
2170 
2171 
2172 // Verification
2173 
2174 void JavaThread::frames_do(void f(frame*, const RegisterMap* map)) {
2175   // ignore if there is no stack
2176   if (!has_last_Java_frame()) return;
2177   // traverse the stack frames. Starts from top frame.
2178   for (StackFrameStream fst(this, true /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
2179     frame* fr = fst.current();
2180     f(fr, fst.register_map());
2181   }
2182 }
2183 
2184 static void frame_verify(frame* f, const RegisterMap *map) { f->verify(map); }
2185 
2186 void JavaThread::verify() {
2187   // Verify oops in the thread.
2188   oops_do(&VerifyOopClosure::verify_oop, NULL);
2189 
2190   // Verify the stack frames.
2191   frames_do(frame_verify);
2192 }
2193 
2194 // CR 6300358 (sub-CR 2137150)
2195 // Most callers of this method assume that it can't return NULL but a
2196 // thread may not have a name whilst it is in the process of attaching to
2197 // the VM - see CR 6412693, and there are places where a JavaThread can be
2198 // seen prior to having its threadObj set (e.g., JNI attaching threads and
2199 // if vm exit occurs during initialization). These cases can all be accounted
2200 // for such that this method never returns NULL.
2201 const char* JavaThread::get_thread_name() const {
2202   if (Thread::is_JavaThread_protected(this)) {
2203     // The target JavaThread is protected so get_thread_name_string() is safe:
2204     return get_thread_name_string();
2205   }
2206 
2207   // The target JavaThread is not protected so we return the default:
2208   return Thread::name();
2209 }
2210 
2211 // Returns a non-NULL representation of this thread's name, or a suitable
2212 // descriptive string if there is no set name.
2213 const char* JavaThread::get_thread_name_string(char* buf, int buflen) const {
2214   const char* name_str;
2215   oop thread_obj = threadObj();
2216   if (thread_obj != NULL) {
2217     oop name = java_lang_Thread::name(thread_obj);
2218     if (name != NULL) {
2219       if (buf == NULL) {
2220         name_str = java_lang_String::as_utf8_string(name);
2221       } else {
2222         name_str = java_lang_String::as_utf8_string(name, buf, buflen);
2223       }
2224     } else if (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
2225       name_str = "<no-name - thread is attaching>";
2226     } else {
2227       name_str = Thread::name();
2228     }
2229   } else {
2230     name_str = Thread::name();
2231   }
2232   assert(name_str != NULL, "unexpected NULL thread name");
2233   return name_str;
2234 }
2235 
2236 // Helper to extract the name from the thread oop for logging.
2237 const char* JavaThread::name_for(oop thread_obj) {
2238   assert(thread_obj != NULL, "precondition");
2239   oop name = java_lang_Thread::name(thread_obj);
2240   const char* name_str;
2241   if (name != NULL) {
2242     name_str = java_lang_String::as_utf8_string(name);
2243   } else {
2244     name_str = "<un-named>";
2245   }
2246   return name_str;
2247 }
2248 
2249 void JavaThread::prepare(jobject jni_thread, ThreadPriority prio) {
2250 
2251   assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
2252   assert(NoPriority <= prio && prio <= MaxPriority, "sanity check");
2253   // Link Java Thread object <-> C++ Thread
2254 
2255   // Get the C++ thread object (an oop) from the JNI handle (a jthread)
2256   // and put it into a new Handle.  The Handle "thread_oop" can then
2257   // be used to pass the C++ thread object to other methods.
2258 
2259   // Set the Java level thread object (jthread) field of the
2260   // new thread (a JavaThread *) to C++ thread object using the
2261   // "thread_oop" handle.
2262 
2263   // Set the thread field (a JavaThread *) of the
2264   // oop representing the java_lang_Thread to the new thread (a JavaThread *).
2265 
2266   Handle thread_oop(Thread::current(),
2267                     JNIHandles::resolve_non_null(jni_thread));
2268   assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
2269          "must be initialized");
2270   set_threadObj(thread_oop());
2271   java_lang_Thread::set_thread(thread_oop(), this);
2272 
2273   if (prio == NoPriority) {
2274     prio = java_lang_Thread::priority(thread_oop());
2275     assert(prio != NoPriority, "A valid priority should be present");
2276   }
2277 
2278   // Push the Java priority down to the native thread; needs Threads_lock
2279   Thread::set_priority(this, prio);
2280 
2281   // Add the new thread to the Threads list and set it in motion.
2282   // We must have threads lock in order to call Threads::add.
2283   // It is crucial that we do not block before the thread is
2284   // added to the Threads list for if a GC happens, then the java_thread oop
2285   // will not be visited by GC.
2286   Threads::add(this);
2287 }
2288 
2289 oop JavaThread::current_park_blocker() {
2290   // Support for JSR-166 locks
2291   oop thread_oop = threadObj();
2292   if (thread_oop != NULL) {
2293     return java_lang_Thread::park_blocker(thread_oop);
2294   }
2295   return NULL;
2296 }
2297 
2298 
2299 void JavaThread::print_stack_on(outputStream* st) {
2300   if (!has_last_Java_frame()) return;
2301 
2302   Thread* current_thread = Thread::current();
2303   ResourceMark rm(current_thread);
2304   HandleMark hm(current_thread);
2305 
2306   RegisterMap reg_map(this);
2307   vframe* start_vf = last_java_vframe(&reg_map);
2308   int count = 0;
2309   for (vframe* f = start_vf; f != NULL; f = f->sender()) {
2310     if (f->is_java_frame()) {
2311       javaVFrame* jvf = javaVFrame::cast(f);
2312       java_lang_Throwable::print_stack_element(st, jvf->method(), jvf->bci());
2313 
2314       // Print out lock information
2315       if (JavaMonitorsInStackTrace) {
2316         jvf->print_lock_info_on(st, count);
2317       }
2318     } else {
2319       // Ignore non-Java frames
2320     }
2321 
2322     // Bail-out case for too deep stacks if MaxJavaStackTraceDepth > 0
2323     count++;
2324     if (MaxJavaStackTraceDepth > 0 && MaxJavaStackTraceDepth == count) return;
2325   }
2326 }
2327 
2328 
2329 // JVMTI PopFrame support
2330 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) {
2331   assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments");
2332   if (in_bytes(size_in_bytes) != 0) {
2333     _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes), mtThread);
2334     _popframe_preserved_args_size = in_bytes(size_in_bytes);
2335     Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size);
2336   }
2337 }
2338 
2339 void* JavaThread::popframe_preserved_args() {
2340   return _popframe_preserved_args;
2341 }
2342 
2343 ByteSize JavaThread::popframe_preserved_args_size() {
2344   return in_ByteSize(_popframe_preserved_args_size);
2345 }
2346 
2347 WordSize JavaThread::popframe_preserved_args_size_in_words() {
2348   int sz = in_bytes(popframe_preserved_args_size());
2349   assert(sz % wordSize == 0, "argument size must be multiple of wordSize");
2350   return in_WordSize(sz / wordSize);
2351 }
2352 
2353 void JavaThread::popframe_free_preserved_args() {
2354   assert(_popframe_preserved_args != NULL, "should not free PopFrame preserved arguments twice");
2355   FREE_C_HEAP_ARRAY(char, (char*)_popframe_preserved_args);
2356   _popframe_preserved_args = NULL;
2357   _popframe_preserved_args_size = 0;
2358 }
2359 
2360 #ifndef PRODUCT
2361 
2362 void JavaThread::trace_frames() {
2363   tty->print_cr("[Describe stack]");
2364   int frame_no = 1;
2365   for (StackFrameStream fst(this, true /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
2366     tty->print("  %d. ", frame_no++);
2367     fst.current()->print_value_on(tty, this);
2368     tty->cr();
2369   }
2370 }
2371 
2372 class PrintAndVerifyOopClosure: public OopClosure {
2373  protected:
2374   template <class T> inline void do_oop_work(T* p) {
2375     oop obj = RawAccess<>::oop_load(p);
2376     if (obj == NULL) return;
2377     tty->print(INTPTR_FORMAT ": ", p2i(p));
2378     if (oopDesc::is_oop_or_null(obj)) {
2379       if (obj->is_objArray()) {
2380         tty->print_cr("valid objArray: " INTPTR_FORMAT, p2i(obj));
2381       } else {
2382         obj->print();
2383       }
2384     } else {
2385       tty->print_cr("invalid oop: " INTPTR_FORMAT, p2i(obj));
2386     }
2387     tty->cr();
2388   }
2389  public:
2390   virtual void do_oop(oop* p) { do_oop_work(p); }
2391   virtual void do_oop(narrowOop* p)  { do_oop_work(p); }
2392 };
2393 
2394 #ifdef ASSERT
2395 // Print or validate the layout of stack frames
2396 void JavaThread::print_frame_layout(int depth, bool validate_only) {
2397   ResourceMark rm;
2398   PreserveExceptionMark pm(this);
2399   FrameValues values;
2400   int frame_no = 0;
2401   for (StackFrameStream fst(this, false /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
2402     fst.current()->describe(values, ++frame_no);
2403     if (depth == frame_no) break;
2404   }
2405   if (validate_only) {
2406     values.validate();
2407   } else {
2408     tty->print_cr("[Describe stack layout]");
2409     values.print(this);
2410   }
2411 }
2412 #endif
2413 
2414 void JavaThread::trace_stack_from(vframe* start_vf) {
2415   ResourceMark rm;
2416   int vframe_no = 1;
2417   for (vframe* f = start_vf; f; f = f->sender()) {
2418     if (f->is_java_frame()) {
2419       javaVFrame::cast(f)->print_activation(vframe_no++);
2420     } else {
2421       f->print();
2422     }
2423     if (vframe_no > StackPrintLimit) {
2424       tty->print_cr("...<more frames>...");
2425       return;
2426     }
2427   }
2428 }
2429 
2430 
2431 void JavaThread::trace_stack() {
2432   if (!has_last_Java_frame()) return;
2433   Thread* current_thread = Thread::current();
2434   ResourceMark rm(current_thread);
2435   HandleMark hm(current_thread);
2436   RegisterMap reg_map(this);
2437   trace_stack_from(last_java_vframe(&reg_map));
2438 }
2439 
2440 
2441 #endif // PRODUCT
2442 
2443 
2444 javaVFrame* JavaThread::last_java_vframe(RegisterMap *reg_map) {
2445   assert(reg_map != NULL, "a map must be given");
2446   frame f = last_frame();
2447   for (vframe* vf = vframe::new_vframe(&f, reg_map, this); vf; vf = vf->sender()) {
2448     if (vf->is_java_frame()) return javaVFrame::cast(vf);
2449   }
2450   return NULL;
2451 }
2452 
2453 
2454 Klass* JavaThread::security_get_caller_class(int depth) {
2455   vframeStream vfst(this);
2456   vfst.security_get_caller_frame(depth);
2457   if (!vfst.at_end()) {
2458     return vfst.method()->method_holder();
2459   }
2460   return NULL;
2461 }
2462 
2463 // java.lang.Thread.sleep support
2464 // Returns true if sleep time elapsed as expected, and false
2465 // if the thread was interrupted.
2466 bool JavaThread::sleep(jlong millis) {
2467   assert(this == Thread::current(),  "thread consistency check");
2468 
2469   ParkEvent * const slp = this->_SleepEvent;
2470   // Because there can be races with thread interruption sending an unpark()
2471   // to the event, we explicitly reset it here to avoid an immediate return.
2472   // The actual interrupt state will be checked before we park().
2473   slp->reset();
2474   // Thread interruption establishes a happens-before ordering in the
2475   // Java Memory Model, so we need to ensure we synchronize with the
2476   // interrupt state.
2477   OrderAccess::fence();
2478 
2479   jlong prevtime = os::javaTimeNanos();
2480 
2481   for (;;) {
2482     // interruption has precedence over timing out
2483     if (this->is_interrupted(true)) {
2484       return false;
2485     }
2486 
2487     if (millis <= 0) {
2488       return true;
2489     }
2490 
2491     {
2492       ThreadBlockInVM tbivm(this);
2493       OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
2494       slp->park(millis);
2495     }
2496 
2497     // Update elapsed time tracking
2498     jlong newtime = os::javaTimeNanos();
2499     if (newtime - prevtime < 0) {
2500       // time moving backwards, should only happen if no monotonic clock
2501       // not a guarantee() because JVM should not abort on kernel/glibc bugs
2502       assert(false,
2503              "unexpected time moving backwards detected in JavaThread::sleep()");
2504     } else {
2505       millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
2506     }
2507     prevtime = newtime;
2508   }
2509 }
2510 
2511 
2512 // ======= Threads ========
2513 
2514 // The Threads class links together all active threads, and provides
2515 // operations over all threads. It is protected by the Threads_lock,
2516 // which is also used in other global contexts like safepointing.
2517 // ThreadsListHandles are used to safely perform operations on one
2518 // or more threads without the risk of the thread exiting during the
2519 // operation.
2520 //
2521 // Note: The Threads_lock is currently more widely used than we
2522 // would like. We are actively migrating Threads_lock uses to other
2523 // mechanisms in order to reduce Threads_lock contention.
2524 
2525 int         Threads::_number_of_threads = 0;
2526 int         Threads::_number_of_non_daemon_threads = 0;
2527 int         Threads::_return_code = 0;
2528 uintx       Threads::_thread_claim_token = 1; // Never zero.
2529 size_t      JavaThread::_stack_size_at_create = 0;
2530 
2531 #ifdef ASSERT
2532 bool        Threads::_vm_complete = false;
2533 #endif
2534 
2535 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
2536   Prefetch::read((void*)addr, prefetch_interval);
2537   return *addr;
2538 }
2539 
2540 // Possibly the ugliest for loop the world has seen. C++ does not allow
2541 // multiple types in the declaration section of the for loop. In this case
2542 // we are only dealing with pointers and hence can cast them. It looks ugly
2543 // but macros are ugly and therefore it's fine to make things absurdly ugly.
2544 #define DO_JAVA_THREADS(LIST, X)                                                                                          \
2545     for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes,                           \
2546              *MACRO_list = (JavaThread*)(LIST),                                                                           \
2547              **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(),  \
2548              **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(),                                     \
2549              *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval);                 \
2550          MACRO_current_p != MACRO_end;                                                                                    \
2551          MACRO_current_p++,                                                                                               \
2552              X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
2553 
2554 // All JavaThreads
2555 #define ALL_JAVA_THREADS(X) DO_JAVA_THREADS(ThreadsSMRSupport::get_java_thread_list(), X)
2556 
2557 // All NonJavaThreads (i.e., every non-JavaThread in the system).
2558 void Threads::non_java_threads_do(ThreadClosure* tc) {
2559   NoSafepointVerifier nsv;
2560   for (NonJavaThread::Iterator njti; !njti.end(); njti.step()) {
2561     tc->do_thread(njti.current());
2562   }
2563 }
2564 
2565 // All JavaThreads
2566 void Threads::java_threads_do(ThreadClosure* tc) {
2567   assert_locked_or_safepoint(Threads_lock);
2568   // ALL_JAVA_THREADS iterates through all JavaThreads.
2569   ALL_JAVA_THREADS(p) {
2570     tc->do_thread(p);
2571   }
2572 }
2573 
2574 void Threads::java_threads_and_vm_thread_do(ThreadClosure* tc) {
2575   assert_locked_or_safepoint(Threads_lock);
2576   java_threads_do(tc);
2577   tc->do_thread(VMThread::vm_thread());
2578 }
2579 
2580 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system).
2581 void Threads::threads_do(ThreadClosure* tc) {
2582   assert_locked_or_safepoint(Threads_lock);
2583   java_threads_do(tc);
2584   non_java_threads_do(tc);
2585 }
2586 
2587 void Threads::possibly_parallel_threads_do(bool is_par, ThreadClosure* tc) {
2588   uintx claim_token = Threads::thread_claim_token();
2589   ALL_JAVA_THREADS(p) {
2590     if (p->claim_threads_do(is_par, claim_token)) {
2591       tc->do_thread(p);
2592     }
2593   }
2594   VMThread* vmt = VMThread::vm_thread();
2595   if (vmt->claim_threads_do(is_par, claim_token)) {
2596     tc->do_thread(vmt);
2597   }
2598 }
2599 
2600 // The system initialization in the library has three phases.
2601 //
2602 // Phase 1: java.lang.System class initialization
2603 //     java.lang.System is a primordial class loaded and initialized
2604 //     by the VM early during startup.  java.lang.System.<clinit>
2605 //     only does registerNatives and keeps the rest of the class
2606 //     initialization work later until thread initialization completes.
2607 //
2608 //     System.initPhase1 initializes the system properties, the static
2609 //     fields in, out, and err. Set up java signal handlers, OS-specific
2610 //     system settings, and thread group of the main thread.
2611 static void call_initPhase1(TRAPS) {
2612   Klass* klass = vmClasses::System_klass();
2613   JavaValue result(T_VOID);
2614   JavaCalls::call_static(&result, klass, vmSymbols::initPhase1_name(),
2615                                          vmSymbols::void_method_signature(), CHECK);
2616 }
2617 
2618 // Phase 2. Module system initialization
2619 //     This will initialize the module system.  Only java.base classes
2620 //     can be loaded until phase 2 completes.
2621 //
2622 //     Call System.initPhase2 after the compiler initialization and jsr292
2623 //     classes get initialized because module initialization runs a lot of java
2624 //     code, that for performance reasons, should be compiled.  Also, this will
2625 //     enable the startup code to use lambda and other language features in this
2626 //     phase and onward.
2627 //
2628 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
2629 static void call_initPhase2(TRAPS) {
2630   TraceTime timer("Initialize module system", TRACETIME_LOG(Info, startuptime));
2631 
2632   Klass* klass = vmClasses::System_klass();
2633 
2634   JavaValue result(T_INT);
2635   JavaCallArguments args;
2636   args.push_int(DisplayVMOutputToStderr);
2637   args.push_int(log_is_enabled(Debug, init)); // print stack trace if exception thrown
2638   JavaCalls::call_static(&result, klass, vmSymbols::initPhase2_name(),
2639                                          vmSymbols::boolean_boolean_int_signature(), &args, CHECK);
2640   if (result.get_jint() != JNI_OK) {
2641     vm_exit_during_initialization(); // no message or exception
2642   }
2643 
2644   universe_post_module_init();
2645 }
2646 
2647 // Phase 3. final setup - set security manager, system class loader and TCCL
2648 //
2649 //     This will instantiate and set the security manager, set the system class
2650 //     loader as well as the thread context class loader.  The security manager
2651 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
2652 //     other modules or the application's classpath.
2653 static void call_initPhase3(TRAPS) {
2654   Klass* klass = vmClasses::System_klass();
2655   JavaValue result(T_VOID);
2656   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
2657                                          vmSymbols::void_method_signature(), CHECK);
2658 }
2659 
2660 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
2661   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
2662 
2663   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
2664     create_vm_init_libraries();
2665   }
2666 
2667   initialize_class(vmSymbols::java_lang_String(), CHECK);
2668 
2669   // Inject CompactStrings value after the static initializers for String ran.
2670   java_lang_String::set_compact_strings(CompactStrings);
2671 
2672   // Initialize java_lang.System (needed before creating the thread)
2673   initialize_class(vmSymbols::java_lang_System(), CHECK);
2674   // The VM creates & returns objects of this class. Make sure it's initialized.
2675   initialize_class(vmSymbols::java_lang_Class(), CHECK);
2676   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
2677   Handle thread_group = create_initial_thread_group(CHECK);
2678   Universe::set_main_thread_group(thread_group());
2679   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
2680   create_initial_thread(thread_group, main_thread, CHECK);
2681 
2682   // The VM creates objects of this class.
2683   initialize_class(vmSymbols::java_lang_Module(), CHECK);
2684 
2685 #ifdef ASSERT
2686   InstanceKlass *k = vmClasses::UnsafeConstants_klass();
2687   assert(k->is_not_initialized(), "UnsafeConstants should not already be initialized");
2688 #endif
2689 
2690   // initialize the hardware-specific constants needed by Unsafe
2691   initialize_class(vmSymbols::jdk_internal_misc_UnsafeConstants(), CHECK);
2692   jdk_internal_misc_UnsafeConstants::set_unsafe_constants();
2693 
2694   // The VM preresolves methods to these classes. Make sure that they get initialized
2695   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
2696   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
2697 
2698   // Phase 1 of the system initialization in the library, java.lang.System class initialization
2699   call_initPhase1(CHECK);
2700 
2701   // Get the Java runtime name, version, and vendor info after java.lang.System is initialized.
2702   // Some values are actually configure-time constants but some can be set via the jlink tool and
2703   // so must be read dynamically. We treat them all the same.
2704   InstanceKlass* ik = SystemDictionary::find_instance_klass(vmSymbols::java_lang_VersionProps(),
2705                                                             Handle(), Handle());
2706   {
2707     ResourceMark rm(main_thread);
2708     JDK_Version::set_java_version(get_java_version_info(ik, vmSymbols::java_version_name()));
2709 
2710     JDK_Version::set_runtime_name(get_java_version_info(ik, vmSymbols::java_runtime_name_name()));
2711 
2712     JDK_Version::set_runtime_version(get_java_version_info(ik, vmSymbols::java_runtime_version_name()));
2713 
2714     JDK_Version::set_runtime_vendor_version(get_java_version_info(ik, vmSymbols::java_runtime_vendor_version_name()));
2715 
2716     JDK_Version::set_runtime_vendor_vm_bug_url(get_java_version_info(ik, vmSymbols::java_runtime_vendor_vm_bug_url_name()));
2717   }
2718 
2719   // an instance of OutOfMemory exception has been allocated earlier
2720   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
2721   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
2722   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
2723   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
2724   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
2725   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
2726   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
2727   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);
2728 }
2729 
2730 void Threads::initialize_jsr292_core_classes(TRAPS) {
2731   TraceTime timer("Initialize java.lang.invoke classes", TRACETIME_LOG(Info, startuptime));
2732 
2733   initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK);
2734   initialize_class(vmSymbols::java_lang_invoke_ResolvedMethodName(), CHECK);
2735   initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK);
2736   initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK);
2737 }
2738 
2739 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
2740   extern void JDK_Version_init();
2741 
2742   // Preinitialize version info.
2743   VM_Version::early_initialize();
2744 
2745   // Check version
2746   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
2747 
2748   // Initialize library-based TLS
2749   ThreadLocalStorage::init();
2750 
2751   // Initialize the output stream module
2752   ostream_init();
2753 
2754   // Process java launcher properties.
2755   Arguments::process_sun_java_launcher_properties(args);
2756 
2757   // Initialize the os module
2758   os::init();
2759 
2760   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXWrite));
2761 
2762   // Record VM creation timing statistics
2763   TraceVmCreationTime create_vm_timer;
2764   create_vm_timer.start();
2765 
2766   // Initialize system properties.
2767   Arguments::init_system_properties();
2768 
2769   // So that JDK version can be used as a discriminator when parsing arguments
2770   JDK_Version_init();
2771 
2772   // Update/Initialize System properties after JDK version number is known
2773   Arguments::init_version_specific_system_properties();
2774 
2775   // Make sure to initialize log configuration *before* parsing arguments
2776   LogConfiguration::initialize(create_vm_timer.begin_time());
2777 
2778   // Parse arguments
2779   // Note: this internally calls os::init_container_support()
2780   jint parse_result = Arguments::parse(args);
2781   if (parse_result != JNI_OK) return parse_result;
2782 
2783 #if INCLUDE_NMT
2784   // Initialize NMT right after argument parsing to keep the pre-NMT-init window small.
2785   MemTracker::initialize();
2786 #endif // INCLUDE_NMT
2787 
2788   os::init_before_ergo();
2789 
2790   jint ergo_result = Arguments::apply_ergo();
2791   if (ergo_result != JNI_OK) return ergo_result;
2792 
2793   // Final check of all ranges after ergonomics which may change values.
2794   if (!JVMFlagLimit::check_all_ranges()) {
2795     return JNI_EINVAL;
2796   }
2797 
2798   // Final check of all 'AfterErgo' constraints after ergonomics which may change values.
2799   bool constraint_result = JVMFlagLimit::check_all_constraints(JVMFlagConstraintPhase::AfterErgo);
2800   if (!constraint_result) {
2801     return JNI_EINVAL;
2802   }
2803 
2804   if (PauseAtStartup) {
2805     os::pause();
2806   }
2807 
2808   HOTSPOT_VM_INIT_BEGIN();
2809 
2810   // Timing (must come after argument parsing)
2811   TraceTime timer("Create VM", TRACETIME_LOG(Info, startuptime));
2812 
2813   // Initialize the os module after parsing the args
2814   jint os_init_2_result = os::init_2();
2815   if (os_init_2_result != JNI_OK) return os_init_2_result;
2816 
2817 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
2818   // Initialize assert poison page mechanism.
2819   if (ShowRegistersOnAssert) {
2820     initialize_assert_poison();
2821   }
2822 #endif // CAN_SHOW_REGISTERS_ON_ASSERT
2823 
2824   SafepointMechanism::initialize();
2825 
2826   jint adjust_after_os_result = Arguments::adjust_after_os();
2827   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
2828 
2829   // Initialize output stream logging
2830   ostream_init_log();
2831 
2832   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
2833   // Must be before create_vm_init_agents()
2834   if (Arguments::init_libraries_at_startup()) {
2835     convert_vm_init_libraries_to_agents();
2836   }
2837 
2838   // Should happen before any agent attaches and pokes into vmStructs
2839 #if INCLUDE_VM_STRUCTS
2840   if (UseCompactObjectHeaders) {
2841     VMStructs::compact_headers_overrides();
2842   }
2843 #endif
2844 
2845   // Launch -agentlib/-agentpath and converted -Xrun agents
2846   if (Arguments::init_agents_at_startup()) {
2847     create_vm_init_agents();
2848   }
2849 
2850   // Initialize Threads state
2851   _number_of_threads = 0;
2852   _number_of_non_daemon_threads = 0;
2853 
2854   // Initialize global data structures and create system classes in heap
2855   vm_init_globals();
2856 
2857 #if INCLUDE_JVMCI
2858   if (JVMCICounterSize > 0) {
2859     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtJVMCI);
2860     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
2861   } else {
2862     JavaThread::_jvmci_old_thread_counters = NULL;
2863   }
2864 #endif // INCLUDE_JVMCI
2865 
2866   // Initialize OopStorage for threadObj
2867   _thread_oop_storage = OopStorageSet::create_strong("Thread OopStorage", mtThread);
2868 
2869   // Attach the main thread to this os thread
2870   JavaThread* main_thread = new JavaThread();
2871   main_thread->set_thread_state(_thread_in_vm);
2872   main_thread->initialize_thread_current();
2873   // must do this before set_active_handles
2874   main_thread->record_stack_base_and_size();
2875   main_thread->register_thread_stack_with_NMT();
2876   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
2877   MACOS_AARCH64_ONLY(main_thread->init_wx());
2878 
2879   if (!main_thread->set_as_starting_thread()) {
2880     vm_shutdown_during_initialization(
2881                                       "Failed necessary internal allocation. Out of swap space");
2882     main_thread->smr_delete();
2883     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
2884     return JNI_ENOMEM;
2885   }
2886 
2887   // Enable guard page *after* os::create_main_thread(), otherwise it would
2888   // crash Linux VM, see notes in os_linux.cpp.
2889   main_thread->stack_overflow_state()->create_stack_guard_pages();
2890 
2891   // Initialize Java-Level synchronization subsystem
2892   ObjectMonitor::Initialize();
2893   ObjectSynchronizer::initialize();
2894 
2895   // Initialize global modules
2896   jint status = init_globals();
2897   if (status != JNI_OK) {
2898     main_thread->smr_delete();
2899     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
2900     return status;
2901   }
2902 
2903   JFR_ONLY(Jfr::on_create_vm_1();)
2904 
2905   // Should be done after the heap is fully created
2906   main_thread->cache_global_variables();
2907 
2908   { MutexLocker mu(Threads_lock);
2909     Threads::add(main_thread);
2910   }
2911 
2912   // Any JVMTI raw monitors entered in onload will transition into
2913   // real raw monitor. VM is setup enough here for raw monitor enter.
2914   JvmtiExport::transition_pending_onload_raw_monitors();
2915 
2916   // Create the VMThread
2917   { TraceTime timer("Start VMThread", TRACETIME_LOG(Info, startuptime));
2918 
2919     VMThread::create();
2920     Thread* vmthread = VMThread::vm_thread();
2921 
2922     if (!os::create_thread(vmthread, os::vm_thread)) {
2923       vm_exit_during_initialization("Cannot create VM thread. "
2924                                     "Out of system resources.");
2925     }
2926 
2927     // Wait for the VM thread to become ready, and VMThread::run to initialize
2928     // Monitors can have spurious returns, must always check another state flag
2929     {
2930       MonitorLocker ml(Notify_lock);
2931       os::start_thread(vmthread);
2932       while (vmthread->active_handles() == NULL) {
2933         ml.wait();
2934       }
2935     }
2936   }
2937 
2938   assert(Universe::is_fully_initialized(), "not initialized");
2939   if (VerifyDuringStartup) {
2940     // Make sure we're starting with a clean slate.
2941     VM_Verify verify_op;
2942     VMThread::execute(&verify_op);
2943   }
2944 
2945   // We need this to update the java.vm.info property in case any flags used
2946   // to initially define it have been changed. This is needed for both CDS
2947   // since UseSharedSpaces may be changed after java.vm.info
2948   // is initially computed. See Abstract_VM_Version::vm_info_string().
2949   // This update must happen before we initialize the java classes, but
2950   // after any initialization logic that might modify the flags.
2951   Arguments::update_vm_info_property(VM_Version::vm_info_string());
2952 
2953   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2954   HandleMark hm(THREAD);
2955 
2956   // Always call even when there are not JVMTI environments yet, since environments
2957   // may be attached late and JVMTI must track phases of VM execution
2958   JvmtiExport::enter_early_start_phase();
2959 
2960   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
2961   JvmtiExport::post_early_vm_start();
2962 
2963   initialize_java_lang_classes(main_thread, CHECK_JNI_ERR);
2964 
2965   quicken_jni_functions();
2966 
2967   // No more stub generation allowed after that point.
2968   StubCodeDesc::freeze();
2969 
2970   // Set flag that basic initialization has completed. Used by exceptions and various
2971   // debug stuff, that does not work until all basic classes have been initialized.
2972   set_init_completed();
2973 
2974   LogConfiguration::post_initialize();
2975   Metaspace::post_initialize();
2976 
2977   HOTSPOT_VM_INIT_END();
2978 
2979   // record VM initialization completion time
2980 #if INCLUDE_MANAGEMENT
2981   Management::record_vm_init_completed();
2982 #endif // INCLUDE_MANAGEMENT
2983 
2984   // Signal Dispatcher needs to be started before VMInit event is posted
2985   os::initialize_jdk_signal_support(CHECK_JNI_ERR);
2986 
2987   // Start Attach Listener if +StartAttachListener or it can't be started lazily
2988   if (!DisableAttachMechanism) {
2989     AttachListener::vm_start();
2990     if (StartAttachListener || AttachListener::init_at_startup()) {
2991       AttachListener::init();
2992     }
2993   }
2994 
2995   // Launch -Xrun agents
2996   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
2997   // back-end can launch with -Xdebug -Xrunjdwp.
2998   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
2999     create_vm_init_libraries();
3000   }
3001 
3002   Chunk::start_chunk_pool_cleaner_task();
3003 
3004   // Start the service thread
3005   // The service thread enqueues JVMTI deferred events and does various hashtable
3006   // and other cleanups.  Needs to start before the compilers start posting events.
3007   ServiceThread::initialize();
3008 
3009   // Start the monitor deflation thread:
3010   MonitorDeflationThread::initialize();
3011 
3012   // initialize compiler(s)
3013 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
3014 #if INCLUDE_JVMCI
3015   bool force_JVMCI_intialization = false;
3016   if (EnableJVMCI) {
3017     // Initialize JVMCI eagerly when it is explicitly requested.
3018     // Or when JVMCILibDumpJNIConfig or JVMCIPrintProperties is enabled.
3019     force_JVMCI_intialization = EagerJVMCI || JVMCIPrintProperties || JVMCILibDumpJNIConfig;
3020 
3021     if (!force_JVMCI_intialization) {
3022       // 8145270: Force initialization of JVMCI runtime otherwise requests for blocking
3023       // compilations via JVMCI will not actually block until JVMCI is initialized.
3024       force_JVMCI_intialization = UseJVMCICompiler && (!UseInterpreter || !BackgroundCompilation);
3025     }
3026   }
3027 #endif
3028   CompileBroker::compilation_init_phase1(CHECK_JNI_ERR);
3029   // Postpone completion of compiler initialization to after JVMCI
3030   // is initialized to avoid timeouts of blocking compilations.
3031   if (JVMCI_ONLY(!force_JVMCI_intialization) NOT_JVMCI(true)) {
3032     CompileBroker::compilation_init_phase2();
3033   }
3034 #endif
3035 
3036   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3037   // It is done after compilers are initialized, because otherwise compilations of
3038   // signature polymorphic MH intrinsics can be missed
3039   // (see SystemDictionary::find_method_handle_intrinsic).
3040   initialize_jsr292_core_classes(CHECK_JNI_ERR);
3041 
3042   // This will initialize the module system.  Only java.base classes can be
3043   // loaded until phase 2 completes
3044   call_initPhase2(CHECK_JNI_ERR);
3045 
3046   JFR_ONLY(Jfr::on_create_vm_2();)
3047 
3048   // Always call even when there are not JVMTI environments yet, since environments
3049   // may be attached late and JVMTI must track phases of VM execution
3050   JvmtiExport::enter_start_phase();
3051 
3052   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
3053   JvmtiExport::post_vm_start();
3054 
3055   // Final system initialization including security manager and system class loader
3056   call_initPhase3(CHECK_JNI_ERR);
3057 
3058   // cache the system and platform class loaders
3059   SystemDictionary::compute_java_loaders(CHECK_JNI_ERR);
3060 
3061 #if INCLUDE_CDS
3062   // capture the module path info from the ModuleEntryTable
3063   ClassLoader::initialize_module_path(THREAD);
3064   if (HAS_PENDING_EXCEPTION) {
3065     java_lang_Throwable::print(PENDING_EXCEPTION, tty);
3066     vm_exit_during_initialization("ClassLoader::initialize_module_path() failed unexpectedly");
3067   }
3068 #endif
3069 
3070 #if INCLUDE_JVMCI
3071   if (force_JVMCI_intialization) {
3072     JVMCI::initialize_compiler(CHECK_JNI_ERR);
3073     CompileBroker::compilation_init_phase2();
3074   }
3075 #endif
3076 
3077   // Always call even when there are not JVMTI environments yet, since environments
3078   // may be attached late and JVMTI must track phases of VM execution
3079   JvmtiExport::enter_live_phase();
3080 
3081   // Make perfmemory accessible
3082   PerfMemory::set_accessible(true);
3083 
3084   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3085   JvmtiExport::post_vm_initialized();
3086 
3087   JFR_ONLY(Jfr::on_create_vm_3();)
3088 
3089 #if INCLUDE_MANAGEMENT
3090   Management::initialize(THREAD);
3091 
3092   if (HAS_PENDING_EXCEPTION) {
3093     // management agent fails to start possibly due to
3094     // configuration problem and is responsible for printing
3095     // stack trace if appropriate. Simply exit VM.
3096     vm_exit(1);
3097   }
3098 #endif // INCLUDE_MANAGEMENT
3099 
3100   StatSampler::engage();
3101   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3102 
3103   BiasedLocking::init();
3104 
3105 #if INCLUDE_RTM_OPT
3106   RTMLockingCounters::init();
3107 #endif
3108 
3109   call_postVMInitHook(THREAD);
3110   // The Java side of PostVMInitHook.run must deal with all
3111   // exceptions and provide means of diagnosis.
3112   if (HAS_PENDING_EXCEPTION) {
3113     CLEAR_PENDING_EXCEPTION;
3114   }
3115 
3116   {
3117     MutexLocker ml(PeriodicTask_lock);
3118     // Make sure the WatcherThread can be started by WatcherThread::start()
3119     // or by dynamic enrollment.
3120     WatcherThread::make_startable();
3121     // Start up the WatcherThread if there are any periodic tasks
3122     // NOTE:  All PeriodicTasks should be registered by now. If they
3123     //   aren't, late joiners might appear to start slowly (we might
3124     //   take a while to process their first tick).
3125     if (PeriodicTask::num_tasks() > 0) {
3126       WatcherThread::start();
3127     }
3128   }
3129 
3130   create_vm_timer.end();
3131 #ifdef ASSERT
3132   _vm_complete = true;
3133 #endif
3134 
3135   if (DumpSharedSpaces) {
3136     MetaspaceShared::preload_and_dump();
3137     ShouldNotReachHere();
3138   }
3139 
3140   return JNI_OK;
3141 }
3142 
3143 // type for the Agent_OnLoad and JVM_OnLoad entry points
3144 extern "C" {
3145   typedef jint (JNICALL *OnLoadEntry_t)(JavaVM *, char *, void *);
3146 }
3147 // Find a command line agent library and return its entry point for
3148 //         -agentlib:  -agentpath:   -Xrun
3149 // num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array.
3150 static OnLoadEntry_t lookup_on_load(AgentLibrary* agent,
3151                                     const char *on_load_symbols[],
3152                                     size_t num_symbol_entries) {
3153   OnLoadEntry_t on_load_entry = NULL;
3154   void *library = NULL;
3155 
3156   if (!agent->valid()) {
3157     char buffer[JVM_MAXPATHLEN];
3158     char ebuf[1024] = "";
3159     const char *name = agent->name();
3160     const char *msg = "Could not find agent library ";
3161 
3162     // First check to see if agent is statically linked into executable
3163     if (os::find_builtin_agent(agent, on_load_symbols, num_symbol_entries)) {
3164       library = agent->os_lib();
3165     } else if (agent->is_absolute_path()) {
3166       library = os::dll_load(name, ebuf, sizeof ebuf);
3167       if (library == NULL) {
3168         const char *sub_msg = " in absolute path, with error: ";
3169         size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3170         char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3171         jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3172         // If we can't find the agent, exit.
3173         vm_exit_during_initialization(buf, NULL);
3174         FREE_C_HEAP_ARRAY(char, buf);
3175       }
3176     } else {
3177       // Try to load the agent from the standard dll directory
3178       if (os::dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
3179                              name)) {
3180         library = os::dll_load(buffer, ebuf, sizeof ebuf);
3181       }
3182       if (library == NULL) { // Try the library path directory.
3183         if (os::dll_build_name(buffer, sizeof(buffer), name)) {
3184           library = os::dll_load(buffer, ebuf, sizeof ebuf);
3185         }
3186         if (library == NULL) {
3187           const char *sub_msg = " on the library path, with error: ";
3188           const char *sub_msg2 = "\nModule java.instrument may be missing from runtime image.";
3189 
3190           size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) +
3191                        strlen(ebuf) + strlen(sub_msg2) + 1;
3192           char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread);
3193           if (!agent->is_instrument_lib()) {
3194             jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3195           } else {
3196             jio_snprintf(buf, len, "%s%s%s%s%s", msg, name, sub_msg, ebuf, sub_msg2);
3197           }
3198           // If we can't find the agent, exit.
3199           vm_exit_during_initialization(buf, NULL);
3200           FREE_C_HEAP_ARRAY(char, buf);
3201         }
3202       }
3203     }
3204     agent->set_os_lib(library);
3205     agent->set_valid();
3206   }
3207 
3208   // Find the OnLoad function.
3209   on_load_entry =
3210     CAST_TO_FN_PTR(OnLoadEntry_t, os::find_agent_function(agent,
3211                                                           false,
3212                                                           on_load_symbols,
3213                                                           num_symbol_entries));
3214   return on_load_entry;
3215 }
3216 
3217 // Find the JVM_OnLoad entry point
3218 static OnLoadEntry_t lookup_jvm_on_load(AgentLibrary* agent) {
3219   const char *on_load_symbols[] = JVM_ONLOAD_SYMBOLS;
3220   return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*));
3221 }
3222 
3223 // Find the Agent_OnLoad entry point
3224 static OnLoadEntry_t lookup_agent_on_load(AgentLibrary* agent) {
3225   const char *on_load_symbols[] = AGENT_ONLOAD_SYMBOLS;
3226   return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*));
3227 }
3228 
3229 // For backwards compatibility with -Xrun
3230 // Convert libraries with no JVM_OnLoad, but which have Agent_OnLoad to be
3231 // treated like -agentpath:
3232 // Must be called before agent libraries are created
3233 void Threads::convert_vm_init_libraries_to_agents() {
3234   AgentLibrary* agent;
3235   AgentLibrary* next;
3236 
3237   for (agent = Arguments::libraries(); agent != NULL; agent = next) {
3238     next = agent->next();  // cache the next agent now as this agent may get moved off this list
3239     OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
3240 
3241     // If there is an JVM_OnLoad function it will get called later,
3242     // otherwise see if there is an Agent_OnLoad
3243     if (on_load_entry == NULL) {
3244       on_load_entry = lookup_agent_on_load(agent);
3245       if (on_load_entry != NULL) {
3246         // switch it to the agent list -- so that Agent_OnLoad will be called,
3247         // JVM_OnLoad won't be attempted and Agent_OnUnload will
3248         Arguments::convert_library_to_agent(agent);
3249       } else {
3250         vm_exit_during_initialization("Could not find JVM_OnLoad or Agent_OnLoad function in the library", agent->name());
3251       }
3252     }
3253   }
3254 }
3255 
3256 // Create agents for -agentlib:  -agentpath:  and converted -Xrun
3257 // Invokes Agent_OnLoad
3258 // Called very early -- before JavaThreads exist
3259 void Threads::create_vm_init_agents() {
3260   extern struct JavaVM_ main_vm;
3261   AgentLibrary* agent;
3262 
3263   JvmtiExport::enter_onload_phase();
3264 
3265   for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
3266     // CDS dumping does not support native JVMTI agent.
3267     // CDS dumping supports Java agent if the AllowArchivingWithJavaAgent diagnostic option is specified.
3268     if (Arguments::is_dumping_archive()) {
3269       if(!agent->is_instrument_lib()) {
3270         vm_exit_during_cds_dumping("CDS dumping does not support native JVMTI agent, name", agent->name());
3271       } else if (!AllowArchivingWithJavaAgent) {
3272         vm_exit_during_cds_dumping(
3273           "Must enable AllowArchivingWithJavaAgent in order to run Java agent during CDS dumping");
3274       }
3275     }
3276 
3277     OnLoadEntry_t  on_load_entry = lookup_agent_on_load(agent);
3278 
3279     if (on_load_entry != NULL) {
3280       // Invoke the Agent_OnLoad function
3281       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3282       if (err != JNI_OK) {
3283         vm_exit_during_initialization("agent library failed to init", agent->name());
3284       }
3285     } else {
3286       vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name());
3287     }
3288   }
3289 
3290   JvmtiExport::enter_primordial_phase();
3291 }
3292 
3293 extern "C" {
3294   typedef void (JNICALL *Agent_OnUnload_t)(JavaVM *);
3295 }
3296 
3297 void Threads::shutdown_vm_agents() {
3298   // Send any Agent_OnUnload notifications
3299   const char *on_unload_symbols[] = AGENT_ONUNLOAD_SYMBOLS;
3300   size_t num_symbol_entries = ARRAY_SIZE(on_unload_symbols);
3301   extern struct JavaVM_ main_vm;
3302   for (AgentLibrary* agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
3303 
3304     // Find the Agent_OnUnload function.
3305     Agent_OnUnload_t unload_entry = CAST_TO_FN_PTR(Agent_OnUnload_t,
3306                                                    os::find_agent_function(agent,
3307                                                    false,
3308                                                    on_unload_symbols,
3309                                                    num_symbol_entries));
3310 
3311     // Invoke the Agent_OnUnload function
3312     if (unload_entry != NULL) {
3313       JavaThread* thread = JavaThread::current();
3314       ThreadToNativeFromVM ttn(thread);
3315       HandleMark hm(thread);
3316       (*unload_entry)(&main_vm);
3317     }
3318   }
3319 }
3320 
3321 // Called for after the VM is initialized for -Xrun libraries which have not been converted to agent libraries
3322 // Invokes JVM_OnLoad
3323 void Threads::create_vm_init_libraries() {
3324   extern struct JavaVM_ main_vm;
3325   AgentLibrary* agent;
3326 
3327   for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
3328     OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
3329 
3330     if (on_load_entry != NULL) {
3331       // Invoke the JVM_OnLoad function
3332       JavaThread* thread = JavaThread::current();
3333       ThreadToNativeFromVM ttn(thread);
3334       HandleMark hm(thread);
3335       jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3336       if (err != JNI_OK) {
3337         vm_exit_during_initialization("-Xrun library failed to init", agent->name());
3338       }
3339     } else {
3340       vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
3341     }
3342   }
3343 }
3344 
3345 
3346 // Last thread running calls java.lang.Shutdown.shutdown()
3347 void JavaThread::invoke_shutdown_hooks() {
3348   HandleMark hm(this);
3349 
3350   // We could get here with a pending exception, if so clear it now or
3351   // it will cause MetaspaceShared::link_and_cleanup_shared_classes to
3352   // fail for dynamic dump.
3353   if (this->has_pending_exception()) {
3354     this->clear_pending_exception();
3355   }
3356 
3357 #if INCLUDE_CDS
3358   // Link all classes for dynamic CDS dumping before vm exit.
3359   // Same operation is being done in JVM_BeforeHalt for handling the
3360   // case where the application calls System.exit().
3361   if (DynamicDumpSharedSpaces) {
3362     DynamicArchive::prepare_for_dynamic_dumping_at_exit();
3363   }
3364 #endif
3365 
3366   EXCEPTION_MARK;
3367   Klass* shutdown_klass =
3368     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
3369                                       THREAD);
3370   if (shutdown_klass != NULL) {
3371     // SystemDictionary::resolve_or_null will return null if there was
3372     // an exception.  If we cannot load the Shutdown class, just don't
3373     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
3374     // won't be run.  Note that if a shutdown hook was registered,
3375     // the Shutdown class would have already been loaded
3376     // (Runtime.addShutdownHook will load it).
3377     JavaValue result(T_VOID);
3378     JavaCalls::call_static(&result,
3379                            shutdown_klass,
3380                            vmSymbols::shutdown_name(),
3381                            vmSymbols::void_method_signature(),
3382                            THREAD);
3383   }
3384   CLEAR_PENDING_EXCEPTION;
3385 }
3386 
3387 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
3388 // the program falls off the end of main(). Another VM exit path is through
3389 // vm_exit() when the program calls System.exit() to return a value or when
3390 // there is a serious error in VM. The two shutdown paths are not exactly
3391 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
3392 // and VM_Exit op at VM level.
3393 //
3394 // Shutdown sequence:
3395 //   + Shutdown native memory tracking if it is on
3396 //   + Wait until we are the last non-daemon thread to execute
3397 //     <-- every thing is still working at this moment -->
3398 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
3399 //        shutdown hooks
3400 //   + Call before_exit(), prepare for VM exit
3401 //      > run VM level shutdown hooks (they are registered through JVM_OnExit(),
3402 //        currently the only user of this mechanism is File.deleteOnExit())
3403 //      > stop StatSampler, watcher thread,
3404 //        post thread end and vm death events to JVMTI,
3405 //        stop signal thread
3406 //   + Call JavaThread::exit(), it will:
3407 //      > release JNI handle blocks, remove stack guard pages
3408 //      > remove this thread from Threads list
3409 //     <-- no more Java code from this thread after this point -->
3410 //   + Stop VM thread, it will bring the remaining VM to a safepoint and stop
3411 //     the compiler threads at safepoint
3412 //     <-- do not use anything that could get blocked by Safepoint -->
3413 //   + Disable tracing at JNI/JVM barriers
3414 //   + Set _vm_exited flag for threads that are still running native code
3415 //   + Call exit_globals()
3416 //      > deletes tty
3417 //      > deletes PerfMemory resources
3418 //   + Delete this thread
3419 //   + Return to caller
3420 
3421 void Threads::destroy_vm() {
3422   JavaThread* thread = JavaThread::current();
3423 
3424 #ifdef ASSERT
3425   _vm_complete = false;
3426 #endif
3427   // Wait until we are the last non-daemon thread to execute
3428   {
3429     MonitorLocker nu(Threads_lock);
3430     while (Threads::number_of_non_daemon_threads() > 1)
3431       // This wait should make safepoint checks, wait without a timeout.
3432       nu.wait(0);
3433   }
3434 
3435   EventShutdown e;
3436   if (e.should_commit()) {
3437     e.set_reason("No remaining non-daemon Java threads");
3438     e.commit();
3439   }
3440 
3441   // Hang forever on exit if we are reporting an error.
3442   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
3443     os::infinite_sleep();
3444   }
3445   os::wait_for_keypress_at_exit();
3446 
3447   // run Java level shutdown hooks
3448   thread->invoke_shutdown_hooks();
3449 
3450   before_exit(thread);
3451 
3452   thread->exit(true);
3453 
3454   // We are no longer on the main thread list but could still be in a
3455   // secondary list where another thread may try to interact with us.
3456   // So wait until all such interactions are complete before we bring
3457   // the VM to the termination safepoint. Normally this would be done
3458   // using thread->smr_delete() below where we delete the thread, but
3459   // we can't call that after the termination safepoint is active as
3460   // we will deadlock on the Threads_lock. Once all interactions are
3461   // complete it is safe to directly delete the thread at any time.
3462   ThreadsSMRSupport::wait_until_not_protected(thread);
3463 
3464   // Stop VM thread.
3465   {
3466     // 4945125 The vm thread comes to a safepoint during exit.
3467     // GC vm_operations can get caught at the safepoint, and the
3468     // heap is unparseable if they are caught. Grab the Heap_lock
3469     // to prevent this. The GC vm_operations will not be able to
3470     // queue until after the vm thread is dead. After this point,
3471     // we'll never emerge out of the safepoint before the VM exits.
3472     // Assert that the thread is terminated so that acquiring the
3473     // Heap_lock doesn't cause the terminated thread to participate in
3474     // the safepoint protocol.
3475 
3476     assert(thread->is_terminated(), "must be terminated here");
3477     MutexLocker ml(Heap_lock);
3478 
3479     VMThread::wait_for_vm_thread_exit();
3480     assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
3481     VMThread::destroy();
3482   }
3483 
3484   // Now, all Java threads are gone except daemon threads. Daemon threads
3485   // running Java code or in VM are stopped by the Safepoint. However,
3486   // daemon threads executing native code are still running.  But they
3487   // will be stopped at native=>Java/VM barriers. Note that we can't
3488   // simply kill or suspend them, as it is inherently deadlock-prone.
3489 
3490   VM_Exit::set_vm_exited();
3491 
3492   // Clean up ideal graph printers after the VMThread has started
3493   // the final safepoint which will block all the Compiler threads.
3494   // Note that this Thread has already logically exited so the
3495   // clean_up() function's use of a JavaThreadIteratorWithHandle
3496   // would be a problem except set_vm_exited() has remembered the
3497   // shutdown thread which is granted a policy exception.
3498 #if defined(COMPILER2) && !defined(PRODUCT)
3499   IdealGraphPrinter::clean_up();
3500 #endif
3501 
3502   notify_vm_shutdown();
3503 
3504   // exit_globals() will delete tty
3505   exit_globals();
3506 
3507   // Deleting the shutdown thread here is safe. See comment on
3508   // wait_until_not_protected() above.
3509   delete thread;
3510 
3511 #if INCLUDE_JVMCI
3512   if (JVMCICounterSize > 0) {
3513     FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
3514   }
3515 #endif
3516 
3517   LogConfiguration::finalize();
3518 }
3519 
3520 
3521 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
3522   if (version == JNI_VERSION_1_1) return JNI_TRUE;
3523   return is_supported_jni_version(version);
3524 }
3525 
3526 
3527 jboolean Threads::is_supported_jni_version(jint version) {
3528   if (version == JNI_VERSION_1_2) return JNI_TRUE;
3529   if (version == JNI_VERSION_1_4) return JNI_TRUE;
3530   if (version == JNI_VERSION_1_6) return JNI_TRUE;
3531   if (version == JNI_VERSION_1_8) return JNI_TRUE;
3532   if (version == JNI_VERSION_9) return JNI_TRUE;
3533   if (version == JNI_VERSION_10) return JNI_TRUE;
3534   return JNI_FALSE;
3535 }
3536 
3537 
3538 void Threads::add(JavaThread* p, bool force_daemon) {
3539   // The threads lock must be owned at this point
3540   assert(Threads_lock->owned_by_self(), "must have threads lock");
3541 
3542   BarrierSet::barrier_set()->on_thread_attach(p);
3543 
3544   // Once a JavaThread is added to the Threads list, smr_delete() has
3545   // to be used to delete it. Otherwise we can just delete it directly.
3546   p->set_on_thread_list();
3547 
3548   _number_of_threads++;
3549   oop threadObj = p->threadObj();
3550   bool daemon = true;
3551   // Bootstrapping problem: threadObj can be null for initial
3552   // JavaThread (or for threads attached via JNI)
3553   if ((!force_daemon) && !is_daemon((threadObj))) {
3554     _number_of_non_daemon_threads++;
3555     daemon = false;
3556   }
3557 
3558   ThreadService::add_thread(p, daemon);
3559 
3560   // Maintain fast thread list
3561   ThreadsSMRSupport::add_thread(p);
3562 
3563   // Increase the ObjectMonitor ceiling for the new thread.
3564   ObjectSynchronizer::inc_in_use_list_ceiling();
3565 
3566   // Possible GC point.
3567   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
3568 
3569   // Make new thread known to active EscapeBarrier
3570   EscapeBarrier::thread_added(p);
3571 }
3572 
3573 void Threads::remove(JavaThread* p, bool is_daemon) {
3574   // Extra scope needed for Thread_lock, so we can check
3575   // that we do not remove thread without safepoint code notice
3576   { MonitorLocker ml(Threads_lock);
3577 
3578     // BarrierSet state must be destroyed after the last thread transition
3579     // before the thread terminates. Thread transitions result in calls to
3580     // StackWatermarkSet::on_safepoint(), which performs GC processing,
3581     // requiring the GC state to be alive.
3582     BarrierSet::barrier_set()->on_thread_detach(p);
3583 
3584     assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
3585 
3586     // Maintain fast thread list
3587     ThreadsSMRSupport::remove_thread(p);
3588 
3589     _number_of_threads--;
3590     if (!is_daemon) {
3591       _number_of_non_daemon_threads--;
3592 
3593       // Only one thread left, do a notify on the Threads_lock so a thread waiting
3594       // on destroy_vm will wake up.
3595       if (number_of_non_daemon_threads() == 1) {
3596         ml.notify_all();
3597       }
3598     }
3599     ThreadService::remove_thread(p, is_daemon);
3600 
3601     // Make sure that safepoint code disregard this thread. This is needed since
3602     // the thread might mess around with locks after this point. This can cause it
3603     // to do callbacks into the safepoint code. However, the safepoint code is not aware
3604     // of this thread since it is removed from the queue.
3605     p->set_terminated(JavaThread::_thread_terminated);
3606 
3607     // Notify threads waiting in EscapeBarriers
3608     EscapeBarrier::thread_removed(p);
3609   } // unlock Threads_lock
3610 
3611   // Reduce the ObjectMonitor ceiling for the exiting thread.
3612   ObjectSynchronizer::dec_in_use_list_ceiling();
3613 
3614   // Since Events::log uses a lock, we grab it outside the Threads_lock
3615   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
3616 }
3617 
3618 // Operations on the Threads list for GC.  These are not explicitly locked,
3619 // but the garbage collector must provide a safe context for them to run.
3620 // In particular, these things should never be called when the Threads_lock
3621 // is held by some other thread. (Note: the Safepoint abstraction also
3622 // uses the Threads_lock to guarantee this property. It also makes sure that
3623 // all threads gets blocked when exiting or starting).
3624 
3625 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
3626   ALL_JAVA_THREADS(p) {
3627     p->oops_do(f, cf);
3628   }
3629   VMThread::vm_thread()->oops_do(f, cf);
3630 }
3631 
3632 void Threads::change_thread_claim_token() {
3633   if (++_thread_claim_token == 0) {
3634     // On overflow of the token counter, there is a risk of future
3635     // collisions between a new global token value and a stale token
3636     // for a thread, because not all iterations visit all threads.
3637     // (Though it's pretty much a theoretical concern for non-trivial
3638     // token counter sizes.)  To deal with the possibility, reset all
3639     // the thread tokens to zero on global token overflow.
3640     struct ResetClaims : public ThreadClosure {
3641       virtual void do_thread(Thread* t) {
3642         t->claim_threads_do(false, 0);
3643       }
3644     } reset_claims;
3645     Threads::threads_do(&reset_claims);
3646     // On overflow, update the global token to non-zero, to
3647     // avoid the special "never claimed" initial thread value.
3648     _thread_claim_token = 1;
3649   }
3650 }
3651 
3652 #ifdef ASSERT
3653 void assert_thread_claimed(const char* kind, Thread* t, uintx expected) {
3654   const uintx token = t->threads_do_token();
3655   assert(token == expected,
3656          "%s " PTR_FORMAT " has incorrect value " UINTX_FORMAT " != "
3657          UINTX_FORMAT, kind, p2i(t), token, expected);
3658 }
3659 
3660 void Threads::assert_all_threads_claimed() {
3661   ALL_JAVA_THREADS(p) {
3662     assert_thread_claimed("Thread", p, _thread_claim_token);
3663   }
3664   assert_thread_claimed("VMThread", VMThread::vm_thread(), _thread_claim_token);
3665 }
3666 #endif // ASSERT
3667 
3668 class ParallelOopsDoThreadClosure : public ThreadClosure {
3669 private:
3670   OopClosure* _f;
3671   CodeBlobClosure* _cf;
3672 public:
3673   ParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf) : _f(f), _cf(cf) {}
3674   void do_thread(Thread* t) {
3675     t->oops_do(_f, _cf);
3676   }
3677 };
3678 
3679 void Threads::possibly_parallel_oops_do(bool is_par, OopClosure* f, CodeBlobClosure* cf) {
3680   ParallelOopsDoThreadClosure tc(f, cf);
3681   possibly_parallel_threads_do(is_par, &tc);
3682 }
3683 
3684 void Threads::metadata_do(MetadataClosure* f) {
3685   ALL_JAVA_THREADS(p) {
3686     p->metadata_do(f);
3687   }
3688 }
3689 
3690 class ThreadHandlesClosure : public ThreadClosure {
3691   void (*_f)(Metadata*);
3692  public:
3693   ThreadHandlesClosure(void f(Metadata*)) : _f(f) {}
3694   virtual void do_thread(Thread* thread) {
3695     thread->metadata_handles_do(_f);
3696   }
3697 };
3698 
3699 void Threads::metadata_handles_do(void f(Metadata*)) {
3700   // Only walk the Handles in Thread.
3701   ThreadHandlesClosure handles_closure(f);
3702   threads_do(&handles_closure);
3703 }
3704 
3705 // Get count Java threads that are waiting to enter the specified monitor.
3706 GrowableArray<JavaThread*>* Threads::get_pending_threads(ThreadsList * t_list,
3707                                                          int count,
3708                                                          address monitor) {
3709   GrowableArray<JavaThread*>* result = new GrowableArray<JavaThread*>(count);
3710 
3711   int i = 0;
3712   DO_JAVA_THREADS(t_list, p) {
3713     if (!p->can_call_java()) continue;
3714 
3715     // The first stage of async deflation does not affect any field
3716     // used by this comparison so the ObjectMonitor* is usable here.
3717     address pending = (address)p->current_pending_monitor();
3718     if (pending == monitor) {             // found a match
3719       if (i < count) result->append(p);   // save the first count matches
3720       i++;
3721     }
3722   }
3723 
3724   return result;
3725 }
3726 
3727 
3728 JavaThread *Threads::owning_thread_from_monitor_owner(ThreadsList * t_list,
3729                                                       address owner) {
3730   assert(LockingMode != LM_LIGHTWEIGHT, "Not with new lightweight locking");
3731   // NULL owner means not locked so we can skip the search
3732   if (owner == NULL) return NULL;
3733 
3734   DO_JAVA_THREADS(t_list, p) {
3735     // first, see if owner is the address of a Java thread
3736     if (owner == (address)p) return p;
3737   }
3738 
3739   // Cannot assert on lack of success here since this function may be
3740   // used by code that is trying to report useful problem information
3741   // like deadlock detection.
3742   if (UseHeavyMonitors) return NULL;
3743 
3744   // If we didn't find a matching Java thread and we didn't force use of
3745   // heavyweight monitors, then the owner is the stack address of the
3746   // Lock Word in the owning Java thread's stack.
3747   //
3748   JavaThread* the_owner = NULL;
3749   DO_JAVA_THREADS(t_list, q) {
3750     if (q->is_lock_owned(owner)) {
3751       the_owner = q;
3752       break;
3753     }
3754   }
3755 
3756   // cannot assert on lack of success here; see above comment
3757   return the_owner;
3758 }
3759 
3760 JavaThread* Threads::owning_thread_from_object(ThreadsList * t_list, oop obj) {
3761   assert(LockingMode == LM_LIGHTWEIGHT, "Only with new lightweight locking");
3762   DO_JAVA_THREADS(t_list, q) {
3763     if (q->lock_stack().contains(obj)) {
3764       return q;
3765     }
3766   }
3767   return NULL;
3768 }
3769 
3770 JavaThread* Threads::owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* monitor) {
3771   if (LockingMode == LM_LIGHTWEIGHT) {
3772     if (monitor->is_owner_anonymous()) {
3773       return owning_thread_from_object(t_list, monitor->object());
3774     } else {
3775       Thread* owner = reinterpret_cast<Thread*>(monitor->owner());
3776       assert(owner == NULL || owner->is_Java_thread(), "only JavaThreads own monitors");
3777       return reinterpret_cast<JavaThread*>(owner);
3778     }
3779   } else {
3780     address owner = (address)monitor->owner();
3781     return owning_thread_from_monitor_owner(t_list, owner);
3782   }
3783 }
3784 
3785 class PrintOnClosure : public ThreadClosure {
3786 private:
3787   outputStream* _st;
3788 
3789 public:
3790   PrintOnClosure(outputStream* st) :
3791       _st(st) {}
3792 
3793   virtual void do_thread(Thread* thread) {
3794     if (thread != NULL) {
3795       thread->print_on(_st);
3796       _st->cr();
3797     }
3798   }
3799 };
3800 
3801 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
3802 void Threads::print_on(outputStream* st, bool print_stacks,
3803                        bool internal_format, bool print_concurrent_locks,
3804                        bool print_extended_info) {
3805   char buf[32];
3806   st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
3807 
3808   st->print_cr("Full thread dump %s (%s %s):",
3809                VM_Version::vm_name(),
3810                VM_Version::vm_release(),
3811                VM_Version::vm_info_string());
3812   st->cr();
3813 
3814 #if INCLUDE_SERVICES
3815   // Dump concurrent locks
3816   ConcurrentLocksDump concurrent_locks;
3817   if (print_concurrent_locks) {
3818     concurrent_locks.dump_at_safepoint();
3819   }
3820 #endif // INCLUDE_SERVICES
3821 
3822   ThreadsSMRSupport::print_info_on(st);
3823   st->cr();
3824 
3825   ALL_JAVA_THREADS(p) {
3826     ResourceMark rm;
3827     p->print_on(st, print_extended_info);
3828     if (print_stacks) {
3829       if (internal_format) {
3830         p->trace_stack();
3831       } else {
3832         p->print_stack_on(st);
3833       }
3834     }
3835     st->cr();
3836 #if INCLUDE_SERVICES
3837     if (print_concurrent_locks) {
3838       concurrent_locks.print_locks_on(p, st);
3839     }
3840 #endif // INCLUDE_SERVICES
3841   }
3842 
3843   PrintOnClosure cl(st);
3844   cl.do_thread(VMThread::vm_thread());
3845   Universe::heap()->gc_threads_do(&cl);
3846   if (StringDedup::is_enabled()) {
3847     StringDedup::threads_do(&cl);
3848   }
3849   cl.do_thread(WatcherThread::watcher_thread());
3850   cl.do_thread(AsyncLogWriter::instance());
3851 
3852   st->flush();
3853 }
3854 
3855 void Threads::print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
3856                              int buflen, bool* found_current) {
3857   if (this_thread != NULL) {
3858     bool is_current = (current == this_thread);
3859     *found_current = *found_current || is_current;
3860     st->print("%s", is_current ? "=>" : "  ");
3861 
3862     st->print(PTR_FORMAT, p2i(this_thread));
3863     st->print(" ");
3864     this_thread->print_on_error(st, buf, buflen);
3865     st->cr();
3866   }
3867 }
3868 
3869 class PrintOnErrorClosure : public ThreadClosure {
3870   outputStream* _st;
3871   Thread* _current;
3872   char* _buf;
3873   int _buflen;
3874   bool* _found_current;
3875  public:
3876   PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
3877                       int buflen, bool* found_current) :
3878    _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
3879 
3880   virtual void do_thread(Thread* thread) {
3881     Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
3882   }
3883 };
3884 
3885 // Threads::print_on_error() is called by fatal error handler. It's possible
3886 // that VM is not at safepoint and/or current thread is inside signal handler.
3887 // Don't print stack trace, as the stack may not be walkable. Don't allocate
3888 // memory (even in resource area), it might deadlock the error handler.
3889 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
3890                              int buflen) {
3891   ThreadsSMRSupport::print_info_on(st);
3892   st->cr();
3893 
3894   bool found_current = false;
3895   st->print_cr("Java Threads: ( => current thread )");
3896   ALL_JAVA_THREADS(thread) {
3897     print_on_error(thread, st, current, buf, buflen, &found_current);
3898   }
3899   st->cr();
3900 
3901   st->print_cr("Other Threads:");
3902   print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
3903   print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
3904   print_on_error(AsyncLogWriter::instance(), st, current, buf, buflen, &found_current);
3905 
3906   if (Universe::heap() != NULL) {
3907     PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
3908     Universe::heap()->gc_threads_do(&print_closure);
3909   }
3910 
3911   if (StringDedup::is_enabled()) {
3912     PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
3913     StringDedup::threads_do(&print_closure);
3914   }
3915 
3916   if (!found_current) {
3917     st->cr();
3918     st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
3919     current->print_on_error(st, buf, buflen);
3920     st->cr();
3921   }
3922   st->cr();
3923 
3924   st->print_cr("Threads with active compile tasks:");
3925   print_threads_compiling(st, buf, buflen);
3926 }
3927 
3928 void Threads::print_threads_compiling(outputStream* st, char* buf, int buflen, bool short_form) {
3929   ALL_JAVA_THREADS(thread) {
3930     if (thread->is_Compiler_thread()) {
3931       CompilerThread* ct = (CompilerThread*) thread;
3932 
3933       // Keep task in local variable for NULL check.
3934       // ct->_task might be set to NULL by concurring compiler thread
3935       // because it completed the compilation. The task is never freed,
3936       // though, just returned to a free list.
3937       CompileTask* task = ct->task();
3938       if (task != NULL) {
3939         thread->print_name_on_error(st, buf, buflen);
3940         st->print("  ");
3941         task->print(st, NULL, short_form, true);
3942       }
3943     }
3944   }
3945 }
3946 
3947 
3948 // Ad-hoc mutual exclusion primitives: SpinLock
3949 //
3950 // We employ SpinLocks _only for low-contention, fixed-length
3951 // short-duration critical sections where we're concerned
3952 // about native mutex_t or HotSpot Mutex:: latency.
3953 //
3954 // TODO-FIXME: ListLock should be of type SpinLock.
3955 // We should make this a 1st-class type, integrated into the lock
3956 // hierarchy as leaf-locks.  Critically, the SpinLock structure
3957 // should have sufficient padding to avoid false-sharing and excessive
3958 // cache-coherency traffic.
3959 
3960 
3961 typedef volatile int SpinLockT;
3962 
3963 void Thread::SpinAcquire(volatile int * adr, const char * LockName) {
3964   if (Atomic::cmpxchg(adr, 0, 1) == 0) {
3965     return;   // normal fast-path return
3966   }
3967 
3968   // Slow-path : We've encountered contention -- Spin/Yield/Block strategy.
3969   int ctr = 0;
3970   int Yields = 0;
3971   for (;;) {
3972     while (*adr != 0) {
3973       ++ctr;
3974       if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
3975         if (Yields > 5) {
3976           os::naked_short_sleep(1);
3977         } else {
3978           os::naked_yield();
3979           ++Yields;
3980         }
3981       } else {
3982         SpinPause();
3983       }
3984     }
3985     if (Atomic::cmpxchg(adr, 0, 1) == 0) return;
3986   }
3987 }
3988 
3989 void Thread::SpinRelease(volatile int * adr) {
3990   assert(*adr != 0, "invariant");
3991   OrderAccess::fence();      // guarantee at least release consistency.
3992   // Roach-motel semantics.
3993   // It's safe if subsequent LDs and STs float "up" into the critical section,
3994   // but prior LDs and STs within the critical section can't be allowed
3995   // to reorder or float past the ST that releases the lock.
3996   // Loads and stores in the critical section - which appear in program
3997   // order before the store that releases the lock - must also appear
3998   // before the store that releases the lock in memory visibility order.
3999   // Conceptually we need a #loadstore|#storestore "release" MEMBAR before
4000   // the ST of 0 into the lock-word which releases the lock, so fence
4001   // more than covers this on all platforms.
4002   *adr = 0;
4003 }
4004 
4005 
4006 void Threads::verify() {
4007   ALL_JAVA_THREADS(p) {
4008     p->verify();
4009   }
4010   VMThread* thread = VMThread::vm_thread();
4011   if (thread != NULL) thread->verify();
4012 }
4013 
4014 #ifndef PRODUCT
4015 void JavaThread::verify_cross_modify_fence_failure(JavaThread *thread) {
4016    report_vm_error(__FILE__, __LINE__, "Cross modify fence failure", "%p", thread);
4017 }
4018 #endif