@@ -746,18 +746,21 @@ class JavaThread: public Thread {
746746 // elided card-marks for performance along the fast-path.
747747 MemRegion _deferred_card_mark;
748748
749- ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread is waiting to lock
749+ ObjectMonitor* volatile _current_pending_monitor; // ObjectMonitor this thread is waiting to lock
750750 bool _current_pending_monitor_is_from_java; // locking is from Java code
751- ObjectMonitor* _current_waiting_monitor; // ObjectMonitor on which this thread called Object.wait()
751+ ObjectMonitor* volatile _current_waiting_monitor; // ObjectMonitor on which this thread called Object.wait()
752752 public:
753753 volatile intptr_t _Stalled;
754754
755755 // For tracking the heavyweight monitor the thread is pending on.
756756 ObjectMonitor* current_pending_monitor () {
757- return _current_pending_monitor;
757+ // Use Atomic::load() to prevent data race between concurrent modification and
758+ // concurrent readers, e.g. ThreadService::get_current_contended_monitor().
759+ // Especially, reloading pointer from thread after NULL check must be prevented.
760+ return Atomic::load (&_current_pending_monitor);
758761 }
759762 void set_current_pending_monitor (ObjectMonitor* monitor) {
760- _current_pending_monitor = monitor;
763+ Atomic::store (& _current_pending_monitor, monitor) ;
761764 }
762765 void set_current_pending_monitor_is_from_java (bool from_java) {
763766 _current_pending_monitor_is_from_java = from_java;
@@ -766,10 +769,11 @@ class JavaThread: public Thread {
766769 return _current_pending_monitor_is_from_java;
767770 }
768771 ObjectMonitor* current_waiting_monitor () {
769- return _current_waiting_monitor;
772+ // See the comment in current_pending_monitor() above.
773+ return Atomic::load (&_current_waiting_monitor);
770774 }
771775 void set_current_waiting_monitor (ObjectMonitor* monitor) {
772- _current_waiting_monitor = monitor;
776+ Atomic::store (& _current_waiting_monitor, monitor) ;
773777 }
774778
775779 private:
0 commit comments