src/hotspot/share/runtime/vmThread.cpp
changeset 54623 1126f0607c70
parent 54278 16999bd91ba6
child 54645 05aaccf7d558
equal deleted inserted replaced
54622:a8dcacf95bff 54623:1126f0607c70
   340     // VM thread to enter any lock at Safepoint as long as its _owner is NULL.
   340     // VM thread to enter any lock at Safepoint as long as its _owner is NULL.
   341     // If that happens after _terminate_lock->wait() has unset _owner
   341     // If that happens after _terminate_lock->wait() has unset _owner
   342     // but before it actually drops the lock and waits, the notification below
   342     // but before it actually drops the lock and waits, the notification below
   343     // may get lost and we will have a hang. To avoid this, we need to use
   343     // may get lost and we will have a hang. To avoid this, we need to use
   344     // Mutex::lock_without_safepoint_check().
   344     // Mutex::lock_without_safepoint_check().
   345     MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
   345     MutexLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
   346     _terminated = true;
   346     _terminated = true;
   347     _terminate_lock->notify();
   347     _terminate_lock->notify();
   348   }
   348   }
   349 
   349 
   350   // We are now racing with the VM termination being carried out in
   350   // We are now racing with the VM termination being carried out in
   357 // Notify the VMThread that the last non-daemon JavaThread has terminated,
   357 // Notify the VMThread that the last non-daemon JavaThread has terminated,
   358 // and wait until operation is performed.
   358 // and wait until operation is performed.
   359 void VMThread::wait_for_vm_thread_exit() {
   359 void VMThread::wait_for_vm_thread_exit() {
   360   assert(Thread::current()->is_Java_thread(), "Should be a JavaThread");
   360   assert(Thread::current()->is_Java_thread(), "Should be a JavaThread");
   361   assert(((JavaThread*)Thread::current())->is_terminated(), "Should be terminated");
   361   assert(((JavaThread*)Thread::current())->is_terminated(), "Should be terminated");
   362   { MutexLockerEx mu(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag);
   362   { MutexLocker mu(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag);
   363     _should_terminate = true;
   363     _should_terminate = true;
   364     VMOperationQueue_lock->notify();
   364     VMOperationQueue_lock->notify();
   365   }
   365   }
   366 
   366 
   367   // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
   367   // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
   371 
   371 
   372   // Wait until VM thread is terminated
   372   // Wait until VM thread is terminated
   373   // Note: it should be OK to use Terminator_lock here. But this is called
   373   // Note: it should be OK to use Terminator_lock here. But this is called
   374   // at a very delicate time (VM shutdown) and we are operating in non- VM
   374   // at a very delicate time (VM shutdown) and we are operating in non- VM
   375   // thread at Safepoint. It's safer to not share lock with other threads.
   375   // thread at Safepoint. It's safer to not share lock with other threads.
   376   { MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
   376   { MutexLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
   377     while(!VMThread::is_terminated()) {
   377     while(!VMThread::is_terminated()) {
   378         _terminate_lock->wait(Mutex::_no_safepoint_check_flag);
   378         _terminate_lock->wait_without_safepoint_check();
   379     }
   379     }
   380   }
   380   }
   381 }
   381 }
   382 
   382 
   383 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
   383 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
   474     VM_Operation* safepoint_ops = NULL;
   474     VM_Operation* safepoint_ops = NULL;
   475     //
   475     //
   476     // Wait for VM operation
   476     // Wait for VM operation
   477     //
   477     //
   478     // use no_safepoint_check to get lock without attempting to "sneak"
   478     // use no_safepoint_check to get lock without attempting to "sneak"
   479     { MutexLockerEx mu_queue(VMOperationQueue_lock,
   479     { MutexLocker mu_queue(VMOperationQueue_lock,
   480                              Mutex::_no_safepoint_check_flag);
   480                            Mutex::_no_safepoint_check_flag);
   481 
   481 
   482       // Look for new operation
   482       // Look for new operation
   483       assert(_cur_vm_operation == NULL, "no current one should be executing");
   483       assert(_cur_vm_operation == NULL, "no current one should be executing");
   484       _cur_vm_operation = _vm_queue->remove_next();
   484       _cur_vm_operation = _vm_queue->remove_next();
   485 
   485 
   492       }
   492       }
   493 
   493 
   494       while (!should_terminate() && _cur_vm_operation == NULL) {
   494       while (!should_terminate() && _cur_vm_operation == NULL) {
   495         // wait with a timeout to guarantee safepoints at regular intervals
   495         // wait with a timeout to guarantee safepoints at regular intervals
   496         bool timedout =
   496         bool timedout =
   497           VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
   497           VMOperationQueue_lock->wait_without_safepoint_check(GuaranteedSafepointInterval);
   498                                       GuaranteedSafepointInterval);
       
   499 
   498 
   500         // Support for self destruction
   499         // Support for self destruction
   501         if ((SelfDestructTimer != 0) && !VMError::is_error_reported() &&
   500         if ((SelfDestructTimer != 0) && !VMError::is_error_reported() &&
   502             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
   501             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
   503           tty->print_cr("VM self-destructed");
   502           tty->print_cr("VM self-destructed");
   505         }
   504         }
   506 
   505 
   507         if (timedout) {
   506         if (timedout) {
   508           // Have to unlock VMOperationQueue_lock just in case no_op_safepoint()
   507           // Have to unlock VMOperationQueue_lock just in case no_op_safepoint()
   509           // has to do a handshake.
   508           // has to do a handshake.
   510           MutexUnlockerEx mul(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag);
   509           MutexUnlocker mul(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag);
   511           if ((_cur_vm_operation = VMThread::no_op_safepoint()) != NULL) {
   510           if ((_cur_vm_operation = VMThread::no_op_safepoint()) != NULL) {
   512             // Force a safepoint since we have not had one for at least
   511             // Force a safepoint since we have not had one for at least
   513             // 'GuaranteedSafepointInterval' milliseconds and we need to clean
   512             // 'GuaranteedSafepointInterval' milliseconds and we need to clean
   514             // something. This will run all the clean-up processing that needs
   513             // something. This will run all the clean-up processing that needs
   515             // to be done at a safepoint.
   514             // to be done at a safepoint.
   582           // that simply means the op will wait for the next major cycle of the
   581           // that simply means the op will wait for the next major cycle of the
   583           // VMThread - just as it would if the GC thread lost the race for
   582           // VMThread - just as it would if the GC thread lost the race for
   584           // the lock.
   583           // the lock.
   585           if (_vm_queue->peek_at_safepoint_priority()) {
   584           if (_vm_queue->peek_at_safepoint_priority()) {
   586             // must hold lock while draining queue
   585             // must hold lock while draining queue
   587             MutexLockerEx mu_queue(VMOperationQueue_lock,
   586             MutexLocker mu_queue(VMOperationQueue_lock,
   588                                      Mutex::_no_safepoint_check_flag);
   587                                  Mutex::_no_safepoint_check_flag);
   589             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
   588             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
   590           } else {
   589           } else {
   591             safepoint_ops = NULL;
   590             safepoint_ops = NULL;
   592           }
   591           }
   593         } while(safepoint_ops != NULL);
   592         } while(safepoint_ops != NULL);
   624     }
   623     }
   625 
   624 
   626     //
   625     //
   627     //  Notify (potential) waiting Java thread(s) - lock without safepoint
   626     //  Notify (potential) waiting Java thread(s) - lock without safepoint
   628     //  check so that sneaking is not possible
   627     //  check so that sneaking is not possible
   629     { MutexLockerEx mu(VMOperationRequest_lock,
   628     { MutexLocker mu(VMOperationRequest_lock,
   630                        Mutex::_no_safepoint_check_flag);
   629                      Mutex::_no_safepoint_check_flag);
   631       VMOperationRequest_lock->notify_all();
   630       VMOperationRequest_lock->notify_all();
   632     }
   631     }
   633 
   632 
   634     // We want to make sure that we get to a safepoint regularly
   633     // We want to make sure that we get to a safepoint regularly
   635     // even when executing VMops that don't require safepoints.
   634     // even when executing VMops that don't require safepoints.
   719     if (!concurrent) {
   718     if (!concurrent) {
   720       // Wait for completion of request (non-concurrent)
   719       // Wait for completion of request (non-concurrent)
   721       // Note: only a JavaThread triggers the safepoint check when locking
   720       // Note: only a JavaThread triggers the safepoint check when locking
   722       MutexLocker mu(VMOperationRequest_lock);
   721       MutexLocker mu(VMOperationRequest_lock);
   723       while(t->vm_operation_completed_count() < ticket) {
   722       while(t->vm_operation_completed_count() < ticket) {
   724         VMOperationRequest_lock->wait(!t->is_Java_thread());
   723         if (t->is_Java_thread()) {
       
   724           VMOperationRequest_lock->wait();
       
   725         } else {
       
   726           VMOperationRequest_lock->wait_without_safepoint_check();
       
   727         }
   725       }
   728       }
   726     }
   729     }
   727 
   730 
   728     if (execute_epilog) {
   731     if (execute_epilog) {
   729       op->doit_epilogue();
   732       op->doit_epilogue();