@@ -327,15 +327,16 @@ void ObjectMonitor::set_object_strong() {
327327 }
328328}
329329
330- void ObjectMonitor::ExitOnSuspend::operator ()(JavaThread* current ) {
331- if (current ->is_suspended ()) {
330+ ObjectMonitor::ExitOnSuspend::~ExitOnSuspend ( ) {
331+ if (_current ->is_suspended ()) {
332332 _om->_recursions = 0 ;
333333 _om->clear_successor ();
334334 // Don't need a full fence after clearing successor here because of the call to exit().
335- _om->exit (current , false /* not_suspended */ );
335+ _om->exit (_current , false /* not_suspended */ );
336336 _om_exited = true ;
337-
338- current->set_current_pending_monitor (_om);
337+ _current->set_current_pending_monitor (_om);
338+ // Process suspend request now
339+ SafepointMechanism::process_if_requested (_current, true /* allow_suspend*/ , false /* check_async_exception*/ );
339340 }
340341}
341342
@@ -590,9 +591,9 @@ void ObjectMonitor::enter_with_contention_mark(JavaThread* current, ObjectMonito
590591 assert (current->thread_state () == _thread_in_vm, " invariant" );
591592
592593 for (;;) {
593- ExitOnSuspend eos ( this ) ;
594+ bool om_exited = false ;
594595 {
595- ThreadBlockInVMPreprocess< ExitOnSuspend> tbivs (current, eos, true /* allow_suspend */ );
596+ ExitOnSuspend eos (current, this , om_exited );
596597 enter_internal (current);
597598 current->set_current_pending_monitor (nullptr );
598599 // We can go to a safepoint at the end of this block. If we
@@ -604,7 +605,7 @@ void ObjectMonitor::enter_with_contention_mark(JavaThread* current, ObjectMonito
604605 // and set the OM as pending, the thread will not be reported as
605606 // having "-locked" the monitor.
606607 }
607- if (!eos. exited () ) {
608+ if (!om_exited ) {
608609 // ExitOnSuspend did not exit the OM
609610 assert (has_owner (current), " invariant" );
610611 break ;
@@ -938,7 +939,7 @@ const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
938939}
939940
940941void ObjectMonitor::enter_internal (JavaThread* current) {
941- assert (current->thread_state () == _thread_blocked , " invariant" );
942+ assert (current->thread_state () == _thread_in_vm , " invariant" );
942943
943944 // Try the lock - TATAS
944945 if (try_lock (current) == TryLockResult::Success) {
@@ -1014,16 +1015,19 @@ void ObjectMonitor::enter_internal(JavaThread* current) {
10141015 }
10151016 assert (!has_owner (current), " invariant" );
10161017
1017- // park self
1018- if (do_timed_parked) {
1019- current->_ParkEvent ->park (recheck_interval);
1020- // Increase the recheck_interval, but clamp the value.
1021- recheck_interval *= 8 ;
1022- if (recheck_interval > MAX_RECHECK_INTERVAL) {
1023- recheck_interval = MAX_RECHECK_INTERVAL;
1018+ {
1019+ ThreadBlockInVM tbivm (current);
1020+ // park self
1021+ if (do_timed_parked) {
1022+ current->_ParkEvent ->park (recheck_interval);
1023+ // Increase the recheck_interval, but clamp the value.
1024+ recheck_interval *= 8 ;
1025+ if (recheck_interval > MAX_RECHECK_INTERVAL) {
1026+ recheck_interval = MAX_RECHECK_INTERVAL;
1027+ }
1028+ } else {
1029+ current->_ParkEvent ->park ();
10241030 }
1025- } else {
1026- current->_ParkEvent ->park ();
10271031 }
10281032
10291033 if (try_lock (current) == TryLockResult::Success) {
@@ -1094,7 +1098,7 @@ void ObjectMonitor::enter_internal(JavaThread* current) {
10941098
10951099void ObjectMonitor::reenter_internal (JavaThread* current, ObjectWaiter* currentNode) {
10961100 assert (current != nullptr , " invariant" );
1097- assert (current->thread_state () == _thread_blocked , " invariant" );
1101+ assert (current->thread_state () == _thread_in_vm , " invariant" );
10981102 assert (currentNode != nullptr , " invariant" );
10991103 assert (currentNode->_thread == current, " invariant" );
11001104 assert (_waiters > 0 , " invariant" );
@@ -1132,6 +1136,7 @@ void ObjectMonitor::reenter_internal(JavaThread* current, ObjectWaiter* currentN
11321136 }
11331137
11341138 {
1139+ ThreadBlockInVM tbivm (current);
11351140 OSThreadContendState osts (current->osthread ());
11361141 if (do_timed_parked) {
11371142 current->_ParkEvent ->park (recheck_interval);
@@ -1644,6 +1649,7 @@ void ObjectMonitor::exit_epilog(JavaThread* current, ObjectWaiter* Wakee) {
16441649 Trigger = t->_ParkEvent ;
16451650 set_successor (t);
16461651 } else {
1652+ assert_not_at_safepoint ();
16471653 vthread = Wakee->vthread ();
16481654 assert (vthread != nullptr , " " );
16491655 Trigger = ObjectMonitor::vthread_unparker_ParkEvent ();
@@ -1962,9 +1968,9 @@ void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
19621968 // This means the thread has been un-parked and added to the entry_list
19631969 // in notify_internal, i.e. notified while waiting.
19641970 guarantee (v == ObjectWaiter::TS_ENTER, " invariant" );
1965- ExitOnSuspend eos ( this ) ;
1971+ bool om_exited = false ;
19661972 {
1967- ThreadBlockInVMPreprocess< ExitOnSuspend> tbivs (current, eos, true /* allow_suspend */ );
1973+ ExitOnSuspend eos (current, this , om_exited );
19681974 reenter_internal (current, &node);
19691975 // We can go to a safepoint at the end of this block. If we
19701976 // do a thread dump during that safepoint, then this thread will show
@@ -1975,7 +1981,7 @@ void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
19751981 // and set the OM as pending, the thread will not be reported as
19761982 // having "-locked" the monitor.
19771983 }
1978- if (eos. exited () ) {
1984+ if (om_exited ) {
19791985 // ExitOnSuspend exit the OM
19801986 assert (!has_owner (current), " invariant" );
19811987 guarantee (node.TState == ObjectWaiter::TS_RUN, " invariant" );
@@ -2396,6 +2402,8 @@ bool ObjectMonitor::short_fixed_spin(JavaThread* current, int spin_count, bool a
23962402
23972403// Spinning: Fixed frequency (100%), vary duration
23982404bool ObjectMonitor::try_spin (JavaThread* current) {
2405+ assert (current->thread_state () == _thread_in_vm || current->thread_state () == _thread_in_Java, " " );
2406+ bool from_java = current->thread_state () == _thread_in_Java;
23992407
24002408 // Dumb, brutal spin. Good for comparative measurements against adaptive spinning.
24012409 int knob_fixed_spin = Knob_FixedSpin; // 0 (don't spin: default), 2000 good test
@@ -2426,6 +2434,13 @@ bool ObjectMonitor::try_spin(JavaThread* current) {
24262434 int ctr = _SpinDuration;
24272435 if (ctr <= 0 ) return false ;
24282436
2437+ // Guarantee that we at least poll once if main
2438+ // spinning loop is executed.
2439+ if (SafepointMechanism::local_poll_armed (current)) {
2440+ if (from_java) return false ;
2441+ ThreadBlockInVM tbivm (current);
2442+ }
2443+
24292444 // We're good to spin ... spin ingress.
24302445 // CONSIDER: use Prefetch::write() to avoid RTS->RTO upgrades
24312446 // when preparing to LD...CAS _owner, etc and the CAS is likely
@@ -2452,11 +2467,9 @@ bool ObjectMonitor::try_spin(JavaThread* current) {
24522467 // This is in keeping with the "no loitering in runtime" rule.
24532468 // We periodically check to see if there's a safepoint pending.
24542469 if ((ctr & 0xFF ) == 0 ) {
2455- // Can't call SafepointMechanism::should_process() since that
2456- // might update the poll values and we could be in a thread_blocked
2457- // state here which is not allowed so just check the poll.
24582470 if (SafepointMechanism::local_poll_armed (current)) {
2459- break ;
2471+ if (from_java) break ;
2472+ ThreadBlockInVM tbivm (current);
24602473 }
24612474 SpinPause ();
24622475 }
0 commit comments