413 void set_next(ExitProc* next) { _next = next; }
414 };
415
416
417 // Linked list of registered on_exit procedures
418
419 static ExitProc* exit_procs = NULL;
420
421
422 extern "C" {
423 void register_on_exit_function(void (*func)(void)) {
424 ExitProc *entry = new ExitProc(func);
425 // Classic vm does not throw an exception in case the allocation failed,
426 if (entry != NULL) {
427 entry->set_next(exit_procs);
428 exit_procs = entry;
429 }
430 }
431 }
432
433 jint volatile vm_getting_terminated = 0;
434
435 // Note: before_exit() can be executed only once, if more than one threads
436 // are trying to shutdown the VM at the same time, only one thread
437 // can run before_exit() and all other threads must wait.
438 void before_exit(JavaThread * thread) {
439 #define BEFORE_EXIT_NOT_RUN 0
440 #define BEFORE_EXIT_RUNNING 1
441 #define BEFORE_EXIT_DONE 2
442 static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
443
444 // Note: don't use a Mutex to guard the entire before_exit(), as
445 // JVMTI post_thread_end_event and post_vm_death_event will run native code.
446 // A CAS or OSMutex would work just fine but then we need to manipulate
447 // thread state for Safepoint. Here we use Monitor wait() and notify_all()
448 // for synchronization.
449 { MutexLocker ml(BeforeExit_lock);
450 switch (_before_exit_status) {
451 case BEFORE_EXIT_NOT_RUN:
452 _before_exit_status = BEFORE_EXIT_RUNNING;
453 break;
454 case BEFORE_EXIT_RUNNING:
455 while (_before_exit_status == BEFORE_EXIT_RUNNING) {
456 BeforeExit_lock->wait();
457 }
458 assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
459 return;
460 case BEFORE_EXIT_DONE:
461 return;
462 }
463 }
464
465 OrderAccess::release_store(&vm_getting_terminated, 1);
466
467 // The only difference between this and Win32's _onexit procs is that
468 // this version is invoked before any threads get killed.
469 ExitProc* current = exit_procs;
470 while (current != NULL) {
471 ExitProc* next = current->next();
472 current->evaluate();
473 delete current;
474 current = next;
475 }
476
477 // Hang forever on exit if we're reporting an error.
478 if (ShowMessageBoxOnError && is_error_reported()) {
479 os::infinite_sleep();
480 }
481
482 // Terminate watcher thread - must before disenrolling any periodic task
483 if (PeriodicTask::num_tasks() > 0)
484 WatcherThread::stop();
485
486 // Print statistics gathered (profiling ...)
570 ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
571 VMThread::execute(&op);
572 // should never reach here; but in case something wrong with VM Thread.
573 vm_direct_exit(code);
574 } else {
575 // VM thread is gone, just exit
576 vm_direct_exit(code);
577 }
578 ShouldNotReachHere();
579 }
580
581 void notify_vm_shutdown() {
582 // For now, just a dtrace probe.
583 HOTSPOT_VM_SHUTDOWN();
584 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
585 }
586
587 void vm_direct_exit(int code) {
588 notify_vm_shutdown();
589 os::wait_for_keypress_at_exit();
590 ::exit(code);
591 }
592
593 void vm_perform_shutdown_actions() {
594 // Warning: do not call 'exit_globals()' here. All threads are still running.
595 // Calling 'exit_globals()' will disable thread-local-storage and cause all
596 // kinds of assertions to trigger in debug mode.
597 if (is_init_completed()) {
598 Thread* thread = ThreadLocalStorage::is_initialized() ?
599 ThreadLocalStorage::get_thread_slow() : NULL;
600 if (thread != NULL && thread->is_Java_thread()) {
601 // We are leaving the VM, set state to native (in case any OS exit
602 // handlers call back to the VM)
603 JavaThread* jt = (JavaThread*)thread;
604 // Must always be walkable or have no last_Java_frame when in
605 // thread_in_native
606 jt->frame_anchor()->make_walkable(jt);
607 jt->set_thread_state(_thread_in_native);
608 }
609 }
610 notify_vm_shutdown();
|
413 void set_next(ExitProc* next) { _next = next; }
414 };
415
416
417 // Linked list of registered on_exit procedures
418
419 static ExitProc* exit_procs = NULL;
420
421
422 extern "C" {
423 void register_on_exit_function(void (*func)(void)) {
424 ExitProc *entry = new ExitProc(func);
425 // Classic vm does not throw an exception in case the allocation failed,
426 if (entry != NULL) {
427 entry->set_next(exit_procs);
428 exit_procs = entry;
429 }
430 }
431 }
432
433 // Note: before_exit() can be executed only once, if more than one threads
434 // are trying to shutdown the VM at the same time, only one thread
435 // can run before_exit() and all other threads must wait.
436 void before_exit(JavaThread * thread) {
437 #define BEFORE_EXIT_NOT_RUN 0
438 #define BEFORE_EXIT_RUNNING 1
439 #define BEFORE_EXIT_DONE 2
440 static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
441
442 // Note: don't use a Mutex to guard the entire before_exit(), as
443 // JVMTI post_thread_end_event and post_vm_death_event will run native code.
444 // A CAS or OSMutex would work just fine but then we need to manipulate
445 // thread state for Safepoint. Here we use Monitor wait() and notify_all()
446 // for synchronization.
447 { MutexLocker ml(BeforeExit_lock);
448 switch (_before_exit_status) {
449 case BEFORE_EXIT_NOT_RUN:
450 _before_exit_status = BEFORE_EXIT_RUNNING;
451 break;
452 case BEFORE_EXIT_RUNNING:
453 while (_before_exit_status == BEFORE_EXIT_RUNNING) {
454 BeforeExit_lock->wait();
455 }
456 assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
457 return;
458 case BEFORE_EXIT_DONE:
459 return;
460 }
461 }
462
463 // The only difference between this and Win32's _onexit procs is that
464 // this version is invoked before any threads get killed.
465 ExitProc* current = exit_procs;
466 while (current != NULL) {
467 ExitProc* next = current->next();
468 current->evaluate();
469 delete current;
470 current = next;
471 }
472
473 // Hang forever on exit if we're reporting an error.
474 if (ShowMessageBoxOnError && is_error_reported()) {
475 os::infinite_sleep();
476 }
477
478 // Terminate watcher thread - must before disenrolling any periodic task
479 if (PeriodicTask::num_tasks() > 0)
480 WatcherThread::stop();
481
482 // Print statistics gathered (profiling ...)
566 ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
567 VMThread::execute(&op);
568 // should never reach here; but in case something wrong with VM Thread.
569 vm_direct_exit(code);
570 } else {
571 // VM thread is gone, just exit
572 vm_direct_exit(code);
573 }
574 ShouldNotReachHere();
575 }
576
577 void notify_vm_shutdown() {
578 // For now, just a dtrace probe.
579 HOTSPOT_VM_SHUTDOWN();
580 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
581 }
582
583 void vm_direct_exit(int code) {
584 notify_vm_shutdown();
585 os::wait_for_keypress_at_exit();
586 os::exit(code);
587 }
588
589 void vm_perform_shutdown_actions() {
590 // Warning: do not call 'exit_globals()' here. All threads are still running.
591 // Calling 'exit_globals()' will disable thread-local-storage and cause all
592 // kinds of assertions to trigger in debug mode.
593 if (is_init_completed()) {
594 Thread* thread = ThreadLocalStorage::is_initialized() ?
595 ThreadLocalStorage::get_thread_slow() : NULL;
596 if (thread != NULL && thread->is_Java_thread()) {
597 // We are leaving the VM, set state to native (in case any OS exit
598 // handlers call back to the VM)
599 JavaThread* jt = (JavaThread*)thread;
600 // Must always be walkable or have no last_Java_frame when in
601 // thread_in_native
602 jt->frame_anchor()->make_walkable(jt);
603 jt->set_thread_state(_thread_in_native);
604 }
605 }
606 notify_vm_shutdown();
|