446 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
447
448 // Change state first to _not_synchronized.
449 // No threads should see _synchronized when running.
450 _state = _not_synchronized;
451
452 // Set the next dormant (even) safepoint id.
453 assert((_safepoint_counter & 0x1) == 1, "must be odd");
454 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1);
455
456 OrderAccess::fence(); // Keep the local state from floating up.
457
458 jtiwh.rewind();
459 for (; JavaThread *current = jtiwh.next(); ) {
460 // Clear the visited flag to ensure that the critical counts are collected properly.
461 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);)
462 ThreadSafepointState* cur_state = current->safepoint_state();
463 assert(!cur_state->is_running(), "Thread not suspended at safepoint");
464 cur_state->restart(); // TSS _running
465 assert(cur_state->is_running(), "safepoint state has not been reset");
466
467 SafepointMechanism::disarm_if_needed(current, false /* NO release */);
468 }
469 } // ~JavaThreadIteratorWithHandle
470
471 // Release threads lock, so threads can be created/destroyed again.
472 Threads_lock->unlock();
473
474 // Wake threads after local state is correctly set.
475 _wait_barrier->disarm();
476 }
477
478 // Wake up all threads, so they are ready to resume execution after the safepoint
479 // operation has been carried out
480 void SafepointSynchronize::end() {
481 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
482 EventSafepointEnd event;
483 assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
484
485 disarm_safepoint();
486
487 Universe::heap()->safepoint_synchronize_end();
688 }
689
690 static bool safepoint_safe_with(JavaThread *thread, JavaThreadState state) {
691 switch(state) {
692 case _thread_in_native:
693 // native threads are safe if they have no java stack or have walkable stack
694 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
695
696 case _thread_blocked:
697 // On wait_barrier or blocked.
698 // Blocked threads should already have walkable stack.
699 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
700 return true;
701
702 default:
703 return false;
704 }
705 }
706
707 bool SafepointSynchronize::handshake_safe(JavaThread *thread) {
708 assert(Thread::current()->is_VM_thread(), "Must be VMThread");
709 if (thread->is_ext_suspended() || thread->is_terminated()) {
710 return true;
711 }
712 JavaThreadState stable_state;
713 if (try_stable_load_state(&stable_state, thread, InactiveSafepointCounter)) {
714 return safepoint_safe_with(thread, stable_state);
715 }
716 return false;
717 }
718
719 // See if the thread is running inside a lazy critical native and
720 // update the thread critical count if so. Also set a suspend flag to
721 // cause the native wrapper to return into the JVM to do the unlock
722 // once the native finishes.
723 static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) {
724 if (state == _thread_in_native &&
725 thread->has_last_Java_frame() &&
726 thread->frame_anchor()->walkable()) {
727 // This thread might be in a critical native nmethod so look at
728 // the top of the stack and increment the critical count if it
|
446 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
447
448 // Change state first to _not_synchronized.
449 // No threads should see _synchronized when running.
450 _state = _not_synchronized;
451
452 // Set the next dormant (even) safepoint id.
453 assert((_safepoint_counter & 0x1) == 1, "must be odd");
454 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1);
455
456 OrderAccess::fence(); // Keep the local state from floating up.
457
458 jtiwh.rewind();
459 for (; JavaThread *current = jtiwh.next(); ) {
460 // Clear the visited flag to ensure that the critical counts are collected properly.
461 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);)
462 ThreadSafepointState* cur_state = current->safepoint_state();
463 assert(!cur_state->is_running(), "Thread not suspended at safepoint");
464 cur_state->restart(); // TSS _running
465 assert(cur_state->is_running(), "safepoint state has not been reset");
466 }
467 } // ~JavaThreadIteratorWithHandle
468
469 // Release threads lock, so threads can be created/destroyed again.
470 Threads_lock->unlock();
471
472 // Wake threads after local state is correctly set.
473 _wait_barrier->disarm();
474 }
475
476 // Wake up all threads, so they are ready to resume execution after the safepoint
477 // operation has been carried out
478 void SafepointSynchronize::end() {
479 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
480 EventSafepointEnd event;
481 assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
482
483 disarm_safepoint();
484
485 Universe::heap()->safepoint_synchronize_end();
686 }
687
688 static bool safepoint_safe_with(JavaThread *thread, JavaThreadState state) {
689 switch(state) {
690 case _thread_in_native:
691 // native threads are safe if they have no java stack or have walkable stack
692 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
693
694 case _thread_blocked:
695 // On wait_barrier or blocked.
696 // Blocked threads should already have walkable stack.
697 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
698 return true;
699
700 default:
701 return false;
702 }
703 }
704
705 bool SafepointSynchronize::handshake_safe(JavaThread *thread) {
706 if (thread->is_ext_suspended() || thread->is_terminated()) {
707 return true;
708 }
709 JavaThreadState stable_state;
710 if (try_stable_load_state(&stable_state, thread, InactiveSafepointCounter)) {
711 return safepoint_safe_with(thread, stable_state);
712 }
713 return false;
714 }
715
716 // See if the thread is running inside a lazy critical native and
717 // update the thread critical count if so. Also set a suspend flag to
718 // cause the native wrapper to return into the JVM to do the unlock
719 // once the native finishes.
720 static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) {
721 if (state == _thread_in_native &&
722 thread->has_last_Java_frame() &&
723 thread->frame_anchor()->walkable()) {
724 // This thread might be in a critical native nmethod so look at
725 // the top of the stack and increment the critical count if it
|