@@ -633,7 +633,7 @@ void LightweightSynchronizer::enter_for(Handle obj, BasicLock* lock, JavaThread*
633633 } else {
634634 do {
635635 // It is assumed that enter_for must enter on an object without contention.
636- monitor = inflate_and_enter (obj (), ObjectSynchronizer::inflate_cause_monitor_enter, locking_thread, current);
636+ monitor = inflate_and_enter (obj (), lock, ObjectSynchronizer::inflate_cause_monitor_enter, locking_thread, current);
637637 // But there may still be a race with deflation.
638638 } while (monitor == nullptr );
639639 }
@@ -689,7 +689,7 @@ void LightweightSynchronizer::enter(Handle obj, BasicLock* lock, JavaThread* cur
689689 spin_yield.wait ();
690690 }
691691
692- ObjectMonitor* monitor = inflate_and_enter (obj (), ObjectSynchronizer::inflate_cause_monitor_enter, current, current);
692+ ObjectMonitor* monitor = inflate_and_enter (obj (), lock, ObjectSynchronizer::inflate_cause_monitor_enter, current, current);
693693 if (monitor != nullptr ) {
694694 cache_setter.set_monitor (monitor);
695695 return ;
@@ -703,7 +703,7 @@ void LightweightSynchronizer::enter(Handle obj, BasicLock* lock, JavaThread* cur
703703 }
704704}
705705
706- void LightweightSynchronizer::exit (oop object, JavaThread* current) {
706+ void LightweightSynchronizer::exit (oop object, BasicLock* lock, JavaThread* current) {
707707 assert (LockingMode == LM_LIGHTWEIGHT, " must be" );
708708 assert (current == Thread::current (), " must be" );
709709
@@ -738,7 +738,18 @@ void LightweightSynchronizer::exit(oop object, JavaThread* current) {
738738
739739 assert (mark.has_monitor (), " must be" );
740740 // The monitor exists
741- ObjectMonitor* monitor = ObjectSynchronizer::read_monitor (current, object, mark);
741+ ObjectMonitor* monitor;
742+ if (UseObjectMonitorTable) {
743+ monitor = lock->object_monitor_cache ();
744+ if (monitor == nullptr ) {
745+ monitor = current->om_get_from_monitor_cache (object);
746+ if (monitor == nullptr ) {
747+ monitor = get_monitor_from_table (current, object);
748+ }
749+ }
750+ } else {
751+ monitor = ObjectSynchronizer::read_monitor (mark);
752+ }
742753 if (monitor->has_anonymous_owner ()) {
743754 assert (current->lock_stack ().contains (object), " current must have object on its lock stack" );
744755 monitor->set_owner_from_anonymous (current);
@@ -977,7 +988,7 @@ ObjectMonitor* LightweightSynchronizer::inflate_fast_locked_object(oop object, O
977988 return monitor;
978989}
979990
980- ObjectMonitor* LightweightSynchronizer::inflate_and_enter (oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current) {
991+ ObjectMonitor* LightweightSynchronizer::inflate_and_enter (oop object, BasicLock* lock, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current) {
981992 assert (LockingMode == LM_LIGHTWEIGHT, " only used for lightweight" );
982993 VerifyThreadState vts (locking_thread, current);
983994
@@ -1003,18 +1014,20 @@ ObjectMonitor* LightweightSynchronizer::inflate_and_enter(oop object, ObjectSync
10031014
10041015 NoSafepointVerifier nsv;
10051016
1006- // Lightweight monitors require that hash codes are installed first
1007- ObjectSynchronizer::FastHashCode (locking_thread, object);
1008-
10091017 // Try to get the monitor from the thread-local cache.
10101018 // There's no need to use the cache if we are locking
10111019 // on behalf of another thread.
10121020 if (current == locking_thread) {
1013- monitor = current->om_get_from_monitor_cache (object);
1021+ monitor = lock->object_monitor_cache ();
1022+ if (monitor == nullptr ) {
1023+ monitor = current->om_get_from_monitor_cache (object);
1024+ }
10141025 }
10151026
10161027 // Get or create the monitor
10171028 if (monitor == nullptr ) {
1029+ // Lightweight monitors require that hash codes are installed first
1030+ ObjectSynchronizer::FastHashCode (locking_thread, object);
10181031 monitor = get_or_insert_monitor (object, current, cause);
10191032 }
10201033
@@ -1162,9 +1175,6 @@ bool LightweightSynchronizer::quick_enter(oop obj, BasicLock* lock, JavaThread*
11621175 assert (obj != nullptr , " must be" );
11631176 NoSafepointVerifier nsv;
11641177
1165- // If quick_enter succeeds with entering, the cache should be in a valid initialized state.
1166- CacheSetter cache_setter (current, lock);
1167-
11681178 LockStack& lock_stack = current->lock_stack ();
11691179 if (lock_stack.is_full ()) {
11701180 // Always go into runtime if the lock stack is full.
@@ -1191,17 +1201,28 @@ bool LightweightSynchronizer::quick_enter(oop obj, BasicLock* lock, JavaThread*
11911201#endif
11921202
11931203 if (mark.has_monitor ()) {
1194- ObjectMonitor* const monitor = UseObjectMonitorTable ? current->om_get_from_monitor_cache (obj) :
1195- ObjectSynchronizer::read_monitor (mark);
1204+ ObjectMonitor* monitor;
1205+ if (UseObjectMonitorTable) {
1206+ // C2 fast-path may have put the monitor in the cache in the BasicLock.
1207+ monitor = lock->object_monitor_cache ();
1208+ if (monitor == nullptr ) {
1209+ // Otherwise look up the monitor in the thread's OMCache.
1210+ monitor = current->om_get_from_monitor_cache (obj);
1211+ }
1212+ } else {
1213+ monitor = ObjectSynchronizer::read_monitor (mark);
1214+ }
11961215
11971216 if (monitor == nullptr ) {
11981217 // Take the slow-path on a cache miss.
11991218 return false ;
12001219 }
12011220
1202- if (monitor->try_enter (current)) {
1203- // ObjectMonitor enter successful.
1204- cache_setter.set_monitor (monitor);
1221+ if (UseObjectMonitorTable) {
1222+ lock->set_object_monitor_cache (monitor);
1223+ }
1224+
1225+ if (monitor->spin_enter (current)) {
12051226 return true ;
12061227 }
12071228 }
0 commit comments