1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "gc/shared/gc_globals.hpp"
26 #include "logging/log.hpp"
27 #include "logging/logStream.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "memory/universe.hpp"
30 #include "runtime/javaThread.hpp"
31 #include "runtime/mutexLocker.hpp"
32 #include "runtime/safepoint.hpp"
33 #include "runtime/vmThread.hpp"
34 #include "utilities/vmError.hpp"
35
36 // Mutexes used in the VM (see comment in mutexLocker.hpp):
37
38 Mutex* NMethodState_lock = nullptr;
39 Monitor* SystemDictionary_lock = nullptr;
40 Mutex* InvokeMethodTypeTable_lock = nullptr;
41 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
42 Mutex* SharedDictionary_lock = nullptr;
43 Monitor* ClassInitError_lock = nullptr;
44 Mutex* Module_lock = nullptr;
45 Mutex* CompiledIC_lock = nullptr;
46 Mutex* VMStatistic_lock = nullptr;
47 Mutex* JmethodIdCreation_lock = nullptr;
48 Mutex* JfieldIdCreation_lock = nullptr;
49 Monitor* JNICritical_lock = nullptr;
50 Mutex* JvmtiThreadState_lock = nullptr;
51 Monitor* EscapeBarrier_lock = nullptr;
52 Monitor* JvmtiVTMSTransition_lock = nullptr;
53 Mutex* JvmtiVThreadSuspend_lock = nullptr;
54 Monitor* Heap_lock = nullptr;
55 #if INCLUDE_PARALLELGC
56 Mutex* PSOldGenExpand_lock = nullptr;
57 #endif
58 Mutex* AdapterHandlerLibrary_lock = nullptr;
59 Mutex* SignatureHandlerLibrary_lock = nullptr;
60 Mutex* VtableStubs_lock = nullptr;
61 Mutex* SymbolArena_lock = nullptr;
62 Monitor* StringDedup_lock = nullptr;
63 Mutex* StringDedupIntern_lock = nullptr;
64 Monitor* CodeCache_lock = nullptr;
65 Mutex* TouchedMethodLog_lock = nullptr;
66 Mutex* RetData_lock = nullptr;
67 Monitor* VMOperation_lock = nullptr;
68 Monitor* ThreadsLockThrottle_lock = nullptr;
69 Monitor* Threads_lock = nullptr;
70 Mutex* NonJavaThreadsList_lock = nullptr;
71 Mutex* NonJavaThreadsListSync_lock = nullptr;
72 Monitor* STS_lock = nullptr;
73 Mutex* MonitoringSupport_lock = nullptr;
74 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
75 Mutex* Compile_lock = nullptr;
76 Monitor* CompileTaskWait_lock = nullptr;
77 Monitor* MethodCompileQueue_lock = nullptr;
78 Monitor* CompileThread_lock = nullptr;
79 Monitor* Compilation_lock = nullptr;
80 Mutex* CompileStatistics_lock = nullptr;
81 Mutex* DirectivesStack_lock = nullptr;
82 Monitor* AOTHeapLoading_lock = nullptr;
83 Monitor* Terminator_lock = nullptr;
84 Monitor* InitCompleted_lock = nullptr;
85 Monitor* BeforeExit_lock = nullptr;
86 Monitor* Notify_lock = nullptr;
87 Mutex* ExceptionCache_lock = nullptr;
88 Mutex* TrainingData_lock = nullptr;
89 Monitor* TrainingReplayQueue_lock = nullptr;
90 #ifndef PRODUCT
91 Mutex* FullGCALot_lock = nullptr;
92 #endif
93
94 Mutex* tty_lock = nullptr;
95
96 Mutex* RawMonitor_lock = nullptr;
97 Mutex* PerfDataMemAlloc_lock = nullptr;
98 Mutex* PerfDataManager_lock = nullptr;
99
100 #if INCLUDE_G1GC
101 Monitor* G1CGC_lock = nullptr;
102 Mutex* G1FreeList_lock = nullptr;
103 Mutex* G1MarkStackChunkList_lock = nullptr;
104 Mutex* G1MarkStackFreeList_lock = nullptr;
105 Monitor* G1OldGCCount_lock = nullptr;
106 Mutex* G1OldSets_lock = nullptr;
107 Mutex* G1ReviseYoungLength_lock = nullptr;
108 Monitor* G1RootRegionScan_lock = nullptr;
109 Mutex* G1RareEvent_lock = nullptr;
110 Mutex* G1Uncommit_lock = nullptr;
111 #endif
112
113 Mutex* Management_lock = nullptr;
114 Monitor* MonitorDeflation_lock = nullptr;
115 Monitor* Service_lock = nullptr;
116 Monitor* Notification_lock = nullptr;
117 Monitor* PeriodicTask_lock = nullptr;
118 Monitor* RedefineClasses_lock = nullptr;
119 Mutex* Verify_lock = nullptr;
120
121 #if INCLUDE_JFR
122 Mutex* JfrStacktrace_lock = nullptr;
123 Monitor* JfrMsg_lock = nullptr;
124 Mutex* JfrBuffer_lock = nullptr;
125 #endif
126
127 Mutex* CodeHeapStateAnalytics_lock = nullptr;
128
129 Mutex* ExternalsRecorder_lock = nullptr;
130
131 Mutex* AOTCodeCStrings_lock = nullptr;
132
133 Monitor* ContinuationRelativize_lock = nullptr;
134
135 Mutex* Metaspace_lock = nullptr;
136 Monitor* MetaspaceCritical_lock = nullptr;
137 Mutex* ClassLoaderDataGraph_lock = nullptr;
138 Monitor* ThreadsSMRDelete_lock = nullptr;
139 Mutex* ThreadIdTableCreate_lock = nullptr;
140 Mutex* SharedDecoder_lock = nullptr;
141 Mutex* DCmdFactory_lock = nullptr;
142 Mutex* NMTQuery_lock = nullptr;
143 Mutex* NMTCompilationCostHistory_lock = nullptr;
144 Mutex* NmtVirtualMemory_lock = nullptr;
145
146 #if INCLUDE_CDS
147 #if INCLUDE_JVMTI
148 Mutex* CDSClassFileStream_lock = nullptr;
149 #endif
150 Mutex* DumpTimeTable_lock = nullptr;
151 Mutex* CDSLambda_lock = nullptr;
152 Mutex* DumpRegion_lock = nullptr;
153 Mutex* ClassListFile_lock = nullptr;
154 Mutex* UnregisteredClassesTable_lock= nullptr;
155 Mutex* LambdaFormInvokers_lock = nullptr;
156 Mutex* ScratchObjects_lock = nullptr;
157 Mutex* FinalImageRecipes_lock = nullptr;
158 #endif // INCLUDE_CDS
159 Mutex* Bootclasspath_lock = nullptr;
160
161 #if INCLUDE_JVMCI
162 Monitor* JVMCI_lock = nullptr;
163 Monitor* JVMCIRuntime_lock = nullptr;
164 #endif
165
166 // Only one RecursiveMutex
167 RecursiveMutex* MultiArray_lock = nullptr;
168
169 #ifdef ASSERT
170 void assert_locked_or_safepoint(const Mutex* lock) {
171 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
172 // check if this thread owns the lock (common case)
173 assert(lock != nullptr, "Need non-null lock");
174 if (lock->owned_by_self()) return;
175 if (SafepointSynchronize::is_at_safepoint()) return;
176 if (!Universe::is_fully_initialized()) return;
177 fatal("must own lock %s", lock->name());
178 }
179
180 // a stronger assertion than the above
181 void assert_lock_strong(const Mutex* lock) {
182 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
183 assert(lock != nullptr, "Need non-null lock");
184 if (lock->owned_by_self()) return;
185 fatal("must own lock %s", lock->name());
186 }
187 #endif
188
189 #define MUTEX_STORAGE_NAME(name) name##_storage
190 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)]
191 #define MUTEX_DEF(name, type, pri, ...) { \
192 assert(name == nullptr, "Mutex/Monitor initialized twice"); \
193 MUTEX_STORAGE(name, type); \
194 name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \
195 Mutex::add_mutex(name); \
196 }
197 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__)
198
199 // Specify relative ranked lock
200 #ifdef ASSERT
201 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__)
202 #else
203 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__)
204 #endif
205
206 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.
207 void mutex_init() {
208 MUTEX_DEFN(tty_lock , PaddedMutex , tty); // allow to lock in VM
209
210 MUTEX_DEFN(STS_lock , PaddedMonitor, nosafepoint);
211
212 #if INCLUDE_G1GC
213 if (UseG1GC) {
214 MUTEX_DEFN(G1CGC_lock , PaddedMonitor, nosafepoint);
215 MUTEX_DEFN(G1FreeList_lock , PaddedMutex , service-1);
216 MUTEX_DEFN(G1MarkStackChunkList_lock , PaddedMutex , nosafepoint);
217 MUTEX_DEFN(G1MarkStackFreeList_lock , PaddedMutex , nosafepoint);
218 MUTEX_DEFN(G1OldSets_lock , PaddedMutex , nosafepoint);
219 MUTEX_DEFN(G1RootRegionScan_lock , PaddedMonitor, nosafepoint-1);
220 MUTEX_DEFN(G1Uncommit_lock , PaddedMutex , service-2);
221 }
222 #endif
223
224 MUTEX_DEFN(MonitoringSupport_lock , PaddedMutex , service-1); // used for serviceability monitoring support
225
226 MUTEX_DEFN(StringDedup_lock , PaddedMonitor, nosafepoint);
227 MUTEX_DEFN(StringDedupIntern_lock , PaddedMutex , nosafepoint);
228 MUTEX_DEFN(RawMonitor_lock , PaddedMutex , nosafepoint-1);
229
230 MUTEX_DEFN(Metaspace_lock , PaddedMutex , nosafepoint-3);
231 MUTEX_DEFN(MetaspaceCritical_lock , PaddedMonitor, nosafepoint-1);
232
233 MUTEX_DEFN(MonitorDeflation_lock , PaddedMonitor, nosafepoint); // used for monitor deflation thread operations
234 MUTEX_DEFN(Service_lock , PaddedMonitor, service); // used for service thread operations
235 MUTEX_DEFN(Notification_lock , PaddedMonitor, service); // used for notification thread operations
236
237 MUTEX_DEFN(JmethodIdCreation_lock , PaddedMutex , nosafepoint-1); // used for creating jmethodIDs can also lock HandshakeState_lock
238 MUTEX_DEFN(InvokeMethodTypeTable_lock , PaddedMutex , safepoint);
239 MUTEX_DEFN(InvokeMethodIntrinsicTable_lock , PaddedMonitor, safepoint);
240 MUTEX_DEFN(AdapterHandlerLibrary_lock , PaddedMutex , safepoint);
241 MUTEX_DEFN(SharedDictionary_lock , PaddedMutex , safepoint);
242 MUTEX_DEFN(VMStatistic_lock , PaddedMutex , safepoint);
243 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint);
244 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint);
245 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint);
246 #ifndef PRODUCT
247 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , safepoint); // a lock to make FullGCALot MT safe
248 #endif
249 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint);
250
251 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1);
252 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint);
253
254 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint);
255 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true);
256 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint);
257 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true);
258
259 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint);
260
261 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock
262 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint);
263 MUTEX_DEFN(TrainingData_lock , PaddedMutex , nosafepoint);
264 MUTEX_DEFN(TrainingReplayQueue_lock , PaddedMonitor, safepoint);
265 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint);
266 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint);
267
268 MUTEX_DEFN(JvmtiVTMSTransition_lock , PaddedMonitor, safepoint); // used for Virtual Thread Mount State transition management
269 MUTEX_DEFN(JvmtiVThreadSuspend_lock , PaddedMutex, nosafepoint-1);
270 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
271 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management
272
273 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true);
274 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint);
275
276 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint);
277 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true);
278 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint);
279 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint);
280 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint);
281
282 if (WhiteBoxAPI) {
283 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint);
284 }
285
286 #if INCLUDE_JFR
287 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event);
288 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event);
289 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event);
290 #endif
291
292 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3);
293 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint);
294 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
295 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint);
296 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint);
297 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint);
298 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint);
299 MUTEX_DEFN(NmtVirtualMemory_lock , PaddedMutex , service-4); // Must be lower than G1Mapper_lock used from G1RegionsSmallerThanCommitSizeMapper::commit_regions
300 #if INCLUDE_CDS
301 #if INCLUDE_JVMTI
302 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint);
303 #endif
304 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint);
305 MUTEX_DEFN(CDSLambda_lock , PaddedMutex , nosafepoint);
306 MUTEX_DEFL(DumpRegion_lock , PaddedMutex , DumpTimeTable_lock);
307 MUTEX_DEFN(ClassListFile_lock , PaddedMutex , nosafepoint);
308 MUTEX_DEFN(UnregisteredClassesTable_lock , PaddedMutex , nosafepoint-1);
309 MUTEX_DEFN(LambdaFormInvokers_lock , PaddedMutex , safepoint);
310 MUTEX_DEFL(ScratchObjects_lock , PaddedMutex , DumpTimeTable_lock);
311 MUTEX_DEFN(FinalImageRecipes_lock , PaddedMutex , nosafepoint);
312 #endif // INCLUDE_CDS
313 MUTEX_DEFN(Bootclasspath_lock , PaddedMutex , nosafepoint);
314
315 #if INCLUDE_JVMCI
316 // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
317 MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true);
318 #endif
319
320 MUTEX_DEFN(ThreadsLockThrottle_lock , PaddedMonitor, safepoint);
321
322 // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
323 MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock
324 MUTEX_DEFL(CodeCache_lock , PaddedMonitor, VtableStubs_lock);
325 MUTEX_DEFL(NMethodState_lock , PaddedMutex , CodeCache_lock);
326
327 // tty_lock is held when printing nmethod and its relocations which use this lock.
328 MUTEX_DEFL(ExternalsRecorder_lock , PaddedMutex , tty_lock);
329
330 MUTEX_DEFL(AOTCodeCStrings_lock , PaddedMutex , tty_lock);
331
332 MUTEX_DEFL(Threads_lock , PaddedMonitor, CompileThread_lock, true);
333 MUTEX_DEFL(Compile_lock , PaddedMutex , MethodCompileQueue_lock);
334 MUTEX_DEFL(Module_lock , PaddedMutex , AdapterHandlerLibrary_lock);
335 MUTEX_DEFL(AOTHeapLoading_lock , PaddedMonitor, Module_lock);
336 MUTEX_DEFL(JNICritical_lock , PaddedMonitor, AOTHeapLoading_lock); // used for JNI critical regions
337 MUTEX_DEFL(Heap_lock , PaddedMonitor, JNICritical_lock);
338
339 MUTEX_DEFL(PerfDataMemAlloc_lock , PaddedMutex , Heap_lock);
340 MUTEX_DEFL(PerfDataManager_lock , PaddedMutex , Heap_lock);
341 MUTEX_DEFL(VMOperation_lock , PaddedMonitor, Heap_lock, true);
342 MUTEX_DEFL(ClassInitError_lock , PaddedMonitor, Threads_lock);
343
344 #if INCLUDE_G1GC
345 if (UseG1GC) {
346 MUTEX_DEFL(G1OldGCCount_lock , PaddedMonitor, Threads_lock, true);
347 MUTEX_DEFL(G1RareEvent_lock , PaddedMutex , Threads_lock, true);
348 MUTEX_DEFL(G1ReviseYoungLength_lock , PaddedMutex , Threads_lock, true);
349 }
350 #endif
351
352 MUTEX_DEFL(CompileTaskWait_lock , PaddedMonitor, MethodCompileQueue_lock);
353
354 #if INCLUDE_PARALLELGC
355 if (UseParallelGC) {
356 MUTEX_DEFL(PSOldGenExpand_lock , PaddedMutex , Heap_lock, true);
357 }
358 #endif
359 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock);
360 #if INCLUDE_JVMCI
361 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
362 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock);
363 #endif
364 MUTEX_DEFL(JvmtiThreadState_lock , PaddedMutex , JvmtiVTMSTransition_lock); // Used by JvmtiThreadState/JvmtiEventController
365 MUTEX_DEFL(SharedDecoder_lock , PaddedMutex , NmtVirtualMemory_lock); // Must be lower than NmtVirtualMemory_lock due to MemTracker::print_containing_region
366
367 // Allocate RecursiveMutex
368 MultiArray_lock = new RecursiveMutex();
369 }
370
371 #undef MUTEX_DEFL
372 #undef MUTEX_DEFN
373 #undef MUTEX_DEF
374 #undef MUTEX_STORAGE
375 #undef MUTEX_STORAGE_NAME
376
377 void MutexLockerImpl::post_initialize() {
378 // Print mutex ranks if requested.
379 LogTarget(Info, vmmutex) lt;
380 if (lt.is_enabled()) {
381 ResourceMark rm;
382 LogStream ls(lt);
383 Mutex::print_lock_ranks(&ls);
384 }
385 }
386
387 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
388 if (SafepointSynchronize::is_at_safepoint()) {
389 _locked = false;
390 } else {
391 _mutex = mutex;
392 _locked = true;
393 _mutex->lock();
394 }
395 }