src/hotspot/share/runtime/thread.cpp
changeset 54645 05aaccf7d558
parent 54631 3a3e4e473622
child 54647 c0d9bc9b4e1f
equal deleted inserted replaced
54644:8d52b4c6f9d8 54645:05aaccf7d558
   792     // on the SR_lock below provides a much more predictable scale up for
   792     // on the SR_lock below provides a much more predictable scale up for
   793     // the delay. It also provides a simple/direct point to check for any
   793     // the delay. It also provides a simple/direct point to check for any
   794     // safepoint requests from the VMThread
   794     // safepoint requests from the VMThread
   795 
   795 
   796     {
   796     {
   797       MutexLocker ml(SR_lock());
   797       Thread* t = Thread::current();
       
   798       MonitorLocker ml(SR_lock(),
       
   799                        t->is_Java_thread() ? Mutex::_safepoint_check_flag : Mutex::_no_safepoint_check_flag);
   798       // wait with safepoint check (if we're a JavaThread - the WatcherThread
   800       // wait with safepoint check (if we're a JavaThread - the WatcherThread
   799       // can also call this)  and increase delay with each retry
   801       // can also call this)  and increase delay with each retry
   800       if (Thread::current()->is_Java_thread()) {
   802       ml.wait(i * delay);
   801         SR_lock()->wait(i * delay);
       
   802       } else {
       
   803         SR_lock()->wait_without_safepoint_check(i * delay);
       
   804       }
       
   805 
   803 
   806       is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
   804       is_suspended = is_ext_suspend_completed(true /* called_by_wait */,
   807                                               delay, bits);
   805                                               delay, bits);
   808 
   806 
   809       // It is possible for the external suspend request to be cancelled
   807       // It is possible for the external suspend request to be cancelled
  1403 }
  1401 }
  1404 
  1402 
  1405 int WatcherThread::sleep() const {
  1403 int WatcherThread::sleep() const {
  1406   // The WatcherThread does not participate in the safepoint protocol
  1404   // The WatcherThread does not participate in the safepoint protocol
  1407   // for the PeriodicTask_lock because it is not a JavaThread.
  1405   // for the PeriodicTask_lock because it is not a JavaThread.
  1408   MutexLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
  1406   MonitorLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
  1409 
  1407 
  1410   if (_should_terminate) {
  1408   if (_should_terminate) {
  1411     // check for termination before we do any housekeeping or wait
  1409     // check for termination before we do any housekeeping or wait
  1412     return 0;  // we did not sleep.
  1410     return 0;  // we did not sleep.
  1413   }
  1411   }
  1423   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
  1421   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
  1424 
  1422 
  1425   jlong time_before_loop = os::javaTimeNanos();
  1423   jlong time_before_loop = os::javaTimeNanos();
  1426 
  1424 
  1427   while (true) {
  1425   while (true) {
  1428     bool timedout = PeriodicTask_lock->wait_without_safepoint_check(remaining);
  1426     bool timedout = ml.wait(remaining);
  1429     jlong now = os::javaTimeNanos();
  1427     jlong now = os::javaTimeNanos();
  1430 
  1428 
  1431     if (remaining == 0) {
  1429     if (remaining == 0) {
  1432       // if we didn't have any tasks we could have waited for a long time
  1430       // if we didn't have any tasks we could have waited for a long time
  1433       // consider the time_slept zero and reset time_before_loop
  1431       // consider the time_slept zero and reset time_before_loop
  1546       // unpark the WatcherThread so it can see that it should terminate
  1544       // unpark the WatcherThread so it can see that it should terminate
  1547       watcher->unpark();
  1545       watcher->unpark();
  1548     }
  1546     }
  1549   }
  1547   }
  1550 
  1548 
  1551   MutexLocker mu(Terminator_lock);
  1549   MonitorLocker mu(Terminator_lock);
  1552 
  1550 
  1553   while (watcher_thread() != NULL) {
  1551   while (watcher_thread() != NULL) {
  1554     // This wait should make safepoint checks, wait without a timeout,
  1552     // This wait should make safepoint checks, wait without a timeout,
  1555     // and wait as a suspend-equivalent condition.
  1553     // and wait as a suspend-equivalent condition.
  1556     Terminator_lock->wait(0, Mutex::_as_suspend_equivalent_flag);
  1554     mu.wait(0, Mutex::_as_suspend_equivalent_flag);
  1557   }
  1555   }
  1558 }
  1556 }
  1559 
  1557 
  1560 void WatcherThread::unpark() {
  1558 void WatcherThread::unpark() {
  1561   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
  1559   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
  2422 
  2420 
  2423   assert(_anchor.walkable() ||
  2421   assert(_anchor.walkable() ||
  2424          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
  2422          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
  2425          "must have walkable stack");
  2423          "must have walkable stack");
  2426 
  2424 
  2427   MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
  2425   MonitorLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
  2428 
  2426 
  2429   assert(!this->is_ext_suspended(),
  2427   assert(!this->is_ext_suspended(),
  2430          "a thread trying to self-suspend should not already be suspended");
  2428          "a thread trying to self-suspend should not already be suspended");
  2431 
  2429 
  2432   if (this->is_suspend_equivalent()) {
  2430   if (this->is_suspend_equivalent()) {
  2450     ret++;
  2448     ret++;
  2451     this->set_ext_suspended();
  2449     this->set_ext_suspended();
  2452 
  2450 
  2453     // _ext_suspended flag is cleared by java_resume()
  2451     // _ext_suspended flag is cleared by java_resume()
  2454     while (is_ext_suspended()) {
  2452     while (is_ext_suspended()) {
  2455       this->SR_lock()->wait_without_safepoint_check();
  2453       ml.wait();
  2456     }
  2454     }
  2457   }
  2455   }
  2458   return ret;
  2456   return ret;
  2459 }
  2457 }
  2460 
  2458 
  3842     }
  3840     }
  3843 
  3841 
  3844     // Wait for the VM thread to become ready, and VMThread::run to initialize
  3842     // Wait for the VM thread to become ready, and VMThread::run to initialize
  3845     // Monitors can have spurious returns, must always check another state flag
  3843     // Monitors can have spurious returns, must always check another state flag
  3846     {
  3844     {
  3847       MutexLocker ml(Notify_lock);
  3845       MonitorLocker ml(Notify_lock);
  3848       os::start_thread(vmthread);
  3846       os::start_thread(vmthread);
  3849       while (vmthread->active_handles() == NULL) {
  3847       while (vmthread->active_handles() == NULL) {
  3850         Notify_lock->wait();
  3848         ml.wait();
  3851       }
  3849       }
  3852     }
  3850     }
  3853   }
  3851   }
  3854 
  3852 
  3855   assert(Universe::is_fully_initialized(), "not initialized");
  3853   assert(Universe::is_fully_initialized(), "not initialized");
  4323 
  4321 
  4324 #ifdef ASSERT
  4322 #ifdef ASSERT
  4325   _vm_complete = false;
  4323   _vm_complete = false;
  4326 #endif
  4324 #endif
  4327   // Wait until we are the last non-daemon thread to execute
  4325   // Wait until we are the last non-daemon thread to execute
  4328   { MutexLocker nu(Threads_lock);
  4326   { MonitorLocker nu(Threads_lock);
  4329     while (Threads::number_of_non_daemon_threads() > 1)
  4327     while (Threads::number_of_non_daemon_threads() > 1)
  4330       // This wait should make safepoint checks, wait without a timeout,
  4328       // This wait should make safepoint checks, wait without a timeout,
  4331       // and wait as a suspend-equivalent condition.
  4329       // and wait as a suspend-equivalent condition.
  4332       Threads_lock->wait(0, Mutex::_as_suspend_equivalent_flag);
  4330       nu.wait(0, Mutex::_as_suspend_equivalent_flag);
  4333   }
  4331   }
  4334 
  4332 
  4335   EventShutdown e;
  4333   EventShutdown e;
  4336   if (e.should_commit()) {
  4334   if (e.should_commit()) {
  4337     e.set_reason("No remaining non-daemon Java threads");
  4335     e.set_reason("No remaining non-daemon Java threads");
  4463   // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread.
  4461   // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread.
  4464   ObjectSynchronizer::omFlush(p);
  4462   ObjectSynchronizer::omFlush(p);
  4465 
  4463 
  4466   // Extra scope needed for Thread_lock, so we can check
  4464   // Extra scope needed for Thread_lock, so we can check
  4467   // that we do not remove thread without safepoint code notice
  4465   // that we do not remove thread without safepoint code notice
  4468   { MutexLocker ml(Threads_lock);
  4466   { MonitorLocker ml(Threads_lock);
  4469 
  4467 
  4470     assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
  4468     assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
  4471 
  4469 
  4472     // Maintain fast thread list
  4470     // Maintain fast thread list
  4473     ThreadsSMRSupport::remove_thread(p);
  4471     ThreadsSMRSupport::remove_thread(p);
  4491       _number_of_non_daemon_threads--;
  4489       _number_of_non_daemon_threads--;
  4492 
  4490 
  4493       // Only one thread left, do a notify on the Threads_lock so a thread waiting
  4491       // Only one thread left, do a notify on the Threads_lock so a thread waiting
  4494       // on destroy_vm will wake up.
  4492       // on destroy_vm will wake up.
  4495       if (number_of_non_daemon_threads() == 1) {
  4493       if (number_of_non_daemon_threads() == 1) {
  4496         Threads_lock->notify_all();
  4494         ml.notify_all();
  4497       }
  4495       }
  4498     }
  4496     }
  4499     ThreadService::remove_thread(p, is_daemon);
  4497     ThreadService::remove_thread(p, is_daemon);
  4500 
  4498 
  4501     // Make sure that safepoint code disregard this thread. This is needed since
  4499     // Make sure that safepoint code disregard this thread. This is needed since