311 // the VM thread issues a memory barrier instruction.
312 // 3. Running compiled Code
313 // Compiled code reads the local polling page that
314 // is set to fault if we are trying to get to a safepoint.
315 // 4. Blocked
316 // A thread which is blocked will not be allowed to return from the
317 // block condition until the safepoint operation is complete.
318 // 5. In VM or Transitioning between states
319 // If a Java thread is currently running in the VM or transitioning
320 // between states, the safepointing code will poll the thread state
321 // until the thread blocks itself when it attempts transitions to a
322 // new state or locking a safepoint checked monitor.
323
324 // We must never miss a thread with correct safepoint id, so we must make sure we arm
325 // the wait barrier for the next safepoint id/counter.
326 // Arming must be done after resetting _current_jni_active_count, _waiting_to_block.
327 _wait_barrier->arm(static_cast<int>(_safepoint_counter + 1));
328
329 assert((_safepoint_counter & 0x1) == 0, "must be even");
330 // The store to _safepoint_counter must happen after any stores in arming.
331 OrderAccess::release_store(&_safepoint_counter, _safepoint_counter + 1);
332
333 // We are synchronizing
334 OrderAccess::storestore(); // Ordered with _safepoint_counter
335 _state = _synchronizing;
336
337 if (SafepointMechanism::uses_thread_local_poll()) {
338 // Arming the per thread poll while having _state != _not_synchronized means safepointing
339 log_trace(safepoint)("Setting thread local yield flag for threads");
340 OrderAccess::storestore(); // storestore, global state -> local state
341 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
342 // Make sure the threads start polling, it is time to yield.
343 SafepointMechanism::arm_local_poll(cur);
344 }
345 }
346 OrderAccess::fence(); // storestore|storeload, global state -> local state
347
348 if (SafepointMechanism::uses_global_page_poll()) {
349 // Make interpreter safepoint aware
350 Interpreter::notice_safepoints();
351
465 #endif // ASSERT
466
467 if (SafepointMechanism::uses_global_page_poll()) {
468 guarantee (PageArmed, "invariant");
469 // Make polling safepoint aware
470 os::make_polling_page_readable();
471 PageArmed = false;
472 // Remove safepoint check from interpreter
473 Interpreter::ignore_safepoints();
474 }
475
476 OrderAccess::fence(); // keep read and write of _state from floating up
477 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
478
479 // Change state first to _not_synchronized.
480 // No threads should see _synchronized when running.
481 _state = _not_synchronized;
482
483 // Set the next dormant (even) safepoint id.
484 assert((_safepoint_counter & 0x1) == 1, "must be odd");
485 OrderAccess::release_store(&_safepoint_counter, _safepoint_counter + 1);
486
487 OrderAccess::fence(); // Keep the local state from floating up.
488
489 jtiwh.rewind();
490 for (; JavaThread *current = jtiwh.next(); ) {
491 // Clear the visited flag to ensure that the critical counts are collected properly.
492 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);)
493 ThreadSafepointState* cur_state = current->safepoint_state();
494 assert(!cur_state->is_running(), "Thread not suspended at safepoint");
495 cur_state->restart(); // TSS _running
496 assert(cur_state->is_running(), "safepoint state has not been reset");
497
498 SafepointMechanism::disarm_if_needed(current, false /* NO release */);
499 }
500 } // ~JavaThreadIteratorWithHandle
501
502 // Release threads lock, so threads can be created/destroyed again.
503 Threads_lock->unlock();
504
505 // Wake threads after local state is correctly set.
951 // Implementation of ThreadSafepointState
952
953 ThreadSafepointState::ThreadSafepointState(JavaThread *thread)
954 : _at_poll_safepoint(false), _thread(thread), _safepoint_safe(false),
955 _safepoint_id(SafepointSynchronize::InactiveSafepointCounter), _next(NULL) {
956 }
957
958 void ThreadSafepointState::create(JavaThread *thread) {
959 ThreadSafepointState *state = new ThreadSafepointState(thread);
960 thread->set_safepoint_state(state);
961 }
962
963 void ThreadSafepointState::destroy(JavaThread *thread) {
964 if (thread->safepoint_state()) {
965 delete(thread->safepoint_state());
966 thread->set_safepoint_state(NULL);
967 }
968 }
969
970 uint64_t ThreadSafepointState::get_safepoint_id() const {
971 return OrderAccess::load_acquire(&_safepoint_id);
972 }
973
974 void ThreadSafepointState::reset_safepoint_id() {
975 OrderAccess::release_store(&_safepoint_id, SafepointSynchronize::InactiveSafepointCounter);
976 }
977
978 void ThreadSafepointState::set_safepoint_id(uint64_t safepoint_id) {
979 OrderAccess::release_store(&_safepoint_id, safepoint_id);
980 }
981
982 void ThreadSafepointState::examine_state_of_thread(uint64_t safepoint_count) {
983 assert(is_running(), "better be running or just have hit safepoint poll");
984
985 JavaThreadState stable_state;
986 if (!SafepointSynchronize::try_stable_load_state(&stable_state, _thread, safepoint_count)) {
987 // We could not get stable state of the JavaThread.
988 // Consider it running and just return.
989 return;
990 }
991
992 // Check for a thread that is suspended. Note that thread resume tries
993 // to grab the Threads_lock which we own here, so a thread cannot be
994 // resumed during safepoint synchronization.
995
996 // We check to see if this thread is suspended without locking to
997 // avoid deadlocking with a third thread that is waiting for this
998 // thread to be suspended. The third thread can notice the safepoint
999 // that we're trying to start at the beginning of its SR_lock->wait()
|
311 // the VM thread issues a memory barrier instruction.
312 // 3. Running compiled Code
313 // Compiled code reads the local polling page that
314 // is set to fault if we are trying to get to a safepoint.
315 // 4. Blocked
316 // A thread which is blocked will not be allowed to return from the
317 // block condition until the safepoint operation is complete.
318 // 5. In VM or Transitioning between states
319 // If a Java thread is currently running in the VM or transitioning
320 // between states, the safepointing code will poll the thread state
321 // until the thread blocks itself when it attempts transitions to a
322 // new state or locking a safepoint checked monitor.
323
324 // We must never miss a thread with correct safepoint id, so we must make sure we arm
325 // the wait barrier for the next safepoint id/counter.
326 // Arming must be done after resetting _current_jni_active_count, _waiting_to_block.
327 _wait_barrier->arm(static_cast<int>(_safepoint_counter + 1));
328
329 assert((_safepoint_counter & 0x1) == 0, "must be even");
330 // The store to _safepoint_counter must happen after any stores in arming.
331 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1);
332
333 // We are synchronizing
334 OrderAccess::storestore(); // Ordered with _safepoint_counter
335 _state = _synchronizing;
336
337 if (SafepointMechanism::uses_thread_local_poll()) {
338 // Arming the per thread poll while having _state != _not_synchronized means safepointing
339 log_trace(safepoint)("Setting thread local yield flag for threads");
340 OrderAccess::storestore(); // storestore, global state -> local state
341 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
342 // Make sure the threads start polling, it is time to yield.
343 SafepointMechanism::arm_local_poll(cur);
344 }
345 }
346 OrderAccess::fence(); // storestore|storeload, global state -> local state
347
348 if (SafepointMechanism::uses_global_page_poll()) {
349 // Make interpreter safepoint aware
350 Interpreter::notice_safepoints();
351
465 #endif // ASSERT
466
467 if (SafepointMechanism::uses_global_page_poll()) {
468 guarantee (PageArmed, "invariant");
469 // Make polling safepoint aware
470 os::make_polling_page_readable();
471 PageArmed = false;
472 // Remove safepoint check from interpreter
473 Interpreter::ignore_safepoints();
474 }
475
476 OrderAccess::fence(); // keep read and write of _state from floating up
477 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
478
479 // Change state first to _not_synchronized.
480 // No threads should see _synchronized when running.
481 _state = _not_synchronized;
482
483 // Set the next dormant (even) safepoint id.
484 assert((_safepoint_counter & 0x1) == 1, "must be odd");
485 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1);
486
487 OrderAccess::fence(); // Keep the local state from floating up.
488
489 jtiwh.rewind();
490 for (; JavaThread *current = jtiwh.next(); ) {
491 // Clear the visited flag to ensure that the critical counts are collected properly.
492 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);)
493 ThreadSafepointState* cur_state = current->safepoint_state();
494 assert(!cur_state->is_running(), "Thread not suspended at safepoint");
495 cur_state->restart(); // TSS _running
496 assert(cur_state->is_running(), "safepoint state has not been reset");
497
498 SafepointMechanism::disarm_if_needed(current, false /* NO release */);
499 }
500 } // ~JavaThreadIteratorWithHandle
501
502 // Release threads lock, so threads can be created/destroyed again.
503 Threads_lock->unlock();
504
505 // Wake threads after local state is correctly set.
951 // Implementation of ThreadSafepointState
952
953 ThreadSafepointState::ThreadSafepointState(JavaThread *thread)
954 : _at_poll_safepoint(false), _thread(thread), _safepoint_safe(false),
955 _safepoint_id(SafepointSynchronize::InactiveSafepointCounter), _next(NULL) {
956 }
957
958 void ThreadSafepointState::create(JavaThread *thread) {
959 ThreadSafepointState *state = new ThreadSafepointState(thread);
960 thread->set_safepoint_state(state);
961 }
962
963 void ThreadSafepointState::destroy(JavaThread *thread) {
964 if (thread->safepoint_state()) {
965 delete(thread->safepoint_state());
966 thread->set_safepoint_state(NULL);
967 }
968 }
969
970 uint64_t ThreadSafepointState::get_safepoint_id() const {
971 return Atomic::load_acquire(&_safepoint_id);
972 }
973
974 void ThreadSafepointState::reset_safepoint_id() {
975 Atomic::release_store(&_safepoint_id, SafepointSynchronize::InactiveSafepointCounter);
976 }
977
978 void ThreadSafepointState::set_safepoint_id(uint64_t safepoint_id) {
979 Atomic::release_store(&_safepoint_id, safepoint_id);
980 }
981
982 void ThreadSafepointState::examine_state_of_thread(uint64_t safepoint_count) {
983 assert(is_running(), "better be running or just have hit safepoint poll");
984
985 JavaThreadState stable_state;
986 if (!SafepointSynchronize::try_stable_load_state(&stable_state, _thread, safepoint_count)) {
987 // We could not get stable state of the JavaThread.
988 // Consider it running and just return.
989 return;
990 }
991
992 // Check for a thread that is suspended. Note that thread resume tries
993 // to grab the Threads_lock which we own here, so a thread cannot be
994 // resumed during safepoint synchronization.
995
996 // We check to see if this thread is suspended without locking to
997 // avoid deadlocking with a third thread that is waiting for this
998 // thread to be suspended. The third thread can notice the safepoint
999 // that we're trying to start at the beginning of its SR_lock->wait()
|