src/share/vm/runtime/java.cpp

Print this page




 424   void set_next(ExitProc* next) { _next = next; }
 425 };
 426 
 427 
 428 // Linked list of registered on_exit procedures
 429 
 430 static ExitProc* exit_procs = NULL;
 431 
 432 
 433 extern "C" {
 434   void register_on_exit_function(void (*func)(void)) {
 435     ExitProc *entry = new ExitProc(func);
 436     // Classic vm does not throw an exception in case the allocation failed,
 437     if (entry != NULL) {
 438       entry->set_next(exit_procs);
 439       exit_procs = entry;
 440     }
 441   }
 442 }
 443 


 444 // Note: before_exit() can be executed only once, if more than one threads
 445 //       are trying to shutdown the VM at the same time, only one thread
 446 //       can run before_exit() and all other threads must wait.
 447 void before_exit(JavaThread * thread) {
 448   #define BEFORE_EXIT_NOT_RUN 0
 449   #define BEFORE_EXIT_RUNNING 1
 450   #define BEFORE_EXIT_DONE    2
 451   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
 452 
 453   // Note: don't use a Mutex to guard the entire before_exit(), as
 454   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
 455   // A CAS or OSMutex would work just fine but then we need to manipulate
 456   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
 457   // for synchronization.
 458   { MutexLocker ml(BeforeExit_lock);
 459     switch (_before_exit_status) {
 460     case BEFORE_EXIT_NOT_RUN:
 461       _before_exit_status = BEFORE_EXIT_RUNNING;
 462       break;
 463     case BEFORE_EXIT_RUNNING:
 464       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
 465         BeforeExit_lock->wait();
 466       }
 467       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
 468       return;
 469     case BEFORE_EXIT_DONE:
 470       return;
 471     }
 472   }


 473 
 474   // The only difference between this and Win32's _onexit procs is that
 475   // this version is invoked before any threads get killed.
 476   ExitProc* current = exit_procs;
 477   while (current != NULL) {
 478     ExitProc* next = current->next();
 479     current->evaluate();
 480     delete current;
 481     current = next;
 482   }
 483 
 484   // Hang forever on exit if we're reporting an error.
 485   if (ShowMessageBoxOnError && is_error_reported()) {
 486     os::infinite_sleep();
 487   }
 488 
 489   // Terminate watcher thread - must before disenrolling any periodic task
 490   if (PeriodicTask::num_tasks() > 0)
 491     WatcherThread::stop();
 492 




 424   void set_next(ExitProc* next) { _next = next; }
 425 };
 426 
 427 
 428 // Linked list of registered on_exit procedures
 429 
 430 static ExitProc* exit_procs = NULL;
 431 
 432 
 433 extern "C" {
 434   void register_on_exit_function(void (*func)(void)) {
 435     ExitProc *entry = new ExitProc(func);
 436     // Classic vm does not throw an exception in case the allocation failed,
 437     if (entry != NULL) {
 438       entry->set_next(exit_procs);
 439       exit_procs = entry;
 440     }
 441   }
 442 }
 443 
 444 int volatile vm_getting_terminated = 0;
 445 
 446 // Note: before_exit() can be executed only once, if more than one threads
 447 //       are trying to shutdown the VM at the same time, only one thread
 448 //       can run before_exit() and all other threads must wait.
 449 void before_exit(JavaThread * thread) {
 450   #define BEFORE_EXIT_NOT_RUN 0
 451   #define BEFORE_EXIT_RUNNING 1
 452   #define BEFORE_EXIT_DONE    2
 453   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
 454 
 455   // Note: don't use a Mutex to guard the entire before_exit(), as
 456   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
 457   // A CAS or OSMutex would work just fine but then we need to manipulate
 458   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
 459   // for synchronization.
 460   { MutexLocker ml(BeforeExit_lock);
 461     switch (_before_exit_status) {
 462     case BEFORE_EXIT_NOT_RUN:
 463       _before_exit_status = BEFORE_EXIT_RUNNING;
 464       break;
 465     case BEFORE_EXIT_RUNNING:
 466       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
 467         BeforeExit_lock->wait();
 468       }
 469       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
 470       return;
 471     case BEFORE_EXIT_DONE:
 472       return;
 473     }
 474   }
 475 
 476   vm_getting_terminated = 1;
 477 
 478   // The only difference between this and Win32's _onexit procs is that
 479   // this version is invoked before any threads get killed.
 480   ExitProc* current = exit_procs;
 481   while (current != NULL) {
 482     ExitProc* next = current->next();
 483     current->evaluate();
 484     delete current;
 485     current = next;
 486   }
 487 
 488   // Hang forever on exit if we're reporting an error.
 489   if (ShowMessageBoxOnError && is_error_reported()) {
 490     os::infinite_sleep();
 491   }
 492 
 493   // Terminate watcher thread - must before disenrolling any periodic task
 494   if (PeriodicTask::num_tasks() > 0)
 495     WatcherThread::stop();
 496