< prev index next >

src/share/vm/runtime/thread.cpp

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/scopeDesc.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "interpreter/oopMapCache.hpp"

  35 #include "jvmtifiles/jvmtiEnv.hpp"
  36 #include "memory/gcLocker.inline.hpp"
  37 #include "memory/metaspaceShared.hpp"
  38 #include "memory/oopFactory.hpp"
  39 #include "memory/universe.inline.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/objArrayOop.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/symbol.hpp"
  44 #include "prims/jvm_misc.hpp"
  45 #include "prims/jvmtiExport.hpp"
  46 #include "prims/jvmtiThreadState.hpp"
  47 #include "prims/privilegedStack.hpp"
  48 #include "runtime/arguments.hpp"
  49 #include "runtime/biasedLocking.hpp"
  50 #include "runtime/deoptimization.hpp"
  51 #include "runtime/fprofiler.hpp"
  52 #include "runtime/frame.inline.hpp"
  53 #include "runtime/init.hpp"
  54 #include "runtime/interfaceSupport.hpp"


  60 #include "runtime/objectMonitor.hpp"
  61 #include "runtime/orderAccess.inline.hpp"
  62 #include "runtime/osThread.hpp"
  63 #include "runtime/safepoint.hpp"
  64 #include "runtime/sharedRuntime.hpp"
  65 #include "runtime/statSampler.hpp"
  66 #include "runtime/stubRoutines.hpp"
  67 #include "runtime/task.hpp"
  68 #include "runtime/thread.inline.hpp"
  69 #include "runtime/threadCritical.hpp"
  70 #include "runtime/threadLocalStorage.hpp"
  71 #include "runtime/vframe.hpp"
  72 #include "runtime/vframeArray.hpp"
  73 #include "runtime/vframe_hp.hpp"
  74 #include "runtime/vmThread.hpp"
  75 #include "runtime/vm_operations.hpp"
  76 #include "services/attachListener.hpp"
  77 #include "services/management.hpp"
  78 #include "services/memTracker.hpp"
  79 #include "services/threadService.hpp"
  80 #include "trace/tracing.hpp"
  81 #include "trace/traceMacros.hpp"
  82 #include "utilities/defaultStream.hpp"
  83 #include "utilities/dtrace.hpp"
  84 #include "utilities/events.hpp"
  85 #include "utilities/preserveException.hpp"
  86 #include "utilities/macros.hpp"
  87 #ifdef TARGET_OS_FAMILY_linux
  88 # include "os_linux.inline.hpp"
  89 #endif
  90 #ifdef TARGET_OS_FAMILY_solaris
  91 # include "os_solaris.inline.hpp"
  92 #endif
  93 #ifdef TARGET_OS_FAMILY_windows
  94 # include "os_windows.inline.hpp"
  95 #endif
  96 #ifdef TARGET_OS_FAMILY_bsd
  97 # include "os_bsd.inline.hpp"
  98 #endif
  99 #if INCLUDE_ALL_GCS
 100 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
 101 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
 102 #include "gc_implementation/parallelScavenge/pcTasks.hpp"
 103 #endif // INCLUDE_ALL_GCS
 104 #ifdef COMPILER1
 105 #include "c1/c1_Compiler.hpp"
 106 #endif
 107 #ifdef COMPILER2
 108 #include "opto/c2compiler.hpp"
 109 #include "opto/idealGraphPrinter.hpp"
 110 #endif
 111 #if INCLUDE_RTM_OPT
 112 #include "runtime/rtmLocking.hpp"
 113 #endif



 114 
 115 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 116 
 117 #ifdef DTRACE_ENABLED
 118 
 119 // Only bother with this argument setup if dtrace is available
 120 
 121 #ifndef USDT2
 122 HS_DTRACE_PROBE_DECL(hotspot, vm__init__begin);
 123 HS_DTRACE_PROBE_DECL(hotspot, vm__init__end);
 124 HS_DTRACE_PROBE_DECL5(hotspot, thread__start, char*, intptr_t,
 125   intptr_t, intptr_t, bool);
 126 HS_DTRACE_PROBE_DECL5(hotspot, thread__stop, char*, intptr_t,
 127   intptr_t, intptr_t, bool);
 128 
 129 #define DTRACE_THREAD_PROBE(probe, javathread)                             \
 130   {                                                                        \
 131     ResourceMark rm(this);                                                 \
 132     int len = 0;                                                           \
 133     const char* name = (javathread)->get_thread_name();                    \


 325   // CR 7190089: on Solaris, primordial thread's stack is adjusted
 326   // in initialize_thread(). Without the adjustment, stack size is
 327   // incorrect if stack is set to unlimited (ulimit -s unlimited).
 328   // So far, only Solaris has real implementation of initialize_thread().
 329   //
 330   // set up any platform-specific state.
 331   os::initialize_thread(this);
 332 
 333 #if INCLUDE_NMT
 334   // record thread's native stack, stack grows downward
 335   address stack_low_addr = stack_base() - stack_size();
 336   MemTracker::record_thread_stack(stack_low_addr, stack_size());
 337 #endif // INCLUDE_NMT
 338 }
 339 
 340 
 341 Thread::~Thread() {
 342   // Reclaim the objectmonitors from the omFreeList of the moribund thread.
 343   ObjectSynchronizer::omFlush (this) ;
 344 
 345   EVENT_THREAD_DESTRUCT(this);
 346 
 347   // stack_base can be NULL if the thread is never started or exited before
 348   // record_stack_base_and_size called. Although, we would like to ensure
 349   // that all started threads do call record_stack_base_and_size(), there is
 350   // not proper way to enforce that.
 351 #if INCLUDE_NMT
 352   if (_stack_base != NULL) {
 353     address low_stack_addr = stack_base() - stack_size();
 354     MemTracker::release_thread_stack(low_stack_addr, stack_size());
 355 #ifdef ASSERT
 356     set_stack_base(NULL);
 357 #endif
 358   }
 359 #endif // INCLUDE_NMT
 360 
 361   // deallocate data structures
 362   delete resource_area();
 363   // since the handle marks are using the handle area, we have to deallocated the root
 364   // handle mark before deallocating the thread's handle area,
 365   assert(last_handle_mark() != NULL, "check we have an element");
 366   delete last_handle_mark();


1195 
1196   JavaCalls::call_special(&result,
1197                          thread_group,
1198                          group,
1199                          vmSymbols::add_method_name(),
1200                          vmSymbols::thread_void_signature(),
1201                          threadObj,          // Arg 1
1202                          THREAD);
1203 
1204 
1205 }
1206 
1207 // NamedThread --  non-JavaThread subclasses with multiple
1208 // uniquely named instances should derive from this.
1209 NamedThread::NamedThread() : Thread() {
1210   _name = NULL;
1211   _processed_thread = NULL;
1212 }
1213 
1214 NamedThread::~NamedThread() {

1215   if (_name != NULL) {
1216     FREE_C_HEAP_ARRAY(char, _name, mtThread);
1217     _name = NULL;
1218   }
1219 }
1220 
1221 void NamedThread::set_name(const char* format, ...) {
1222   guarantee(_name == NULL, "Only get to set name once.");
1223   _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread);
1224   guarantee(_name != NULL, "alloc failure");
1225   va_list ap;
1226   va_start(ap, format);
1227   jio_vsnprintf(_name, max_name_len, format, ap);
1228   va_end(ap);
1229 }
1230 
1231 // ======= WatcherThread ========
1232 
1233 // The watcher thread exists to simulate timer interrupts.  It should
1234 // be replaced by an abstraction over whatever native support for
1235 // timer interrupts exists on the platform.
1236 
1237 WatcherThread* WatcherThread::_watcher_thread   = NULL;
1238 bool WatcherThread::_startable = false;
1239 volatile bool  WatcherThread::_should_terminate = false;
1240 
1241 WatcherThread::WatcherThread() : Thread(), _crash_protection(NULL) {
1242   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1243   if (os::create_thread(this, os::watcher_thread)) {
1244     _watcher_thread = this;
1245 
1246     // Set the watcher thread to the highest OS priority which should not be
1247     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1248     // is created. The only normal thread using this priority is the reference
1249     // handler thread, which runs for very short intervals only.
1250     // If the VMThread's priority is not lower than the WatcherThread profiling
1251     // will be inaccurate.
1252     os::set_priority(this, MaxPriority);
1253     if (!DisableStartThread) {
1254       os::start_thread(this);
1255     }
1256   }
1257 }
1258 
1259 int WatcherThread::sleep() const {
1260   MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1261 


1652 
1653   this->cache_global_variables();
1654 
1655   // Thread is now sufficient initialized to be handled by the safepoint code as being
1656   // in the VM. Change thread state from _thread_new to _thread_in_vm
1657   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1658 
1659   assert(JavaThread::current() == this, "sanity check");
1660   assert(!Thread::current()->owns_locks(), "sanity check");
1661 
1662   DTRACE_THREAD_PROBE(start, this);
1663 
1664   // This operation might block. We call that after all safepoint checks for a new thread has
1665   // been completed.
1666   this->set_active_handles(JNIHandleBlock::allocate_block());
1667 
1668   if (JvmtiExport::should_post_thread_life()) {
1669     JvmtiExport::post_thread_start(this);
1670   }
1671 
1672   EventThreadStart event;
1673   if (event.should_commit()) {
1674      event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1675      event.commit();
1676   }
1677 
1678   // We call another function to do the rest so we are sure that the stack addresses used
1679   // from there will be lower than the stack base just computed
1680   thread_main_inner();
1681 
1682   // Note, thread is no longer valid at this point!
1683 }
1684 
1685 
1686 void JavaThread::thread_main_inner() {
1687   assert(JavaThread::current() == this, "sanity check");
1688   assert(this->threadObj() != NULL, "just checking");
1689 
1690   // Execute thread entry point unless this thread has a pending exception
1691   // or has been stopped before starting.
1692   // Note: Due to JVM_StopThread we can have pending exceptions already!
1693   if (!this->has_pending_exception() &&
1694       !java_lang_Thread::is_stillborn(this->threadObj())) {
1695     {
1696       ResourceMark rm(this);


1783           JavaValue result(T_VOID);
1784           JavaCalls::call_virtual(&result,
1785                                   group, thread_group,
1786                                   vmSymbols::uncaughtException_name(),
1787                                   vmSymbols::thread_throwable_void_signature(),
1788                                   threadObj,           // Arg 1
1789                                   uncaught_exception,  // Arg 2
1790                                   THREAD);
1791         }
1792         if (HAS_PENDING_EXCEPTION) {
1793           ResourceMark rm(this);
1794           jio_fprintf(defaultStream::error_stream(),
1795                 "\nException: %s thrown from the UncaughtExceptionHandler"
1796                 " in thread \"%s\"\n",
1797                 pending_exception()->klass()->external_name(),
1798                 get_thread_name());
1799           CLEAR_PENDING_EXCEPTION;
1800         }
1801       }
1802     }
1803 
1804     // Called before the java thread exit since we want to read info
1805     // from java_lang_Thread object
1806     EventThreadEnd event;
1807     if (event.should_commit()) {
1808         event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1809         event.commit();
1810     }
1811 
1812     // Call after last event on thread
1813     EVENT_THREAD_EXIT(this);
1814 
1815     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1816     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1817     // is deprecated anyhow.
1818     if (!is_Compiler_thread()) {
1819       int count = 3;
1820       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1821         EXCEPTION_MARK;
1822         JavaValue result(T_VOID);
1823         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1824         JavaCalls::call_virtual(&result,
1825                               threadObj, thread_klass,
1826                               vmSymbols::exit_method_name(),
1827                               vmSymbols::void_method_signature(),
1828                               THREAD);
1829         CLEAR_PENDING_EXCEPTION;
1830       }
1831     }
1832     // notify JVMTI
1833     if (JvmtiExport::should_post_thread_life()) {


1870   // group should have the destroyed bit set before waiters are notified).
1871   ensure_join(this);
1872   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1873 
1874   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1875   // held by this thread must be released.  A detach operation must only
1876   // get here if there are no Java frames on the stack.  Therefore, any
1877   // owned monitors at this point MUST be JNI-acquired monitors which are
1878   // pre-inflated and in the monitor cache.
1879   //
1880   // ensure_join() ignores IllegalThreadStateExceptions, and so does this.
1881   if (exit_type == jni_detach && JNIDetachReleasesMonitors) {
1882     assert(!this->has_last_Java_frame(), "detaching with Java frames?");
1883     ObjectSynchronizer::release_monitors_owned_by_thread(this);
1884     assert(!this->has_pending_exception(), "release_monitors should have cleared");
1885   }
1886 
1887   // These things needs to be done while we are still a Java Thread. Make sure that thread
1888   // is in a consistent state, in case GC happens
1889   assert(_privileged_stack_top == NULL, "must be NULL when we get here");

1890 
1891   if (active_handles() != NULL) {
1892     JNIHandleBlock* block = active_handles();
1893     set_active_handles(NULL);
1894     JNIHandleBlock::release_block(block);
1895   }
1896 
1897   if (free_handle_block() != NULL) {
1898     JNIHandleBlock* block = free_handle_block();
1899     set_free_handle_block(NULL);
1900     JNIHandleBlock::release_block(block);
1901   }
1902 
1903   // These have to be removed while this is still a valid thread.
1904   remove_stack_guard_pages();
1905 
1906   if (UseTLAB) {
1907     tlab().make_parsable(true);  // retire TLAB
1908   }
1909 


2169     // }
2170     // set_thread_state(_thread_in_vm_trans);
2171     // if (safepoint) block;
2172     // set_thread_state(state);
2173     //
2174     // but that is pretty messy. Instead we just go with the way the
2175     // code has worked before and note that this is the only path to
2176     // java_suspend_self that doesn't put the thread in _thread_blocked
2177     // mode.
2178 
2179     frame_anchor()->make_walkable(this);
2180     java_suspend_self();
2181 
2182     // We might be here for reasons in addition to the self-suspend request
2183     // so check for other async requests.
2184   }
2185 
2186   if (check_asyncs) {
2187     check_and_handle_async_exceptions();
2188   }


2189 }
2190 
2191 void JavaThread::send_thread_stop(oop java_throwable)  {
2192   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2193   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2194   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2195 
2196   // Do not throw asynchronous exceptions against the compiler thread
2197   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2198   if (is_Compiler_thread()) return;
2199 
2200   {
2201     // Actually throw the Throwable against the target Thread - however
2202     // only if there is no thread death exception installed already.
2203     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2204       // If the topmost frame is a runtime stub, then we are calling into
2205       // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2206       // must deoptimize the caller before continuing, as the compiled  exception handler table
2207       // may not be valid
2208       if (has_last_Java_frame()) {


2407   if (SafepointSynchronize::do_call_back()) {
2408     // If we are safepointing, then block the caller which may not be
2409     // the same as the target thread (see above).
2410     SafepointSynchronize::block(curJT);
2411   }
2412 
2413   if (thread->is_deopt_suspend()) {
2414     thread->clear_deopt_suspend();
2415     RegisterMap map(thread, false);
2416     frame f = thread->last_frame();
2417     while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2418       f = f.sender(&map);
2419     }
2420     if (f.id() == thread->must_deopt_id()) {
2421       thread->clear_must_deopt_id();
2422       f.deoptimize(thread);
2423     } else {
2424       fatal("missed deoptimization!");
2425     }
2426   }


2427 }
2428 
2429 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2430 // progress or when _suspend_flags is non-zero.
2431 // Current thread needs to self-suspend if there is a suspend request and/or
2432 // block if a safepoint is in progress.
2433 // Also check for pending async exception (not including unsafe access error).
2434 // Note only the native==>VM/Java barriers can call this function and when
2435 // thread state is _thread_in_native_trans.
2436 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2437   check_safepoint_and_suspend_for_native_trans(thread);
2438 
2439   if (thread->has_async_exception()) {
2440     // We are in _thread_in_native_trans state, don't handle unsafe
2441     // access error since that may block.
2442     thread->check_and_handle_async_exceptions(false);
2443   }
2444 }
2445 
2446 // This is a variant of the normal


3295   assert_locked_or_safepoint(Threads_lock);
3296   // ALL_JAVA_THREADS iterates through all JavaThreads
3297   ALL_JAVA_THREADS(p) {
3298     tc->do_thread(p);
3299   }
3300   // Someday we could have a table or list of all non-JavaThreads.
3301   // For now, just manually iterate through them.
3302   tc->do_thread(VMThread::vm_thread());
3303   Universe::heap()->gc_threads_do(tc);
3304   WatcherThread *wt = WatcherThread::watcher_thread();
3305   // Strictly speaking, the following NULL check isn't sufficient to make sure
3306   // the data for WatcherThread is still valid upon being examined. However,
3307   // considering that WatchThread terminates when the VM is on the way to
3308   // exit at safepoint, the chance of the above is extremely small. The right
3309   // way to prevent termination of WatcherThread would be to acquire
3310   // Terminator_lock, but we can't do that without violating the lock rank
3311   // checking in some cases.
3312   if (wt != NULL)
3313     tc->do_thread(wt);
3314 








3315   // If CompilerThreads ever become non-JavaThreads, add them here
3316 }
3317 
3318 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
3319 
3320   extern void JDK_Version_init();
3321 
3322   // Preinitialize version info.
3323   VM_Version::early_initialize();
3324 
3325   // Check version
3326   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
3327 
3328   // Initialize the output stream module
3329   ostream_init();
3330 
3331   // Process java launcher properties.
3332   Arguments::process_sun_java_launcher_properties(args);
3333 
3334   // Initialize the os module before using TLS


3421     delete main_thread;
3422     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3423     return JNI_ENOMEM;
3424   }
3425 
3426   // Enable guard page *after* os::create_main_thread(), otherwise it would
3427   // crash Linux VM, see notes in os_linux.cpp.
3428   main_thread->create_stack_guard_pages();
3429 
3430   // Initialize Java-Level synchronization subsystem
3431   ObjectMonitor::Initialize() ;
3432 
3433   // Initialize global modules
3434   jint status = init_globals();
3435   if (status != JNI_OK) {
3436     delete main_thread;
3437     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3438     return status;
3439   }
3440 


3441   // Should be done after the heap is fully created
3442   main_thread->cache_global_variables();
3443 
3444   HandleMark hm;
3445 
3446   { MutexLocker mu(Threads_lock);
3447     Threads::add(main_thread);
3448   }
3449 
3450   // Any JVMTI raw monitors entered in onload will transition into
3451   // real raw monitor. VM is setup enough here for raw monitor enter.
3452   JvmtiExport::transition_pending_onload_raw_monitors();
3453 
3454   // Create the VMThread
3455   { TraceTime timer("Start VMThread", TraceStartupTime);
3456     VMThread::create();
3457     Thread* vmthread = VMThread::vm_thread();
3458 
3459     if (!os::create_thread(vmthread, os::vm_thread))
3460       vm_exit_during_initialization("Cannot create VM thread. Out of system resources.");


3548   //              resulting in a NoClassDefFoundError.  This can happen in any
3549   //              user code which calls methods in java.lang.Compiler.
3550   // Hack :       the hack is to pre-load and initialize this class, so that only
3551   //              system domains are on the stack when the properties are read.
3552   //              Currently even the AWT code has calls to methods in java.lang.Compiler.
3553   //              On the classic VM, java.lang.Compiler is loaded very early to load the JIT.
3554   // Future Fix : the best fix is to grant everyone permissions to read "java.compiler" and
3555   //              read and write"java.vm.info" in the default policy file. See bugid 4211383
3556   //              Once that is done, we should remove this hack.
3557   initialize_class(vmSymbols::java_lang_Compiler(), CHECK_0);
3558 
3559   // More hackery - the static initializer of java.lang.Compiler adds the string "nojit" to
3560   // the java.vm.info property if no jit gets loaded through java.lang.Compiler (the hotspot
3561   // compiler does not get loaded through java.lang.Compiler).  "java -version" with the
3562   // hotspot vm says "nojit" all the time which is confusing.  So, we reset it here.
3563   // This should also be taken out as soon as 4211383 gets fixed.
3564   reset_vm_info_property(CHECK_0);
3565 
3566   quicken_jni_functions();
3567 
3568   // Must be run after init_ft which initializes ft_enabled
3569   if (TRACE_INITIALIZE() != JNI_OK) {
3570     vm_exit_during_initialization("Failed to initialize tracing backend");
3571   }
3572 
3573   // Set flag that basic initialization has completed. Used by exceptions and various
3574   // debug stuff, that does not work until all basic classes have been initialized.
3575   set_init_completed();
3576 
3577   Metaspace::post_initialize();
3578 
3579 #ifndef USDT2
3580   HS_DTRACE_PROBE(hotspot, vm__init__end);
3581 #else /* USDT2 */
3582   HOTSPOT_VM_INIT_END();
3583 #endif /* USDT2 */
3584 
3585   // record VM initialization completion time
3586 #if INCLUDE_MANAGEMENT
3587   Management::record_vm_init_completed();
3588 #endif // INCLUDE_MANAGEMENT
3589 
3590   // Compute system loader. Note that this has to occur after set_init_completed, since
3591   // valid exceptions may be thrown in the process.
3592   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and


3621   os::signal_init();
3622 
3623   // Start Attach Listener if +StartAttachListener or it can't be started lazily
3624   if (!DisableAttachMechanism) {
3625     AttachListener::vm_start();
3626     if (StartAttachListener || AttachListener::init_at_startup()) {
3627       AttachListener::init();
3628     }
3629   }
3630 
3631   // Launch -Xrun agents
3632   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3633   // back-end can launch with -Xdebug -Xrunjdwp.
3634   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3635     create_vm_init_libraries();
3636   }
3637 
3638   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3639   JvmtiExport::post_vm_initialized();
3640 
3641   if (TRACE_START() != JNI_OK) {
3642     vm_exit_during_initialization("Failed to start tracing backend.");
3643   }
3644 
3645   if (CleanChunkPoolAsync) {
3646     Chunk::start_chunk_pool_cleaner_task();
3647   }
3648 
3649   // initialize compiler(s)
3650 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3651   CompileBroker::compilation_init();
3652 #endif
3653 
3654   if (EnableInvokeDynamic) {
3655     // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3656     // It is done after compilers are initialized, because otherwise compilations of
3657     // signature polymorphic MH intrinsics can be missed
3658     // (see SystemDictionary::find_method_handle_intrinsic).
3659     initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK_0);
3660     initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK_0);
3661     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3662   }
3663 


3983   JavaThread* thread = JavaThread::current();
3984 
3985 #ifdef ASSERT
3986   _vm_complete = false;
3987 #endif
3988   // Wait until we are the last non-daemon thread to execute
3989   { MutexLocker nu(Threads_lock);
3990     while (Threads::number_of_non_daemon_threads() > 1 )
3991       // This wait should make safepoint checks, wait without a timeout,
3992       // and wait as a suspend-equivalent condition.
3993       //
3994       // Note: If the FlatProfiler is running and this thread is waiting
3995       // for another non-daemon thread to finish, then the FlatProfiler
3996       // is waiting for the external suspend request on this thread to
3997       // complete. wait_for_ext_suspend_completion() will eventually
3998       // timeout, but that takes time. Making this wait a suspend-
3999       // equivalent condition solves that timeout problem.
4000       //
4001       Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
4002                          Mutex::_as_suspend_equivalent_flag);






4003   }
4004 
4005   // Hang forever on exit if we are reporting an error.
4006   if (ShowMessageBoxOnError && is_error_reported()) {
4007     os::infinite_sleep();
4008   }
4009   os::wait_for_keypress_at_exit();
4010 
4011   if (JDK_Version::is_jdk12x_version()) {
4012     // We are the last thread running, so check if finalizers should be run.
4013     // For 1.3 or later this is done in thread->invoke_shutdown_hooks()
4014     HandleMark rm(thread);
4015     Universe::run_finalizers_on_exit();
4016   } else {
4017     // run Java level shutdown hooks
4018     thread->invoke_shutdown_hooks();
4019   }
4020 
4021   before_exit(thread);
4022 




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/scopeDesc.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "interpreter/oopMapCache.hpp"
  35 #include "jfr/jfrEvents.hpp"
  36 #include "jvmtifiles/jvmtiEnv.hpp"
  37 #include "memory/gcLocker.inline.hpp"
  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/oopFactory.hpp"
  40 #include "memory/universe.inline.hpp"
  41 #include "oops/instanceKlass.hpp"
  42 #include "oops/objArrayOop.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "oops/symbol.hpp"
  45 #include "prims/jvm_misc.hpp"
  46 #include "prims/jvmtiExport.hpp"
  47 #include "prims/jvmtiThreadState.hpp"
  48 #include "prims/privilegedStack.hpp"
  49 #include "runtime/arguments.hpp"
  50 #include "runtime/biasedLocking.hpp"
  51 #include "runtime/deoptimization.hpp"
  52 #include "runtime/fprofiler.hpp"
  53 #include "runtime/frame.inline.hpp"
  54 #include "runtime/init.hpp"
  55 #include "runtime/interfaceSupport.hpp"


  61 #include "runtime/objectMonitor.hpp"
  62 #include "runtime/orderAccess.inline.hpp"
  63 #include "runtime/osThread.hpp"
  64 #include "runtime/safepoint.hpp"
  65 #include "runtime/sharedRuntime.hpp"
  66 #include "runtime/statSampler.hpp"
  67 #include "runtime/stubRoutines.hpp"
  68 #include "runtime/task.hpp"
  69 #include "runtime/thread.inline.hpp"
  70 #include "runtime/threadCritical.hpp"
  71 #include "runtime/threadLocalStorage.hpp"
  72 #include "runtime/vframe.hpp"
  73 #include "runtime/vframeArray.hpp"
  74 #include "runtime/vframe_hp.hpp"
  75 #include "runtime/vmThread.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/attachListener.hpp"
  78 #include "services/management.hpp"
  79 #include "services/memTracker.hpp"
  80 #include "services/threadService.hpp"


  81 #include "utilities/defaultStream.hpp"
  82 #include "utilities/dtrace.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/preserveException.hpp"
  85 #include "utilities/macros.hpp"
  86 #ifdef TARGET_OS_FAMILY_linux
  87 # include "os_linux.inline.hpp"
  88 #endif
  89 #ifdef TARGET_OS_FAMILY_solaris
  90 # include "os_solaris.inline.hpp"
  91 #endif
  92 #ifdef TARGET_OS_FAMILY_windows
  93 # include "os_windows.inline.hpp"
  94 #endif
  95 #ifdef TARGET_OS_FAMILY_bsd
  96 # include "os_bsd.inline.hpp"
  97 #endif
  98 #if INCLUDE_ALL_GCS
  99 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
 100 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
 101 #include "gc_implementation/parallelScavenge/pcTasks.hpp"
 102 #endif // INCLUDE_ALL_GCS
 103 #ifdef COMPILER1
 104 #include "c1/c1_Compiler.hpp"
 105 #endif
 106 #ifdef COMPILER2
 107 #include "opto/c2compiler.hpp"
 108 #include "opto/idealGraphPrinter.hpp"
 109 #endif
 110 #if INCLUDE_RTM_OPT
 111 #include "runtime/rtmLocking.hpp"
 112 #endif
 113 #if INCLUDE_JFR
 114 #include "jfr/jfr.hpp"
 115 #endif
 116 
 117 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 118 
 119 #ifdef DTRACE_ENABLED
 120 
 121 // Only bother with this argument setup if dtrace is available
 122 
 123 #ifndef USDT2
 124 HS_DTRACE_PROBE_DECL(hotspot, vm__init__begin);
 125 HS_DTRACE_PROBE_DECL(hotspot, vm__init__end);
 126 HS_DTRACE_PROBE_DECL5(hotspot, thread__start, char*, intptr_t,
 127   intptr_t, intptr_t, bool);
 128 HS_DTRACE_PROBE_DECL5(hotspot, thread__stop, char*, intptr_t,
 129   intptr_t, intptr_t, bool);
 130 
 131 #define DTRACE_THREAD_PROBE(probe, javathread)                             \
 132   {                                                                        \
 133     ResourceMark rm(this);                                                 \
 134     int len = 0;                                                           \
 135     const char* name = (javathread)->get_thread_name();                    \


 327   // CR 7190089: on Solaris, primordial thread's stack is adjusted
 328   // in initialize_thread(). Without the adjustment, stack size is
 329   // incorrect if stack is set to unlimited (ulimit -s unlimited).
 330   // So far, only Solaris has real implementation of initialize_thread().
 331   //
 332   // set up any platform-specific state.
 333   os::initialize_thread(this);
 334 
 335 #if INCLUDE_NMT
 336   // record thread's native stack, stack grows downward
 337   address stack_low_addr = stack_base() - stack_size();
 338   MemTracker::record_thread_stack(stack_low_addr, stack_size());
 339 #endif // INCLUDE_NMT
 340 }
 341 
 342 
 343 Thread::~Thread() {
 344   // Reclaim the objectmonitors from the omFreeList of the moribund thread.
 345   ObjectSynchronizer::omFlush (this) ;
 346 


 347   // stack_base can be NULL if the thread is never started or exited before
 348   // record_stack_base_and_size called. Although, we would like to ensure
 349   // that all started threads do call record_stack_base_and_size(), there is
 350   // not proper way to enforce that.
 351 #if INCLUDE_NMT
 352   if (_stack_base != NULL) {
 353     address low_stack_addr = stack_base() - stack_size();
 354     MemTracker::release_thread_stack(low_stack_addr, stack_size());
 355 #ifdef ASSERT
 356     set_stack_base(NULL);
 357 #endif
 358   }
 359 #endif // INCLUDE_NMT
 360 
 361   // deallocate data structures
 362   delete resource_area();
 363   // since the handle marks are using the handle area, we have to deallocated the root
 364   // handle mark before deallocating the thread's handle area,
 365   assert(last_handle_mark() != NULL, "check we have an element");
 366   delete last_handle_mark();


1195 
1196   JavaCalls::call_special(&result,
1197                          thread_group,
1198                          group,
1199                          vmSymbols::add_method_name(),
1200                          vmSymbols::thread_void_signature(),
1201                          threadObj,          // Arg 1
1202                          THREAD);
1203 
1204 
1205 }
1206 
1207 // NamedThread --  non-JavaThread subclasses with multiple
1208 // uniquely named instances should derive from this.
1209 NamedThread::NamedThread() : Thread() {
1210   _name = NULL;
1211   _processed_thread = NULL;
1212 }
1213 
1214 NamedThread::~NamedThread() {
1215   JFR_ONLY(Jfr::on_thread_exit(this);)
1216   if (_name != NULL) {
1217     FREE_C_HEAP_ARRAY(char, _name, mtThread);
1218     _name = NULL;
1219   }
1220 }
1221 
1222 void NamedThread::set_name(const char* format, ...) {
1223   guarantee(_name == NULL, "Only get to set name once.");
1224   _name = NEW_C_HEAP_ARRAY(char, max_name_len, mtThread);
1225   guarantee(_name != NULL, "alloc failure");
1226   va_list ap;
1227   va_start(ap, format);
1228   jio_vsnprintf(_name, max_name_len, format, ap);
1229   va_end(ap);
1230 }
1231 
1232 // ======= WatcherThread ========
1233 
1234 // The watcher thread exists to simulate timer interrupts.  It should
1235 // be replaced by an abstraction over whatever native support for
1236 // timer interrupts exists on the platform.
1237 
1238 WatcherThread* WatcherThread::_watcher_thread   = NULL;
1239 bool WatcherThread::_startable = false;
1240 volatile bool  WatcherThread::_should_terminate = false;
1241 
1242 WatcherThread::WatcherThread() : Thread() {
1243   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1244   if (os::create_thread(this, os::watcher_thread)) {
1245     _watcher_thread = this;
1246 
1247     // Set the watcher thread to the highest OS priority which should not be
1248     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1249     // is created. The only normal thread using this priority is the reference
1250     // handler thread, which runs for very short intervals only.
1251     // If the VMThread's priority is not lower than the WatcherThread profiling
1252     // will be inaccurate.
1253     os::set_priority(this, MaxPriority);
1254     if (!DisableStartThread) {
1255       os::start_thread(this);
1256     }
1257   }
1258 }
1259 
1260 int WatcherThread::sleep() const {
1261   MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1262 


1653 
1654   this->cache_global_variables();
1655 
1656   // Thread is now sufficient initialized to be handled by the safepoint code as being
1657   // in the VM. Change thread state from _thread_new to _thread_in_vm
1658   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1659 
1660   assert(JavaThread::current() == this, "sanity check");
1661   assert(!Thread::current()->owns_locks(), "sanity check");
1662 
1663   DTRACE_THREAD_PROBE(start, this);
1664 
1665   // This operation might block. We call that after all safepoint checks for a new thread has
1666   // been completed.
1667   this->set_active_handles(JNIHandleBlock::allocate_block());
1668 
1669   if (JvmtiExport::should_post_thread_life()) {
1670     JvmtiExport::post_thread_start(this);
1671   }
1672 
1673   JFR_ONLY(Jfr::on_thread_start(this);)




1674 
1675   // We call another function to do the rest so we are sure that the stack addresses used
1676   // from there will be lower than the stack base just computed
1677   thread_main_inner();
1678 
1679   // Note, thread is no longer valid at this point!
1680 }
1681 
1682 
1683 void JavaThread::thread_main_inner() {
1684   assert(JavaThread::current() == this, "sanity check");
1685   assert(this->threadObj() != NULL, "just checking");
1686 
1687   // Execute thread entry point unless this thread has a pending exception
1688   // or has been stopped before starting.
1689   // Note: Due to JVM_StopThread we can have pending exceptions already!
1690   if (!this->has_pending_exception() &&
1691       !java_lang_Thread::is_stillborn(this->threadObj())) {
1692     {
1693       ResourceMark rm(this);


1780           JavaValue result(T_VOID);
1781           JavaCalls::call_virtual(&result,
1782                                   group, thread_group,
1783                                   vmSymbols::uncaughtException_name(),
1784                                   vmSymbols::thread_throwable_void_signature(),
1785                                   threadObj,           // Arg 1
1786                                   uncaught_exception,  // Arg 2
1787                                   THREAD);
1788         }
1789         if (HAS_PENDING_EXCEPTION) {
1790           ResourceMark rm(this);
1791           jio_fprintf(defaultStream::error_stream(),
1792                 "\nException: %s thrown from the UncaughtExceptionHandler"
1793                 " in thread \"%s\"\n",
1794                 pending_exception()->klass()->external_name(),
1795                 get_thread_name());
1796           CLEAR_PENDING_EXCEPTION;
1797         }
1798       }
1799     }
1800     JFR_ONLY(Jfr::on_java_thread_dismantle(this);)










1801 
1802     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1803     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1804     // is deprecated anyhow.
1805     if (!is_Compiler_thread()) {
1806       int count = 3;
1807       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1808         EXCEPTION_MARK;
1809         JavaValue result(T_VOID);
1810         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1811         JavaCalls::call_virtual(&result,
1812                               threadObj, thread_klass,
1813                               vmSymbols::exit_method_name(),
1814                               vmSymbols::void_method_signature(),
1815                               THREAD);
1816         CLEAR_PENDING_EXCEPTION;
1817       }
1818     }
1819     // notify JVMTI
1820     if (JvmtiExport::should_post_thread_life()) {


1857   // group should have the destroyed bit set before waiters are notified).
1858   ensure_join(this);
1859   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1860 
1861   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1862   // held by this thread must be released.  A detach operation must only
1863   // get here if there are no Java frames on the stack.  Therefore, any
1864   // owned monitors at this point MUST be JNI-acquired monitors which are
1865   // pre-inflated and in the monitor cache.
1866   //
1867   // ensure_join() ignores IllegalThreadStateExceptions, and so does this.
1868   if (exit_type == jni_detach && JNIDetachReleasesMonitors) {
1869     assert(!this->has_last_Java_frame(), "detaching with Java frames?");
1870     ObjectSynchronizer::release_monitors_owned_by_thread(this);
1871     assert(!this->has_pending_exception(), "release_monitors should have cleared");
1872   }
1873 
1874   // These things needs to be done while we are still a Java Thread. Make sure that thread
1875   // is in a consistent state, in case GC happens
1876   assert(_privileged_stack_top == NULL, "must be NULL when we get here");
1877   JFR_ONLY(Jfr::on_thread_exit(this);)
1878 
1879   if (active_handles() != NULL) {
1880     JNIHandleBlock* block = active_handles();
1881     set_active_handles(NULL);
1882     JNIHandleBlock::release_block(block);
1883   }
1884 
1885   if (free_handle_block() != NULL) {
1886     JNIHandleBlock* block = free_handle_block();
1887     set_free_handle_block(NULL);
1888     JNIHandleBlock::release_block(block);
1889   }
1890 
1891   // These have to be removed while this is still a valid thread.
1892   remove_stack_guard_pages();
1893 
1894   if (UseTLAB) {
1895     tlab().make_parsable(true);  // retire TLAB
1896   }
1897 


2157     // }
2158     // set_thread_state(_thread_in_vm_trans);
2159     // if (safepoint) block;
2160     // set_thread_state(state);
2161     //
2162     // but that is pretty messy. Instead we just go with the way the
2163     // code has worked before and note that this is the only path to
2164     // java_suspend_self that doesn't put the thread in _thread_blocked
2165     // mode.
2166 
2167     frame_anchor()->make_walkable(this);
2168     java_suspend_self();
2169 
2170     // We might be here for reasons in addition to the self-suspend request
2171     // so check for other async requests.
2172   }
2173 
2174   if (check_asyncs) {
2175     check_and_handle_async_exceptions();
2176   }
2177 
2178   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2179 }
2180 
2181 void JavaThread::send_thread_stop(oop java_throwable)  {
2182   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2183   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2184   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2185 
2186   // Do not throw asynchronous exceptions against the compiler thread
2187   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2188   if (is_Compiler_thread()) return;
2189 
2190   {
2191     // Actually throw the Throwable against the target Thread - however
2192     // only if there is no thread death exception installed already.
2193     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2194       // If the topmost frame is a runtime stub, then we are calling into
2195       // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2196       // must deoptimize the caller before continuing, as the compiled  exception handler table
2197       // may not be valid
2198       if (has_last_Java_frame()) {


2397   if (SafepointSynchronize::do_call_back()) {
2398     // If we are safepointing, then block the caller which may not be
2399     // the same as the target thread (see above).
2400     SafepointSynchronize::block(curJT);
2401   }
2402 
2403   if (thread->is_deopt_suspend()) {
2404     thread->clear_deopt_suspend();
2405     RegisterMap map(thread, false);
2406     frame f = thread->last_frame();
2407     while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2408       f = f.sender(&map);
2409     }
2410     if (f.id() == thread->must_deopt_id()) {
2411       thread->clear_must_deopt_id();
2412       f.deoptimize(thread);
2413     } else {
2414       fatal("missed deoptimization!");
2415     }
2416   }
2417 
2418   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2419 }
2420 
2421 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2422 // progress or when _suspend_flags is non-zero.
2423 // Current thread needs to self-suspend if there is a suspend request and/or
2424 // block if a safepoint is in progress.
2425 // Also check for pending async exception (not including unsafe access error).
2426 // Note only the native==>VM/Java barriers can call this function and when
2427 // thread state is _thread_in_native_trans.
2428 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2429   check_safepoint_and_suspend_for_native_trans(thread);
2430 
2431   if (thread->has_async_exception()) {
2432     // We are in _thread_in_native_trans state, don't handle unsafe
2433     // access error since that may block.
2434     thread->check_and_handle_async_exceptions(false);
2435   }
2436 }
2437 
2438 // This is a variant of the normal


3287   assert_locked_or_safepoint(Threads_lock);
3288   // ALL_JAVA_THREADS iterates through all JavaThreads
3289   ALL_JAVA_THREADS(p) {
3290     tc->do_thread(p);
3291   }
3292   // Someday we could have a table or list of all non-JavaThreads.
3293   // For now, just manually iterate through them.
3294   tc->do_thread(VMThread::vm_thread());
3295   Universe::heap()->gc_threads_do(tc);
3296   WatcherThread *wt = WatcherThread::watcher_thread();
3297   // Strictly speaking, the following NULL check isn't sufficient to make sure
3298   // the data for WatcherThread is still valid upon being examined. However,
3299   // considering that WatchThread terminates when the VM is on the way to
3300   // exit at safepoint, the chance of the above is extremely small. The right
3301   // way to prevent termination of WatcherThread would be to acquire
3302   // Terminator_lock, but we can't do that without violating the lock rank
3303   // checking in some cases.
3304   if (wt != NULL)
3305     tc->do_thread(wt);
3306 
3307 #if INCLUDE_JFR
3308   Thread* sampler_thread = Jfr::sampler_thread();
3309   if (sampler_thread != NULL) {
3310     tc->do_thread(sampler_thread);
3311   }
3312 
3313 #endif
3314 
3315   // If CompilerThreads ever become non-JavaThreads, add them here
3316 }
3317 
3318 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
3319 
3320   extern void JDK_Version_init();
3321 
3322   // Preinitialize version info.
3323   VM_Version::early_initialize();
3324 
3325   // Check version
3326   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
3327 
3328   // Initialize the output stream module
3329   ostream_init();
3330 
3331   // Process java launcher properties.
3332   Arguments::process_sun_java_launcher_properties(args);
3333 
3334   // Initialize the os module before using TLS


3421     delete main_thread;
3422     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3423     return JNI_ENOMEM;
3424   }
3425 
3426   // Enable guard page *after* os::create_main_thread(), otherwise it would
3427   // crash Linux VM, see notes in os_linux.cpp.
3428   main_thread->create_stack_guard_pages();
3429 
3430   // Initialize Java-Level synchronization subsystem
3431   ObjectMonitor::Initialize() ;
3432 
3433   // Initialize global modules
3434   jint status = init_globals();
3435   if (status != JNI_OK) {
3436     delete main_thread;
3437     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3438     return status;
3439   }
3440 
3441   JFR_ONLY(Jfr::on_vm_init();)
3442 
3443   // Should be done after the heap is fully created
3444   main_thread->cache_global_variables();
3445 
3446   HandleMark hm;
3447 
3448   { MutexLocker mu(Threads_lock);
3449     Threads::add(main_thread);
3450   }
3451 
3452   // Any JVMTI raw monitors entered in onload will transition into
3453   // real raw monitor. VM is setup enough here for raw monitor enter.
3454   JvmtiExport::transition_pending_onload_raw_monitors();
3455 
3456   // Create the VMThread
3457   { TraceTime timer("Start VMThread", TraceStartupTime);
3458     VMThread::create();
3459     Thread* vmthread = VMThread::vm_thread();
3460 
3461     if (!os::create_thread(vmthread, os::vm_thread))
3462       vm_exit_during_initialization("Cannot create VM thread. Out of system resources.");


3550   //              resulting in a NoClassDefFoundError.  This can happen in any
3551   //              user code which calls methods in java.lang.Compiler.
3552   // Hack :       the hack is to pre-load and initialize this class, so that only
3553   //              system domains are on the stack when the properties are read.
3554   //              Currently even the AWT code has calls to methods in java.lang.Compiler.
3555   //              On the classic VM, java.lang.Compiler is loaded very early to load the JIT.
3556   // Future Fix : the best fix is to grant everyone permissions to read "java.compiler" and
3557   //              read and write"java.vm.info" in the default policy file. See bugid 4211383
3558   //              Once that is done, we should remove this hack.
3559   initialize_class(vmSymbols::java_lang_Compiler(), CHECK_0);
3560 
3561   // More hackery - the static initializer of java.lang.Compiler adds the string "nojit" to
3562   // the java.vm.info property if no jit gets loaded through java.lang.Compiler (the hotspot
3563   // compiler does not get loaded through java.lang.Compiler).  "java -version" with the
3564   // hotspot vm says "nojit" all the time which is confusing.  So, we reset it here.
3565   // This should also be taken out as soon as 4211383 gets fixed.
3566   reset_vm_info_property(CHECK_0);
3567 
3568   quicken_jni_functions();
3569 





3570   // Set flag that basic initialization has completed. Used by exceptions and various
3571   // debug stuff, that does not work until all basic classes have been initialized.
3572   set_init_completed();
3573 
3574   Metaspace::post_initialize();
3575 
3576 #ifndef USDT2
3577   HS_DTRACE_PROBE(hotspot, vm__init__end);
3578 #else /* USDT2 */
3579   HOTSPOT_VM_INIT_END();
3580 #endif /* USDT2 */
3581 
3582   // record VM initialization completion time
3583 #if INCLUDE_MANAGEMENT
3584   Management::record_vm_init_completed();
3585 #endif // INCLUDE_MANAGEMENT
3586 
3587   // Compute system loader. Note that this has to occur after set_init_completed, since
3588   // valid exceptions may be thrown in the process.
3589   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and


3618   os::signal_init();
3619 
3620   // Start Attach Listener if +StartAttachListener or it can't be started lazily
3621   if (!DisableAttachMechanism) {
3622     AttachListener::vm_start();
3623     if (StartAttachListener || AttachListener::init_at_startup()) {
3624       AttachListener::init();
3625     }
3626   }
3627 
3628   // Launch -Xrun agents
3629   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3630   // back-end can launch with -Xdebug -Xrunjdwp.
3631   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3632     create_vm_init_libraries();
3633   }
3634 
3635   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3636   JvmtiExport::post_vm_initialized();
3637 
3638   JFR_ONLY(Jfr::on_vm_start();)


3639 
3640   if (CleanChunkPoolAsync) {
3641     Chunk::start_chunk_pool_cleaner_task();
3642   }
3643 
3644   // initialize compiler(s)
3645 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3646   CompileBroker::compilation_init();
3647 #endif
3648 
3649   if (EnableInvokeDynamic) {
3650     // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3651     // It is done after compilers are initialized, because otherwise compilations of
3652     // signature polymorphic MH intrinsics can be missed
3653     // (see SystemDictionary::find_method_handle_intrinsic).
3654     initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK_0);
3655     initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK_0);
3656     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3657   }
3658 


3978   JavaThread* thread = JavaThread::current();
3979 
3980 #ifdef ASSERT
3981   _vm_complete = false;
3982 #endif
3983   // Wait until we are the last non-daemon thread to execute
3984   { MutexLocker nu(Threads_lock);
3985     while (Threads::number_of_non_daemon_threads() > 1 )
3986       // This wait should make safepoint checks, wait without a timeout,
3987       // and wait as a suspend-equivalent condition.
3988       //
3989       // Note: If the FlatProfiler is running and this thread is waiting
3990       // for another non-daemon thread to finish, then the FlatProfiler
3991       // is waiting for the external suspend request on this thread to
3992       // complete. wait_for_ext_suspend_completion() will eventually
3993       // timeout, but that takes time. Making this wait a suspend-
3994       // equivalent condition solves that timeout problem.
3995       //
3996       Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
3997                          Mutex::_as_suspend_equivalent_flag);
3998   }
3999 
4000   EventShutdown e;
4001   if (e.should_commit()) {
4002     e.set_reason("No remaining non-daemon Java threads");
4003     e.commit();
4004   }
4005 
4006   // Hang forever on exit if we are reporting an error.
4007   if (ShowMessageBoxOnError && is_error_reported()) {
4008     os::infinite_sleep();
4009   }
4010   os::wait_for_keypress_at_exit();
4011 
4012   if (JDK_Version::is_jdk12x_version()) {
4013     // We are the last thread running, so check if finalizers should be run.
4014     // For 1.3 or later this is done in thread->invoke_shutdown_hooks()
4015     HandleMark rm(thread);
4016     Universe::run_finalizers_on_exit();
4017   } else {
4018     // run Java level shutdown hooks
4019     thread->invoke_shutdown_hooks();
4020   }
4021 
4022   before_exit(thread);
4023 


< prev index next >