< prev index next >

src/hotspot/share/runtime/handshake.cpp

Print this page

420 void Handshake::execute(AsyncHandshakeClosure* hs_cl, JavaThread* target) {
421   jlong start_time_ns = os::javaTimeNanos();
422   AsyncHandshakeOperation* op = new AsyncHandshakeOperation(hs_cl, target, start_time_ns);
423 
424   guarantee(target != nullptr, "must be");
425 
426   Thread* current = Thread::current();
427   if (current != target) {
428     // Another thread is handling the request and it must be protecting
429     // the target.
430     guarantee(Thread::is_JavaThread_protected_by_TLH(target),
431               "missing ThreadsListHandle in calling context.");
432   }
433   // Implied else:
434   // The target is handling the request itself so it can't be dead.
435 
436   target->handshake_state()->add_operation(op);
437 }
438 
439 // Filters
440 static bool non_self_executable_filter(HandshakeOperation* op) {
441   return !op->is_async();
442 }
443 static bool no_async_exception_filter(HandshakeOperation* op) {
444   return !op->is_async_exception();
445 }
446 static bool async_exception_filter(HandshakeOperation* op) {
447   return op->is_async_exception();
448 }
449 static bool no_suspend_no_async_exception_filter(HandshakeOperation* op) {
450   return !op->is_suspend() && !op->is_async_exception();
451 }
452 static bool all_ops_filter(HandshakeOperation* op) {
453   return true;
454 }
455 
456 HandshakeState::HandshakeState(JavaThread* target) :
457   _handshakee(target),
458   _queue(),
459   _lock(Monitor::nosafepoint, "HandshakeState_lock"),
460   _active_handshaker(),
461   _async_exceptions_blocked(false),
462   _suspended(false),
463   _async_suspend_handshake(false) {
464 }
465 
466 HandshakeState::~HandshakeState() {
467   while (has_operation()) {
468     HandshakeOperation* op = _queue.pop(all_ops_filter);
469     guarantee(op->is_async(), "Only async operations may still be present on queue");
470     delete op;
471   }
472 }
473 
474 void HandshakeState::add_operation(HandshakeOperation* op) {
475   // Adds are done lock free and so is arming.
476   _queue.push(op);
477   SafepointMechanism::arm_local_poll_release(_handshakee);
478 }
479 
480 bool HandshakeState::operation_pending(HandshakeOperation* op) {
481   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
482   MatchOp mo(op);
483   return _queue.contains(mo);
484 }
485 
486 HandshakeOperation* HandshakeState::get_op_for_self(bool allow_suspend, bool check_async_exception) {
487   assert(_handshakee == Thread::current(), "Must be called by self");
488   assert(_lock.owned_by_self(), "Lock must be held");
489   assert(allow_suspend || !check_async_exception, "invalid case");
490 #if INCLUDE_JVMTI
491   if (allow_suspend && _handshakee->is_disable_suspend()) {
492     // filter out suspend operations while JavaThread is in disable_suspend mode
493     allow_suspend = false;
494   }
495 #endif
496   if (!allow_suspend) {
497     return _queue.peek(no_suspend_no_async_exception_filter);
498   } else if (check_async_exception && !_async_exceptions_blocked) {
499     return _queue.peek();
500   } else {
501     return _queue.peek(no_async_exception_filter);

515   }
516   return ret;
517 }
518 
519 bool HandshakeState::has_async_exception_operation() {
520   if (!has_operation()) return false;
521   ConditionalMutexLocker ml(&_lock, !_lock.owned_by_self(), Mutex::_no_safepoint_check_flag);
522   return _queue.peek(async_exception_filter) != nullptr;
523 }
524 
525 void HandshakeState::clean_async_exception_operation() {
526   while (has_async_exception_operation()) {
527     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
528     HandshakeOperation* op;
529     op = _queue.peek(async_exception_filter);
530     remove_op(op);
531     delete op;
532   }
533 }
534 
535 bool HandshakeState::have_non_self_executable_operation() {
536   assert(_handshakee != Thread::current(), "Must not be called by self");
537   assert(_lock.owned_by_self(), "Lock must be held");
538   return _queue.contains(non_self_executable_filter);
539 }
540 
541 HandshakeOperation* HandshakeState::get_op() {
542   assert(_handshakee != Thread::current(), "Must not be called by self");
543   assert(_lock.owned_by_self(), "Lock must be held");
544   return _queue.peek(non_self_executable_filter);
545 };
546 
547 void HandshakeState::remove_op(HandshakeOperation* op) {
548   assert(_lock.owned_by_self(), "Lock must be held");
549   MatchOp mo(op);
550   HandshakeOperation* ret = _queue.pop(mo);
551   assert(ret == op, "Popped op must match requested op");
552 };
553 
554 bool HandshakeState::process_by_self(bool allow_suspend, bool check_async_exception) {
555   assert(Thread::current() == _handshakee, "should call from _handshakee");
556   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
557 
558   _handshakee->frame_anchor()->make_walkable();
559   // Threads shouldn't block if they are in the middle of printing, but...
560   ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
561 
562   while (has_operation()) {
563     // Handshakes cannot safely safepoint. The exceptions to this rule are
564     // the asynchronous suspension and unsafe access error handshakes.
565     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
566 
567     HandshakeOperation* op = get_op_for_self(allow_suspend, check_async_exception);
568     if (op != nullptr) {
569       assert(op->_target == nullptr || op->_target == Thread::current(), "Wrong thread");
570       bool async = op->is_async();
571       log_trace(handshake)("Proc handshake %s " INTPTR_FORMAT " on " INTPTR_FORMAT " by self",
572                            async ? "asynchronous" : "synchronous", p2i(op), p2i(_handshakee));

608   case _thread_in_native:
609     // native threads are safe if they have no java stack or have walkable stack
610     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
611 
612   case _thread_blocked:
613     return true;
614 
615   default:
616     return false;
617   }
618 }
619 
620 bool HandshakeState::claim_handshake() {
621   if (!_lock.try_lock()) {
622     return false;
623   }
624   // Operations are added lock free and then the poll is armed.
625   // If all handshake operations for the handshakee are finished and someone
626   // just adds an operation we may see it here. But if the handshakee is not
627   // armed yet it is not safe to proceed.
628   if (have_non_self_executable_operation()) {
629     OrderAccess::loadload(); // Matches the implicit storestore in add_operation()
630     if (SafepointMechanism::local_poll_armed(_handshakee)) {
631       return true;
632     }
633   }
634   _lock.unlock();
635   return false;
636 }
637 
638 HandshakeState::ProcessResult HandshakeState::try_process(HandshakeOperation* match_op) {
639   if (!has_operation()) {
640     // JT has already cleared its handshake
641     return HandshakeState::_no_operation;
642   }
643 
644   if (!possibly_can_process_handshake()) {
645     // JT is observed in an unsafe state, it must notice the handshake itself
646     return HandshakeState::_not_safe;
647   }
648 
649   // Claim the mutex if there still an operation to be executed.
650   if (!claim_handshake()) {
651     return HandshakeState::_claim_failed;
652   }
653 
654   // If we own the mutex at this point and while owning the mutex we
655   // can observe a safe state the thread cannot possibly continue without
656   // getting caught by the mutex.
657   if (!can_process_handshake()) {
658     _lock.unlock();
659     return HandshakeState::_not_safe;
660   }
661 
662   Thread* current_thread = Thread::current();
663 
664   HandshakeOperation* op = get_op();
665 
666   assert(op != nullptr, "Must have an op");
667   assert(SafepointMechanism::local_poll_armed(_handshakee), "Must be");
668   assert(op->_target == nullptr || _handshakee == op->_target, "Wrong thread");
669 
670   log_trace(handshake)("Processing handshake " INTPTR_FORMAT " by %s(%s)", p2i(op),
671                        op == match_op ? "handshaker" : "cooperative",
672                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread");
673 
674   op->prepare(_handshakee, current_thread);
675 
676   set_active_handshaker(current_thread);
677   op->do_handshake(_handshakee); // acquire, op removed after
678   set_active_handshaker(nullptr);
679   remove_op(op);
680 
681   _lock.unlock();
682 
683   log_trace(handshake)("%s(" INTPTR_FORMAT ") executed an op for JavaThread: " INTPTR_FORMAT " %s target op: " INTPTR_FORMAT,
684                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread",

420 void Handshake::execute(AsyncHandshakeClosure* hs_cl, JavaThread* target) {
421   jlong start_time_ns = os::javaTimeNanos();
422   AsyncHandshakeOperation* op = new AsyncHandshakeOperation(hs_cl, target, start_time_ns);
423 
424   guarantee(target != nullptr, "must be");
425 
426   Thread* current = Thread::current();
427   if (current != target) {
428     // Another thread is handling the request and it must be protecting
429     // the target.
430     guarantee(Thread::is_JavaThread_protected_by_TLH(target),
431               "missing ThreadsListHandle in calling context.");
432   }
433   // Implied else:
434   // The target is handling the request itself so it can't be dead.
435 
436   target->handshake_state()->add_operation(op);
437 }
438 
439 // Filters
440 static bool handshaker_filter(HandshakeOperation* op) {
441   return !op->is_async();
442 }
443 static bool no_async_exception_filter(HandshakeOperation* op) {
444   return !op->is_async_exception();
445 }
446 static bool async_exception_filter(HandshakeOperation* op) {
447   return op->is_async_exception();
448 }
449 static bool no_suspend_no_async_exception_filter(HandshakeOperation* op) {
450   return !op->is_suspend() && !op->is_async_exception();
451 }
452 static bool all_ops_filter(HandshakeOperation* op) {
453   return true;
454 }
455 
456 HandshakeState::HandshakeState(JavaThread* target) :
457   _handshakee(target),
458   _queue(),
459   _lock(Monitor::nosafepoint, "HandshakeState_lock"),
460   _active_handshaker(),
461   _async_exceptions_blocked(false),
462   _suspended(false),
463   _async_suspend_handshake(false) {
464 }
465 
466 HandshakeState::~HandshakeState() {
467   while (has_operation()) {
468     HandshakeOperation* op = _queue.pop(all_ops_filter);
469     guarantee(op->is_async(), "Only async operations may still be present on queue");
470     delete op;
471   }
472 }
473 
474 void HandshakeState::add_operation(HandshakeOperation* op) {
475   // Adds are done lock free and so is arming.
476   _queue.push(op);
477   SafepointMechanism::arm_local_poll_release(_handshakee);
478 }
479 
480 bool HandshakeState::operation_pending(HandshakeOperation* op) {
481   ConditionalMutexLocker ml(&_lock, !_lock.owned_by_self(), Mutex::_no_safepoint_check_flag);
482   MatchOp mo(op);
483   return _queue.contains(mo);
484 }
485 
486 HandshakeOperation* HandshakeState::get_op_for_self(bool allow_suspend, bool check_async_exception) {
487   assert(_handshakee == Thread::current(), "Must be called by self");
488   assert(_lock.owned_by_self(), "Lock must be held");
489   assert(allow_suspend || !check_async_exception, "invalid case");
490 #if INCLUDE_JVMTI
491   if (allow_suspend && _handshakee->is_disable_suspend()) {
492     // filter out suspend operations while JavaThread is in disable_suspend mode
493     allow_suspend = false;
494   }
495 #endif
496   if (!allow_suspend) {
497     return _queue.peek(no_suspend_no_async_exception_filter);
498   } else if (check_async_exception && !_async_exceptions_blocked) {
499     return _queue.peek();
500   } else {
501     return _queue.peek(no_async_exception_filter);

515   }
516   return ret;
517 }
518 
519 bool HandshakeState::has_async_exception_operation() {
520   if (!has_operation()) return false;
521   ConditionalMutexLocker ml(&_lock, !_lock.owned_by_self(), Mutex::_no_safepoint_check_flag);
522   return _queue.peek(async_exception_filter) != nullptr;
523 }
524 
525 void HandshakeState::clean_async_exception_operation() {
526   while (has_async_exception_operation()) {
527     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
528     HandshakeOperation* op;
529     op = _queue.peek(async_exception_filter);
530     remove_op(op);
531     delete op;
532   }
533 }
534 
535 bool HandshakeState::has_handshaker_operation() {
536   assert(_handshakee != Thread::current(), "Must not be called by self");
537   assert(_lock.owned_by_self(), "Lock must be held");
538   return _queue.contains(handshaker_filter);
539 }
540 
541 HandshakeOperation* HandshakeState::get_op_for_handshaker() {
542   assert(_handshakee != Thread::current(), "Must not be called by self");
543   assert(_lock.owned_by_self(), "Lock must be held");
544   return _queue.peek(handshaker_filter);
545 }
546 
547 void HandshakeState::remove_op(HandshakeOperation* op) {
548   assert(_lock.owned_by_self(), "Lock must be held");
549   MatchOp mo(op);
550   HandshakeOperation* ret = _queue.pop(mo);
551   assert(ret == op, "Popped op must match requested op");
552 }
553 
554 bool HandshakeState::process_by_self(bool allow_suspend, bool check_async_exception) {
555   assert(Thread::current() == _handshakee, "should call from _handshakee");
556   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
557 
558   _handshakee->frame_anchor()->make_walkable();
559   // Threads shouldn't block if they are in the middle of printing, but...
560   ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
561 
562   while (has_operation()) {
563     // Handshakes cannot safely safepoint. The exceptions to this rule are
564     // the asynchronous suspension and unsafe access error handshakes.
565     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
566 
567     HandshakeOperation* op = get_op_for_self(allow_suspend, check_async_exception);
568     if (op != nullptr) {
569       assert(op->_target == nullptr || op->_target == Thread::current(), "Wrong thread");
570       bool async = op->is_async();
571       log_trace(handshake)("Proc handshake %s " INTPTR_FORMAT " on " INTPTR_FORMAT " by self",
572                            async ? "asynchronous" : "synchronous", p2i(op), p2i(_handshakee));

608   case _thread_in_native:
609     // native threads are safe if they have no java stack or have walkable stack
610     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
611 
612   case _thread_blocked:
613     return true;
614 
615   default:
616     return false;
617   }
618 }
619 
620 bool HandshakeState::claim_handshake() {
621   if (!_lock.try_lock()) {
622     return false;
623   }
624   // Operations are added lock free and then the poll is armed.
625   // If all handshake operations for the handshakee are finished and someone
626   // just adds an operation we may see it here. But if the handshakee is not
627   // armed yet it is not safe to proceed.
628   if (has_handshaker_operation()) {
629     OrderAccess::loadload(); // Matches the implicit storestore in add_operation()
630     if (SafepointMechanism::local_poll_armed(_handshakee)) {
631       return true;
632     }
633   }
634   _lock.unlock();
635   return false;
636 }
637 
638 HandshakeState::ProcessResult HandshakeState::try_process(HandshakeOperation* match_op) {
639   if (!has_operation()) {
640     // JT has already cleared its handshake
641     return HandshakeState::_no_operation;
642   }
643 
644   if (!possibly_can_process_handshake()) {
645     // JT is observed in an unsafe state, it must notice the handshake itself
646     return HandshakeState::_not_safe;
647   }
648 
649   // Claim the mutex if there still an operation to be executed.
650   if (!claim_handshake()) {
651     return HandshakeState::_claim_failed;
652   }
653 
654   // If we own the mutex at this point and while owning the mutex we
655   // can observe a safe state the thread cannot possibly continue without
656   // getting caught by the mutex.
657   if (!can_process_handshake()) {
658     _lock.unlock();
659     return HandshakeState::_not_safe;
660   }
661 
662   Thread* current_thread = Thread::current();
663 
664   HandshakeOperation* op = get_op_for_handshaker();
665 
666   assert(op != nullptr, "Must have an op");
667   assert(SafepointMechanism::local_poll_armed(_handshakee), "Must be");
668   assert(op->_target == nullptr || _handshakee == op->_target, "Wrong thread");
669 
670   log_trace(handshake)("Processing handshake " INTPTR_FORMAT " by %s(%s)", p2i(op),
671                        op == match_op ? "handshaker" : "cooperative",
672                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread");
673 
674   op->prepare(_handshakee, current_thread);
675 
676   set_active_handshaker(current_thread);
677   op->do_handshake(_handshakee); // acquire, op removed after
678   set_active_handshaker(nullptr);
679   remove_op(op);
680 
681   _lock.unlock();
682 
683   log_trace(handshake)("%s(" INTPTR_FORMAT ") executed an op for JavaThread: " INTPTR_FORMAT " %s target op: " INTPTR_FORMAT,
684                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread",
< prev index next >