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