src/share/vm/runtime/thread.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File webrev Sdiff src/share/vm/runtime

src/share/vm/runtime/thread.cpp

Print this page




  45 #include "memory/oopFactory.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "memory/universe.inline.hpp"
  48 #include "oops/instanceKlass.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/symbol.hpp"
  52 #include "oops/verifyOopClosure.hpp"
  53 #include "prims/jvm.h"
  54 #include "prims/jvm_misc.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "prims/jvmtiThreadState.hpp"
  57 #include "prims/privilegedStack.hpp"
  58 #include "runtime/arguments.hpp"
  59 #include "runtime/atomic.hpp"
  60 #include "runtime/biasedLocking.hpp"
  61 #include "runtime/commandLineFlagConstraintList.hpp"
  62 #include "runtime/commandLineFlagWriteableList.hpp"
  63 #include "runtime/commandLineFlagRangeList.hpp"
  64 #include "runtime/deoptimization.hpp"
  65 #include "runtime/fprofiler.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/globals.hpp"
  68 #include "runtime/init.hpp"
  69 #include "runtime/interfaceSupport.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/jniPeriodicChecker.hpp"
  73 #include "runtime/timerTrace.hpp"
  74 #include "runtime/memprofiler.hpp"
  75 #include "runtime/mutexLocker.hpp"
  76 #include "runtime/objectMonitor.hpp"
  77 #include "runtime/orderAccess.inline.hpp"
  78 #include "runtime/osThread.hpp"
  79 #include "runtime/safepoint.hpp"
  80 #include "runtime/sharedRuntime.hpp"
  81 #include "runtime/statSampler.hpp"
  82 #include "runtime/stubRoutines.hpp"
  83 #include "runtime/sweeper.hpp"
  84 #include "runtime/task.hpp"
  85 #include "runtime/thread.inline.hpp"


 729   return false;
 730 }
 731 
 732 #ifndef PRODUCT
 733 void JavaThread::record_jump(address target, address instr, const char* file,
 734                              int line) {
 735 
 736   // This should not need to be atomic as the only way for simultaneous
 737   // updates is via interrupts. Even then this should be rare or non-existent
 738   // and we don't care that much anyway.
 739 
 740   int index = _jmp_ring_index;
 741   _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
 742   _jmp_ring[index]._target = (intptr_t) target;
 743   _jmp_ring[index]._instruction = (intptr_t) instr;
 744   _jmp_ring[index]._file = file;
 745   _jmp_ring[index]._line = line;
 746 }
 747 #endif // PRODUCT
 748 
 749 // Called by flat profiler
 750 // Callers have already called wait_for_ext_suspend_completion
 751 // The assertion for that is currently too complex to put here:
 752 bool JavaThread::profile_last_Java_frame(frame* _fr) {
 753   bool gotframe = false;
 754   // self suspension saves needed state.
 755   if (has_last_Java_frame() && _anchor.walkable()) {
 756     *_fr = pd_last_frame();
 757     gotframe = true;
 758   }
 759   return gotframe;
 760 }
 761 
 762 void Thread::interrupt(Thread* thread) {
 763   debug_only(check_for_dangling_thread_pointer(thread);)
 764   os::interrupt(thread);
 765 }
 766 
 767 bool Thread::is_interrupted(Thread* thread, bool clear_interrupted) {
 768   debug_only(check_for_dangling_thread_pointer(thread);)
 769   // Note:  If clear_interrupted==false, this simply fetches and
 770   // returns the value of the field osthread()->interrupted().
 771   return os::is_interrupted(thread, clear_interrupted);
 772 }
 773 
 774 
 775 // GC Support
 776 bool Thread::claim_oops_do_par_case(int strong_roots_parity) {
 777   jint thread_parity = _oops_do_parity;
 778   if (thread_parity != strong_roots_parity) {
 779     jint res = Atomic::cmpxchg(strong_roots_parity, &_oops_do_parity, thread_parity);
 780     if (res == thread_parity) {
 781       return true;


1355 
1356 void WatcherThread::stop() {
1357   {
1358     // Follow normal safepoint aware lock enter protocol since the
1359     // WatcherThread is stopped by another JavaThread.
1360     MutexLocker ml(PeriodicTask_lock);
1361     _should_terminate = true;
1362 
1363     WatcherThread* watcher = watcher_thread();
1364     if (watcher != NULL) {
1365       // unpark the WatcherThread so it can see that it should terminate
1366       watcher->unpark();
1367     }
1368   }
1369 
1370   MutexLocker mu(Terminator_lock);
1371 
1372   while (watcher_thread() != NULL) {
1373     // This wait should make safepoint checks, wait without a timeout,
1374     // and wait as a suspend-equivalent condition.
1375     //
1376     // Note: If the FlatProfiler is running, then this thread is waiting
1377     // for the WatcherThread to terminate and the WatcherThread, via the
1378     // FlatProfiler task, is waiting for the external suspend request on
1379     // this thread to complete. wait_for_ext_suspend_completion() will
1380     // eventually timeout, but that takes time. Making this wait a
1381     // suspend-equivalent condition solves that timeout problem.
1382     //
1383     Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
1384                           Mutex::_as_suspend_equivalent_flag);
1385   }
1386 }
1387 
1388 void WatcherThread::unpark() {
1389   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1390   PeriodicTask_lock->notify();
1391 }
1392 
1393 void WatcherThread::print_on(outputStream* st) const {
1394   st->print("\"%s\" ", name());
1395   Thread::print_on(st);
1396   st->cr();
1397 }
1398 
1399 // ======= JavaThread ========
1400 
1401 #if INCLUDE_JVMCI
1402 


1479   _jvmti_get_loaded_classes_closure = NULL;
1480   _interp_only_mode    = 0;
1481   _special_runtime_exit_condition = _no_async_condition;
1482   _pending_async_exception = NULL;
1483   _thread_stat = NULL;
1484   _thread_stat = new ThreadStatistics();
1485   _blocked_on_compilation = false;
1486   _jni_active_critical = 0;
1487   _pending_jni_exception_check_fn = NULL;
1488   _do_not_unlock_if_synchronized = false;
1489   _cached_monitor_info = NULL;
1490   _parker = Parker::Allocate(this);
1491 
1492 #ifndef PRODUCT
1493   _jmp_ring_index = 0;
1494   for (int ji = 0; ji < jump_ring_buffer_size; ji++) {
1495     record_jump(NULL, NULL, NULL, 0);
1496   }
1497 #endif // PRODUCT
1498 
1499   set_thread_profiler(NULL);
1500   if (FlatProfiler::is_active()) {
1501     // This is where we would decide to either give each thread it's own profiler
1502     // or use one global one from FlatProfiler,
1503     // or up to some count of the number of profiled threads, etc.
1504     ThreadProfiler* pp = new ThreadProfiler();
1505     pp->engage();
1506     set_thread_profiler(pp);
1507   }
1508 
1509   // Setup safepoint state info for this thread
1510   ThreadSafepointState::create(this);
1511 
1512   debug_only(_java_call_counter = 0);
1513 
1514   // JVMTI PopFrame support
1515   _popframe_condition = popframe_inactive;
1516   _popframe_preserved_args = NULL;
1517   _popframe_preserved_args_size = 0;
1518   _frames_to_pop_failed_realloc = 0;
1519 
1520   pd_initialize();
1521 }
1522 
1523 #if INCLUDE_ALL_GCS
1524 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1525 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1526 #endif // INCLUDE_ALL_GCS
1527 
1528 JavaThread::JavaThread(bool is_attaching_via_jni) :


1634     old_array->set_unroll_block(NULL);
1635     delete old_info;
1636     delete old_array;
1637   }
1638 
1639   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1640   if (deferred != NULL) {
1641     // This can only happen if thread is destroyed before deoptimization occurs.
1642     assert(deferred->length() != 0, "empty array!");
1643     do {
1644       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1645       deferred->remove_at(0);
1646       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1647       delete dlv;
1648     } while (deferred->length() != 0);
1649     delete deferred;
1650   }
1651 
1652   // All Java related clean up happens in exit
1653   ThreadSafepointState::destroy(this);
1654   if (_thread_profiler != NULL) delete _thread_profiler;
1655   if (_thread_stat != NULL) delete _thread_stat;
1656 
1657 #if INCLUDE_JVMCI
1658   if (JVMCICounterSize > 0) {
1659     if (jvmci_counters_include(this)) {
1660       for (int i = 0; i < JVMCICounterSize; i++) {
1661         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1662       }
1663     }
1664     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1665   }
1666 #endif // INCLUDE_JVMCI
1667 }
1668 
1669 
1670 // The first routine called by a new Java thread
1671 void JavaThread::run() {
1672   // initialize thread-local alloc buffer related fields
1673   this->initialize_tlab();
1674 


1749   // Clear the native thread instance - this makes isAlive return false and allows the join()
1750   // to complete once we've done the notify_all below
1751   java_lang_Thread::set_thread(threadObj(), NULL);
1752   lock.notify_all(thread);
1753   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1754   thread->clear_pending_exception();
1755 }
1756 
1757 
1758 // For any new cleanup additions, please check to see if they need to be applied to
1759 // cleanup_failed_attach_current_thread as well.
1760 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1761   assert(this == JavaThread::current(), "thread consistency check");
1762 
1763   HandleMark hm(this);
1764   Handle uncaught_exception(this, this->pending_exception());
1765   this->clear_pending_exception();
1766   Handle threadObj(this, this->threadObj());
1767   assert(threadObj.not_null(), "Java thread object should be created");
1768 
1769   if (get_thread_profiler() != NULL) {
1770     get_thread_profiler()->disengage();
1771     ResourceMark rm;
1772     get_thread_profiler()->print(get_thread_name());
1773   }
1774 
1775 
1776   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1777   {
1778     EXCEPTION_MARK;
1779 
1780     CLEAR_PENDING_EXCEPTION;
1781   }
1782   if (!destroy_vm) {
1783     if (uncaught_exception.not_null()) {
1784       EXCEPTION_MARK;
1785       // Call method Thread.dispatchUncaughtException().
1786       Klass* thread_klass = SystemDictionary::Thread_klass();
1787       JavaValue result(T_VOID);
1788       JavaCalls::call_virtual(&result,
1789                               threadObj, thread_klass,
1790                               vmSymbols::dispatchUncaughtException_name(),
1791                               vmSymbols::throwable_void_signature(),
1792                               uncaught_exception,
1793                               THREAD);
1794       if (HAS_PENDING_EXCEPTION) {
1795         ResourceMark rm(this);


1957   SATBMarkQueue& satb_queue = satb_mark_queue();
1958   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1959   // The SATB queue should have been constructed with its active
1960   // field set to false.
1961   assert(!satb_queue.is_active(), "SATB queue should not be active");
1962   assert(satb_queue.is_empty(), "SATB queue should be empty");
1963   // If we are creating the thread during a marking cycle, we should
1964   // set the active field of the SATB queue to true.
1965   if (satb_queue_set.is_active()) {
1966     satb_queue.set_active(true);
1967   }
1968 
1969   DirtyCardQueue& dirty_queue = dirty_card_queue();
1970   // The dirty card queue should have been constructed with its
1971   // active field set to true.
1972   assert(dirty_queue.is_active(), "dirty card queue should be active");
1973 }
1974 #endif // INCLUDE_ALL_GCS
1975 
1976 void JavaThread::cleanup_failed_attach_current_thread() {
1977   if (get_thread_profiler() != NULL) {
1978     get_thread_profiler()->disengage();
1979     ResourceMark rm;
1980     get_thread_profiler()->print(get_thread_name());
1981   }
1982 
1983   if (active_handles() != NULL) {
1984     JNIHandleBlock* block = active_handles();
1985     set_active_handles(NULL);
1986     JNIHandleBlock::release_block(block);
1987   }
1988 
1989   if (free_handle_block() != NULL) {
1990     JNIHandleBlock* block = free_handle_block();
1991     set_free_handle_block(NULL);
1992     JNIHandleBlock::release_block(block);
1993   }
1994 
1995   // These have to be removed while this is still a valid thread.
1996   remove_stack_guard_pages();
1997 
1998   if (UseTLAB) {
1999     tlab().make_parsable(true);  // retire TLAB, if any
2000   }
2001 
2002 #if INCLUDE_ALL_GCS


2746     Thread* thread = Thread::current();
2747     if (thread->is_Named_thread()) {
2748       _cur_thr = (NamedThread *)thread;
2749       _cur_thr->set_processed_thread(jthr);
2750     } else {
2751       _cur_thr = NULL;
2752     }
2753   }
2754 
2755   ~RememberProcessedThread() {
2756     if (_cur_thr) {
2757       _cur_thr->set_processed_thread(NULL);
2758     }
2759   }
2760 };
2761 
2762 void JavaThread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
2763   // Verify that the deferred card marks have been flushed.
2764   assert(deferred_card_mark().is_empty(), "Should be empty during GC");
2765 
2766   // The ThreadProfiler oops_do is done from FlatProfiler::oops_do
2767   // since there may be more than one thread using each ThreadProfiler.
2768 
2769   // Traverse the GCHandles
2770   Thread::oops_do(f, cf);
2771 
2772   JVMCI_ONLY(f->do_oop((oop*)&_pending_failed_speculation);)
2773 
2774   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
2775          (has_last_Java_frame() && java_call_counter() > 0), "wrong java_sp info!");
2776 
2777   if (has_last_Java_frame()) {
2778     // Record JavaThread to GC thread
2779     RememberProcessedThread rpt(this);
2780 
2781     // Traverse the privileged stack
2782     if (_privileged_stack_top != NULL) {
2783       _privileged_stack_top->oops_do(f);
2784     }
2785 
2786     // traverse the registered growable array
2787     if (_array_for_gc != NULL) {
2788       for (int index = 0; index < _array_for_gc->length(); index++) {


3793   JvmtiExport::enter_live_phase();
3794 
3795   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3796   JvmtiExport::post_vm_initialized();
3797 
3798   if (TRACE_START() != JNI_OK) {
3799     vm_exit_during_initialization("Failed to start tracing backend.");
3800   }
3801 
3802 #if INCLUDE_MANAGEMENT
3803   Management::initialize(THREAD);
3804 
3805   if (HAS_PENDING_EXCEPTION) {
3806     // management agent fails to start possibly due to
3807     // configuration problem and is responsible for printing
3808     // stack trace if appropriate. Simply exit VM.
3809     vm_exit(1);
3810   }
3811 #endif // INCLUDE_MANAGEMENT
3812 
3813   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
3814   if (MemProfiling)                   MemProfiler::engage();
3815   StatSampler::engage();
3816   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3817 
3818   BiasedLocking::init();
3819 
3820 #if INCLUDE_RTM_OPT
3821   RTMLockingCounters::init();
3822 #endif
3823 
3824   if (JDK_Version::current().post_vm_init_hook_enabled()) {
3825     call_postVMInitHook(THREAD);
3826     // The Java side of PostVMInitHook.run must deal with all
3827     // exceptions and provide means of diagnosis.
3828     if (HAS_PENDING_EXCEPTION) {
3829       CLEAR_PENDING_EXCEPTION;
3830     }
3831   }
3832 
3833   {


4083   }
4084   CLEAR_PENDING_EXCEPTION;
4085 }
4086 
4087 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4088 // the program falls off the end of main(). Another VM exit path is through
4089 // vm_exit() when the program calls System.exit() to return a value or when
4090 // there is a serious error in VM. The two shutdown paths are not exactly
4091 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4092 // and VM_Exit op at VM level.
4093 //
4094 // Shutdown sequence:
4095 //   + Shutdown native memory tracking if it is on
4096 //   + Wait until we are the last non-daemon thread to execute
4097 //     <-- every thing is still working at this moment -->
4098 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
4099 //        shutdown hooks, run finalizers if finalization-on-exit
4100 //   + Call before_exit(), prepare for VM exit
4101 //      > run VM level shutdown hooks (they are registered through JVM_OnExit(),
4102 //        currently the only user of this mechanism is File.deleteOnExit())
4103 //      > stop flat profiler, StatSampler, watcher thread, CMS threads,
4104 //        post thread end and vm death events to JVMTI,
4105 //        stop signal thread
4106 //   + Call JavaThread::exit(), it will:
4107 //      > release JNI handle blocks, remove stack guard pages
4108 //      > remove this thread from Threads list
4109 //     <-- no more Java code from this thread after this point -->
4110 //   + Stop VM thread, it will bring the remaining VM to a safepoint and stop
4111 //     the compiler threads at safepoint
4112 //     <-- do not use anything that could get blocked by Safepoint -->
4113 //   + Disable tracing at JNI/JVM barriers
4114 //   + Set _vm_exited flag for threads that are still running native code
4115 //   + Delete this thread
4116 //   + Call exit_globals()
4117 //      > deletes tty
4118 //      > deletes PerfMemory resources
4119 //   + Return to caller
4120 
4121 bool Threads::destroy_vm() {
4122   JavaThread* thread = JavaThread::current();
4123 
4124 #ifdef ASSERT
4125   _vm_complete = false;
4126 #endif
4127   // Wait until we are the last non-daemon thread to execute
4128   { MutexLocker nu(Threads_lock);
4129     while (Threads::number_of_non_daemon_threads() > 1)
4130       // This wait should make safepoint checks, wait without a timeout,
4131       // and wait as a suspend-equivalent condition.
4132       //
4133       // Note: If the FlatProfiler is running and this thread is waiting
4134       // for another non-daemon thread to finish, then the FlatProfiler
4135       // is waiting for the external suspend request on this thread to
4136       // complete. wait_for_ext_suspend_completion() will eventually
4137       // timeout, but that takes time. Making this wait a suspend-
4138       // equivalent condition solves that timeout problem.
4139       //
4140       Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
4141                          Mutex::_as_suspend_equivalent_flag);
4142   }
4143 
4144   // Hang forever on exit if we are reporting an error.
4145   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
4146     os::infinite_sleep();
4147   }
4148   os::wait_for_keypress_at_exit();
4149 
4150   // run Java level shutdown hooks
4151   thread->invoke_shutdown_hooks();
4152 
4153   before_exit(thread);
4154 
4155   thread->exit(true);
4156 
4157   // Stop VM thread.
4158   {
4159     // 4945125 The vm thread comes to a safepoint during exit.




  45 #include "memory/oopFactory.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "memory/universe.inline.hpp"
  48 #include "oops/instanceKlass.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/symbol.hpp"
  52 #include "oops/verifyOopClosure.hpp"
  53 #include "prims/jvm.h"
  54 #include "prims/jvm_misc.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "prims/jvmtiThreadState.hpp"
  57 #include "prims/privilegedStack.hpp"
  58 #include "runtime/arguments.hpp"
  59 #include "runtime/atomic.hpp"
  60 #include "runtime/biasedLocking.hpp"
  61 #include "runtime/commandLineFlagConstraintList.hpp"
  62 #include "runtime/commandLineFlagWriteableList.hpp"
  63 #include "runtime/commandLineFlagRangeList.hpp"
  64 #include "runtime/deoptimization.hpp"

  65 #include "runtime/frame.inline.hpp"
  66 #include "runtime/globals.hpp"
  67 #include "runtime/init.hpp"
  68 #include "runtime/interfaceSupport.hpp"
  69 #include "runtime/java.hpp"
  70 #include "runtime/javaCalls.hpp"
  71 #include "runtime/jniPeriodicChecker.hpp"
  72 #include "runtime/timerTrace.hpp"
  73 #include "runtime/memprofiler.hpp"
  74 #include "runtime/mutexLocker.hpp"
  75 #include "runtime/objectMonitor.hpp"
  76 #include "runtime/orderAccess.inline.hpp"
  77 #include "runtime/osThread.hpp"
  78 #include "runtime/safepoint.hpp"
  79 #include "runtime/sharedRuntime.hpp"
  80 #include "runtime/statSampler.hpp"
  81 #include "runtime/stubRoutines.hpp"
  82 #include "runtime/sweeper.hpp"
  83 #include "runtime/task.hpp"
  84 #include "runtime/thread.inline.hpp"


 728   return false;
 729 }
 730 
 731 #ifndef PRODUCT
 732 void JavaThread::record_jump(address target, address instr, const char* file,
 733                              int line) {
 734 
 735   // This should not need to be atomic as the only way for simultaneous
 736   // updates is via interrupts. Even then this should be rare or non-existent
 737   // and we don't care that much anyway.
 738 
 739   int index = _jmp_ring_index;
 740   _jmp_ring_index = (index + 1) & (jump_ring_buffer_size - 1);
 741   _jmp_ring[index]._target = (intptr_t) target;
 742   _jmp_ring[index]._instruction = (intptr_t) instr;
 743   _jmp_ring[index]._file = file;
 744   _jmp_ring[index]._line = line;
 745 }
 746 #endif // PRODUCT
 747 













 748 void Thread::interrupt(Thread* thread) {
 749   debug_only(check_for_dangling_thread_pointer(thread);)
 750   os::interrupt(thread);
 751 }
 752 
 753 bool Thread::is_interrupted(Thread* thread, bool clear_interrupted) {
 754   debug_only(check_for_dangling_thread_pointer(thread);)
 755   // Note:  If clear_interrupted==false, this simply fetches and
 756   // returns the value of the field osthread()->interrupted().
 757   return os::is_interrupted(thread, clear_interrupted);
 758 }
 759 
 760 
 761 // GC Support
 762 bool Thread::claim_oops_do_par_case(int strong_roots_parity) {
 763   jint thread_parity = _oops_do_parity;
 764   if (thread_parity != strong_roots_parity) {
 765     jint res = Atomic::cmpxchg(strong_roots_parity, &_oops_do_parity, thread_parity);
 766     if (res == thread_parity) {
 767       return true;


1341 
1342 void WatcherThread::stop() {
1343   {
1344     // Follow normal safepoint aware lock enter protocol since the
1345     // WatcherThread is stopped by another JavaThread.
1346     MutexLocker ml(PeriodicTask_lock);
1347     _should_terminate = true;
1348 
1349     WatcherThread* watcher = watcher_thread();
1350     if (watcher != NULL) {
1351       // unpark the WatcherThread so it can see that it should terminate
1352       watcher->unpark();
1353     }
1354   }
1355 
1356   MutexLocker mu(Terminator_lock);
1357 
1358   while (watcher_thread() != NULL) {
1359     // This wait should make safepoint checks, wait without a timeout,
1360     // and wait as a suspend-equivalent condition.








1361     Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
1362                           Mutex::_as_suspend_equivalent_flag);
1363   }
1364 }
1365 
1366 void WatcherThread::unpark() {
1367   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1368   PeriodicTask_lock->notify();
1369 }
1370 
1371 void WatcherThread::print_on(outputStream* st) const {
1372   st->print("\"%s\" ", name());
1373   Thread::print_on(st);
1374   st->cr();
1375 }
1376 
1377 // ======= JavaThread ========
1378 
1379 #if INCLUDE_JVMCI
1380 


1457   _jvmti_get_loaded_classes_closure = NULL;
1458   _interp_only_mode    = 0;
1459   _special_runtime_exit_condition = _no_async_condition;
1460   _pending_async_exception = NULL;
1461   _thread_stat = NULL;
1462   _thread_stat = new ThreadStatistics();
1463   _blocked_on_compilation = false;
1464   _jni_active_critical = 0;
1465   _pending_jni_exception_check_fn = NULL;
1466   _do_not_unlock_if_synchronized = false;
1467   _cached_monitor_info = NULL;
1468   _parker = Parker::Allocate(this);
1469 
1470 #ifndef PRODUCT
1471   _jmp_ring_index = 0;
1472   for (int ji = 0; ji < jump_ring_buffer_size; ji++) {
1473     record_jump(NULL, NULL, NULL, 0);
1474   }
1475 #endif // PRODUCT
1476 










1477   // Setup safepoint state info for this thread
1478   ThreadSafepointState::create(this);
1479 
1480   debug_only(_java_call_counter = 0);
1481 
1482   // JVMTI PopFrame support
1483   _popframe_condition = popframe_inactive;
1484   _popframe_preserved_args = NULL;
1485   _popframe_preserved_args_size = 0;
1486   _frames_to_pop_failed_realloc = 0;
1487 
1488   pd_initialize();
1489 }
1490 
1491 #if INCLUDE_ALL_GCS
1492 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1493 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1494 #endif // INCLUDE_ALL_GCS
1495 
1496 JavaThread::JavaThread(bool is_attaching_via_jni) :


1602     old_array->set_unroll_block(NULL);
1603     delete old_info;
1604     delete old_array;
1605   }
1606 
1607   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1608   if (deferred != NULL) {
1609     // This can only happen if thread is destroyed before deoptimization occurs.
1610     assert(deferred->length() != 0, "empty array!");
1611     do {
1612       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1613       deferred->remove_at(0);
1614       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1615       delete dlv;
1616     } while (deferred->length() != 0);
1617     delete deferred;
1618   }
1619 
1620   // All Java related clean up happens in exit
1621   ThreadSafepointState::destroy(this);

1622   if (_thread_stat != NULL) delete _thread_stat;
1623 
1624 #if INCLUDE_JVMCI
1625   if (JVMCICounterSize > 0) {
1626     if (jvmci_counters_include(this)) {
1627       for (int i = 0; i < JVMCICounterSize; i++) {
1628         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1629       }
1630     }
1631     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1632   }
1633 #endif // INCLUDE_JVMCI
1634 }
1635 
1636 
1637 // The first routine called by a new Java thread
1638 void JavaThread::run() {
1639   // initialize thread-local alloc buffer related fields
1640   this->initialize_tlab();
1641 


1716   // Clear the native thread instance - this makes isAlive return false and allows the join()
1717   // to complete once we've done the notify_all below
1718   java_lang_Thread::set_thread(threadObj(), NULL);
1719   lock.notify_all(thread);
1720   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1721   thread->clear_pending_exception();
1722 }
1723 
1724 
1725 // For any new cleanup additions, please check to see if they need to be applied to
1726 // cleanup_failed_attach_current_thread as well.
1727 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1728   assert(this == JavaThread::current(), "thread consistency check");
1729 
1730   HandleMark hm(this);
1731   Handle uncaught_exception(this, this->pending_exception());
1732   this->clear_pending_exception();
1733   Handle threadObj(this, this->threadObj());
1734   assert(threadObj.not_null(), "Java thread object should be created");
1735 







1736   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1737   {
1738     EXCEPTION_MARK;
1739 
1740     CLEAR_PENDING_EXCEPTION;
1741   }
1742   if (!destroy_vm) {
1743     if (uncaught_exception.not_null()) {
1744       EXCEPTION_MARK;
1745       // Call method Thread.dispatchUncaughtException().
1746       Klass* thread_klass = SystemDictionary::Thread_klass();
1747       JavaValue result(T_VOID);
1748       JavaCalls::call_virtual(&result,
1749                               threadObj, thread_klass,
1750                               vmSymbols::dispatchUncaughtException_name(),
1751                               vmSymbols::throwable_void_signature(),
1752                               uncaught_exception,
1753                               THREAD);
1754       if (HAS_PENDING_EXCEPTION) {
1755         ResourceMark rm(this);


1917   SATBMarkQueue& satb_queue = satb_mark_queue();
1918   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1919   // The SATB queue should have been constructed with its active
1920   // field set to false.
1921   assert(!satb_queue.is_active(), "SATB queue should not be active");
1922   assert(satb_queue.is_empty(), "SATB queue should be empty");
1923   // If we are creating the thread during a marking cycle, we should
1924   // set the active field of the SATB queue to true.
1925   if (satb_queue_set.is_active()) {
1926     satb_queue.set_active(true);
1927   }
1928 
1929   DirtyCardQueue& dirty_queue = dirty_card_queue();
1930   // The dirty card queue should have been constructed with its
1931   // active field set to true.
1932   assert(dirty_queue.is_active(), "dirty card queue should be active");
1933 }
1934 #endif // INCLUDE_ALL_GCS
1935 
1936 void JavaThread::cleanup_failed_attach_current_thread() {






1937   if (active_handles() != NULL) {
1938     JNIHandleBlock* block = active_handles();
1939     set_active_handles(NULL);
1940     JNIHandleBlock::release_block(block);
1941   }
1942 
1943   if (free_handle_block() != NULL) {
1944     JNIHandleBlock* block = free_handle_block();
1945     set_free_handle_block(NULL);
1946     JNIHandleBlock::release_block(block);
1947   }
1948 
1949   // These have to be removed while this is still a valid thread.
1950   remove_stack_guard_pages();
1951 
1952   if (UseTLAB) {
1953     tlab().make_parsable(true);  // retire TLAB, if any
1954   }
1955 
1956 #if INCLUDE_ALL_GCS


2700     Thread* thread = Thread::current();
2701     if (thread->is_Named_thread()) {
2702       _cur_thr = (NamedThread *)thread;
2703       _cur_thr->set_processed_thread(jthr);
2704     } else {
2705       _cur_thr = NULL;
2706     }
2707   }
2708 
2709   ~RememberProcessedThread() {
2710     if (_cur_thr) {
2711       _cur_thr->set_processed_thread(NULL);
2712     }
2713   }
2714 };
2715 
2716 void JavaThread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
2717   // Verify that the deferred card marks have been flushed.
2718   assert(deferred_card_mark().is_empty(), "Should be empty during GC");
2719 



2720   // Traverse the GCHandles
2721   Thread::oops_do(f, cf);
2722 
2723   JVMCI_ONLY(f->do_oop((oop*)&_pending_failed_speculation);)
2724 
2725   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
2726          (has_last_Java_frame() && java_call_counter() > 0), "wrong java_sp info!");
2727 
2728   if (has_last_Java_frame()) {
2729     // Record JavaThread to GC thread
2730     RememberProcessedThread rpt(this);
2731 
2732     // Traverse the privileged stack
2733     if (_privileged_stack_top != NULL) {
2734       _privileged_stack_top->oops_do(f);
2735     }
2736 
2737     // traverse the registered growable array
2738     if (_array_for_gc != NULL) {
2739       for (int index = 0; index < _array_for_gc->length(); index++) {


3744   JvmtiExport::enter_live_phase();
3745 
3746   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3747   JvmtiExport::post_vm_initialized();
3748 
3749   if (TRACE_START() != JNI_OK) {
3750     vm_exit_during_initialization("Failed to start tracing backend.");
3751   }
3752 
3753 #if INCLUDE_MANAGEMENT
3754   Management::initialize(THREAD);
3755 
3756   if (HAS_PENDING_EXCEPTION) {
3757     // management agent fails to start possibly due to
3758     // configuration problem and is responsible for printing
3759     // stack trace if appropriate. Simply exit VM.
3760     vm_exit(1);
3761   }
3762 #endif // INCLUDE_MANAGEMENT
3763 

3764   if (MemProfiling)                   MemProfiler::engage();
3765   StatSampler::engage();
3766   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3767 
3768   BiasedLocking::init();
3769 
3770 #if INCLUDE_RTM_OPT
3771   RTMLockingCounters::init();
3772 #endif
3773 
3774   if (JDK_Version::current().post_vm_init_hook_enabled()) {
3775     call_postVMInitHook(THREAD);
3776     // The Java side of PostVMInitHook.run must deal with all
3777     // exceptions and provide means of diagnosis.
3778     if (HAS_PENDING_EXCEPTION) {
3779       CLEAR_PENDING_EXCEPTION;
3780     }
3781   }
3782 
3783   {


4033   }
4034   CLEAR_PENDING_EXCEPTION;
4035 }
4036 
4037 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4038 // the program falls off the end of main(). Another VM exit path is through
4039 // vm_exit() when the program calls System.exit() to return a value or when
4040 // there is a serious error in VM. The two shutdown paths are not exactly
4041 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4042 // and VM_Exit op at VM level.
4043 //
4044 // Shutdown sequence:
4045 //   + Shutdown native memory tracking if it is on
4046 //   + Wait until we are the last non-daemon thread to execute
4047 //     <-- every thing is still working at this moment -->
4048 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
4049 //        shutdown hooks, run finalizers if finalization-on-exit
4050 //   + Call before_exit(), prepare for VM exit
4051 //      > run VM level shutdown hooks (they are registered through JVM_OnExit(),
4052 //        currently the only user of this mechanism is File.deleteOnExit())
4053 //      > stop StatSampler, watcher thread, CMS threads,
4054 //        post thread end and vm death events to JVMTI,
4055 //        stop signal thread
4056 //   + Call JavaThread::exit(), it will:
4057 //      > release JNI handle blocks, remove stack guard pages
4058 //      > remove this thread from Threads list
4059 //     <-- no more Java code from this thread after this point -->
4060 //   + Stop VM thread, it will bring the remaining VM to a safepoint and stop
4061 //     the compiler threads at safepoint
4062 //     <-- do not use anything that could get blocked by Safepoint -->
4063 //   + Disable tracing at JNI/JVM barriers
4064 //   + Set _vm_exited flag for threads that are still running native code
4065 //   + Delete this thread
4066 //   + Call exit_globals()
4067 //      > deletes tty
4068 //      > deletes PerfMemory resources
4069 //   + Return to caller
4070 
4071 bool Threads::destroy_vm() {
4072   JavaThread* thread = JavaThread::current();
4073 
4074 #ifdef ASSERT
4075   _vm_complete = false;
4076 #endif
4077   // Wait until we are the last non-daemon thread to execute
4078   { MutexLocker nu(Threads_lock);
4079     while (Threads::number_of_non_daemon_threads() > 1)
4080       // This wait should make safepoint checks, wait without a timeout,
4081       // and wait as a suspend-equivalent condition.








4082       Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
4083                          Mutex::_as_suspend_equivalent_flag);
4084   }
4085 
4086   // Hang forever on exit if we are reporting an error.
4087   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
4088     os::infinite_sleep();
4089   }
4090   os::wait_for_keypress_at_exit();
4091 
4092   // run Java level shutdown hooks
4093   thread->invoke_shutdown_hooks();
4094 
4095   before_exit(thread);
4096 
4097   thread->exit(true);
4098 
4099   // Stop VM thread.
4100   {
4101     // 4945125 The vm thread comes to a safepoint during exit.


src/share/vm/runtime/thread.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File