@@ -407,15 +407,15 @@ class Thread: public ThreadShadow {
407407
408408 JFR_ONLY (DEFINE_THREAD_LOCAL_FIELD_JFR;) // Thread-local data for jfr
409409
410- ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread
410+ ObjectMonitor* volatile _current_pending_monitor; // ObjectMonitor this thread
411411 // is waiting to lock
412412 bool _current_pending_monitor_is_from_java; // locking is from Java code
413413 JvmtiRawMonitor* _current_pending_raw_monitor; // JvmtiRawMonitor this thread
414414 // is waiting to lock
415415
416416
417417 // ObjectMonitor on which this thread called Object.wait()
418- ObjectMonitor* _current_waiting_monitor;
418+ ObjectMonitor* volatile _current_waiting_monitor;
419419
420420#ifdef ASSERT
421421 private:
@@ -620,10 +620,13 @@ class Thread: public ThreadShadow {
620620
621621 // For tracking the heavyweight monitor the thread is pending on.
622622 ObjectMonitor* current_pending_monitor () {
623- return _current_pending_monitor;
623+ // Use Atomic::load() to prevent data race between concurrent modification and
624+ // concurrent readers, e.g. ThreadService::get_current_contended_monitor().
625+ // Especially, reloading pointer from thread after NULL check must be prevented.
626+ return Atomic::load (&_current_pending_monitor);
624627 }
625628 void set_current_pending_monitor (ObjectMonitor* monitor) {
626- _current_pending_monitor = monitor;
629+ Atomic::store (& _current_pending_monitor, monitor) ;
627630 }
628631 void set_current_pending_monitor_is_from_java (bool from_java) {
629632 _current_pending_monitor_is_from_java = from_java;
@@ -634,10 +637,11 @@ class Thread: public ThreadShadow {
634637
635638 // For tracking the ObjectMonitor on which this thread called Object.wait()
636639 ObjectMonitor* current_waiting_monitor () {
637- return _current_waiting_monitor;
640+ // See the comment in current_pending_monitor() above.
641+ return Atomic::load (&_current_waiting_monitor);
638642 }
639643 void set_current_waiting_monitor (ObjectMonitor* monitor) {
640- _current_waiting_monitor = monitor;
644+ Atomic::store (& _current_waiting_monitor, monitor) ;
641645 }
642646
643647 // For tracking the Jvmti raw monitor the thread is pending on.
0 commit comments