< 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;


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






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


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





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


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

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


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



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


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



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


1972   remove_stack_guard_pages();
1973 
1974   if (UseTLAB) {
1975     tlab().make_parsable(true);  // retire TLAB, if any
1976   }
1977 
1978 #if INCLUDE_ALL_GCS
1979   if (UseG1GC) {
1980     flush_barrier_queues();
1981   }
1982 #endif // INCLUDE_ALL_GCS
1983 
1984   Threads::remove(this);
1985   delete this;
1986 }
1987 
1988 
1989 
1990 
1991 JavaThread* JavaThread::active() {
1992   Thread* thread = Thread::current();
1993   assert(thread != NULL, "just checking");
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;


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



3446   main_thread->record_stack_base_and_size();


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


< prev index next >