hotspot/src/share/vm/runtime/thread.cpp
changeset 6176 4d9030fe341f
parent 5888 8eac4eb75d6e
child 6184 a017b5ba6782
child 6189 9dcd5f57dcc3
equal deleted inserted replaced
6175:86dbf3cacacc 6176:4d9030fe341f
  1018 // The watcher thread exists to simulate timer interrupts.  It should
  1018 // The watcher thread exists to simulate timer interrupts.  It should
  1019 // be replaced by an abstraction over whatever native support for
  1019 // be replaced by an abstraction over whatever native support for
  1020 // timer interrupts exists on the platform.
  1020 // timer interrupts exists on the platform.
  1021 
  1021 
  1022 WatcherThread* WatcherThread::_watcher_thread   = NULL;
  1022 WatcherThread* WatcherThread::_watcher_thread   = NULL;
  1023 bool           WatcherThread::_should_terminate = false;
  1023 volatile bool  WatcherThread::_should_terminate = false;
  1024 
  1024 
  1025 WatcherThread::WatcherThread() : Thread() {
  1025 WatcherThread::WatcherThread() : Thread() {
  1026   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
  1026   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
  1027   if (os::create_thread(this, os::watcher_thread)) {
  1027   if (os::create_thread(this, os::watcher_thread)) {
  1028     _watcher_thread = this;
  1028     _watcher_thread = this;
  1050     assert(watcher_thread() == Thread::current(),  "thread consistency check");
  1050     assert(watcher_thread() == Thread::current(),  "thread consistency check");
  1051     assert(watcher_thread() == this,  "thread consistency check");
  1051     assert(watcher_thread() == this,  "thread consistency check");
  1052 
  1052 
  1053     // Calculate how long it'll be until the next PeriodicTask work
  1053     // Calculate how long it'll be until the next PeriodicTask work
  1054     // should be done, and sleep that amount of time.
  1054     // should be done, and sleep that amount of time.
  1055     const size_t time_to_wait = PeriodicTask::time_to_wait();
  1055     size_t time_to_wait = PeriodicTask::time_to_wait();
  1056     os::sleep(this, time_to_wait, false);
  1056 
       
  1057     // we expect this to timeout - we only ever get unparked when
       
  1058     // we should terminate
       
  1059     {
       
  1060       OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
       
  1061 
       
  1062       jlong prev_time = os::javaTimeNanos();
       
  1063       for (;;) {
       
  1064         int res= _SleepEvent->park(time_to_wait);
       
  1065         if (res == OS_TIMEOUT || _should_terminate)
       
  1066           break;
       
  1067         // spurious wakeup of some kind
       
  1068         jlong now = os::javaTimeNanos();
       
  1069         time_to_wait -= (now - prev_time) / 1000000;
       
  1070         if (time_to_wait <= 0)
       
  1071           break;
       
  1072         prev_time = now;
       
  1073       }
       
  1074     }
  1057 
  1075 
  1058     if (is_error_reported()) {
  1076     if (is_error_reported()) {
  1059       // A fatal error has happened, the error handler(VMError::report_and_die)
  1077       // A fatal error has happened, the error handler(VMError::report_and_die)
  1060       // should abort JVM after creating an error log file. However in some
  1078       // should abort JVM after creating an error log file. However in some
  1061       // rare cases, the error handler itself might deadlock. Here we try to
  1079       // rare cases, the error handler itself might deadlock. Here we try to
  1113 
  1131 
  1114 void WatcherThread::stop() {
  1132 void WatcherThread::stop() {
  1115   // it is ok to take late safepoints here, if needed
  1133   // it is ok to take late safepoints here, if needed
  1116   MutexLocker mu(Terminator_lock);
  1134   MutexLocker mu(Terminator_lock);
  1117   _should_terminate = true;
  1135   _should_terminate = true;
       
  1136   OrderAccess::fence();  // ensure WatcherThread sees update in main loop
       
  1137 
       
  1138   Thread* watcher = watcher_thread();
       
  1139   if (watcher != NULL)
       
  1140     watcher->_SleepEvent->unpark();
       
  1141 
  1118   while(watcher_thread() != NULL) {
  1142   while(watcher_thread() != NULL) {
  1119     // This wait should make safepoint checks, wait without a timeout,
  1143     // This wait should make safepoint checks, wait without a timeout,
  1120     // and wait as a suspend-equivalent condition.
  1144     // and wait as a suspend-equivalent condition.
  1121     //
  1145     //
  1122     // Note: If the FlatProfiler is running, then this thread is waiting
  1146     // Note: If the FlatProfiler is running, then this thread is waiting
  1361 
  1385 
  1362   // Initialize thread local storage; set before calling MutexLocker
  1386   // Initialize thread local storage; set before calling MutexLocker
  1363   this->initialize_thread_local_storage();
  1387   this->initialize_thread_local_storage();
  1364 
  1388 
  1365   this->create_stack_guard_pages();
  1389   this->create_stack_guard_pages();
       
  1390 
       
  1391   this->cache_global_variables();
  1366 
  1392 
  1367   // Thread is now sufficient initialized to be handled by the safepoint code as being
  1393   // Thread is now sufficient initialized to be handled by the safepoint code as being
  1368   // in the VM. Change thread state from _thread_new to _thread_in_vm
  1394   // in the VM. Change thread state from _thread_new to _thread_in_vm
  1369   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
  1395   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
  1370 
  1396 
  2953     delete main_thread;
  2979     delete main_thread;
  2954     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
  2980     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
  2955     return status;
  2981     return status;
  2956   }
  2982   }
  2957 
  2983 
       
  2984   // Should be done after the heap is fully created
       
  2985   main_thread->cache_global_variables();
       
  2986 
  2958   HandleMark hm;
  2987   HandleMark hm;
  2959 
  2988 
  2960   { MutexLocker mu(Threads_lock);
  2989   { MutexLocker mu(Threads_lock);
  2961     Threads::add(main_thread);
  2990     Threads::add(main_thread);
  2962   }
  2991   }
  3227   //   aren't, late joiners might appear to start slowly (we might
  3256   //   aren't, late joiners might appear to start slowly (we might
  3228   //   take a while to process their first tick).
  3257   //   take a while to process their first tick).
  3229   if (PeriodicTask::num_tasks() > 0) {
  3258   if (PeriodicTask::num_tasks() > 0) {
  3230     WatcherThread::start();
  3259     WatcherThread::start();
  3231   }
  3260   }
       
  3261 
       
  3262   // Give os specific code one last chance to start
       
  3263   os::init_3();
  3232 
  3264 
  3233   create_vm_timer.end();
  3265   create_vm_timer.end();
  3234   return JNI_OK;
  3266   return JNI_OK;
  3235 }
  3267 }
  3236 
  3268