aoqi@0: /* redestad@7653: * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/safepoint.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/threadLocalStorage.hpp" aoqi@0: #include "runtime/vmThread.hpp" aoqi@0: aoqi@0: // Mutexes used in the VM (see comment in mutexLocker.hpp): aoqi@0: // aoqi@0: // Note that the following pointers are effectively final -- after having been aoqi@0: // set at JVM startup-time, they should never be subsequently mutated. aoqi@0: // Instead of using pointers to malloc()ed monitors and mutexes we should consider aoqi@0: // eliminating the indirection and using instances instead. aoqi@0: // Consider using GCC's __read_mostly. aoqi@0: aoqi@0: Mutex* Patching_lock = NULL; aoqi@0: Monitor* SystemDictionary_lock = NULL; aoqi@0: Mutex* PackageTable_lock = NULL; aoqi@0: Mutex* CompiledIC_lock = NULL; aoqi@0: Mutex* InlineCacheBuffer_lock = NULL; aoqi@0: Mutex* VMStatistic_lock = NULL; aoqi@0: Mutex* JNIGlobalHandle_lock = NULL; aoqi@0: Mutex* JNIHandleBlockFreeList_lock = NULL; aoqi@0: Mutex* MemberNameTable_lock = NULL; aoqi@0: Mutex* JmethodIdCreation_lock = NULL; aoqi@0: Mutex* JfieldIdCreation_lock = NULL; aoqi@0: Monitor* JNICritical_lock = NULL; aoqi@0: Mutex* JvmtiThreadState_lock = NULL; aoqi@0: Monitor* JvmtiPendingEvent_lock = NULL; aoqi@0: Monitor* Heap_lock = NULL; aoqi@0: Mutex* ExpandHeap_lock = NULL; aoqi@0: Mutex* AdapterHandlerLibrary_lock = NULL; aoqi@0: Mutex* SignatureHandlerLibrary_lock = NULL; aoqi@0: Mutex* VtableStubs_lock = NULL; aoqi@0: Mutex* SymbolTable_lock = NULL; aoqi@0: Mutex* StringTable_lock = NULL; aoqi@0: Monitor* StringDedupQueue_lock = NULL; aoqi@0: Mutex* StringDedupTable_lock = NULL; aoqi@0: Mutex* CodeCache_lock = NULL; aoqi@0: Mutex* MethodData_lock = NULL; aoqi@0: Mutex* RetData_lock = NULL; aoqi@0: Monitor* VMOperationQueue_lock = NULL; aoqi@0: Monitor* VMOperationRequest_lock = NULL; aoqi@0: Monitor* Safepoint_lock = NULL; aoqi@0: Monitor* SerializePage_lock = NULL; aoqi@0: Monitor* Threads_lock = NULL; aoqi@0: Monitor* CGC_lock = NULL; pliden@6906: Monitor* STS_lock = NULL; aoqi@0: Monitor* SLT_lock = NULL; aoqi@0: Monitor* iCMS_lock = NULL; aoqi@0: Monitor* FullGCCount_lock = NULL; aoqi@0: Monitor* CMark_lock = NULL; aoqi@0: Mutex* CMRegionStack_lock = NULL; aoqi@0: Mutex* SATB_Q_FL_lock = NULL; aoqi@0: Monitor* SATB_Q_CBL_mon = NULL; aoqi@0: Mutex* Shared_SATB_Q_lock = NULL; aoqi@0: Mutex* DirtyCardQ_FL_lock = NULL; aoqi@0: Monitor* DirtyCardQ_CBL_mon = NULL; aoqi@0: Mutex* Shared_DirtyCardQ_lock = NULL; aoqi@0: Mutex* ParGCRareEvent_lock = NULL; aoqi@0: Mutex* EvacFailureStack_lock = NULL; aoqi@0: Mutex* DerivedPointerTableGC_lock = NULL; aoqi@0: Mutex* Compile_lock = NULL; aoqi@0: Monitor* MethodCompileQueue_lock = NULL; aoqi@0: Monitor* CompileThread_lock = NULL; aoqi@0: Mutex* CompileTaskAlloc_lock = NULL; aoqi@0: Mutex* CompileStatistics_lock = NULL; aoqi@0: Mutex* MultiArray_lock = NULL; aoqi@0: Monitor* Terminator_lock = NULL; aoqi@0: Monitor* BeforeExit_lock = NULL; aoqi@0: Monitor* Notify_lock = NULL; aoqi@0: Monitor* Interrupt_lock = NULL; aoqi@0: Monitor* ProfileVM_lock = NULL; aoqi@0: Mutex* ProfilePrint_lock = NULL; aoqi@0: Mutex* ExceptionCache_lock = NULL; aoqi@0: Monitor* ObjAllocPost_lock = NULL; aoqi@0: Mutex* OsrList_lock = NULL; aoqi@0: #ifndef PRODUCT aoqi@0: Mutex* FullGCALot_lock = NULL; aoqi@0: #endif aoqi@0: aoqi@0: Mutex* Debug1_lock = NULL; aoqi@0: Mutex* Debug2_lock = NULL; aoqi@0: Mutex* Debug3_lock = NULL; aoqi@0: aoqi@0: Mutex* tty_lock = NULL; aoqi@0: aoqi@0: Mutex* RawMonitor_lock = NULL; aoqi@0: Mutex* PerfDataMemAlloc_lock = NULL; aoqi@0: Mutex* PerfDataManager_lock = NULL; aoqi@0: Mutex* OopMapCacheAlloc_lock = NULL; aoqi@0: aoqi@0: Mutex* FreeList_lock = NULL; aoqi@0: Monitor* SecondaryFreeList_lock = NULL; aoqi@0: Mutex* OldSets_lock = NULL; aoqi@0: Monitor* RootRegionScan_lock = NULL; aoqi@0: Mutex* MMUTracker_lock = NULL; aoqi@0: aoqi@0: Monitor* GCTaskManager_lock = NULL; aoqi@0: aoqi@0: Mutex* Management_lock = NULL; aoqi@0: Monitor* Service_lock = NULL; aoqi@0: Monitor* PeriodicTask_lock = NULL; aoqi@0: aoqi@0: #ifdef INCLUDE_TRACE aoqi@0: Mutex* JfrStacktrace_lock = NULL; aoqi@0: Monitor* JfrMsg_lock = NULL; aoqi@0: Mutex* JfrBuffer_lock = NULL; aoqi@0: Mutex* JfrStream_lock = NULL; aoqi@0: Mutex* JfrThreadGroups_lock = NULL; aoqi@0: #endif aoqi@0: dholmes@7547: #ifndef SUPPORTS_NATIVE_CX8 dholmes@7547: Mutex* UnsafeJlong_lock = NULL; dholmes@7547: #endif dholmes@7547: aoqi@0: #define MAX_NUM_MUTEX 128 aoqi@0: static Monitor * _mutex_array[MAX_NUM_MUTEX]; aoqi@0: static int _num_mutex; aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: void assert_locked_or_safepoint(const Monitor * lock) { aoqi@0: // check if this thread owns the lock (common case) aoqi@0: if (IgnoreLockingAssertions) return; aoqi@0: assert(lock != NULL, "Need non-NULL lock"); aoqi@0: if (lock->owned_by_self()) return; aoqi@0: if (SafepointSynchronize::is_at_safepoint()) return; aoqi@0: if (!Universe::is_fully_initialized()) return; aoqi@0: // see if invoker of VM operation owns it aoqi@0: VM_Operation* op = VMThread::vm_operation(); aoqi@0: if (op != NULL && op->calling_thread() == lock->owner()) return; aoqi@0: fatal(err_msg("must own lock %s", lock->name())); aoqi@0: } aoqi@0: aoqi@0: // a stronger assertion than the above aoqi@0: void assert_lock_strong(const Monitor * lock) { aoqi@0: if (IgnoreLockingAssertions) return; aoqi@0: assert(lock != NULL, "Need non-NULL lock"); aoqi@0: if (lock->owned_by_self()) return; aoqi@0: fatal(err_msg("must own lock %s", lock->name())); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: #define def(var, type, pri, vm_block) { \ aoqi@0: var = new type(Mutex::pri, #var, vm_block); \ aoqi@0: assert(_num_mutex < MAX_NUM_MUTEX, \ aoqi@0: "increase MAX_NUM_MUTEX"); \ aoqi@0: _mutex_array[_num_mutex++] = var; \ aoqi@0: } aoqi@0: aoqi@0: void mutex_init() { aoqi@0: def(tty_lock , Mutex , event, true ); // allow to lock in VM aoqi@0: aoqi@0: def(CGC_lock , Monitor, special, true ); // coordinate between fore- and background GC pliden@6906: def(STS_lock , Monitor, leaf, true ); aoqi@0: if (UseConcMarkSweepGC) { aoqi@0: def(iCMS_lock , Monitor, special, true ); // CMS incremental mode start/stop notification aoqi@0: } aoqi@0: if (UseConcMarkSweepGC || UseG1GC) { aoqi@0: def(FullGCCount_lock , Monitor, leaf, true ); // in support of ExplicitGCInvokesConcurrent aoqi@0: } aoqi@0: if (UseG1GC) { aoqi@0: def(CMark_lock , Monitor, nonleaf, true ); // coordinate concurrent mark thread aoqi@0: def(CMRegionStack_lock , Mutex, leaf, true ); aoqi@0: def(SATB_Q_FL_lock , Mutex , special, true ); aoqi@0: def(SATB_Q_CBL_mon , Monitor, nonleaf, true ); aoqi@0: def(Shared_SATB_Q_lock , Mutex, nonleaf, true ); aoqi@0: aoqi@0: def(DirtyCardQ_FL_lock , Mutex , special, true ); aoqi@0: def(DirtyCardQ_CBL_mon , Monitor, nonleaf, true ); aoqi@0: def(Shared_DirtyCardQ_lock , Mutex, nonleaf, true ); aoqi@0: aoqi@0: def(FreeList_lock , Mutex, leaf , true ); aoqi@0: def(SecondaryFreeList_lock , Monitor, leaf , true ); aoqi@0: def(OldSets_lock , Mutex , leaf , true ); aoqi@0: def(RootRegionScan_lock , Monitor, leaf , true ); aoqi@0: def(MMUTracker_lock , Mutex , leaf , true ); aoqi@0: def(EvacFailureStack_lock , Mutex , nonleaf , true ); aoqi@0: aoqi@0: def(StringDedupQueue_lock , Monitor, leaf, true ); aoqi@0: def(StringDedupTable_lock , Mutex , leaf, true ); aoqi@0: } aoqi@0: def(ParGCRareEvent_lock , Mutex , leaf , true ); aoqi@0: def(DerivedPointerTableGC_lock , Mutex, leaf, true ); aoqi@0: def(CodeCache_lock , Mutex , special, true ); aoqi@0: def(Interrupt_lock , Monitor, special, true ); // used for interrupt processing aoqi@0: def(RawMonitor_lock , Mutex, special, true ); aoqi@0: def(OopMapCacheAlloc_lock , Mutex, leaf, true ); // used for oop_map_cache allocation. aoqi@0: aoqi@0: def(Patching_lock , Mutex , special, true ); // used for safepointing and code patching. aoqi@0: def(ObjAllocPost_lock , Monitor, special, false); aoqi@0: def(Service_lock , Monitor, special, true ); // used for service thread operations aoqi@0: def(JmethodIdCreation_lock , Mutex , leaf, true ); // used for creating jmethodIDs. aoqi@0: aoqi@0: def(SystemDictionary_lock , Monitor, leaf, true ); // lookups done by VM thread aoqi@0: def(PackageTable_lock , Mutex , leaf, false); aoqi@0: def(InlineCacheBuffer_lock , Mutex , leaf, true ); aoqi@0: def(VMStatistic_lock , Mutex , leaf, false); aoqi@0: def(ExpandHeap_lock , Mutex , leaf, true ); // Used during compilation by VM thread aoqi@0: def(JNIHandleBlockFreeList_lock , Mutex , leaf, true ); // handles are used by VM thread aoqi@0: def(SignatureHandlerLibrary_lock , Mutex , leaf, false); aoqi@0: def(SymbolTable_lock , Mutex , leaf+2, true ); aoqi@0: def(StringTable_lock , Mutex , leaf, true ); aoqi@0: def(ProfilePrint_lock , Mutex , leaf, false); // serial profile printing aoqi@0: def(ExceptionCache_lock , Mutex , leaf, false); // serial profile printing aoqi@0: def(OsrList_lock , Mutex , leaf, true ); aoqi@0: def(Debug1_lock , Mutex , leaf, true ); aoqi@0: #ifndef PRODUCT aoqi@0: def(FullGCALot_lock , Mutex , leaf, false); // a lock to make FullGCALot MT safe aoqi@0: #endif aoqi@0: def(BeforeExit_lock , Monitor, leaf, true ); aoqi@0: def(PerfDataMemAlloc_lock , Mutex , leaf, true ); // used for allocating PerfData memory for performance data aoqi@0: def(PerfDataManager_lock , Mutex , leaf, true ); // used for synchronized access to PerfDataManager resources aoqi@0: aoqi@0: // CMS_modUnionTable_lock leaf aoqi@0: // CMS_bitMap_lock leaf + 1 aoqi@0: // CMS_freeList_lock leaf + 2 aoqi@0: aoqi@0: def(Safepoint_lock , Monitor, safepoint, true ); // locks SnippetCache_lock/Threads_lock aoqi@0: aoqi@0: def(Threads_lock , Monitor, barrier, true ); aoqi@0: aoqi@0: def(VMOperationQueue_lock , Monitor, nonleaf, true ); // VM_thread allowed to block on these aoqi@0: def(VMOperationRequest_lock , Monitor, nonleaf, true ); aoqi@0: def(RetData_lock , Mutex , nonleaf, false); aoqi@0: def(Terminator_lock , Monitor, nonleaf, true ); aoqi@0: def(VtableStubs_lock , Mutex , nonleaf, true ); aoqi@0: def(Notify_lock , Monitor, nonleaf, true ); aoqi@0: def(JNIGlobalHandle_lock , Mutex , nonleaf, true ); // locks JNIHandleBlockFreeList_lock aoqi@0: def(JNICritical_lock , Monitor, nonleaf, true ); // used for JNI critical regions aoqi@0: def(AdapterHandlerLibrary_lock , Mutex , nonleaf, true); aoqi@0: if (UseConcMarkSweepGC) { aoqi@0: def(SLT_lock , Monitor, nonleaf, false ); aoqi@0: // used in CMS GC for locking PLL lock aoqi@0: } aoqi@0: def(Heap_lock , Monitor, nonleaf+1, false); aoqi@0: def(JfieldIdCreation_lock , Mutex , nonleaf+1, true ); // jfieldID, Used in VM_Operation aoqi@0: def(MemberNameTable_lock , Mutex , nonleaf+1, false); // Used to protect MemberNameTable aoqi@0: aoqi@0: def(CompiledIC_lock , Mutex , nonleaf+2, false); // locks VtableStubs_lock, InlineCacheBuffer_lock aoqi@0: def(CompileTaskAlloc_lock , Mutex , nonleaf+2, true ); aoqi@0: def(CompileStatistics_lock , Mutex , nonleaf+2, false); aoqi@0: def(MultiArray_lock , Mutex , nonleaf+2, false); // locks SymbolTable_lock aoqi@0: aoqi@0: def(JvmtiThreadState_lock , Mutex , nonleaf+2, false); // Used by JvmtiThreadState/JvmtiEventController aoqi@0: def(JvmtiPendingEvent_lock , Monitor, nonleaf, false); // Used by JvmtiCodeBlobEvents aoqi@0: def(Management_lock , Mutex , nonleaf+2, false); // used for JVM management aoqi@0: aoqi@0: def(Compile_lock , Mutex , nonleaf+3, true ); aoqi@0: def(MethodData_lock , Mutex , nonleaf+3, false); aoqi@0: aoqi@0: def(MethodCompileQueue_lock , Monitor, nonleaf+4, true ); aoqi@0: def(Debug2_lock , Mutex , nonleaf+4, true ); aoqi@0: def(Debug3_lock , Mutex , nonleaf+4, true ); aoqi@0: def(ProfileVM_lock , Monitor, special, false); // used for profiling of the VMThread aoqi@0: def(CompileThread_lock , Monitor, nonleaf+5, false ); aoqi@0: def(PeriodicTask_lock , Monitor, nonleaf+5, true); aoqi@0: aoqi@0: #ifdef INCLUDE_TRACE aoqi@0: def(JfrMsg_lock , Monitor, leaf, true); mlarsson@7689: def(JfrBuffer_lock , Mutex, leaf, true); mlarsson@7689: def(JfrThreadGroups_lock , Mutex, leaf, true); mlarsson@7689: def(JfrStream_lock , Mutex, nonleaf, true); mlarsson@7689: def(JfrStacktrace_lock , Mutex, special, true); aoqi@0: #endif aoqi@0: dholmes@7547: #ifndef SUPPORTS_NATIVE_CX8 dholmes@7547: def(UnsafeJlong_lock , Mutex, special, false); dholmes@7547: #endif aoqi@0: } aoqi@0: aoqi@0: GCMutexLocker::GCMutexLocker(Monitor * mutex) { aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: _locked = false; aoqi@0: } else { aoqi@0: _mutex = mutex; aoqi@0: _locked = true; aoqi@0: _mutex->lock(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Print all mutexes/monitors that are currently owned by a thread; called aoqi@0: // by fatal error handler. aoqi@0: void print_owned_locks_on_error(outputStream* st) { aoqi@0: st->print("VM Mutex/Monitor currently owned by a thread: "); aoqi@0: bool none = true; aoqi@0: for (int i = 0; i < _num_mutex; i++) { aoqi@0: // see if it has an owner aoqi@0: if (_mutex_array[i]->owner() != NULL) { aoqi@0: if (none) { aoqi@0: // print format used by Mutex::print_on_error() aoqi@0: st->print_cr(" ([mutex/lock_event])"); aoqi@0: none = false; aoqi@0: } aoqi@0: _mutex_array[i]->print_on_error(st); aoqi@0: st->cr(); aoqi@0: } aoqi@0: } aoqi@0: if (none) st->print_cr("None"); aoqi@0: }