< prev index next >

src/share/vm/runtime/thread.cpp

Print this page




  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"


 240   new HandleMark(this);
 241 
 242   // plain initialization
 243   debug_only(_owned_locks = NULL;)
 244   debug_only(_allow_allocation_count = 0;)
 245   NOT_PRODUCT(_allow_safepoint_count = 0;)
 246   NOT_PRODUCT(_skip_gcalot = false;)
 247   _jvmti_env_iteration_count = 0;
 248   set_allocated_bytes(0);
 249   _vm_operation_started_count = 0;
 250   _vm_operation_completed_count = 0;
 251   _current_pending_monitor = NULL;
 252   _current_pending_monitor_is_from_java = true;
 253   _current_waiting_monitor = NULL;
 254   _num_nested_signal = 0;
 255   omFreeList = NULL ;
 256   omFreeCount = 0 ;
 257   omFreeProvision = 32 ;
 258   omInUseList = NULL ;
 259   omInUseCount = 0 ;







 260 
 261 #ifdef ASSERT
 262   _visited_for_critical_count = false;
 263 #endif
 264 
 265   _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
 266   _suspend_flags = 0;
 267 
 268   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
 269   _hashStateX = os::random() ;
 270   _hashStateY = 842502087 ;
 271   _hashStateZ = 0x8767 ;    // (int)(3579807591LL & 0xffff) ;
 272   _hashStateW = 273326509 ;
 273 
 274   _OnTrap   = 0 ;
 275   _schedctl = NULL ;
 276   _Stalled  = 0 ;
 277   _TypeTag  = 0x2BAD ;
 278 
 279   // Many of the following fields are effectively final - immutable


 374   ParkEvent::Release (_MuxEvent)    ; _MuxEvent    = NULL ;
 375 
 376   delete handle_area();
 377   delete metadata_handles();
 378 
 379   // osthread() can be NULL, if creation of thread failed.
 380   if (osthread() != NULL) os::free_thread(osthread());
 381 
 382   delete _SR_lock;
 383 
 384   // clear thread local storage if the Thread is deleting itself
 385   if (this == Thread::current()) {
 386     ThreadLocalStorage::set_thread(NULL);
 387   } else {
 388     // In the case where we're not the current thread, invalidate all the
 389     // caches in case some code tries to get the current thread or the
 390     // thread that was destroyed, and gets stale information.
 391     ThreadLocalStorage::invalidate_all();
 392   }
 393   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)


 394 }
 395 
 396 // NOTE: dummy function for assertion purpose.
 397 void Thread::run() {
 398   ShouldNotReachHere();
 399 }
 400 
 401 #ifdef ASSERT
 402 // Private method to check for dangling thread pointer
 403 void check_for_dangling_thread_pointer(Thread *thread) {
 404  assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
 405          "possibility of dangling Thread pointer");
 406 }
 407 #endif
 408 
 409 
 410 #ifndef PRODUCT
 411 // Tracing method for basic thread operations
 412 void Thread::trace(const char* msg, const Thread* const thread) {
 413   if (!TraceThreadEvents) return;


 787   _jmp_ring[index]._line = line;
 788 }
 789 #endif /* PRODUCT */
 790 
 791 // Called by flat profiler
 792 // Callers have already called wait_for_ext_suspend_completion
 793 // The assertion for that is currently too complex to put here:
 794 bool JavaThread::profile_last_Java_frame(frame* _fr) {
 795   bool gotframe = false;
 796   // self suspension saves needed state.
 797   if (has_last_Java_frame() && _anchor.walkable()) {
 798      *_fr = pd_last_frame();
 799      gotframe = true;
 800   }
 801   return gotframe;
 802 }
 803 
 804 void Thread::interrupt(Thread* thread) {
 805   trace("interrupt", thread);
 806   debug_only(check_for_dangling_thread_pointer(thread);)



 807   os::interrupt(thread);
 808 }
 809 
 810 bool Thread::is_interrupted(Thread* thread, bool clear_interrupted) {
 811   trace("is_interrupted", thread);
 812   debug_only(check_for_dangling_thread_pointer(thread);)
 813   // Note:  If clear_interrupted==false, this simply fetches and
 814   // returns the value of the field osthread()->interrupted().
 815   return os::is_interrupted(thread, clear_interrupted);
 816 }
 817 
 818 
 819 // GC Support
 820 bool Thread::claim_oops_do_par_case(int strong_roots_parity) {
 821   jint thread_parity = _oops_do_parity;
 822   if (thread_parity != strong_roots_parity) {
 823     jint res = Atomic::cmpxchg(strong_roots_parity, &_oops_do_parity, thread_parity);
 824     if (res == thread_parity) {
 825       return true;
 826     } else {


1641   // used to test validitity of stack trace backs
1642   this->record_base_of_stack_pointer();
1643 
1644   // Record real stack base and size.
1645   this->record_stack_base_and_size();
1646 
1647   // Initialize thread local storage; set before calling MutexLocker
1648   this->initialize_thread_local_storage();
1649 
1650   this->create_stack_guard_pages();
1651 
1652   this->cache_global_variables();
1653 
1654   // Thread is now sufficient initialized to be handled by the safepoint code as being
1655   // in the VM. Change thread state from _thread_new to _thread_in_vm
1656   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1657 
1658   assert(JavaThread::current() == this, "sanity check");
1659   assert(!Thread::current()->owns_locks(), "sanity check");
1660 




1661   DTRACE_THREAD_PROBE(start, this);
1662 
1663   // This operation might block. We call that after all safepoint checks for a new thread has
1664   // been completed.
1665   this->set_active_handles(JNIHandleBlock::allocate_block());
1666 
1667   if (JvmtiExport::should_post_thread_life()) {
1668     JvmtiExport::post_thread_start(this);
1669   }
1670 
1671   EventThreadStart event;
1672   if (event.should_commit()) {
1673      event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1674      event.commit();
1675   }
1676 
1677   // We call another function to do the rest so we are sure that the stack addresses used
1678   // from there will be lower than the stack base just computed
1679   thread_main_inner();
1680 


1684 
1685 void JavaThread::thread_main_inner() {
1686   assert(JavaThread::current() == this, "sanity check");
1687   assert(this->threadObj() != NULL, "just checking");
1688 
1689   // Execute thread entry point unless this thread has a pending exception
1690   // or has been stopped before starting.
1691   // Note: Due to JVM_StopThread we can have pending exceptions already!
1692   if (!this->has_pending_exception() &&
1693       !java_lang_Thread::is_stillborn(this->threadObj())) {
1694     {
1695       ResourceMark rm(this);
1696       this->set_native_thread_name(this->get_thread_name());
1697     }
1698     HandleMark hm(this);
1699     this->entry_point()(this, this);
1700   }
1701 
1702   DTRACE_THREAD_PROBE(stop, this);
1703 




1704   this->exit(false);
1705   delete this;
1706 }
1707 
1708 
1709 static void ensure_join(JavaThread* thread) {
1710   // We do not need to grap the Threads_lock, since we are operating on ourself.
1711   Handle threadObj(thread, thread->threadObj());
1712   assert(threadObj.not_null(), "java thread object must exist");
1713   ObjectLocker lock(threadObj, thread);
1714   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1715   thread->clear_pending_exception();
1716   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1717   java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1718   // Clear the native thread instance - this makes isAlive return false and allows the join()
1719   // to complete once we've done the notify_all below
1720   java_lang_Thread::set_thread(threadObj(), NULL);
1721   lock.notify_all(thread);
1722   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1723   thread->clear_pending_exception();


1908 
1909   if (JvmtiEnv::environments_might_exist()) {
1910     JvmtiExport::cleanup_thread(this);
1911   }
1912 
1913   // We must flush any deferred card marks before removing a thread from
1914   // the list of active threads.
1915   Universe::heap()->flush_deferred_store_barrier(this);
1916   assert(deferred_card_mark().is_empty(), "Should have been flushed");
1917 
1918 #if INCLUDE_ALL_GCS
1919   // We must flush the G1-related buffers before removing a thread
1920   // from the list of active threads. We must do this after any deferred
1921   // card marks have been flushed (above) so that any entries that are
1922   // added to the thread's dirty card queue as a result are not lost.
1923   if (UseG1GC) {
1924     flush_barrier_queues();
1925   }
1926 #endif // INCLUDE_ALL_GCS
1927 








1928   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1929   Threads::remove(this);
1930 }
1931 
1932 #if INCLUDE_ALL_GCS
1933 // Flush G1-related queues.
1934 void JavaThread::flush_barrier_queues() {
1935   satb_mark_queue().flush();
1936   dirty_card_queue().flush();
1937 }
1938 
1939 void JavaThread::initialize_queues() {
1940   assert(!SafepointSynchronize::is_at_safepoint(),
1941          "we should not be at a safepoint");
1942 
1943   ObjPtrQueue& satb_queue = satb_mark_queue();
1944   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1945   // The SATB queue should have been constructed with its active
1946   // field set to false.
1947   assert(!satb_queue.is_active(), "SATB queue should not be active");


3408     delete main_thread;
3409     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3410     return JNI_ENOMEM;
3411   }
3412 
3413   // Enable guard page *after* os::create_main_thread(), otherwise it would
3414   // crash Linux VM, see notes in os_linux.cpp.
3415   main_thread->create_stack_guard_pages();
3416 
3417   // Initialize Java-Level synchronization subsystem
3418   ObjectMonitor::Initialize() ;
3419 
3420   // Initialize global modules
3421   jint status = init_globals();
3422   if (status != JNI_OK) {
3423     delete main_thread;
3424     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3425     return status;
3426   }
3427 





3428   // Should be done after the heap is fully created
3429   main_thread->cache_global_variables();
3430 
3431   HandleMark hm;
3432 
3433   { MutexLocker mu(Threads_lock);
3434     Threads::add(main_thread);
3435   }
3436 
3437   // Any JVMTI raw monitors entered in onload will transition into
3438   // real raw monitor. VM is setup enough here for raw monitor enter.
3439   JvmtiExport::transition_pending_onload_raw_monitors();
3440 
3441   // Create the VMThread
3442   { TraceTime timer("Start VMThread", TraceStartupTime);
3443     VMThread::create();
3444     Thread* vmthread = VMThread::vm_thread();
3445 
3446     if (!os::create_thread(vmthread, os::vm_thread))
3447       vm_exit_during_initialization("Cannot create VM thread. Out of system resources.");


3648     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3649   }
3650 
3651 #if INCLUDE_MANAGEMENT
3652   Management::initialize(THREAD);
3653 #endif // INCLUDE_MANAGEMENT
3654 
3655   if (HAS_PENDING_EXCEPTION) {
3656     // management agent fails to start possibly due to
3657     // configuration problem and is responsible for printing
3658     // stack trace if appropriate. Simply exit VM.
3659     vm_exit(1);
3660   }
3661 
3662   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
3663   if (MemProfiling)                   MemProfiler::engage();
3664   StatSampler::engage();
3665   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3666 
3667   BiasedLocking::init();




3668 
3669 #if INCLUDE_RTM_OPT
3670   RTMLockingCounters::init();
3671 #endif
3672 
3673   if (JDK_Version::current().post_vm_init_hook_enabled()) {
3674     call_postVMInitHook(THREAD);
3675     // The Java side of PostVMInitHook.run must deal with all
3676     // exceptions and provide means of diagnosis.
3677     if (HAS_PENDING_EXCEPTION) {
3678       CLEAR_PENDING_EXCEPTION;
3679     }
3680   }
3681 
3682   {
3683       MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
3684       // Make sure the watcher thread can be started by WatcherThread::start()
3685       // or by dynamic enrollment.
3686       WatcherThread::make_startable();
3687       // Start up the WatcherThread if there are any periodic tasks




  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 "evtrace/traceEvents.hpp"
  83 #include "utilities/defaultStream.hpp"
  84 #include "utilities/dtrace.hpp"
  85 #include "utilities/events.hpp"
  86 #include "utilities/preserveException.hpp"
  87 #include "utilities/macros.hpp"
  88 #ifdef TARGET_OS_FAMILY_linux
  89 # include "os_linux.inline.hpp"
  90 #endif
  91 #ifdef TARGET_OS_FAMILY_solaris
  92 # include "os_solaris.inline.hpp"
  93 #endif
  94 #ifdef TARGET_OS_FAMILY_windows
  95 # include "os_windows.inline.hpp"
  96 #endif
  97 #ifdef TARGET_OS_FAMILY_bsd
  98 # include "os_bsd.inline.hpp"
  99 #endif
 100 #if INCLUDE_ALL_GCS
 101 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
 102 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"


 241   new HandleMark(this);
 242 
 243   // plain initialization
 244   debug_only(_owned_locks = NULL;)
 245   debug_only(_allow_allocation_count = 0;)
 246   NOT_PRODUCT(_allow_safepoint_count = 0;)
 247   NOT_PRODUCT(_skip_gcalot = false;)
 248   _jvmti_env_iteration_count = 0;
 249   set_allocated_bytes(0);
 250   _vm_operation_started_count = 0;
 251   _vm_operation_completed_count = 0;
 252   _current_pending_monitor = NULL;
 253   _current_pending_monitor_is_from_java = true;
 254   _current_waiting_monitor = NULL;
 255   _num_nested_signal = 0;
 256   omFreeList = NULL ;
 257   omFreeCount = 0 ;
 258   omFreeProvision = 32 ;
 259   omInUseList = NULL ;
 260   omInUseCount = 0 ;
 261   _trace_buffer = NULL;
 262   debug_only(_trace_active = false;)
 263   _park_last_global_seq = 0;
 264   _park_priority = 0;
 265   _nesting_level = 0;
 266   _memento_original_return_address = NULL;
 267   _memento_stack_trace = NULL;
 268 
 269 #ifdef ASSERT
 270   _visited_for_critical_count = false;
 271 #endif
 272 
 273   _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
 274   _suspend_flags = 0;
 275 
 276   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
 277   _hashStateX = os::random() ;
 278   _hashStateY = 842502087 ;
 279   _hashStateZ = 0x8767 ;    // (int)(3579807591LL & 0xffff) ;
 280   _hashStateW = 273326509 ;
 281 
 282   _OnTrap   = 0 ;
 283   _schedctl = NULL ;
 284   _Stalled  = 0 ;
 285   _TypeTag  = 0x2BAD ;
 286 
 287   // Many of the following fields are effectively final - immutable


 382   ParkEvent::Release (_MuxEvent)    ; _MuxEvent    = NULL ;
 383 
 384   delete handle_area();
 385   delete metadata_handles();
 386 
 387   // osthread() can be NULL, if creation of thread failed.
 388   if (osthread() != NULL) os::free_thread(osthread());
 389 
 390   delete _SR_lock;
 391 
 392   // clear thread local storage if the Thread is deleting itself
 393   if (this == Thread::current()) {
 394     ThreadLocalStorage::set_thread(NULL);
 395   } else {
 396     // In the case where we're not the current thread, invalidate all the
 397     // caches in case some code tries to get the current thread or the
 398     // thread that was destroyed, and gets stale information.
 399     ThreadLocalStorage::invalidate_all();
 400   }
 401   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 402 
 403   assert (_trace_buffer == NULL, "leaking unsubmitted trace buffer");
 404 }
 405 
 406 // NOTE: dummy function for assertion purpose.
 407 void Thread::run() {
 408   ShouldNotReachHere();
 409 }
 410 
 411 #ifdef ASSERT
 412 // Private method to check for dangling thread pointer
 413 void check_for_dangling_thread_pointer(Thread *thread) {
 414  assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
 415          "possibility of dangling Thread pointer");
 416 }
 417 #endif
 418 
 419 
 420 #ifndef PRODUCT
 421 // Tracing method for basic thread operations
 422 void Thread::trace(const char* msg, const Thread* const thread) {
 423   if (!TraceThreadEvents) return;


 797   _jmp_ring[index]._line = line;
 798 }
 799 #endif /* PRODUCT */
 800 
 801 // Called by flat profiler
 802 // Callers have already called wait_for_ext_suspend_completion
 803 // The assertion for that is currently too complex to put here:
 804 bool JavaThread::profile_last_Java_frame(frame* _fr) {
 805   bool gotframe = false;
 806   // self suspension saves needed state.
 807   if (has_last_Java_frame() && _anchor.walkable()) {
 808      *_fr = pd_last_frame();
 809      gotframe = true;
 810   }
 811   return gotframe;
 812 }
 813 
 814 void Thread::interrupt(Thread* thread) {
 815   trace("interrupt", thread);
 816   debug_only(check_for_dangling_thread_pointer(thread);)
 817   if (EnableEventTracing) {
 818     TraceEvents::write_thread_interrupt(thread);
 819   }
 820   os::interrupt(thread);
 821 }
 822 
 823 bool Thread::is_interrupted(Thread* thread, bool clear_interrupted) {
 824   trace("is_interrupted", thread);
 825   debug_only(check_for_dangling_thread_pointer(thread);)
 826   // Note:  If clear_interrupted==false, this simply fetches and
 827   // returns the value of the field osthread()->interrupted().
 828   return os::is_interrupted(thread, clear_interrupted);
 829 }
 830 
 831 
 832 // GC Support
 833 bool Thread::claim_oops_do_par_case(int strong_roots_parity) {
 834   jint thread_parity = _oops_do_parity;
 835   if (thread_parity != strong_roots_parity) {
 836     jint res = Atomic::cmpxchg(strong_roots_parity, &_oops_do_parity, thread_parity);
 837     if (res == thread_parity) {
 838       return true;
 839     } else {


1654   // used to test validitity of stack trace backs
1655   this->record_base_of_stack_pointer();
1656 
1657   // Record real stack base and size.
1658   this->record_stack_base_and_size();
1659 
1660   // Initialize thread local storage; set before calling MutexLocker
1661   this->initialize_thread_local_storage();
1662 
1663   this->create_stack_guard_pages();
1664 
1665   this->cache_global_variables();
1666 
1667   // Thread is now sufficient initialized to be handled by the safepoint code as being
1668   // in the VM. Change thread state from _thread_new to _thread_in_vm
1669   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1670 
1671   assert(JavaThread::current() == this, "sanity check");
1672   assert(!Thread::current()->owns_locks(), "sanity check");
1673 
1674   if (EnableEventTracing) {
1675     TraceEvents::write_thread_start();
1676   }
1677 
1678   DTRACE_THREAD_PROBE(start, this);
1679 
1680   // This operation might block. We call that after all safepoint checks for a new thread has
1681   // been completed.
1682   this->set_active_handles(JNIHandleBlock::allocate_block());
1683 
1684   if (JvmtiExport::should_post_thread_life()) {
1685     JvmtiExport::post_thread_start(this);
1686   }
1687 
1688   EventThreadStart event;
1689   if (event.should_commit()) {
1690      event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1691      event.commit();
1692   }
1693 
1694   // We call another function to do the rest so we are sure that the stack addresses used
1695   // from there will be lower than the stack base just computed
1696   thread_main_inner();
1697 


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


1929 
1930   if (JvmtiEnv::environments_might_exist()) {
1931     JvmtiExport::cleanup_thread(this);
1932   }
1933 
1934   // We must flush any deferred card marks before removing a thread from
1935   // the list of active threads.
1936   Universe::heap()->flush_deferred_store_barrier(this);
1937   assert(deferred_card_mark().is_empty(), "Should have been flushed");
1938 
1939 #if INCLUDE_ALL_GCS
1940   // We must flush the G1-related buffers before removing a thread
1941   // from the list of active threads. We must do this after any deferred
1942   // card marks have been flushed (above) so that any entries that are
1943   // added to the thread's dirty card queue as a result are not lost.
1944   if (UseG1GC) {
1945     flush_barrier_queues();
1946   }
1947 #endif // INCLUDE_ALL_GCS
1948 
1949   { // Submit trace buffer if we still hold one
1950     No_Safepoint_Verifier nsv;
1951     if (trace_buffer() != NULL) {
1952       TraceManager::submit_buffer(trace_buffer());
1953       set_trace_buffer(NULL);
1954     }
1955   }
1956 
1957   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1958   Threads::remove(this);
1959 }
1960 
1961 #if INCLUDE_ALL_GCS
1962 // Flush G1-related queues.
1963 void JavaThread::flush_barrier_queues() {
1964   satb_mark_queue().flush();
1965   dirty_card_queue().flush();
1966 }
1967 
1968 void JavaThread::initialize_queues() {
1969   assert(!SafepointSynchronize::is_at_safepoint(),
1970          "we should not be at a safepoint");
1971 
1972   ObjPtrQueue& satb_queue = satb_mark_queue();
1973   SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1974   // The SATB queue should have been constructed with its active
1975   // field set to false.
1976   assert(!satb_queue.is_active(), "SATB queue should not be active");


3437     delete main_thread;
3438     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3439     return JNI_ENOMEM;
3440   }
3441 
3442   // Enable guard page *after* os::create_main_thread(), otherwise it would
3443   // crash Linux VM, see notes in os_linux.cpp.
3444   main_thread->create_stack_guard_pages();
3445 
3446   // Initialize Java-Level synchronization subsystem
3447   ObjectMonitor::Initialize() ;
3448 
3449   // Initialize global modules
3450   jint status = init_globals();
3451   if (status != JNI_OK) {
3452     delete main_thread;
3453     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3454     return status;
3455   }
3456 
3457   // Initialize event tracing
3458   if (EnableEventTracing) {
3459     TraceManager::initialize();
3460   }
3461 
3462   // Should be done after the heap is fully created
3463   main_thread->cache_global_variables();
3464 
3465   HandleMark hm;
3466 
3467   { MutexLocker mu(Threads_lock);
3468     Threads::add(main_thread);
3469   }
3470 
3471   // Any JVMTI raw monitors entered in onload will transition into
3472   // real raw monitor. VM is setup enough here for raw monitor enter.
3473   JvmtiExport::transition_pending_onload_raw_monitors();
3474 
3475   // Create the VMThread
3476   { TraceTime timer("Start VMThread", TraceStartupTime);
3477     VMThread::create();
3478     Thread* vmthread = VMThread::vm_thread();
3479 
3480     if (!os::create_thread(vmthread, os::vm_thread))
3481       vm_exit_during_initialization("Cannot create VM thread. Out of system resources.");


3682     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3683   }
3684 
3685 #if INCLUDE_MANAGEMENT
3686   Management::initialize(THREAD);
3687 #endif // INCLUDE_MANAGEMENT
3688 
3689   if (HAS_PENDING_EXCEPTION) {
3690     // management agent fails to start possibly due to
3691     // configuration problem and is responsible for printing
3692     // stack trace if appropriate. Simply exit VM.
3693     vm_exit(1);
3694   }
3695 
3696   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
3697   if (MemProfiling)                   MemProfiler::engage();
3698   StatSampler::engage();
3699   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3700 
3701   BiasedLocking::init();
3702 
3703   if (EnableEventTracing) {
3704     TraceManager::start_threads(CHECK_0); // needs a fully functional Java environment
3705   }
3706 
3707 #if INCLUDE_RTM_OPT
3708   RTMLockingCounters::init();
3709 #endif
3710 
3711   if (JDK_Version::current().post_vm_init_hook_enabled()) {
3712     call_postVMInitHook(THREAD);
3713     // The Java side of PostVMInitHook.run must deal with all
3714     // exceptions and provide means of diagnosis.
3715     if (HAS_PENDING_EXCEPTION) {
3716       CLEAR_PENDING_EXCEPTION;
3717     }
3718   }
3719 
3720   {
3721       MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
3722       // Make sure the watcher thread can be started by WatcherThread::start()
3723       // or by dynamic enrollment.
3724       WatcherThread::make_startable();
3725       // Start up the WatcherThread if there are any periodic tasks


< prev index next >