Skip to content

Commit 8f7bf0a

Browse files
authored
Don't let VMThread acquire Heap_lock (#300)
The Heap_lock performs safepoint check. Mutators may hold such locks and enter safepoint. If the VMThread attempts to acquire this lock in a VMOperation while some mutators are still holding this lock, the VMThread will have to wait forever because mutators will not resume until the VMOperation ends. Later versions of OpenJDK forbid the VMThread from acquiring any lock that performs safepoint check. We notify the ReferenceHandler thread when resuming mutators. But the current code does this notification in a VMOperation, and will cause the deadlock. When this happens, the process will freeze when a GC is triggered. This is likely to happen when the heap size is small and GC is triggered often. We move the notification code to `mmtk_resume_mutators` which is executed on a GC worker thread *after* mutators have been resumed. This solves the deadlock issue.
1 parent 427eab6 commit 8f7bf0a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

openjdk/mmtkUpcalls.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ static void mmtk_resume_mutators(void *tls) {
8787
MutexLockerEx locker(MMTkHeap::heap()->gc_lock(), Mutex::_no_safepoint_check_flag);
8888
MMTkHeap::heap()->gc_lock()->notify_all();
8989
}
90+
91+
log_debug(gc)("Notifying mutators blocking on Heap_lock for reference pending list...");
92+
// Note: That's the ReferenceHandler thread.
93+
{
94+
MutexLockerEx x(Heap_lock, Mutex::_no_safepoint_check_flag);
95+
if (Universe::has_reference_pending_list()) {
96+
Heap_lock->notify_all();
97+
}
98+
}
9099
}
91100

92101
static const int GC_THREAD_KIND_WORKER = 1;

openjdk/mmtkVMCompanionThread.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,4 @@ void MMTkVMCompanionThread::do_mmtk_stw_operation() {
166166
log_trace(gc)("do_mmtk_stw_operation: Reached _thread_resumed state. Notifying...");
167167
_lock->notify_all();
168168
}
169-
170-
{
171-
MutexLockerEx x(Heap_lock, Mutex::_no_safepoint_check_flag);
172-
if (Universe::has_reference_pending_list()) {
173-
Heap_lock->notify_all();
174-
}
175-
}
176169
}

0 commit comments

Comments
 (0)