@@ -465,9 +465,7 @@ HandshakeState::HandshakeState(JavaThread* target) :
465465 _queue(),
466466 _lock(Monitor::nosafepoint, " HandshakeState_lock" ),
467467 _active_handshaker(),
468- _async_exceptions_blocked(false ),
469- _suspended(false ),
470- _async_suspend_handshake(false ) {
468+ _async_exceptions_blocked(false ) {
471469}
472470
473471HandshakeState::~HandshakeState () {
@@ -699,128 +697,8 @@ HandshakeState::ProcessResult HandshakeState::try_process(HandshakeOperation* ma
699697 return op == match_op ? HandshakeState::_succeeded : HandshakeState::_processed;
700698}
701699
702- void HandshakeState::do_self_suspend () {
703- assert (Thread::current () == _handshakee, " should call from _handshakee" );
704- assert (_lock.owned_by_self (), " Lock must be held" );
705- assert (!_handshakee->has_last_Java_frame () || _handshakee->frame_anchor ()->walkable (), " should have walkable stack" );
706- assert (_handshakee->thread_state () == _thread_blocked, " Caller should have transitioned to _thread_blocked" );
707-
708- while (is_suspended ()) {
709- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " suspended" , p2i (_handshakee));
710- _lock.wait_without_safepoint_check ();
711- }
712- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " resumed" , p2i (_handshakee));
713- }
714-
715- // This is the closure that prevents a suspended JavaThread from
716- // escaping the suspend request.
717- class ThreadSelfSuspensionHandshake : public AsyncHandshakeClosure {
718- public:
719- ThreadSelfSuspensionHandshake () : AsyncHandshakeClosure(" ThreadSelfSuspensionHandshake" ) {}
720- void do_thread (Thread* thr) {
721- JavaThread* current = JavaThread::cast (thr);
722- assert (current == Thread::current (), " Must be self executed." );
723- JavaThreadState jts = current->thread_state ();
724-
725- current->set_thread_state (_thread_blocked);
726- current->handshake_state ()->do_self_suspend ();
727- current->set_thread_state (jts);
728- current->handshake_state ()->set_async_suspend_handshake (false );
729- }
730- virtual bool is_suspend () { return true ; }
731- };
732-
733- bool HandshakeState::suspend_with_handshake (bool register_vthread_SR) {
734- assert (_handshakee->threadObj () != nullptr , " cannot suspend with a null threadObj" );
735- if (_handshakee->is_exiting ()) {
736- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " exiting" , p2i (_handshakee));
737- return false ;
738- }
739- if (has_async_suspend_handshake ()) {
740- if (is_suspended ()) {
741- // Target is already suspended.
742- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " already suspended" , p2i (_handshakee));
743- return false ;
744- } else {
745- // Target is going to wake up and leave suspension.
746- // Let's just stop the thread from doing that.
747- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " re-suspended" , p2i (_handshakee));
748- set_suspended (true , register_vthread_SR);
749- return true ;
750- }
751- }
752- // no suspend request
753- assert (!is_suspended (), " cannot be suspended without a suspend request" );
754- // Thread is safe, so it must execute the request, thus we can count it as suspended
755- // from this point.
756- set_suspended (true , register_vthread_SR);
757- set_async_suspend_handshake (true );
758- log_trace (thread, suspend)(" JavaThread:" INTPTR_FORMAT " suspended, arming ThreadSuspension" , p2i (_handshakee));
759- ThreadSelfSuspensionHandshake* ts = new ThreadSelfSuspensionHandshake ();
760- Handshake::execute (ts, _handshakee);
761- return true ;
762- }
763-
764- // This is the closure that synchronously honors the suspend request.
765- class SuspendThreadHandshake : public HandshakeClosure {
766- bool _register_vthread_SR;
767- bool _did_suspend;
768- public:
769- SuspendThreadHandshake (bool register_vthread_SR) : HandshakeClosure(" SuspendThread" ),
770- _register_vthread_SR (register_vthread_SR), _did_suspend(false ) {}
771- void do_thread (Thread* thr) {
772- JavaThread* target = JavaThread::cast (thr);
773- _did_suspend = target->handshake_state ()->suspend_with_handshake (_register_vthread_SR);
774- }
775- bool did_suspend () { return _did_suspend; }
776- };
777-
778- bool HandshakeState::suspend (bool register_vthread_SR) {
779- JVMTI_ONLY (assert (!_handshakee->is_in_VTMS_transition (), " no suspend allowed in VTMS transition" );)
780- JavaThread* self = JavaThread::current ();
781- if (_handshakee == self) {
782- // If target is the current thread we can bypass the handshake machinery
783- // and just suspend directly
784- ThreadBlockInVM tbivm (self);
785- MutexLocker ml (&_lock, Mutex::_no_safepoint_check_flag);
786- set_suspended (true , register_vthread_SR);
787- do_self_suspend ();
788- return true ;
789- } else {
790- SuspendThreadHandshake st (register_vthread_SR);
791- Handshake::execute (&st, _handshakee);
792- return st.did_suspend ();
793- }
794- }
795-
796- bool HandshakeState::resume (bool register_vthread_SR) {
797- MutexLocker ml (&_lock, Mutex::_no_safepoint_check_flag);
798- if (!is_suspended ()) {
799- assert (!_handshakee->is_suspended (), " cannot be suspended without a suspend request" );
800- return false ;
801- }
802- // Resume the thread.
803- set_suspended (false , register_vthread_SR);
804- _lock.notify ();
805- return true ;
806- }
807-
808- void HandshakeState::set_suspended (bool is_suspend, bool register_vthread_SR) {
809- #if INCLUDE_JVMTI
810- if (register_vthread_SR) {
811- assert (_handshakee->is_vthread_mounted (), " sanity check" );
812- if (is_suspend) {
813- JvmtiVTSuspender::register_vthread_suspend (_handshakee->vthread ());
814- } else {
815- JvmtiVTSuspender::register_vthread_resume (_handshakee->vthread ());
816- }
817- }
818- #endif
819- Atomic::store (&_suspended, is_suspend);
820- }
821-
822700void HandshakeState::handle_unsafe_access_error () {
823- if (is_suspended ()) {
701+ if (_handshakee-> is_suspended ()) {
824702 // A suspend handshake was added to the queue after the
825703 // unsafe access error. Since the suspender has already
826704 // considered this JT as suspended and assumes it won't go
0 commit comments