mutexLocker.cpp revision 7551:c75901698a47
172172Sphantom/* 2290494Sbapt * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3290494Sbapt * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 472172Sphantom * 5290083Sbdrewery * This code is free software; you can redistribute it and/or modify it 6290494Sbapt * under the terms of the GNU General Public License version 2 only, as 7290494Sbapt * published by the Free Software Foundation. 872172Sphantom * 972172Sphantom * This code is distributed in the hope that it will be useful, but WITHOUT 10290494Sbapt * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1172172Sphantom * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12290494Sbapt * version 2 for more details (a copy is included in the LICENSE file that 13290494Sbapt * accompanied this code). 14290494Sbapt * 15290494Sbapt * You should have received a copy of the GNU General Public License version 16290494Sbapt * 2 along with this work; if not, write to the Free Software Foundation, 17290494Sbapt * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18290494Sbapt * 19290494Sbapt * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20290494Sbapt * or visit www.oracle.com if you need additional information or have any 21290494Sbapt * questions. 22290494Sbapt * 23290494Sbapt */ 24290494Sbapt 25290494Sbapt#include "precompiled.hpp" 26290494Sbapt#include "runtime/mutexLocker.hpp" 27290866Sbapt#include "runtime/os.inline.hpp" 28290494Sbapt#include "runtime/safepoint.hpp" 29290494Sbapt#include "runtime/thread.inline.hpp" 30290494Sbapt#include "runtime/threadLocalStorage.hpp" 31290494Sbapt#include "runtime/vmThread.hpp" 32290494Sbapt 33290494Sbapt// Mutexes used in the VM (see comment in mutexLocker.hpp): 34290494Sbapt// 35290866Sbapt// Note that the following pointers are effectively final -- after having been 36290494Sbapt// set at JVM startup-time, they should never be subsequently mutated. 37290494Sbapt// Instead of using pointers to malloc()ed monitors and mutexes we should consider 38290494Sbapt// eliminating the indirection and using instances instead. 39290494Sbapt// Consider using GCC's __read_mostly. 40290494Sbapt 41290494SbaptMutex* Patching_lock = NULL; 42290928SbaptMonitor* SystemDictionary_lock = NULL; 43290494SbaptMutex* PackageTable_lock = NULL; 44290494SbaptMutex* CompiledIC_lock = NULL; 45290494SbaptMutex* InlineCacheBuffer_lock = NULL; 46290494SbaptMutex* VMStatistic_lock = NULL; 47290494SbaptMutex* JNIGlobalHandle_lock = NULL; 48290928SbaptMutex* JNIHandleBlockFreeList_lock = NULL; 49290494SbaptMutex* MemberNameTable_lock = NULL; 50290494SbaptMutex* JmethodIdCreation_lock = NULL; 51290494SbaptMutex* JfieldIdCreation_lock = NULL; 52290494SbaptMonitor* JNICritical_lock = NULL; 53290494SbaptMutex* JvmtiThreadState_lock = NULL; 54290494SbaptMonitor* JvmtiPendingEvent_lock = NULL; 55290494SbaptMonitor* Heap_lock = NULL; 56290494SbaptMutex* ExpandHeap_lock = NULL; 57290494SbaptMutex* AdapterHandlerLibrary_lock = NULL; 58290494SbaptMutex* SignatureHandlerLibrary_lock = NULL; 59290494SbaptMutex* VtableStubs_lock = NULL; 60290494SbaptMutex* SymbolTable_lock = NULL; 61325928SbaptMutex* StringTable_lock = NULL; 62290494SbaptMonitor* StringDedupQueue_lock = NULL; 63290494SbaptMutex* StringDedupTable_lock = NULL; 64290494SbaptMonitor* CodeCache_lock = NULL; 65290494SbaptMutex* MethodData_lock = NULL; 66302329SbaptMutex* RetData_lock = NULL; 67290494SbaptMonitor* VMOperationQueue_lock = NULL; 68290494SbaptMonitor* VMOperationRequest_lock = NULL; 69302329SbaptMonitor* Safepoint_lock = NULL; 70290494SbaptMonitor* SerializePage_lock = NULL; 71290494SbaptMonitor* Threads_lock = NULL; 72290494SbaptMonitor* CGC_lock = NULL; 73312336SbaptMonitor* STS_lock = NULL; 74290866SbaptMonitor* SLT_lock = NULL; 75290494SbaptMonitor* FullGCCount_lock = NULL; 76290494SbaptMonitor* CMark_lock = NULL; 77290494SbaptMutex* CMRegionStack_lock = NULL; 78290494SbaptMutex* SATB_Q_FL_lock = NULL; 79290494SbaptMonitor* SATB_Q_CBL_mon = NULL; 80290494SbaptMutex* Shared_SATB_Q_lock = NULL; 81290494SbaptMutex* DirtyCardQ_FL_lock = NULL; 82290494SbaptMonitor* DirtyCardQ_CBL_mon = NULL; 83290494SbaptMutex* Shared_DirtyCardQ_lock = NULL; 84290494SbaptMutex* ParGCRareEvent_lock = NULL; 85290494SbaptMutex* EvacFailureStack_lock = NULL; 86290494SbaptMutex* DerivedPointerTableGC_lock = NULL; 87290494SbaptMutex* Compile_lock = NULL; 88290494SbaptMonitor* MethodCompileQueue_lock = NULL; 89290494SbaptMonitor* CompileThread_lock = NULL; 90302329SbaptMonitor* Compilation_lock = NULL; 91290866SbaptMutex* CompileTaskAlloc_lock = NULL; 92290494SbaptMutex* CompileStatistics_lock = NULL; 93290494SbaptMutex* MultiArray_lock = NULL; 94290494SbaptMonitor* Terminator_lock = NULL; 95290494SbaptMonitor* BeforeExit_lock = NULL; 96290494SbaptMonitor* Notify_lock = NULL; 97290494SbaptMonitor* Interrupt_lock = NULL; 98290494SbaptMonitor* ProfileVM_lock = NULL; 99290494SbaptMutex* ProfilePrint_lock = NULL; 100290494SbaptMutex* ExceptionCache_lock = NULL; 101290494SbaptMonitor* ObjAllocPost_lock = NULL; 102302329SbaptMutex* OsrList_lock = NULL; 103302329Sbapt#ifndef PRODUCT 104302329SbaptMutex* FullGCALot_lock = NULL; 105302329Sbapt#endif 106302329Sbapt 107302329SbaptMutex* Debug1_lock = NULL; 108302329SbaptMutex* Debug2_lock = NULL; 10972172SphantomMutex* Debug3_lock = NULL; 110298121Sbapt 111298121SbaptMutex* tty_lock = NULL; 112292453Sbapt 113292453SbaptMutex* RawMonitor_lock = NULL; 114292453SbaptMutex* PerfDataMemAlloc_lock = NULL; 115292453SbaptMutex* PerfDataManager_lock = NULL; 116312336SbaptMutex* OopMapCacheAlloc_lock = NULL; 117292453Sbapt 118292453SbaptMutex* FreeList_lock = NULL; 119292453SbaptMonitor* SecondaryFreeList_lock = NULL; 120292453SbaptMutex* OldSets_lock = NULL; 121292453SbaptMonitor* RootRegionScan_lock = NULL; 122292453SbaptMutex* MMUTracker_lock = NULL; 123292453SbaptMutex* HotCardCache_lock = NULL; 124292453Sbapt 125292453SbaptMonitor* GCTaskManager_lock = NULL; 126292453Sbapt 127292453SbaptMutex* Management_lock = NULL; 128292453SbaptMonitor* Service_lock = NULL; 129292453SbaptMonitor* PeriodicTask_lock = NULL; 130292453Sbapt 131292453Sbapt#ifdef INCLUDE_TRACE 132292453SbaptMutex* JfrStacktrace_lock = NULL; 133292453SbaptMonitor* JfrMsg_lock = NULL; 134292453SbaptMutex* JfrBuffer_lock = NULL; 135312336SbaptMutex* JfrStream_lock = NULL; 136312336SbaptMutex* JfrThreadGroups_lock = NULL; 137312336Sbapt#endif 138312336Sbapt 139312336Sbapt#ifndef SUPPORTS_NATIVE_CX8 140312336SbaptMutex* UnsafeJlong_lock = NULL; 141312336Sbapt#endif 142312336Sbapt 143292453Sbapt#define MAX_NUM_MUTEX 128 144292453Sbaptstatic Monitor * _mutex_array[MAX_NUM_MUTEX]; 145325928Sbaptstatic int _num_mutex; 146325928Sbapt 147325928Sbapt#ifdef ASSERT 148292453Sbaptvoid assert_locked_or_safepoint(const Monitor * lock) { 149292453Sbapt // check if this thread owns the lock (common case) 150292453Sbapt if (IgnoreLockingAssertions) return; 151292453Sbapt assert(lock != NULL, "Need non-NULL lock"); 152292453Sbapt if (lock->owned_by_self()) return; 153292453Sbapt if (SafepointSynchronize::is_at_safepoint()) return; 154292453Sbapt if (!Universe::is_fully_initialized()) return; 155292453Sbapt // see if invoker of VM operation owns it 156302329Sbapt VM_Operation* op = VMThread::vm_operation(); 157302329Sbapt if (op != NULL && op->calling_thread() == lock->owner()) return; 158292453Sbapt fatal(err_msg("must own lock %s", lock->name())); 159292453Sbapt} 160292453Sbapt 161292453Sbapt// a stronger assertion than the above 162292453Sbaptvoid assert_lock_strong(const Monitor * lock) { 163292453Sbapt if (IgnoreLockingAssertions) return; 164292453Sbapt assert(lock != NULL, "Need non-NULL lock"); 165292453Sbapt if (lock->owned_by_self()) return; 166292453Sbapt fatal(err_msg("must own lock %s", lock->name())); 167292453Sbapt} 168292453Sbapt#endif 169312336Sbapt 170292453Sbapt#define def(var, type, pri, vm_block, safepoint_check_allowed ) { \ 171292453Sbapt var = new type(Mutex::pri, #var, vm_block, safepoint_check_allowed); \ 172312336Sbapt assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX"); \ 173292453Sbapt _mutex_array[_num_mutex] = var; \ 174292453Sbapt} 175292453Sbapt 176292453Sbaptvoid mutex_init() { 177312336Sbapt def(tty_lock , Mutex , event, true, Monitor::_safepoint_check_never); // allow to lock in VM 178292453Sbapt 179292453Sbapt def(CGC_lock , Monitor, special, true, Monitor::_safepoint_check_never); // coordinate between fore- and background GC 180292453Sbapt def(STS_lock , Monitor, leaf, true, Monitor::_safepoint_check_never); 181292453Sbapt 182292453Sbapt if (UseConcMarkSweepGC || UseG1GC) { 183312336Sbapt def(FullGCCount_lock , Monitor, leaf, true, Monitor::_safepoint_check_never); // in support of ExplicitGCInvokesConcurrent 184325928Sbapt } 185312336Sbapt if (UseG1GC) { 186312336Sbapt 187312336Sbapt def(CMark_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_never); // coordinate concurrent mark thread 188312336Sbapt def(CMRegionStack_lock , Mutex, leaf, true, Monitor::_safepoint_check_never); 189292453Sbapt def(SATB_Q_FL_lock , Mutex , special, true, Monitor::_safepoint_check_never); 190292453Sbapt def(SATB_Q_CBL_mon , Monitor, nonleaf, true, Monitor::_safepoint_check_never); 191292453Sbapt def(Shared_SATB_Q_lock , Mutex, nonleaf, true, Monitor::_safepoint_check_never); 192292453Sbapt 193292453Sbapt def(DirtyCardQ_FL_lock , Mutex , special, true, Monitor::_safepoint_check_never); 194292453Sbapt def(DirtyCardQ_CBL_mon , Monitor, nonleaf, true, Monitor::_safepoint_check_never); 195292453Sbapt def(Shared_DirtyCardQ_lock , Mutex, nonleaf, true, Monitor::_safepoint_check_never); 196292453Sbapt 197292453Sbapt def(FreeList_lock , Mutex, leaf , true, Monitor::_safepoint_check_never); 198292453Sbapt def(SecondaryFreeList_lock , Monitor, leaf , true, Monitor::_safepoint_check_never); 199312336Sbapt def(OldSets_lock , Mutex , leaf , true, Monitor::_safepoint_check_never); 200302329Sbapt def(RootRegionScan_lock , Monitor, leaf , true, Monitor::_safepoint_check_never); 201302329Sbapt def(MMUTracker_lock , Mutex , leaf , true, Monitor::_safepoint_check_never); 202302329Sbapt def(HotCardCache_lock , Mutex , special , true, Monitor::_safepoint_check_never); 203292453Sbapt def(EvacFailureStack_lock , Mutex , nonleaf , true, Monitor::_safepoint_check_never); 204302329Sbapt 205292453Sbapt def(StringDedupQueue_lock , Monitor, leaf, true, Monitor::_safepoint_check_never); 206136487Sru def(StringDedupTable_lock , Mutex , leaf, true, Monitor::_safepoint_check_never); 207290494Sbapt } 208136487Sru def(ParGCRareEvent_lock , Mutex , leaf , true, Monitor::_safepoint_check_sometimes); 209136487Sru def(DerivedPointerTableGC_lock , Mutex, leaf, true, Monitor::_safepoint_check_never); 210292453Sbapt def(CodeCache_lock , Mutex , special, true, Monitor::_safepoint_check_never); 211292453Sbapt def(Interrupt_lock , Monitor, special, true, Monitor::_safepoint_check_never); // used for interrupt processing 212292453Sbapt def(RawMonitor_lock , Mutex, special, true, Monitor::_safepoint_check_never); 213290494Sbapt def(OopMapCacheAlloc_lock , Mutex, leaf, true, Monitor::_safepoint_check_always); // used for oop_map_cache allocation. 214136487Sru 215290494Sbapt def(Patching_lock , Mutex , special, true, Monitor::_safepoint_check_never); // used for safepointing and code patching. 216290494Sbapt def(ObjAllocPost_lock , Monitor, special, false, Monitor::_safepoint_check_never); 21778010Sache def(Service_lock , Monitor, special, true, Monitor::_safepoint_check_never); // used for service thread operations 218127474Stjr def(JmethodIdCreation_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); // used for creating jmethodIDs. 21972172Sphantom 220 def(SystemDictionary_lock , Monitor, leaf, true, Monitor::_safepoint_check_always); // lookups done by VM thread 221 def(PackageTable_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); 222 def(InlineCacheBuffer_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); 223 def(VMStatistic_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); 224 def(ExpandHeap_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); // Used during compilation by VM thread 225 def(JNIHandleBlockFreeList_lock , Mutex , leaf, true, Monitor::_safepoint_check_never); // handles are used by VM thread 226 def(SignatureHandlerLibrary_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); 227 def(SymbolTable_lock , Mutex , leaf+2, true, Monitor::_safepoint_check_always); 228 def(StringTable_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); 229 def(ProfilePrint_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); // serial profile printing 230 def(ExceptionCache_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); // serial profile printing 231 def(OsrList_lock , Mutex , leaf, true, Monitor::_safepoint_check_never); 232 def(Debug1_lock , Mutex , leaf, true, Monitor::_safepoint_check_never); 233#ifndef PRODUCT 234 def(FullGCALot_lock , Mutex , leaf, false, Monitor::_safepoint_check_always); // a lock to make FullGCALot MT safe 235#endif 236 def(BeforeExit_lock , Monitor, leaf, true, Monitor::_safepoint_check_always); 237 def(PerfDataMemAlloc_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); // used for allocating PerfData memory for performance data 238 def(PerfDataManager_lock , Mutex , leaf, true, Monitor::_safepoint_check_always); // used for synchronized access to PerfDataManager resources 239 240 // CMS_modUnionTable_lock leaf 241 // CMS_bitMap_lock leaf 1 242 // CMS_freeList_lock leaf 2 243 244 def(Safepoint_lock , Monitor, safepoint, true, Monitor::_safepoint_check_sometimes); // locks SnippetCache_lock/Threads_lock 245 246 def(Threads_lock , Monitor, barrier, true, Monitor::_safepoint_check_sometimes); 247 248 def(VMOperationQueue_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_sometimes); // VM_thread allowed to block on these 249 def(VMOperationRequest_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_sometimes); 250 def(RetData_lock , Mutex , nonleaf, false, Monitor::_safepoint_check_always); 251 def(Terminator_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_sometimes); 252 def(VtableStubs_lock , Mutex , nonleaf, true, Monitor::_safepoint_check_always); 253 def(Notify_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_always); 254 def(JNIGlobalHandle_lock , Mutex , nonleaf, true, Monitor::_safepoint_check_always); // locks JNIHandleBlockFreeList_lock 255 def(JNICritical_lock , Monitor, nonleaf, true, Monitor::_safepoint_check_always); // used for JNI critical regions 256 def(AdapterHandlerLibrary_lock , Mutex , nonleaf, true, Monitor::_safepoint_check_always); 257 if (UseConcMarkSweepGC) { 258 def(SLT_lock , Monitor, nonleaf, false, Monitor::_safepoint_check_never); // used in CMS GC for locking PLL lock 259 } 260 261 def(Heap_lock , Monitor, nonleaf+1, false, Monitor::_safepoint_check_sometimes); 262 def(JfieldIdCreation_lock , Mutex , nonleaf+1, true, Monitor::_safepoint_check_always); // jfieldID, Used in VM_Operation 263 def(MemberNameTable_lock , Mutex , nonleaf+1, false, Monitor::_safepoint_check_always); // Used to protect MemberNameTable 264 265 def(CompiledIC_lock , Mutex , nonleaf+2, false, Monitor::_safepoint_check_always); // locks VtableStubs_lock, InlineCacheBuffer_lock 266 def(CompileTaskAlloc_lock , Mutex , nonleaf+2, true, Monitor::_safepoint_check_always); 267 def(CompileStatistics_lock , Mutex , nonleaf+2, false, Monitor::_safepoint_check_always); 268 def(MultiArray_lock , Mutex , nonleaf+2, false, Monitor::_safepoint_check_always); // locks SymbolTable_lock 269 270 def(JvmtiThreadState_lock , Mutex , nonleaf+2, false, Monitor::_safepoint_check_always); // Used by JvmtiThreadState/JvmtiEventController 271 def(JvmtiPendingEvent_lock , Monitor, nonleaf, false, Monitor::_safepoint_check_never); // Used by JvmtiCodeBlobEvents 272 def(Management_lock , Mutex , nonleaf+2, false, Monitor::_safepoint_check_always); // used for JVM management 273 274 def(Compile_lock , Mutex , nonleaf+3, true, Monitor::_safepoint_check_sometimes); 275 def(MethodData_lock , Mutex , nonleaf+3, false, Monitor::_safepoint_check_always); 276 277 def(MethodCompileQueue_lock , Monitor, nonleaf+4, true, Monitor::_safepoint_check_always); 278 def(Debug2_lock , Mutex , nonleaf+4, true, Monitor::_safepoint_check_never); 279 def(Debug3_lock , Mutex , nonleaf+4, true, Monitor::_safepoint_check_never); 280 def(ProfileVM_lock , Monitor, special, false, Monitor::_safepoint_check_never); // used for profiling of the VMThread 281 def(CompileThread_lock , Monitor, nonleaf+5, false, Monitor::_safepoint_check_always); 282 def(PeriodicTask_lock , Monitor, nonleaf+5, true, Monitor::_safepoint_check_sometimes); 283 if (WhiteBoxAPI) { 284 def(Compilation_lock , Monitor, leaf, false, Monitor::_safepoint_check_never); 285 } 286 287#ifdef INCLUDE_TRACE 288 def(JfrMsg_lock , Monitor, leaf, true, Monitor::_safepoint_check_always); 289 def(JfrBuffer_lock , Mutex, leaf, true, Monitor::_safepoint_check_never); 290 def(JfrThreadGroups_lock , Mutex, leaf, true, Monitor::_safepoint_check_always); 291 def(JfrStream_lock , Mutex, nonleaf, true, Monitor::_safepoint_check_never); 292 def(JfrStacktrace_lock , Mutex, special, true, Monitor::_safepoint_check_sometimes); 293#endif 294 295#ifndef SUPPORTS_NATIVE_CX8 296 def(UnsafeJlong_lock , Mutex, special, false, Monitor::_safepoint_check_never); 297#endif 298} 299 300GCMutexLocker::GCMutexLocker(Monitor * mutex) { 301 if (SafepointSynchronize::is_at_safepoint()) { 302 _locked = false; 303 } else { 304 _mutex = mutex; 305 _locked = true; 306 _mutex->lock(); 307 } 308} 309 310// Print all mutexes/monitors that are currently owned by a thread; called 311// by fatal error handler. 312void print_owned_locks_on_error(outputStream* st) { 313 st->print("VM Mutex/Monitor currently owned by a thread: "); 314 bool none = true; 315 for (int i = 0; i < _num_mutex; i++) { 316 // see if it has an owner 317 if (_mutex_array[i]->owner() != NULL) { 318 if (none) { 319 // print format used by Mutex::print_on_error() 320 st->print_cr(" ([mutex/lock_event])"); 321 none = false; 322 } 323 _mutex_array[i]->print_on_error(st); 324 st->cr(); 325 } 326 } 327 if (none) st->print_cr("None"); 328} 329