< prev index next >

src/share/vm/runtime/thread.cpp

Print this page




  61 #include "runtime/frame.inline.hpp"
  62 #include "runtime/globals.hpp"
  63 #include "runtime/init.hpp"
  64 #include "runtime/interfaceSupport.hpp"
  65 #include "runtime/java.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/jniPeriodicChecker.hpp"
  68 #include "runtime/memprofiler.hpp"
  69 #include "runtime/mutexLocker.hpp"
  70 #include "runtime/objectMonitor.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/osThread.hpp"
  73 #include "runtime/safepoint.hpp"
  74 #include "runtime/sharedRuntime.hpp"
  75 #include "runtime/statSampler.hpp"
  76 #include "runtime/stubRoutines.hpp"
  77 #include "runtime/sweeper.hpp"
  78 #include "runtime/task.hpp"
  79 #include "runtime/thread.inline.hpp"
  80 #include "runtime/threadCritical.hpp"
  81 #include "runtime/threadLocalStorage.hpp"
  82 #include "runtime/vframe.hpp"
  83 #include "runtime/vframeArray.hpp"
  84 #include "runtime/vframe_hp.hpp"
  85 #include "runtime/vmThread.hpp"
  86 #include "runtime/vm_operations.hpp"
  87 #include "runtime/vm_version.hpp"
  88 #include "services/attachListener.hpp"
  89 #include "services/management.hpp"
  90 #include "services/memTracker.hpp"
  91 #include "services/threadService.hpp"
  92 #include "trace/traceMacros.hpp"
  93 #include "trace/tracing.hpp"
  94 #include "utilities/defaultStream.hpp"
  95 #include "utilities/dtrace.hpp"
  96 #include "utilities/events.hpp"
  97 #include "utilities/macros.hpp"
  98 #include "utilities/preserveException.hpp"
  99 #if INCLUDE_ALL_GCS
 100 #include "gc/cms/concurrentMarkSweepThread.hpp"
 101 #include "gc/g1/concurrentMarkThread.inline.hpp"


 125 
 126   #define DTRACE_THREAD_PROBE(probe, javathread)                           \
 127     {                                                                      \
 128       ResourceMark rm(this);                                               \
 129       int len = 0;                                                         \
 130       const char* name = (javathread)->get_thread_name();                  \
 131       len = strlen(name);                                                  \
 132       HOTSPOT_THREAD_PROBE_##probe(/* probe = start, stop */               \
 133         (char *) name, len,                                                \
 134         java_lang_Thread::thread_id((javathread)->threadObj()),            \
 135         (uintptr_t) (javathread)->osthread()->thread_id(),                 \
 136         java_lang_Thread::is_daemon((javathread)->threadObj()));           \
 137     }
 138 
 139 #else //  ndef DTRACE_ENABLED
 140 
 141   #define DTRACE_THREAD_PROBE(probe, javathread)
 142 
 143 #endif // ndef DTRACE_ENABLED
 144 




 145 
 146 // Class hierarchy
 147 // - Thread
 148 //   - VMThread
 149 //   - WatcherThread
 150 //   - ConcurrentMarkSweepThread
 151 //   - JavaThread
 152 //     - CompilerThread
 153 
 154 // ======= Thread ========
 155 // Support for forcing alignment of thread objects for biased locking
 156 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
 157   if (UseBiasedLocking) {
 158     const int alignment = markOopDesc::biased_lock_alignment;
 159     size_t aligned_size = size + (alignment - sizeof(intptr_t));
 160     void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
 161                                           : AllocateHeap(aligned_size, flags, CURRENT_PC,
 162                                                          AllocFailStrategy::RETURN_NULL);
 163     void* aligned_addr     = (void*) align_size_up((intptr_t) real_malloc_addr, alignment);
 164     assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=


 264   _ParkEvent   = ParkEvent::Allocate(this);
 265   _SleepEvent  = ParkEvent::Allocate(this);
 266   _MutexEvent  = ParkEvent::Allocate(this);
 267   _MuxEvent    = ParkEvent::Allocate(this);
 268 
 269 #ifdef CHECK_UNHANDLED_OOPS
 270   if (CheckUnhandledOops) {
 271     _unhandled_oops = new UnhandledOops(this);
 272   }
 273 #endif // CHECK_UNHANDLED_OOPS
 274 #ifdef ASSERT
 275   if (UseBiasedLocking) {
 276     assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
 277     assert(this == _real_malloc_address ||
 278            this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
 279            "bug in forced alignment of thread objects");
 280   }
 281 #endif // ASSERT
 282 }
 283 
 284 // Non-inlined version to be used where thread.inline.hpp shouldn't be included.
 285 Thread* Thread::current_noinline() {
 286   return Thread::current();





 287 }
 288 
 289 void Thread::initialize_thread_local_storage() {
 290   // Note: Make sure this method only calls
 291   // non-blocking operations. Otherwise, it might not work
 292   // with the thread-startup/safepoint interaction.
 293 
 294   // During Java thread startup, safepoint code should allow this
 295   // method to complete because it may need to allocate memory to
 296   // store information for the new thread.
 297 
 298   // initialize structure dependent on thread local storage
 299   ThreadLocalStorage::set_thread(this);
 300 }
 301 
 302 void Thread::record_stack_base_and_size() {
 303   set_stack_base(os::current_stack_base());
 304   set_stack_size(os::current_stack_size());
 305   if (is_Java_thread()) {
 306     ((JavaThread*) this)->set_stack_overflow_limit();
 307   }
 308   // CR 7190089: on Solaris, primordial thread's stack is adjusted
 309   // in initialize_thread(). Without the adjustment, stack size is
 310   // incorrect if stack is set to unlimited (ulimit -s unlimited).
 311   // So far, only Solaris has real implementation of initialize_thread().
 312   //
 313   // set up any platform-specific state.
 314   os::initialize_thread(this);
 315 
 316 #if INCLUDE_NMT
 317   // record thread's native stack, stack grows downward
 318   address stack_low_addr = stack_base() - stack_size();
 319   MemTracker::record_thread_stack(stack_low_addr, stack_size());


 347   // handle mark before deallocating the thread's handle area,
 348   assert(last_handle_mark() != NULL, "check we have an element");
 349   delete last_handle_mark();
 350   assert(last_handle_mark() == NULL, "check we have reached the end");
 351 
 352   // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
 353   // We NULL out the fields for good hygiene.
 354   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;
 355   ParkEvent::Release(_SleepEvent); _SleepEvent  = NULL;
 356   ParkEvent::Release(_MutexEvent); _MutexEvent  = NULL;
 357   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 358 
 359   delete handle_area();
 360   delete metadata_handles();
 361 
 362   // osthread() can be NULL, if creation of thread failed.
 363   if (osthread() != NULL) os::free_thread(osthread());
 364 
 365   delete _SR_lock;
 366 
 367   // clear thread local storage if the Thread is deleting itself

 368   if (this == Thread::current()) {
 369     ThreadLocalStorage::set_thread(NULL);
 370   } else {
 371     // In the case where we're not the current thread, invalidate all the
 372     // caches in case some code tries to get the current thread or the
 373     // thread that was destroyed, and gets stale information.
 374     ThreadLocalStorage::invalidate_all();
 375   }

 376   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 377 }
 378 
 379 // NOTE: dummy function for assertion purpose.
 380 void Thread::run() {
 381   ShouldNotReachHere();
 382 }
 383 
 384 #ifdef ASSERT
 385 // Private method to check for dangling thread pointer
 386 void check_for_dangling_thread_pointer(Thread *thread) {
 387   assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
 388          "possibility of dangling Thread pointer");
 389 }
 390 #endif
 391 
 392 ThreadPriority Thread::get_priority(const Thread* const thread) {
 393   ThreadPriority priority;
 394   // Can return an error!
 395   (void)os::get_priority(thread, priority);


1256     remaining = PeriodicTask::time_to_wait();
1257     if (remaining == 0) {
1258       // Last task was just disenrolled so loop around and wait until
1259       // another task gets enrolled
1260       continue;
1261     }
1262 
1263     remaining -= time_slept;
1264     if (remaining <= 0) {
1265       break;
1266     }
1267   }
1268 
1269   return time_slept;
1270 }
1271 
1272 void WatcherThread::run() {
1273   assert(this == watcher_thread(), "just checking");
1274 
1275   this->record_stack_base_and_size();
1276   this->initialize_thread_local_storage();
1277   this->set_native_thread_name(this->name());
1278   this->set_active_handles(JNIHandleBlock::allocate_block());
1279   while (true) {
1280     assert(watcher_thread() == Thread::current(), "thread consistency check");
1281     assert(watcher_thread() == this, "thread consistency check");
1282 
1283     // Calculate how long it'll be until the next PeriodicTask work
1284     // should be done, and sleep that amount of time.
1285     int time_waited = sleep();
1286 
1287     if (is_error_reported()) {
1288       // A fatal error has happened, the error handler(VMError::report_and_die)
1289       // should abort JVM after creating an error log file. However in some
1290       // rare cases, the error handler itself might deadlock. Here we try to
1291       // kill JVM if the fatal error handler fails to abort in 2 minutes.
1292       //
1293       // This code is in WatcherThread because WatcherThread wakes up
1294       // periodically so the fatal error handler doesn't need to do anything;
1295       // also because the WatcherThread is less likely to crash than other
1296       // threads.


1309         // Wake up 5 seconds later, the fatal handler may reset OnError or
1310         // ShowMessageBoxOnError when it is ready to abort.
1311         os::sleep(this, 5 * 1000, false);
1312       }
1313     }
1314 
1315     if (_should_terminate) {
1316       // check for termination before posting the next tick
1317       break;
1318     }
1319 
1320     PeriodicTask::real_time_tick(time_waited);
1321   }
1322 
1323   // Signal that it is terminated
1324   {
1325     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1326     _watcher_thread = NULL;
1327     Terminator_lock->notify();
1328   }
1329 
1330   // Thread destructor usually does this..
1331   ThreadLocalStorage::set_thread(NULL);
1332 }
1333 
1334 void WatcherThread::start() {
1335   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1336 
1337   if (watcher_thread() == NULL && _startable) {
1338     _should_terminate = false;
1339     // Create the single instance of WatcherThread
1340     new WatcherThread();
1341   }
1342 }
1343 
1344 void WatcherThread::make_startable() {
1345   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1346   _startable = true;
1347 }
1348 
1349 void WatcherThread::stop() {
1350   {
1351     // Follow normal safepoint aware lock enter protocol since the


1646         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1647       }
1648     }
1649     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1650   }
1651 #endif // INCLUDE_JVMCI
1652 }
1653 
1654 
1655 // The first routine called by a new Java thread
1656 void JavaThread::run() {
1657   // initialize thread-local alloc buffer related fields
1658   this->initialize_tlab();
1659 
1660   // used to test validity of stack trace backs
1661   this->record_base_of_stack_pointer();
1662 
1663   // Record real stack base and size.
1664   this->record_stack_base_and_size();
1665 
1666   // Initialize thread local storage; set before calling MutexLocker
1667   this->initialize_thread_local_storage();
1668 
1669   this->create_stack_guard_pages();
1670 
1671   this->cache_global_variables();
1672 
1673   // Thread is now sufficient initialized to be handled by the safepoint code as being
1674   // in the VM. Change thread state from _thread_new to _thread_in_vm
1675   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1676 
1677   assert(JavaThread::current() == this, "sanity check");
1678   assert(!Thread::current()->owns_locks(), "sanity check");
1679 
1680   DTRACE_THREAD_PROBE(start, this);
1681 
1682   // This operation might block. We call that after all safepoint checks for a new thread has
1683   // been completed.
1684   this->set_active_handles(JNIHandleBlock::allocate_block());
1685 
1686   if (JvmtiExport::should_post_thread_life()) {
1687     JvmtiExport::post_thread_start(this);
1688   }


1980   remove_stack_guard_pages();
1981 
1982   if (UseTLAB) {
1983     tlab().make_parsable(true);  // retire TLAB, if any
1984   }
1985 
1986 #if INCLUDE_ALL_GCS
1987   if (UseG1GC) {
1988     flush_barrier_queues();
1989   }
1990 #endif // INCLUDE_ALL_GCS
1991 
1992   Threads::remove(this);
1993   delete this;
1994 }
1995 
1996 
1997 
1998 
1999 JavaThread* JavaThread::active() {
2000   Thread* thread = ThreadLocalStorage::thread();
2001   assert(thread != NULL, "just checking");
2002   if (thread->is_Java_thread()) {
2003     return (JavaThread*) thread;
2004   } else {
2005     assert(thread->is_VM_thread(), "this must be a vm thread");
2006     VM_Operation* op = ((VMThread*) thread)->vm_operation();
2007     JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
2008     assert(ret->is_Java_thread(), "must be a Java thread");
2009     return ret;
2010   }
2011 }
2012 
2013 bool JavaThread::is_lock_owned(address adr) const {
2014   if (Thread::is_lock_owned(adr)) return true;
2015 
2016   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
2017     if (chunk->contains(adr)) return true;
2018   }
2019 
2020   return false;
2021 }


3390   if (!constraint_result) {
3391     return JNI_EINVAL;
3392   }
3393 
3394   if (PauseAtStartup) {
3395     os::pause();
3396   }
3397 
3398   HOTSPOT_VM_INIT_BEGIN();
3399 
3400   // Timing (must come after argument parsing)
3401   TraceTime timer("Create VM", TraceStartupTime);
3402 
3403   // Initialize the os module after parsing the args
3404   jint os_init_2_result = os::init_2();
3405   if (os_init_2_result != JNI_OK) return os_init_2_result;
3406 
3407   jint adjust_after_os_result = Arguments::adjust_after_os();
3408   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3409 
3410   // initialize TLS
3411   ThreadLocalStorage::init();
3412 
3413   // Initialize output stream logging
3414   ostream_init_log();
3415 
3416   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3417   // Must be before create_vm_init_agents()
3418   if (Arguments::init_libraries_at_startup()) {
3419     convert_vm_init_libraries_to_agents();
3420   }
3421 
3422   // Launch -agentlib/-agentpath and converted -Xrun agents
3423   if (Arguments::init_agents_at_startup()) {
3424     create_vm_init_agents();
3425   }
3426 
3427   // Initialize Threads state
3428   _thread_list = NULL;
3429   _number_of_threads = 0;
3430   _number_of_non_daemon_threads = 0;
3431 
3432   // Initialize global data structures and create system classes in heap
3433   vm_init_globals();
3434 
3435 #if INCLUDE_JVMCI
3436   if (JVMCICounterSize > 0) {
3437     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3438     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3439   } else {
3440     JavaThread::_jvmci_old_thread_counters = NULL;
3441   }
3442 #endif // INCLUDE_JVMCI
3443 
3444   // Attach the main thread to this os thread
3445   JavaThread* main_thread = new JavaThread();
3446   main_thread->set_thread_state(_thread_in_vm);
3447   // must do this before set_active_handles and initialize_thread_local_storage
3448   // Note: on solaris initialize_thread_local_storage() will (indirectly)
3449   // change the stack size recorded here to one based on the java thread
3450   // stacksize. This adjusted size is what is used to figure the placement
3451   // of the guard pages.
3452   main_thread->record_stack_base_and_size();
3453   main_thread->initialize_thread_local_storage();
3454 
3455   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3456 
3457   if (!main_thread->set_as_starting_thread()) {
3458     vm_shutdown_during_initialization(
3459                                       "Failed necessary internal allocation. Out of swap space");
3460     delete main_thread;
3461     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3462     return JNI_ENOMEM;
3463   }
3464 
3465   // Enable guard page *after* os::create_main_thread(), otherwise it would
3466   // crash Linux VM, see notes in os_linux.cpp.
3467   main_thread->create_stack_guard_pages();
3468 
3469   // Initialize Java-Level synchronization subsystem
3470   ObjectMonitor::Initialize();
3471 
3472   // Initialize global modules
3473   jint status = init_globals();
3474   if (status != JNI_OK) {




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

  81 #include "runtime/vframe.hpp"
  82 #include "runtime/vframeArray.hpp"
  83 #include "runtime/vframe_hp.hpp"
  84 #include "runtime/vmThread.hpp"
  85 #include "runtime/vm_operations.hpp"
  86 #include "runtime/vm_version.hpp"
  87 #include "services/attachListener.hpp"
  88 #include "services/management.hpp"
  89 #include "services/memTracker.hpp"
  90 #include "services/threadService.hpp"
  91 #include "trace/traceMacros.hpp"
  92 #include "trace/tracing.hpp"
  93 #include "utilities/defaultStream.hpp"
  94 #include "utilities/dtrace.hpp"
  95 #include "utilities/events.hpp"
  96 #include "utilities/macros.hpp"
  97 #include "utilities/preserveException.hpp"
  98 #if INCLUDE_ALL_GCS
  99 #include "gc/cms/concurrentMarkSweepThread.hpp"
 100 #include "gc/g1/concurrentMarkThread.inline.hpp"


 124 
 125   #define DTRACE_THREAD_PROBE(probe, javathread)                           \
 126     {                                                                      \
 127       ResourceMark rm(this);                                               \
 128       int len = 0;                                                         \
 129       const char* name = (javathread)->get_thread_name();                  \
 130       len = strlen(name);                                                  \
 131       HOTSPOT_THREAD_PROBE_##probe(/* probe = start, stop */               \
 132         (char *) name, len,                                                \
 133         java_lang_Thread::thread_id((javathread)->threadObj()),            \
 134         (uintptr_t) (javathread)->osthread()->thread_id(),                 \
 135         java_lang_Thread::is_daemon((javathread)->threadObj()));           \
 136     }
 137 
 138 #else //  ndef DTRACE_ENABLED
 139 
 140   #define DTRACE_THREAD_PROBE(probe, javathread)
 141 
 142 #endif // ndef DTRACE_ENABLED
 143 
 144 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 145 // Current thread is maintained as a thread-local variable
 146 THREAD_LOCAL_DECL Thread* Thread::_thr_current = NULL;
 147 #endif
 148 
 149 // Class hierarchy
 150 // - Thread
 151 //   - VMThread
 152 //   - WatcherThread
 153 //   - ConcurrentMarkSweepThread
 154 //   - JavaThread
 155 //     - CompilerThread
 156 
 157 // ======= Thread ========
 158 // Support for forcing alignment of thread objects for biased locking
 159 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
 160   if (UseBiasedLocking) {
 161     const int alignment = markOopDesc::biased_lock_alignment;
 162     size_t aligned_size = size + (alignment - sizeof(intptr_t));
 163     void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
 164                                           : AllocateHeap(aligned_size, flags, CURRENT_PC,
 165                                                          AllocFailStrategy::RETURN_NULL);
 166     void* aligned_addr     = (void*) align_size_up((intptr_t) real_malloc_addr, alignment);
 167     assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=


 267   _ParkEvent   = ParkEvent::Allocate(this);
 268   _SleepEvent  = ParkEvent::Allocate(this);
 269   _MutexEvent  = ParkEvent::Allocate(this);
 270   _MuxEvent    = ParkEvent::Allocate(this);
 271 
 272 #ifdef CHECK_UNHANDLED_OOPS
 273   if (CheckUnhandledOops) {
 274     _unhandled_oops = new UnhandledOops(this);
 275   }
 276 #endif // CHECK_UNHANDLED_OOPS
 277 #ifdef ASSERT
 278   if (UseBiasedLocking) {
 279     assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
 280     assert(this == _real_malloc_address ||
 281            this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
 282            "bug in forced alignment of thread objects");
 283   }
 284 #endif // ASSERT
 285 }
 286 
 287 void Thread::initialize_thread_current() {
 288 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 289   assert(_thr_current == NULL, "Thread::current already initialized");
 290   _thr_current = this;
 291 #endif
 292   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 293   ThreadLocalStorage::set_thread(this);
 294   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 295 }
 296 
 297 void Thread::clear_thread_current() {
 298   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 299 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 300   _thr_current = NULL;
 301 #endif
 302   ThreadLocalStorage::set_thread(NULL);





 303 }
 304 
 305 void Thread::record_stack_base_and_size() {
 306   set_stack_base(os::current_stack_base());
 307   set_stack_size(os::current_stack_size());
 308   if (is_Java_thread()) {
 309     ((JavaThread*) this)->set_stack_overflow_limit();
 310   }
 311   // CR 7190089: on Solaris, primordial thread's stack is adjusted
 312   // in initialize_thread(). Without the adjustment, stack size is
 313   // incorrect if stack is set to unlimited (ulimit -s unlimited).
 314   // So far, only Solaris has real implementation of initialize_thread().
 315   //
 316   // set up any platform-specific state.
 317   os::initialize_thread(this);
 318 
 319 #if INCLUDE_NMT
 320   // record thread's native stack, stack grows downward
 321   address stack_low_addr = stack_base() - stack_size();
 322   MemTracker::record_thread_stack(stack_low_addr, stack_size());


 350   // handle mark before deallocating the thread's handle area,
 351   assert(last_handle_mark() != NULL, "check we have an element");
 352   delete last_handle_mark();
 353   assert(last_handle_mark() == NULL, "check we have reached the end");
 354 
 355   // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
 356   // We NULL out the fields for good hygiene.
 357   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;
 358   ParkEvent::Release(_SleepEvent); _SleepEvent  = NULL;
 359   ParkEvent::Release(_MutexEvent); _MutexEvent  = NULL;
 360   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 361 
 362   delete handle_area();
 363   delete metadata_handles();
 364 
 365   // osthread() can be NULL, if creation of thread failed.
 366   if (osthread() != NULL) os::free_thread(osthread());
 367 
 368   delete _SR_lock;
 369 
 370   // clear Thread::current if thread is deleting itself.
 371   // Needed to ensure JNI correctly detects non-attached threads.
 372   if (this == Thread::current()) {
 373     clear_thread_current();





 374   }
 375 
 376   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 377 }
 378 
 379 // NOTE: dummy function for assertion purpose.
 380 void Thread::run() {
 381   ShouldNotReachHere();
 382 }
 383 
 384 #ifdef ASSERT
 385 // Private method to check for dangling thread pointer
 386 void check_for_dangling_thread_pointer(Thread *thread) {
 387   assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
 388          "possibility of dangling Thread pointer");
 389 }
 390 #endif
 391 
 392 ThreadPriority Thread::get_priority(const Thread* const thread) {
 393   ThreadPriority priority;
 394   // Can return an error!
 395   (void)os::get_priority(thread, priority);


1256     remaining = PeriodicTask::time_to_wait();
1257     if (remaining == 0) {
1258       // Last task was just disenrolled so loop around and wait until
1259       // another task gets enrolled
1260       continue;
1261     }
1262 
1263     remaining -= time_slept;
1264     if (remaining <= 0) {
1265       break;
1266     }
1267   }
1268 
1269   return time_slept;
1270 }
1271 
1272 void WatcherThread::run() {
1273   assert(this == watcher_thread(), "just checking");
1274 
1275   this->record_stack_base_and_size();

1276   this->set_native_thread_name(this->name());
1277   this->set_active_handles(JNIHandleBlock::allocate_block());
1278   while (true) {
1279     assert(watcher_thread() == Thread::current(), "thread consistency check");
1280     assert(watcher_thread() == this, "thread consistency check");
1281 
1282     // Calculate how long it'll be until the next PeriodicTask work
1283     // should be done, and sleep that amount of time.
1284     int time_waited = sleep();
1285 
1286     if (is_error_reported()) {
1287       // A fatal error has happened, the error handler(VMError::report_and_die)
1288       // should abort JVM after creating an error log file. However in some
1289       // rare cases, the error handler itself might deadlock. Here we try to
1290       // kill JVM if the fatal error handler fails to abort in 2 minutes.
1291       //
1292       // This code is in WatcherThread because WatcherThread wakes up
1293       // periodically so the fatal error handler doesn't need to do anything;
1294       // also because the WatcherThread is less likely to crash than other
1295       // threads.


1308         // Wake up 5 seconds later, the fatal handler may reset OnError or
1309         // ShowMessageBoxOnError when it is ready to abort.
1310         os::sleep(this, 5 * 1000, false);
1311       }
1312     }
1313 
1314     if (_should_terminate) {
1315       // check for termination before posting the next tick
1316       break;
1317     }
1318 
1319     PeriodicTask::real_time_tick(time_waited);
1320   }
1321 
1322   // Signal that it is terminated
1323   {
1324     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1325     _watcher_thread = NULL;
1326     Terminator_lock->notify();
1327   }



1328 }
1329 
1330 void WatcherThread::start() {
1331   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1332 
1333   if (watcher_thread() == NULL && _startable) {
1334     _should_terminate = false;
1335     // Create the single instance of WatcherThread
1336     new WatcherThread();
1337   }
1338 }
1339 
1340 void WatcherThread::make_startable() {
1341   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1342   _startable = true;
1343 }
1344 
1345 void WatcherThread::stop() {
1346   {
1347     // Follow normal safepoint aware lock enter protocol since the


1642         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1643       }
1644     }
1645     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1646   }
1647 #endif // INCLUDE_JVMCI
1648 }
1649 
1650 
1651 // The first routine called by a new Java thread
1652 void JavaThread::run() {
1653   // initialize thread-local alloc buffer related fields
1654   this->initialize_tlab();
1655 
1656   // used to test validity of stack trace backs
1657   this->record_base_of_stack_pointer();
1658 
1659   // Record real stack base and size.
1660   this->record_stack_base_and_size();
1661 



1662   this->create_stack_guard_pages();
1663 
1664   this->cache_global_variables();
1665 
1666   // Thread is now sufficient initialized to be handled by the safepoint code as being
1667   // in the VM. Change thread state from _thread_new to _thread_in_vm
1668   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1669 
1670   assert(JavaThread::current() == this, "sanity check");
1671   assert(!Thread::current()->owns_locks(), "sanity check");
1672 
1673   DTRACE_THREAD_PROBE(start, this);
1674 
1675   // This operation might block. We call that after all safepoint checks for a new thread has
1676   // been completed.
1677   this->set_active_handles(JNIHandleBlock::allocate_block());
1678 
1679   if (JvmtiExport::should_post_thread_life()) {
1680     JvmtiExport::post_thread_start(this);
1681   }


1973   remove_stack_guard_pages();
1974 
1975   if (UseTLAB) {
1976     tlab().make_parsable(true);  // retire TLAB, if any
1977   }
1978 
1979 #if INCLUDE_ALL_GCS
1980   if (UseG1GC) {
1981     flush_barrier_queues();
1982   }
1983 #endif // INCLUDE_ALL_GCS
1984 
1985   Threads::remove(this);
1986   delete this;
1987 }
1988 
1989 
1990 
1991 
1992 JavaThread* JavaThread::active() {
1993   Thread* thread = Thread::current();

1994   if (thread->is_Java_thread()) {
1995     return (JavaThread*) thread;
1996   } else {
1997     assert(thread->is_VM_thread(), "this must be a vm thread");
1998     VM_Operation* op = ((VMThread*) thread)->vm_operation();
1999     JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
2000     assert(ret->is_Java_thread(), "must be a Java thread");
2001     return ret;
2002   }
2003 }
2004 
2005 bool JavaThread::is_lock_owned(address adr) const {
2006   if (Thread::is_lock_owned(adr)) return true;
2007 
2008   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
2009     if (chunk->contains(adr)) return true;
2010   }
2011 
2012   return false;
2013 }


3382   if (!constraint_result) {
3383     return JNI_EINVAL;
3384   }
3385 
3386   if (PauseAtStartup) {
3387     os::pause();
3388   }
3389 
3390   HOTSPOT_VM_INIT_BEGIN();
3391 
3392   // Timing (must come after argument parsing)
3393   TraceTime timer("Create VM", TraceStartupTime);
3394 
3395   // Initialize the os module after parsing the args
3396   jint os_init_2_result = os::init_2();
3397   if (os_init_2_result != JNI_OK) return os_init_2_result;
3398 
3399   jint adjust_after_os_result = Arguments::adjust_after_os();
3400   if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3401 
3402   // Initialize library-based TLS
3403   ThreadLocalStorage::init();
3404 
3405   // Initialize output stream logging
3406   ostream_init_log();
3407 
3408   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3409   // Must be before create_vm_init_agents()
3410   if (Arguments::init_libraries_at_startup()) {
3411     convert_vm_init_libraries_to_agents();
3412   }
3413 
3414   // Launch -agentlib/-agentpath and converted -Xrun agents
3415   if (Arguments::init_agents_at_startup()) {
3416     create_vm_init_agents();
3417   }
3418 
3419   // Initialize Threads state
3420   _thread_list = NULL;
3421   _number_of_threads = 0;
3422   _number_of_non_daemon_threads = 0;
3423 
3424   // Initialize global data structures and create system classes in heap
3425   vm_init_globals();
3426 
3427 #if INCLUDE_JVMCI
3428   if (JVMCICounterSize > 0) {
3429     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3430     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3431   } else {
3432     JavaThread::_jvmci_old_thread_counters = NULL;
3433   }
3434 #endif // INCLUDE_JVMCI
3435 
3436   // Attach the main thread to this os thread
3437   JavaThread* main_thread = new JavaThread();
3438   main_thread->set_thread_state(_thread_in_vm);
3439   main_thread->initialize_thread_current();
3440   // must do this before set_active_handles



3441   main_thread->record_stack_base_and_size();


3442   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3443 
3444   if (!main_thread->set_as_starting_thread()) {
3445     vm_shutdown_during_initialization(
3446                                       "Failed necessary internal allocation. Out of swap space");
3447     delete main_thread;
3448     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3449     return JNI_ENOMEM;
3450   }
3451 
3452   // Enable guard page *after* os::create_main_thread(), otherwise it would
3453   // crash Linux VM, see notes in os_linux.cpp.
3454   main_thread->create_stack_guard_pages();
3455 
3456   // Initialize Java-Level synchronization subsystem
3457   ObjectMonitor::Initialize();
3458 
3459   // Initialize global modules
3460   jint status = init_globals();
3461   if (status != JNI_OK) {


< prev index next >