< 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) <=


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


 270 #ifdef CHECK_UNHANDLED_OOPS
 271   if (CheckUnhandledOops) {
 272     _unhandled_oops = new UnhandledOops(this);
 273   }
 274 #endif // CHECK_UNHANDLED_OOPS
 275 #ifdef ASSERT
 276   if (UseBiasedLocking) {
 277     assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
 278     assert(this == _real_malloc_address ||
 279            this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
 280            "bug in forced alignment of thread objects");
 281   }
 282 #endif // ASSERT
 283 }
 284 
 285 // Non-inlined version to be used where thread.inline.hpp shouldn't be included.
 286 Thread* Thread::current_noinline() {
 287   return Thread::current();
 288 }
 289 
 290 void Thread::initialize_thread_current() {
 291   assert(_thr_current == NULL, "Thread::current already initialized");
 292   _thr_current = this;
 293 }




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


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





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


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

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


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



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


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



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


1967   remove_stack_guard_pages();
1968 
1969   if (UseTLAB) {
1970     tlab().make_parsable(true);  // retire TLAB, if any
1971   }
1972 
1973 #if INCLUDE_ALL_GCS
1974   if (UseG1GC) {
1975     flush_barrier_queues();
1976   }
1977 #endif // INCLUDE_ALL_GCS
1978 
1979   Threads::remove(this);
1980   delete this;
1981 }
1982 
1983 
1984 
1985 
1986 JavaThread* JavaThread::active() {
1987   Thread* thread = Thread::current();
1988   assert(thread != NULL, "just checking");
1989   if (thread->is_Java_thread()) {
1990     return (JavaThread*) thread;
1991   } else {
1992     assert(thread->is_VM_thread(), "this must be a vm thread");
1993     VM_Operation* op = ((VMThread*) thread)->vm_operation();
1994     JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1995     assert(ret->is_Java_thread(), "must be a Java thread");
1996     return ret;
1997   }
1998 }
1999 
2000 bool JavaThread::is_lock_owned(address adr) const {
2001   if (Thread::is_lock_owned(adr)) return true;
2002 
2003   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
2004     if (chunk->contains(adr)) return true;
2005   }
2006 
2007   return false;


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 output stream logging
3403   ostream_init_log();
3404 
3405   // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3406   // Must be before create_vm_init_agents()
3407   if (Arguments::init_libraries_at_startup()) {
3408     convert_vm_init_libraries_to_agents();
3409   }
3410 
3411   // Launch -agentlib/-agentpath and converted -Xrun agents
3412   if (Arguments::init_agents_at_startup()) {
3413     create_vm_init_agents();
3414   }
3415 
3416   // Initialize Threads state
3417   _thread_list = NULL;
3418   _number_of_threads = 0;
3419   _number_of_non_daemon_threads = 0;
3420 
3421   // Initialize global data structures and create system classes in heap
3422   vm_init_globals();
3423 
3424 #if INCLUDE_JVMCI
3425   if (JVMCICounterSize > 0) {
3426     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3427     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3428   } else {
3429     JavaThread::_jvmci_old_thread_counters = NULL;
3430   }
3431 #endif // INCLUDE_JVMCI
3432 
3433   // Attach the main thread to this os thread
3434   JavaThread* main_thread = new JavaThread();
3435   main_thread->set_thread_state(_thread_in_vm);
3436   main_thread->initialize_thread_current();
3437   // must do this before set_active_handles



3438   main_thread->record_stack_base_and_size();


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


< prev index next >