@@ -407,15 +407,15 @@ class Thread: public ThreadShadow {
407
407
408
408
JFR_ONLY (DEFINE_THREAD_LOCAL_FIELD_JFR;) // Thread-local data for jfr
409
409
410
- ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread
410
+ ObjectMonitor* volatile _current_pending_monitor; // ObjectMonitor this thread
411
411
// is waiting to lock
412
412
bool _current_pending_monitor_is_from_java; // locking is from Java code
413
413
JvmtiRawMonitor* _current_pending_raw_monitor; // JvmtiRawMonitor this thread
414
414
// is waiting to lock
415
415
416
416
417
417
// ObjectMonitor on which this thread called Object.wait()
418
- ObjectMonitor* _current_waiting_monitor;
418
+ ObjectMonitor* volatile _current_waiting_monitor;
419
419
420
420
#ifdef ASSERT
421
421
private:
@@ -620,10 +620,13 @@ class Thread: public ThreadShadow {
620
620
621
621
// For tracking the heavyweight monitor the thread is pending on.
622
622
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);
624
627
}
625
628
void set_current_pending_monitor (ObjectMonitor* monitor) {
626
- _current_pending_monitor = monitor;
629
+ Atomic::store (& _current_pending_monitor, monitor) ;
627
630
}
628
631
void set_current_pending_monitor_is_from_java (bool from_java) {
629
632
_current_pending_monitor_is_from_java = from_java;
@@ -634,10 +637,11 @@ class Thread: public ThreadShadow {
634
637
635
638
// For tracking the ObjectMonitor on which this thread called Object.wait()
636
639
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);
638
642
}
639
643
void set_current_waiting_monitor (ObjectMonitor* monitor) {
640
- _current_waiting_monitor = monitor;
644
+ Atomic::store (& _current_waiting_monitor, monitor) ;
641
645
}
642
646
643
647
// For tracking the Jvmti raw monitor the thread is pending on.
0 commit comments