1 /*
2 * Copyright (c) 1997, 2021, 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 "precompiled.hpp"
26 #include "gc/shared/gc_globals.hpp"
27 #include "memory/universe.hpp"
28 #include "runtime/mutexLocker.hpp"
29 #include "runtime/os.inline.hpp"
30 #include "runtime/safepoint.hpp"
31 #include "runtime/thread.inline.hpp"
32 #include "runtime/vmThread.hpp"
33
34 // Mutexes used in the VM (see comment in mutexLocker.hpp):
35 //
36 // Note that the following pointers are effectively final -- after having been
37 // set at JVM startup-time, they should never be subsequently mutated.
38 // Instead of using pointers to malloc()ed monitors and mutexes we should consider
39 // eliminating the indirection and using instances instead.
40 // Consider using GCC's __read_mostly.
41
42 Mutex* Patching_lock = NULL;
43 Mutex* CompiledMethod_lock = NULL;
44 Monitor* SystemDictionary_lock = NULL;
45 Mutex* SharedDictionary_lock = NULL;
46 Mutex* Module_lock = NULL;
47 Mutex* CompiledIC_lock = NULL;
48 Mutex* InlineCacheBuffer_lock = NULL;
49 Mutex* VMStatistic_lock = NULL;
50 Mutex* JNIHandleBlockFreeList_lock = NULL;
51 Mutex* JmethodIdCreation_lock = NULL;
52 Mutex* JfieldIdCreation_lock = NULL;
53 Monitor* JNICritical_lock = NULL;
54 Mutex* JvmtiThreadState_lock = NULL;
55 Monitor* EscapeBarrier_lock = NULL;
56 Monitor* Heap_lock = NULL;
57 Mutex* ExpandHeap_lock = NULL;
58 Mutex* AdapterHandlerLibrary_lock = NULL;
59 Mutex* SignatureHandlerLibrary_lock = NULL;
60 Mutex* VtableStubs_lock = NULL;
61 Mutex* SymbolArena_lock = NULL;
62 Monitor* StringDedup_lock = NULL;
63 Mutex* StringDedupIntern_lock = NULL;
64 Monitor* CodeCache_lock = NULL;
65 Monitor* CodeSweeper_lock = NULL;
66 Mutex* MethodData_lock = NULL;
67 Mutex* TouchedMethodLog_lock = NULL;
68 Mutex* RetData_lock = NULL;
69 Monitor* VMOperation_lock = NULL;
70 Monitor* Threads_lock = NULL;
71 Mutex* NonJavaThreadsList_lock = NULL;
72 Mutex* NonJavaThreadsListSync_lock = NULL;
73 Monitor* CGC_lock = NULL;
74 Monitor* STS_lock = NULL;
75 Monitor* G1OldGCCount_lock = NULL;
76 Mutex* Shared_DirtyCardQ_lock = NULL;
77 Mutex* G1DetachedRefinementStats_lock = NULL;
78 Mutex* MarkStackFreeList_lock = NULL;
79 Mutex* MarkStackChunkList_lock = NULL;
80 Mutex* MonitoringSupport_lock = NULL;
81 Mutex* ParGCRareEvent_lock = NULL;
82 Monitor* ConcurrentGCBreakpoints_lock = NULL;
83 Mutex* Compile_lock = NULL;
84 Monitor* MethodCompileQueue_lock = NULL;
85 Monitor* CompileThread_lock = NULL;
86 Monitor* Compilation_lock = NULL;
87 Mutex* CompileTaskAlloc_lock = NULL;
88 Mutex* CompileStatistics_lock = NULL;
89 Mutex* DirectivesStack_lock = NULL;
90 Mutex* MultiArray_lock = NULL;
91 Monitor* Terminator_lock = NULL;
92 Monitor* InitCompleted_lock = NULL;
93 Monitor* BeforeExit_lock = NULL;
94 Monitor* Notify_lock = NULL;
95 Mutex* ProfilePrint_lock = NULL;
96 Mutex* ExceptionCache_lock = NULL;
97 Mutex* NMethodSweeperStats_lock = NULL;
98 #ifndef PRODUCT
99 Mutex* FullGCALot_lock = NULL;
100 #endif
101
102 Mutex* Debug1_lock = NULL;
103 Mutex* Debug2_lock = NULL;
104 Mutex* Debug3_lock = NULL;
105
106 Mutex* tty_lock = NULL;
107
108 Mutex* RawMonitor_lock = NULL;
109 Mutex* PerfDataMemAlloc_lock = NULL;
110 Mutex* PerfDataManager_lock = NULL;
111 Mutex* OopMapCacheAlloc_lock = NULL;
112
113 Mutex* FreeList_lock = NULL;
114 Mutex* OldSets_lock = NULL;
115 Mutex* Uncommit_lock = NULL;
116 Monitor* RootRegionScan_lock = NULL;
117
118 Mutex* Management_lock = NULL;
119 Monitor* MonitorDeflation_lock = NULL;
120 Monitor* Service_lock = NULL;
121 Monitor* Notification_lock = NULL;
122 Monitor* PeriodicTask_lock = NULL;
123 Monitor* RedefineClasses_lock = NULL;
124 Mutex* Verify_lock = NULL;
125 Monitor* Zip_lock = NULL;
126
127 #if INCLUDE_JFR
128 Mutex* JfrStacktrace_lock = NULL;
129 Monitor* JfrMsg_lock = NULL;
130 Mutex* JfrBuffer_lock = NULL;
131 Mutex* JfrStream_lock = NULL;
132 Monitor* JfrThreadSampler_lock = NULL;
133 #endif
134
135 #ifndef SUPPORTS_NATIVE_CX8
136 Mutex* UnsafeJlong_lock = NULL;
137 #endif
138 Mutex* CodeHeapStateAnalytics_lock = NULL;
139
140 Mutex* Metaspace_lock = NULL;
141 Mutex* ClassLoaderDataGraph_lock = NULL;
142 Monitor* ThreadsSMRDelete_lock = NULL;
143 Mutex* ThreadIdTableCreate_lock = NULL;
144 Mutex* SharedDecoder_lock = NULL;
145 Mutex* DCmdFactory_lock = NULL;
146 #if INCLUDE_NMT
147 Mutex* NMTQuery_lock = NULL;
148 #endif
149 #if INCLUDE_CDS
150 #if INCLUDE_JVMTI
151 Mutex* CDSClassFileStream_lock = NULL;
152 #endif
153 Mutex* DumpTimeTable_lock = NULL;
154 Mutex* CDSLambda_lock = NULL;
155 Mutex* DumpRegion_lock = NULL;
156 Mutex* ClassListFile_lock = NULL;
157 Mutex* LambdaFormInvokers_lock = NULL;
158 #endif // INCLUDE_CDS
159 Mutex* Bootclasspath_lock = NULL;
160
161 #if INCLUDE_JVMCI
162 Monitor* JVMCI_lock = NULL;
163 #endif
164
165
166 #define MAX_NUM_MUTEX 128
167 static Mutex* _mutex_array[MAX_NUM_MUTEX];
168 static int _num_mutex;
169
170 #ifdef ASSERT
assert_locked_or_safepoint(const Mutex * lock)171 void assert_locked_or_safepoint(const Mutex* lock) {
172 // check if this thread owns the lock (common case)
173 assert(lock != NULL, "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 weaker assertion than the above
assert_locked_or_safepoint_weak(const Mutex * lock)181 void assert_locked_or_safepoint_weak(const Mutex* lock) {
182 assert(lock != NULL, "Need non-NULL lock");
183 if (lock->is_locked()) return;
184 if (SafepointSynchronize::is_at_safepoint()) return;
185 if (!Universe::is_fully_initialized()) return;
186 fatal("must own lock %s", lock->name());
187 }
188
189 // a stronger assertion than the above
assert_lock_strong(const Mutex * lock)190 void assert_lock_strong(const Mutex* lock) {
191 assert(lock != NULL, "Need non-NULL lock");
192 if (lock->owned_by_self()) return;
193 fatal("must own lock %s", lock->name());
194 }
195
assert_locked_or_safepoint_or_handshake(const Mutex * lock,const JavaThread * thread)196 void assert_locked_or_safepoint_or_handshake(const Mutex* lock, const JavaThread* thread) {
197 if (thread->is_handshake_safe_for(Thread::current())) return;
198 assert_locked_or_safepoint(lock);
199 }
200 #endif
201
202 #define def(var, type, pri, vm_block, safepoint_check_allowed ) { \
203 var = new type(Mutex::pri, #var, vm_block, Mutex::safepoint_check_allowed); \
204 assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX"); \
205 _mutex_array[_num_mutex++] = var; \
206 }
207
208 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.
mutex_init()209 void mutex_init() {
210 def(tty_lock , PaddedMutex , tty, true, _safepoint_check_never); // allow to lock in VM
211
212 def(CGC_lock , PaddedMonitor, special, true, _safepoint_check_never); // coordinate between fore- and background GC
213 def(STS_lock , PaddedMonitor, leaf, true, _safepoint_check_never);
214
215 if (UseG1GC) {
216 def(G1OldGCCount_lock , PaddedMonitor, leaf, true, _safepoint_check_always);
217
218 def(Shared_DirtyCardQ_lock , PaddedMutex , access + 1, true, _safepoint_check_never);
219
220 def(G1DetachedRefinementStats_lock, PaddedMutex, leaf , true, _safepoint_check_never);
221
222 def(FreeList_lock , PaddedMutex , leaf , true, _safepoint_check_never);
223 def(OldSets_lock , PaddedMutex , leaf , true, _safepoint_check_never);
224 def(Uncommit_lock , PaddedMutex , leaf + 1 , true, _safepoint_check_never);
225 def(RootRegionScan_lock , PaddedMonitor, leaf , true, _safepoint_check_never);
226
227 def(MarkStackFreeList_lock , PaddedMutex , leaf , true, _safepoint_check_never);
228 def(MarkStackChunkList_lock , PaddedMutex , leaf , true, _safepoint_check_never);
229
230 def(MonitoringSupport_lock , PaddedMutex , native , true, _safepoint_check_never); // used for serviceability monitoring support
231 }
232 def(StringDedup_lock , PaddedMonitor, leaf, true, _safepoint_check_never);
233 def(StringDedupIntern_lock , PaddedMutex , leaf, true, _safepoint_check_never);
234 def(ParGCRareEvent_lock , PaddedMutex , leaf, true, _safepoint_check_always);
235 def(CodeCache_lock , PaddedMonitor, special, true, _safepoint_check_never);
236 def(CodeSweeper_lock , PaddedMonitor, special-2, true, _safepoint_check_never);
237 def(RawMonitor_lock , PaddedMutex , special, true, _safepoint_check_never);
238 def(OopMapCacheAlloc_lock , PaddedMutex , leaf, true, _safepoint_check_always); // used for oop_map_cache allocation.
239
240 def(Metaspace_lock , PaddedMutex , leaf-1, true, _safepoint_check_never);
241 def(ClassLoaderDataGraph_lock , PaddedMutex , nonleaf, false, _safepoint_check_always);
242
243 def(Patching_lock , PaddedMutex , special, true, _safepoint_check_never); // used for safepointing and code patching.
244 def(CompiledMethod_lock , PaddedMutex , special-1, true, _safepoint_check_never);
245 def(MonitorDeflation_lock , PaddedMonitor, tty-2, true, _safepoint_check_never); // used for monitor deflation thread operations
246 def(Service_lock , PaddedMonitor, tty-2, true, _safepoint_check_never); // used for service thread operations
247
248 if (UseNotificationThread) {
249 def(Notification_lock , PaddedMonitor, special, true, _safepoint_check_never); // used for notification thread operations
250 } else {
251 Notification_lock = Service_lock;
252 }
253
254 def(JmethodIdCreation_lock , PaddedMutex , special-2, true, _safepoint_check_never); // used for creating jmethodIDs.
255
256 def(SystemDictionary_lock , PaddedMonitor, leaf, true, _safepoint_check_always);
257 def(SharedDictionary_lock , PaddedMutex , leaf, true, _safepoint_check_always);
258 def(Module_lock , PaddedMutex , leaf+2, false, _safepoint_check_always);
259 def(InlineCacheBuffer_lock , PaddedMutex , leaf, true, _safepoint_check_never);
260 def(VMStatistic_lock , PaddedMutex , leaf, false, _safepoint_check_always);
261 def(ExpandHeap_lock , PaddedMutex , leaf, true, _safepoint_check_always); // Used during compilation by VM thread
262 def(JNIHandleBlockFreeList_lock , PaddedMutex , leaf-1, true, _safepoint_check_never); // handles are used by VM thread
263 def(SignatureHandlerLibrary_lock , PaddedMutex , leaf, false, _safepoint_check_always);
264 def(SymbolArena_lock , PaddedMutex , leaf+2, true, _safepoint_check_never);
265 def(ProfilePrint_lock , PaddedMutex , leaf, false, _safepoint_check_always); // serial profile printing
266 def(ExceptionCache_lock , PaddedMutex , leaf, false, _safepoint_check_always); // serial profile printing
267 def(Debug1_lock , PaddedMutex , leaf, true, _safepoint_check_never);
268 #ifndef PRODUCT
269 def(FullGCALot_lock , PaddedMutex , leaf, false, _safepoint_check_always); // a lock to make FullGCALot MT safe
270 #endif
271 def(BeforeExit_lock , PaddedMonitor, leaf, true, _safepoint_check_always);
272 def(PerfDataMemAlloc_lock , PaddedMutex , leaf, true, _safepoint_check_always); // used for allocating PerfData memory for performance data
273 def(PerfDataManager_lock , PaddedMutex , leaf, true, _safepoint_check_always); // used for synchronized access to PerfDataManager resources
274
275 def(Threads_lock , PaddedMonitor, barrier, true, _safepoint_check_always); // Used for safepoint protocol.
276 def(NonJavaThreadsList_lock , PaddedMutex, barrier, true, _safepoint_check_never);
277 def(NonJavaThreadsListSync_lock , PaddedMutex, leaf, true, _safepoint_check_never);
278
279 def(VMOperation_lock , PaddedMonitor, nonleaf, true, _safepoint_check_always); // VM_thread allowed to block on these
280 def(RetData_lock , PaddedMutex , nonleaf, false, _safepoint_check_always);
281 def(Terminator_lock , PaddedMonitor, nonleaf, true, _safepoint_check_always);
282 def(InitCompleted_lock , PaddedMonitor, leaf, true, _safepoint_check_never);
283 def(VtableStubs_lock , PaddedMutex , nonleaf, true, _safepoint_check_never);
284 def(Notify_lock , PaddedMonitor, nonleaf, true, _safepoint_check_always);
285 def(JNICritical_lock , PaddedMonitor, nonleaf, true, _safepoint_check_always); // used for JNI critical regions
286 def(AdapterHandlerLibrary_lock , PaddedMutex , nonleaf, true, _safepoint_check_always);
287
288 def(Heap_lock , PaddedMonitor, nonleaf+1, false, _safepoint_check_always); // Doesn't safepoint check during termination.
289 def(JfieldIdCreation_lock , PaddedMutex , nonleaf+1, true, _safepoint_check_always); // jfieldID, Used in VM_Operation
290
291 def(CompiledIC_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_never); // locks VtableStubs_lock, InlineCacheBuffer_lock
292 def(CompileTaskAlloc_lock , PaddedMutex , nonleaf+2, true, _safepoint_check_always);
293 def(CompileStatistics_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_always);
294 def(DirectivesStack_lock , PaddedMutex , special, true, _safepoint_check_never);
295 def(MultiArray_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_always);
296
297 def(JvmtiThreadState_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_always); // Used by JvmtiThreadState/JvmtiEventController
298 def(EscapeBarrier_lock , PaddedMonitor, leaf, false, _safepoint_check_never); // Used to synchronize object reallocation/relocking triggered by JVMTI
299 def(Management_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_always); // used for JVM management
300
301 def(ConcurrentGCBreakpoints_lock , PaddedMonitor, nonleaf, true, _safepoint_check_always);
302 def(Compile_lock , PaddedMutex , nonleaf+3, false, _safepoint_check_always);
303 def(MethodData_lock , PaddedMutex , nonleaf+3, false, _safepoint_check_always);
304 def(TouchedMethodLog_lock , PaddedMutex , nonleaf+3, false, _safepoint_check_always);
305
306 def(MethodCompileQueue_lock , PaddedMonitor, nonleaf+4, false, _safepoint_check_always);
307 def(Debug2_lock , PaddedMutex , nonleaf+4, true, _safepoint_check_never);
308 def(Debug3_lock , PaddedMutex , nonleaf+4, true, _safepoint_check_never);
309 def(CompileThread_lock , PaddedMonitor, nonleaf+5, false, _safepoint_check_always);
310 def(PeriodicTask_lock , PaddedMonitor, nonleaf+5, true, _safepoint_check_always);
311 def(RedefineClasses_lock , PaddedMonitor, nonleaf+5, true, _safepoint_check_always);
312 def(Verify_lock , PaddedMutex, nonleaf+5, true, _safepoint_check_always);
313 def(Zip_lock , PaddedMonitor, leaf, true, _safepoint_check_never);
314
315 if (WhiteBoxAPI) {
316 def(Compilation_lock , PaddedMonitor, leaf, false, _safepoint_check_never);
317 }
318
319 #if INCLUDE_JFR
320 def(JfrMsg_lock , PaddedMonitor, leaf, true, _safepoint_check_always);
321 def(JfrBuffer_lock , PaddedMutex , leaf, true, _safepoint_check_never);
322 def(JfrStream_lock , PaddedMutex , nonleaf + 1, false, _safepoint_check_never);
323 def(JfrStacktrace_lock , PaddedMutex , tty-2, true, _safepoint_check_never);
324 def(JfrThreadSampler_lock , PaddedMonitor, leaf, true, _safepoint_check_never);
325 #endif
326
327 #ifndef SUPPORTS_NATIVE_CX8
328 def(UnsafeJlong_lock , PaddedMutex , special, false, _safepoint_check_never);
329 #endif
330
331 def(CodeHeapStateAnalytics_lock , PaddedMutex , nonleaf+6, false, _safepoint_check_always);
332 def(NMethodSweeperStats_lock , PaddedMutex , special, true, _safepoint_check_never);
333 def(ThreadsSMRDelete_lock , PaddedMonitor, special, true, _safepoint_check_never);
334 def(ThreadIdTableCreate_lock , PaddedMutex , leaf, false, _safepoint_check_always);
335 def(SharedDecoder_lock , PaddedMutex , native, true, _safepoint_check_never);
336 def(DCmdFactory_lock , PaddedMutex , leaf, true, _safepoint_check_never);
337 #if INCLUDE_NMT
338 def(NMTQuery_lock , PaddedMutex , max_nonleaf, false, _safepoint_check_always);
339 #endif
340 #if INCLUDE_CDS
341 #if INCLUDE_JVMTI
342 def(CDSClassFileStream_lock , PaddedMutex , max_nonleaf, false, _safepoint_check_always);
343 #endif
344 def(DumpTimeTable_lock , PaddedMutex , leaf - 1, true, _safepoint_check_never);
345 def(CDSLambda_lock , PaddedMutex , leaf, true, _safepoint_check_never);
346 def(DumpRegion_lock , PaddedMutex , leaf, true, _safepoint_check_never);
347 def(ClassListFile_lock , PaddedMutex , leaf, true, _safepoint_check_never);
348 def(LambdaFormInvokers_lock , PaddedMutex , nonleaf+2, false, _safepoint_check_always);
349 #endif // INCLUDE_CDS
350 def(Bootclasspath_lock , PaddedMutex , leaf, false, _safepoint_check_never);
351
352 #if INCLUDE_JVMCI
353 def(JVMCI_lock , PaddedMonitor, nonleaf+2, true, _safepoint_check_always);
354 #endif
355 }
356
GCMutexLocker(Mutex * mutex)357 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
358 if (SafepointSynchronize::is_at_safepoint()) {
359 _locked = false;
360 } else {
361 _mutex = mutex;
362 _locked = true;
363 _mutex->lock();
364 }
365 }
366
367 // Print all mutexes/monitors that are currently owned by a thread; called
368 // by fatal error handler.
print_owned_locks_on_error(outputStream * st)369 void print_owned_locks_on_error(outputStream* st) {
370 st->print("VM Mutex/Monitor currently owned by a thread: ");
371 bool none = true;
372 for (int i = 0; i < _num_mutex; i++) {
373 // see if it has an owner
374 if (_mutex_array[i]->owner() != NULL) {
375 if (none) {
376 // print format used by Mutex::print_on_error()
377 st->print_cr(" ([mutex/lock_event])");
378 none = false;
379 }
380 _mutex_array[i]->print_on_error(st);
381 st->cr();
382 }
383 }
384 if (none) st->print_cr("None");
385 }
386