src/share/vm/runtime/mutexLocker.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6906
581e70386ec9
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "runtime/mutexLocker.hpp"
aoqi@0 27 #include "runtime/safepoint.hpp"
aoqi@0 28 #include "runtime/thread.inline.hpp"
aoqi@0 29 #include "runtime/threadLocalStorage.hpp"
aoqi@0 30 #include "runtime/vmThread.hpp"
aoqi@0 31
aoqi@0 32 // Mutexes used in the VM (see comment in mutexLocker.hpp):
aoqi@0 33 //
aoqi@0 34 // Note that the following pointers are effectively final -- after having been
aoqi@0 35 // set at JVM startup-time, they should never be subsequently mutated.
aoqi@0 36 // Instead of using pointers to malloc()ed monitors and mutexes we should consider
aoqi@0 37 // eliminating the indirection and using instances instead.
aoqi@0 38 // Consider using GCC's __read_mostly.
aoqi@0 39
aoqi@0 40 Mutex* Patching_lock = NULL;
aoqi@0 41 Monitor* SystemDictionary_lock = NULL;
aoqi@0 42 Mutex* PackageTable_lock = NULL;
aoqi@0 43 Mutex* CompiledIC_lock = NULL;
aoqi@0 44 Mutex* InlineCacheBuffer_lock = NULL;
aoqi@0 45 Mutex* VMStatistic_lock = NULL;
aoqi@0 46 Mutex* JNIGlobalHandle_lock = NULL;
aoqi@0 47 Mutex* JNIHandleBlockFreeList_lock = NULL;
aoqi@0 48 Mutex* MemberNameTable_lock = NULL;
aoqi@0 49 Mutex* JmethodIdCreation_lock = NULL;
aoqi@0 50 Mutex* JfieldIdCreation_lock = NULL;
aoqi@0 51 Monitor* JNICritical_lock = NULL;
aoqi@0 52 Mutex* JvmtiThreadState_lock = NULL;
aoqi@0 53 Monitor* JvmtiPendingEvent_lock = NULL;
aoqi@0 54 Monitor* Heap_lock = NULL;
aoqi@0 55 Mutex* ExpandHeap_lock = NULL;
aoqi@0 56 Mutex* AdapterHandlerLibrary_lock = NULL;
aoqi@0 57 Mutex* SignatureHandlerLibrary_lock = NULL;
aoqi@0 58 Mutex* VtableStubs_lock = NULL;
aoqi@0 59 Mutex* SymbolTable_lock = NULL;
aoqi@0 60 Mutex* StringTable_lock = NULL;
aoqi@0 61 Monitor* StringDedupQueue_lock = NULL;
aoqi@0 62 Mutex* StringDedupTable_lock = NULL;
aoqi@0 63 Mutex* CodeCache_lock = NULL;
aoqi@0 64 Mutex* MethodData_lock = NULL;
aoqi@0 65 Mutex* RetData_lock = NULL;
aoqi@0 66 Monitor* VMOperationQueue_lock = NULL;
aoqi@0 67 Monitor* VMOperationRequest_lock = NULL;
aoqi@0 68 Monitor* Safepoint_lock = NULL;
aoqi@0 69 Monitor* SerializePage_lock = NULL;
aoqi@0 70 Monitor* Threads_lock = NULL;
aoqi@0 71 Monitor* CGC_lock = NULL;
pliden@6906 72 Monitor* STS_lock = NULL;
aoqi@0 73 Monitor* SLT_lock = NULL;
aoqi@0 74 Monitor* iCMS_lock = NULL;
aoqi@0 75 Monitor* FullGCCount_lock = NULL;
aoqi@0 76 Monitor* CMark_lock = NULL;
aoqi@0 77 Mutex* CMRegionStack_lock = NULL;
aoqi@0 78 Mutex* SATB_Q_FL_lock = NULL;
aoqi@0 79 Monitor* SATB_Q_CBL_mon = NULL;
aoqi@0 80 Mutex* Shared_SATB_Q_lock = NULL;
aoqi@0 81 Mutex* DirtyCardQ_FL_lock = NULL;
aoqi@0 82 Monitor* DirtyCardQ_CBL_mon = NULL;
aoqi@0 83 Mutex* Shared_DirtyCardQ_lock = NULL;
aoqi@0 84 Mutex* ParGCRareEvent_lock = NULL;
aoqi@0 85 Mutex* EvacFailureStack_lock = NULL;
aoqi@0 86 Mutex* DerivedPointerTableGC_lock = NULL;
aoqi@0 87 Mutex* Compile_lock = NULL;
aoqi@0 88 Monitor* MethodCompileQueue_lock = NULL;
aoqi@0 89 Monitor* CompileThread_lock = NULL;
aoqi@0 90 Mutex* CompileTaskAlloc_lock = NULL;
aoqi@0 91 Mutex* CompileStatistics_lock = NULL;
aoqi@0 92 Mutex* MultiArray_lock = NULL;
aoqi@0 93 Monitor* Terminator_lock = NULL;
aoqi@0 94 Monitor* BeforeExit_lock = NULL;
aoqi@0 95 Monitor* Notify_lock = NULL;
aoqi@0 96 Monitor* Interrupt_lock = NULL;
aoqi@0 97 Monitor* ProfileVM_lock = NULL;
aoqi@0 98 Mutex* ProfilePrint_lock = NULL;
aoqi@0 99 Mutex* ExceptionCache_lock = NULL;
aoqi@0 100 Monitor* ObjAllocPost_lock = NULL;
aoqi@0 101 Mutex* OsrList_lock = NULL;
aoqi@0 102 #ifndef PRODUCT
aoqi@0 103 Mutex* FullGCALot_lock = NULL;
aoqi@0 104 #endif
aoqi@0 105
aoqi@0 106 Mutex* Debug1_lock = NULL;
aoqi@0 107 Mutex* Debug2_lock = NULL;
aoqi@0 108 Mutex* Debug3_lock = NULL;
aoqi@0 109
aoqi@0 110 Mutex* tty_lock = NULL;
aoqi@0 111
aoqi@0 112 Mutex* RawMonitor_lock = NULL;
aoqi@0 113 Mutex* PerfDataMemAlloc_lock = NULL;
aoqi@0 114 Mutex* PerfDataManager_lock = NULL;
aoqi@0 115 Mutex* OopMapCacheAlloc_lock = NULL;
aoqi@0 116
aoqi@0 117 Mutex* FreeList_lock = NULL;
aoqi@0 118 Monitor* SecondaryFreeList_lock = NULL;
aoqi@0 119 Mutex* OldSets_lock = NULL;
aoqi@0 120 Monitor* RootRegionScan_lock = NULL;
aoqi@0 121 Mutex* MMUTracker_lock = NULL;
aoqi@0 122 Mutex* HotCardCache_lock = NULL;
aoqi@0 123
aoqi@0 124 Monitor* GCTaskManager_lock = NULL;
aoqi@0 125
aoqi@0 126 Mutex* Management_lock = NULL;
aoqi@0 127 Monitor* Service_lock = NULL;
aoqi@0 128 Monitor* PeriodicTask_lock = NULL;
aoqi@0 129
aoqi@0 130 #ifdef INCLUDE_TRACE
aoqi@0 131 Mutex* JfrStacktrace_lock = NULL;
aoqi@0 132 Monitor* JfrMsg_lock = NULL;
aoqi@0 133 Mutex* JfrBuffer_lock = NULL;
aoqi@0 134 Mutex* JfrStream_lock = NULL;
aoqi@0 135 Mutex* JfrThreadGroups_lock = NULL;
aoqi@0 136 #endif
aoqi@0 137
aoqi@0 138 #define MAX_NUM_MUTEX 128
aoqi@0 139 static Monitor * _mutex_array[MAX_NUM_MUTEX];
aoqi@0 140 static int _num_mutex;
aoqi@0 141
aoqi@0 142 #ifdef ASSERT
aoqi@0 143 void assert_locked_or_safepoint(const Monitor * lock) {
aoqi@0 144 // check if this thread owns the lock (common case)
aoqi@0 145 if (IgnoreLockingAssertions) return;
aoqi@0 146 assert(lock != NULL, "Need non-NULL lock");
aoqi@0 147 if (lock->owned_by_self()) return;
aoqi@0 148 if (SafepointSynchronize::is_at_safepoint()) return;
aoqi@0 149 if (!Universe::is_fully_initialized()) return;
aoqi@0 150 // see if invoker of VM operation owns it
aoqi@0 151 VM_Operation* op = VMThread::vm_operation();
aoqi@0 152 if (op != NULL && op->calling_thread() == lock->owner()) return;
aoqi@0 153 fatal(err_msg("must own lock %s", lock->name()));
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 // a stronger assertion than the above
aoqi@0 157 void assert_lock_strong(const Monitor * lock) {
aoqi@0 158 if (IgnoreLockingAssertions) return;
aoqi@0 159 assert(lock != NULL, "Need non-NULL lock");
aoqi@0 160 if (lock->owned_by_self()) return;
aoqi@0 161 fatal(err_msg("must own lock %s", lock->name()));
aoqi@0 162 }
aoqi@0 163 #endif
aoqi@0 164
aoqi@0 165 #define def(var, type, pri, vm_block) { \
aoqi@0 166 var = new type(Mutex::pri, #var, vm_block); \
aoqi@0 167 assert(_num_mutex < MAX_NUM_MUTEX, \
aoqi@0 168 "increase MAX_NUM_MUTEX"); \
aoqi@0 169 _mutex_array[_num_mutex++] = var; \
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 void mutex_init() {
aoqi@0 173 def(tty_lock , Mutex , event, true ); // allow to lock in VM
aoqi@0 174
aoqi@0 175 def(CGC_lock , Monitor, special, true ); // coordinate between fore- and background GC
pliden@6906 176 def(STS_lock , Monitor, leaf, true );
aoqi@0 177 if (UseConcMarkSweepGC) {
aoqi@0 178 def(iCMS_lock , Monitor, special, true ); // CMS incremental mode start/stop notification
aoqi@0 179 }
aoqi@0 180 if (UseConcMarkSweepGC || UseG1GC) {
aoqi@0 181 def(FullGCCount_lock , Monitor, leaf, true ); // in support of ExplicitGCInvokesConcurrent
aoqi@0 182 }
aoqi@0 183 if (UseG1GC) {
aoqi@0 184 def(CMark_lock , Monitor, nonleaf, true ); // coordinate concurrent mark thread
aoqi@0 185 def(CMRegionStack_lock , Mutex, leaf, true );
aoqi@0 186 def(SATB_Q_FL_lock , Mutex , special, true );
aoqi@0 187 def(SATB_Q_CBL_mon , Monitor, nonleaf, true );
aoqi@0 188 def(Shared_SATB_Q_lock , Mutex, nonleaf, true );
aoqi@0 189
aoqi@0 190 def(DirtyCardQ_FL_lock , Mutex , special, true );
aoqi@0 191 def(DirtyCardQ_CBL_mon , Monitor, nonleaf, true );
aoqi@0 192 def(Shared_DirtyCardQ_lock , Mutex, nonleaf, true );
aoqi@0 193
aoqi@0 194 def(FreeList_lock , Mutex, leaf , true );
aoqi@0 195 def(SecondaryFreeList_lock , Monitor, leaf , true );
aoqi@0 196 def(OldSets_lock , Mutex , leaf , true );
aoqi@0 197 def(RootRegionScan_lock , Monitor, leaf , true );
aoqi@0 198 def(MMUTracker_lock , Mutex , leaf , true );
aoqi@0 199 def(HotCardCache_lock , Mutex , special , true );
aoqi@0 200 def(EvacFailureStack_lock , Mutex , nonleaf , true );
aoqi@0 201
aoqi@0 202 def(StringDedupQueue_lock , Monitor, leaf, true );
aoqi@0 203 def(StringDedupTable_lock , Mutex , leaf, true );
aoqi@0 204 }
aoqi@0 205 def(ParGCRareEvent_lock , Mutex , leaf , true );
aoqi@0 206 def(DerivedPointerTableGC_lock , Mutex, leaf, true );
aoqi@0 207 def(CodeCache_lock , Mutex , special, true );
aoqi@0 208 def(Interrupt_lock , Monitor, special, true ); // used for interrupt processing
aoqi@0 209 def(RawMonitor_lock , Mutex, special, true );
aoqi@0 210 def(OopMapCacheAlloc_lock , Mutex, leaf, true ); // used for oop_map_cache allocation.
aoqi@0 211
aoqi@0 212 def(Patching_lock , Mutex , special, true ); // used for safepointing and code patching.
aoqi@0 213 def(ObjAllocPost_lock , Monitor, special, false);
aoqi@0 214 def(Service_lock , Monitor, special, true ); // used for service thread operations
aoqi@0 215 def(JmethodIdCreation_lock , Mutex , leaf, true ); // used for creating jmethodIDs.
aoqi@0 216
aoqi@0 217 def(SystemDictionary_lock , Monitor, leaf, true ); // lookups done by VM thread
aoqi@0 218 def(PackageTable_lock , Mutex , leaf, false);
aoqi@0 219 def(InlineCacheBuffer_lock , Mutex , leaf, true );
aoqi@0 220 def(VMStatistic_lock , Mutex , leaf, false);
aoqi@0 221 def(ExpandHeap_lock , Mutex , leaf, true ); // Used during compilation by VM thread
aoqi@0 222 def(JNIHandleBlockFreeList_lock , Mutex , leaf, true ); // handles are used by VM thread
aoqi@0 223 def(SignatureHandlerLibrary_lock , Mutex , leaf, false);
aoqi@0 224 def(SymbolTable_lock , Mutex , leaf+2, true );
aoqi@0 225 def(StringTable_lock , Mutex , leaf, true );
aoqi@0 226 def(ProfilePrint_lock , Mutex , leaf, false); // serial profile printing
aoqi@0 227 def(ExceptionCache_lock , Mutex , leaf, false); // serial profile printing
aoqi@0 228 def(OsrList_lock , Mutex , leaf, true );
aoqi@0 229 def(Debug1_lock , Mutex , leaf, true );
aoqi@0 230 #ifndef PRODUCT
aoqi@0 231 def(FullGCALot_lock , Mutex , leaf, false); // a lock to make FullGCALot MT safe
aoqi@0 232 #endif
aoqi@0 233 def(BeforeExit_lock , Monitor, leaf, true );
aoqi@0 234 def(PerfDataMemAlloc_lock , Mutex , leaf, true ); // used for allocating PerfData memory for performance data
aoqi@0 235 def(PerfDataManager_lock , Mutex , leaf, true ); // used for synchronized access to PerfDataManager resources
aoqi@0 236
aoqi@0 237 // CMS_modUnionTable_lock leaf
aoqi@0 238 // CMS_bitMap_lock leaf + 1
aoqi@0 239 // CMS_freeList_lock leaf + 2
aoqi@0 240
aoqi@0 241 def(Safepoint_lock , Monitor, safepoint, true ); // locks SnippetCache_lock/Threads_lock
aoqi@0 242
aoqi@0 243 def(Threads_lock , Monitor, barrier, true );
aoqi@0 244
aoqi@0 245 def(VMOperationQueue_lock , Monitor, nonleaf, true ); // VM_thread allowed to block on these
aoqi@0 246 def(VMOperationRequest_lock , Monitor, nonleaf, true );
aoqi@0 247 def(RetData_lock , Mutex , nonleaf, false);
aoqi@0 248 def(Terminator_lock , Monitor, nonleaf, true );
aoqi@0 249 def(VtableStubs_lock , Mutex , nonleaf, true );
aoqi@0 250 def(Notify_lock , Monitor, nonleaf, true );
aoqi@0 251 def(JNIGlobalHandle_lock , Mutex , nonleaf, true ); // locks JNIHandleBlockFreeList_lock
aoqi@0 252 def(JNICritical_lock , Monitor, nonleaf, true ); // used for JNI critical regions
aoqi@0 253 def(AdapterHandlerLibrary_lock , Mutex , nonleaf, true);
aoqi@0 254 if (UseConcMarkSweepGC) {
aoqi@0 255 def(SLT_lock , Monitor, nonleaf, false );
aoqi@0 256 // used in CMS GC for locking PLL lock
aoqi@0 257 }
aoqi@0 258 def(Heap_lock , Monitor, nonleaf+1, false);
aoqi@0 259 def(JfieldIdCreation_lock , Mutex , nonleaf+1, true ); // jfieldID, Used in VM_Operation
aoqi@0 260 def(MemberNameTable_lock , Mutex , nonleaf+1, false); // Used to protect MemberNameTable
aoqi@0 261
aoqi@0 262 def(CompiledIC_lock , Mutex , nonleaf+2, false); // locks VtableStubs_lock, InlineCacheBuffer_lock
aoqi@0 263 def(CompileTaskAlloc_lock , Mutex , nonleaf+2, true );
aoqi@0 264 def(CompileStatistics_lock , Mutex , nonleaf+2, false);
aoqi@0 265 def(MultiArray_lock , Mutex , nonleaf+2, false); // locks SymbolTable_lock
aoqi@0 266
aoqi@0 267 def(JvmtiThreadState_lock , Mutex , nonleaf+2, false); // Used by JvmtiThreadState/JvmtiEventController
aoqi@0 268 def(JvmtiPendingEvent_lock , Monitor, nonleaf, false); // Used by JvmtiCodeBlobEvents
aoqi@0 269 def(Management_lock , Mutex , nonleaf+2, false); // used for JVM management
aoqi@0 270
aoqi@0 271 def(Compile_lock , Mutex , nonleaf+3, true );
aoqi@0 272 def(MethodData_lock , Mutex , nonleaf+3, false);
aoqi@0 273
aoqi@0 274 def(MethodCompileQueue_lock , Monitor, nonleaf+4, true );
aoqi@0 275 def(Debug2_lock , Mutex , nonleaf+4, true );
aoqi@0 276 def(Debug3_lock , Mutex , nonleaf+4, true );
aoqi@0 277 def(ProfileVM_lock , Monitor, special, false); // used for profiling of the VMThread
aoqi@0 278 def(CompileThread_lock , Monitor, nonleaf+5, false );
aoqi@0 279 def(PeriodicTask_lock , Monitor, nonleaf+5, true);
aoqi@0 280
aoqi@0 281 #ifdef INCLUDE_TRACE
aoqi@0 282 def(JfrMsg_lock , Monitor, leaf, true);
aoqi@0 283 def(JfrBuffer_lock , Mutex, nonleaf+1, true);
aoqi@0 284 def(JfrThreadGroups_lock , Mutex, nonleaf+1, true);
aoqi@0 285 def(JfrStream_lock , Mutex, nonleaf+2, true);
aoqi@0 286 def(JfrStacktrace_lock , Mutex, special, true );
aoqi@0 287 #endif
aoqi@0 288
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 GCMutexLocker::GCMutexLocker(Monitor * mutex) {
aoqi@0 292 if (SafepointSynchronize::is_at_safepoint()) {
aoqi@0 293 _locked = false;
aoqi@0 294 } else {
aoqi@0 295 _mutex = mutex;
aoqi@0 296 _locked = true;
aoqi@0 297 _mutex->lock();
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 // Print all mutexes/monitors that are currently owned by a thread; called
aoqi@0 302 // by fatal error handler.
aoqi@0 303 void print_owned_locks_on_error(outputStream* st) {
aoqi@0 304 st->print("VM Mutex/Monitor currently owned by a thread: ");
aoqi@0 305 bool none = true;
aoqi@0 306 for (int i = 0; i < _num_mutex; i++) {
aoqi@0 307 // see if it has an owner
aoqi@0 308 if (_mutex_array[i]->owner() != NULL) {
aoqi@0 309 if (none) {
aoqi@0 310 // print format used by Mutex::print_on_error()
aoqi@0 311 st->print_cr(" ([mutex/lock_event])");
aoqi@0 312 none = false;
aoqi@0 313 }
aoqi@0 314 _mutex_array[i]->print_on_error(st);
aoqi@0 315 st->cr();
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318 if (none) st->print_cr("None");
aoqi@0 319 }

mercurial