< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




  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 "jvm.h"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/compileTask.hpp"

  36 #include "gc/shared/gcId.hpp"
  37 #include "gc/shared/gcLocker.inline.hpp"
  38 #include "gc/shared/workgroup.hpp"
  39 #include "interpreter/interpreter.hpp"
  40 #include "interpreter/linkResolver.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "jvmtifiles/jvmtiEnv.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logConfiguration.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/metaspaceShared.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/access.inline.hpp"
  52 #include "oops/instanceKlass.hpp"
  53 #include "oops/objArrayOop.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "oops/symbol.hpp"


  97 #include "runtime/vframeArray.hpp"
  98 #include "runtime/vframe_hp.hpp"
  99 #include "runtime/vmThread.hpp"
 100 #include "runtime/vm_operations.hpp"
 101 #include "runtime/vm_version.hpp"
 102 #include "services/attachListener.hpp"
 103 #include "services/management.hpp"
 104 #include "services/memTracker.hpp"
 105 #include "services/threadService.hpp"
 106 #include "trace/traceMacros.hpp"
 107 #include "trace/tracing.hpp"
 108 #include "utilities/align.hpp"
 109 #include "utilities/defaultStream.hpp"
 110 #include "utilities/dtrace.hpp"
 111 #include "utilities/events.hpp"
 112 #include "utilities/macros.hpp"
 113 #include "utilities/preserveException.hpp"
 114 #include "utilities/vmError.hpp"
 115 #if INCLUDE_ALL_GCS
 116 #include "gc/cms/concurrentMarkSweepThread.hpp"
 117 #include "gc/g1/g1BarrierSet.hpp"
 118 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
 119 #include "gc/parallel/pcTasks.hpp"
 120 #endif // INCLUDE_ALL_GCS
 121 #if INCLUDE_JVMCI
 122 #include "jvmci/jvmciCompiler.hpp"
 123 #include "jvmci/jvmciRuntime.hpp"
 124 #include "logging/logHandle.hpp"
 125 #endif
 126 #ifdef COMPILER1
 127 #include "c1/c1_Compiler.hpp"
 128 #endif
 129 #ifdef COMPILER2
 130 #include "opto/c2compiler.hpp"
 131 #include "opto/idealGraphPrinter.hpp"
 132 #endif
 133 #if INCLUDE_RTM_OPT
 134 #include "runtime/rtmLocking.hpp"
 135 #endif
 136 
 137 // Initialization after module runtime initialization


 294   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
 295   // and ::Release()
 296   _ParkEvent   = ParkEvent::Allocate(this);
 297   _SleepEvent  = ParkEvent::Allocate(this);
 298   _MutexEvent  = ParkEvent::Allocate(this);
 299   _MuxEvent    = ParkEvent::Allocate(this);
 300 
 301 #ifdef CHECK_UNHANDLED_OOPS
 302   if (CheckUnhandledOops) {
 303     _unhandled_oops = new UnhandledOops(this);
 304   }
 305 #endif // CHECK_UNHANDLED_OOPS
 306 #ifdef ASSERT
 307   if (UseBiasedLocking) {
 308     assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
 309     assert(this == _real_malloc_address ||
 310            this == align_up(_real_malloc_address, (int)markOopDesc::biased_lock_alignment),
 311            "bug in forced alignment of thread objects");
 312   }
 313 #endif // ASSERT









 314 }
 315 
 316 void Thread::initialize_thread_current() {
 317 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 318   assert(_thr_current == NULL, "Thread::current already initialized");
 319   _thr_current = this;
 320 #endif
 321   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 322   ThreadLocalStorage::set_thread(this);
 323   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 324 }
 325 
 326 void Thread::clear_thread_current() {
 327   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 328 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 329   _thr_current = NULL;
 330 #endif
 331   ThreadLocalStorage::set_thread(NULL);
 332 }
 333 


 344 
 345   // Set stack limits after thread is initialized.
 346   if (is_Java_thread()) {
 347     ((JavaThread*) this)->set_stack_overflow_limit();
 348     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
 349   }
 350 #if INCLUDE_NMT
 351   // record thread's native stack, stack grows downward
 352   MemTracker::record_thread_stack(stack_end(), stack_size());
 353 #endif // INCLUDE_NMT
 354   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
 355     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
 356     os::current_thread_id(), p2i(stack_base() - stack_size()),
 357     p2i(stack_base()), stack_size()/1024);
 358 }
 359 
 360 
 361 Thread::~Thread() {
 362   EVENT_THREAD_DESTRUCT(this);
 363 







 364   // stack_base can be NULL if the thread is never started or exited before
 365   // record_stack_base_and_size called. Although, we would like to ensure
 366   // that all started threads do call record_stack_base_and_size(), there is
 367   // not proper way to enforce that.
 368 #if INCLUDE_NMT
 369   if (_stack_base != NULL) {
 370     MemTracker::release_thread_stack(stack_end(), stack_size());
 371 #ifdef ASSERT
 372     set_stack_base(NULL);
 373 #endif
 374   }
 375 #endif // INCLUDE_NMT
 376 
 377   // deallocate data structures
 378   delete resource_area();
 379   // since the handle marks are using the handle area, we have to deallocated the root
 380   // handle mark before deallocating the thread's handle area,
 381   assert(last_handle_mark() != NULL, "check we have an element");
 382   delete last_handle_mark();
 383   assert(last_handle_mark() == NULL, "check we have reached the end");


1573 
1574   // Setup safepoint state info for this thread
1575   ThreadSafepointState::create(this);
1576 
1577   debug_only(_java_call_counter = 0);
1578 
1579   // JVMTI PopFrame support
1580   _popframe_condition = popframe_inactive;
1581   _popframe_preserved_args = NULL;
1582   _popframe_preserved_args_size = 0;
1583   _frames_to_pop_failed_realloc = 0;
1584 
1585   if (SafepointMechanism::uses_thread_local_poll()) {
1586     SafepointMechanism::initialize_header(this);
1587   }
1588 
1589   pd_initialize();
1590 }
1591 
1592 JavaThread::JavaThread(bool is_attaching_via_jni) :
1593                        Thread()
1594 #if INCLUDE_ALL_GCS
1595                        , _satb_mark_queue(&G1BarrierSet::satb_mark_queue_set()),
1596                        _dirty_card_queue(&G1BarrierSet::dirty_card_queue_set())
1597 #endif // INCLUDE_ALL_GCS
1598 {
1599   initialize();
1600   if (is_attaching_via_jni) {
1601     _jni_attach_state = _attaching_via_jni;
1602   } else {
1603     _jni_attach_state = _not_attaching_via_jni;
1604   }
1605   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1606 }
1607 
1608 bool JavaThread::reguard_stack(address cur_sp) {
1609   if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1610       && _stack_guard_state != stack_guard_reserved_disabled) {
1611     return true; // Stack already guarded or guard pages not needed.
1612   }
1613 
1614   if (register_stack_overflow()) {
1615     // For those architectures which have separate register and
1616     // memory stacks, we must check the register stack to see if
1617     // it has overflowed.
1618     return false;


1640 bool JavaThread::reguard_stack(void) {
1641   return reguard_stack(os::current_stack_pointer());
1642 }
1643 
1644 
1645 void JavaThread::block_if_vm_exited() {
1646   if (_terminated == _vm_exited) {
1647     // _vm_exited is set at safepoint, and Threads_lock is never released
1648     // we will block here forever
1649     Threads_lock->lock_without_safepoint_check();
1650     ShouldNotReachHere();
1651   }
1652 }
1653 
1654 
1655 // Remove this ifdef when C1 is ported to the compiler interface.
1656 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1657 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1658 
1659 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1660                        Thread()
1661 #if INCLUDE_ALL_GCS
1662                        , _satb_mark_queue(&G1BarrierSet::satb_mark_queue_set()),
1663                        _dirty_card_queue(&G1BarrierSet::dirty_card_queue_set())
1664 #endif // INCLUDE_ALL_GCS
1665 {
1666   initialize();
1667   _jni_attach_state = _not_attaching_via_jni;
1668   set_entry_point(entry_point);
1669   // Create the native thread itself.
1670   // %note runtime_23
1671   os::ThreadType thr_type = os::java_thread;
1672   thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1673                                                      os::java_thread;
1674   os::create_thread(this, thr_type, stack_sz);
1675   // The _osthread may be NULL here because we ran out of memory (too many threads active).
1676   // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1677   // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1678   // the exception consists of creating the exception object & initializing it, initialization
1679   // will leave the VM via a JavaCall and then all locks must be unlocked).
1680   //
1681   // The thread is still suspended when we reach here. Thread must be explicit started
1682   // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1683   // by calling Threads:add. The reason why this is not done here, is because the thread
1684   // object must be fully initialized (take a look at JVM_Start)
1685 }




  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 "jvm.h"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/compileTask.hpp"
  36 #include "gc/shared/barrierSet.hpp"
  37 #include "gc/shared/gcId.hpp"
  38 #include "gc/shared/gcLocker.inline.hpp"
  39 #include "gc/shared/workgroup.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/linkResolver.hpp"
  42 #include "interpreter/oopMapCache.hpp"
  43 #include "jvmtifiles/jvmtiEnv.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logConfiguration.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/metaspaceShared.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "oops/access.inline.hpp"
  53 #include "oops/instanceKlass.hpp"
  54 #include "oops/objArrayOop.hpp"
  55 #include "oops/oop.inline.hpp"
  56 #include "oops/symbol.hpp"


  98 #include "runtime/vframeArray.hpp"
  99 #include "runtime/vframe_hp.hpp"
 100 #include "runtime/vmThread.hpp"
 101 #include "runtime/vm_operations.hpp"
 102 #include "runtime/vm_version.hpp"
 103 #include "services/attachListener.hpp"
 104 #include "services/management.hpp"
 105 #include "services/memTracker.hpp"
 106 #include "services/threadService.hpp"
 107 #include "trace/traceMacros.hpp"
 108 #include "trace/tracing.hpp"
 109 #include "utilities/align.hpp"
 110 #include "utilities/defaultStream.hpp"
 111 #include "utilities/dtrace.hpp"
 112 #include "utilities/events.hpp"
 113 #include "utilities/macros.hpp"
 114 #include "utilities/preserveException.hpp"
 115 #include "utilities/vmError.hpp"
 116 #if INCLUDE_ALL_GCS
 117 #include "gc/cms/concurrentMarkSweepThread.hpp"

 118 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
 119 #include "gc/parallel/pcTasks.hpp"
 120 #endif // INCLUDE_ALL_GCS
 121 #if INCLUDE_JVMCI
 122 #include "jvmci/jvmciCompiler.hpp"
 123 #include "jvmci/jvmciRuntime.hpp"
 124 #include "logging/logHandle.hpp"
 125 #endif
 126 #ifdef COMPILER1
 127 #include "c1/c1_Compiler.hpp"
 128 #endif
 129 #ifdef COMPILER2
 130 #include "opto/c2compiler.hpp"
 131 #include "opto/idealGraphPrinter.hpp"
 132 #endif
 133 #if INCLUDE_RTM_OPT
 134 #include "runtime/rtmLocking.hpp"
 135 #endif
 136 
 137 // Initialization after module runtime initialization


 294   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
 295   // and ::Release()
 296   _ParkEvent   = ParkEvent::Allocate(this);
 297   _SleepEvent  = ParkEvent::Allocate(this);
 298   _MutexEvent  = ParkEvent::Allocate(this);
 299   _MuxEvent    = ParkEvent::Allocate(this);
 300 
 301 #ifdef CHECK_UNHANDLED_OOPS
 302   if (CheckUnhandledOops) {
 303     _unhandled_oops = new UnhandledOops(this);
 304   }
 305 #endif // CHECK_UNHANDLED_OOPS
 306 #ifdef ASSERT
 307   if (UseBiasedLocking) {
 308     assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
 309     assert(this == _real_malloc_address ||
 310            this == align_up(_real_malloc_address, (int)markOopDesc::biased_lock_alignment),
 311            "bug in forced alignment of thread objects");
 312   }
 313 #endif // ASSERT
 314 
 315   // Notify the barrier set that a thread is being created. Note that the
 316   // main thread is created before a barrier set is available. The call to
 317   // BarrierSet::on_thread_create() for the main thread is therefore deferred
 318   // until it calls BarrierSet::set_barrier_set().
 319   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 320   if (barrier_set != NULL) {
 321     barrier_set->on_thread_create(this);
 322   }
 323 }
 324 
 325 void Thread::initialize_thread_current() {
 326 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 327   assert(_thr_current == NULL, "Thread::current already initialized");
 328   _thr_current = this;
 329 #endif
 330   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 331   ThreadLocalStorage::set_thread(this);
 332   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 333 }
 334 
 335 void Thread::clear_thread_current() {
 336   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 337 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 338   _thr_current = NULL;
 339 #endif
 340   ThreadLocalStorage::set_thread(NULL);
 341 }
 342 


 353 
 354   // Set stack limits after thread is initialized.
 355   if (is_Java_thread()) {
 356     ((JavaThread*) this)->set_stack_overflow_limit();
 357     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
 358   }
 359 #if INCLUDE_NMT
 360   // record thread's native stack, stack grows downward
 361   MemTracker::record_thread_stack(stack_end(), stack_size());
 362 #endif // INCLUDE_NMT
 363   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
 364     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
 365     os::current_thread_id(), p2i(stack_base() - stack_size()),
 366     p2i(stack_base()), stack_size()/1024);
 367 }
 368 
 369 
 370 Thread::~Thread() {
 371   EVENT_THREAD_DESTRUCT(this);
 372 
 373   // Notify the barrier set that a thread is being destroyed. Note that a barrier
 374   // set might not be available if we encountered errors during bootstrapping.
 375   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 376   if (barrier_set != NULL) {
 377     barrier_set->on_thread_destroy(this);
 378   }
 379 
 380   // stack_base can be NULL if the thread is never started or exited before
 381   // record_stack_base_and_size called. Although, we would like to ensure
 382   // that all started threads do call record_stack_base_and_size(), there is
 383   // not proper way to enforce that.
 384 #if INCLUDE_NMT
 385   if (_stack_base != NULL) {
 386     MemTracker::release_thread_stack(stack_end(), stack_size());
 387 #ifdef ASSERT
 388     set_stack_base(NULL);
 389 #endif
 390   }
 391 #endif // INCLUDE_NMT
 392 
 393   // deallocate data structures
 394   delete resource_area();
 395   // since the handle marks are using the handle area, we have to deallocated the root
 396   // handle mark before deallocating the thread's handle area,
 397   assert(last_handle_mark() != NULL, "check we have an element");
 398   delete last_handle_mark();
 399   assert(last_handle_mark() == NULL, "check we have reached the end");


1589 
1590   // Setup safepoint state info for this thread
1591   ThreadSafepointState::create(this);
1592 
1593   debug_only(_java_call_counter = 0);
1594 
1595   // JVMTI PopFrame support
1596   _popframe_condition = popframe_inactive;
1597   _popframe_preserved_args = NULL;
1598   _popframe_preserved_args_size = 0;
1599   _frames_to_pop_failed_realloc = 0;
1600 
1601   if (SafepointMechanism::uses_thread_local_poll()) {
1602     SafepointMechanism::initialize_header(this);
1603   }
1604 
1605   pd_initialize();
1606 }
1607 
1608 JavaThread::JavaThread(bool is_attaching_via_jni) :
1609                        Thread() {





1610   initialize();
1611   if (is_attaching_via_jni) {
1612     _jni_attach_state = _attaching_via_jni;
1613   } else {
1614     _jni_attach_state = _not_attaching_via_jni;
1615   }
1616   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1617 }
1618 
1619 bool JavaThread::reguard_stack(address cur_sp) {
1620   if (_stack_guard_state != stack_guard_yellow_reserved_disabled
1621       && _stack_guard_state != stack_guard_reserved_disabled) {
1622     return true; // Stack already guarded or guard pages not needed.
1623   }
1624 
1625   if (register_stack_overflow()) {
1626     // For those architectures which have separate register and
1627     // memory stacks, we must check the register stack to see if
1628     // it has overflowed.
1629     return false;


1651 bool JavaThread::reguard_stack(void) {
1652   return reguard_stack(os::current_stack_pointer());
1653 }
1654 
1655 
1656 void JavaThread::block_if_vm_exited() {
1657   if (_terminated == _vm_exited) {
1658     // _vm_exited is set at safepoint, and Threads_lock is never released
1659     // we will block here forever
1660     Threads_lock->lock_without_safepoint_check();
1661     ShouldNotReachHere();
1662   }
1663 }
1664 
1665 
1666 // Remove this ifdef when C1 is ported to the compiler interface.
1667 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1668 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1669 
1670 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1671                        Thread() {





1672   initialize();
1673   _jni_attach_state = _not_attaching_via_jni;
1674   set_entry_point(entry_point);
1675   // Create the native thread itself.
1676   // %note runtime_23
1677   os::ThreadType thr_type = os::java_thread;
1678   thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1679                                                      os::java_thread;
1680   os::create_thread(this, thr_type, stack_sz);
1681   // The _osthread may be NULL here because we ran out of memory (too many threads active).
1682   // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1683   // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1684   // the exception consists of creating the exception object & initializing it, initialization
1685   // will leave the VM via a JavaCall and then all locks must be unlocked).
1686   //
1687   // The thread is still suspended when we reach here. Thread must be explicit started
1688   // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1689   // by calling Threads:add. The reason why this is not done here, is because the thread
1690   // object must be fully initialized (take a look at JVM_Start)
1691 }


< prev index next >