duke@435: /* drchase@5732: * Copyright (c) 1997, 2013, 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: #ifndef SHARE_VM_RUNTIME_MUTEXLOCKER_HPP stefank@2314: #define SHARE_VM_RUNTIME_MUTEXLOCKER_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "runtime/mutex.hpp" stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "os_linux.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "os_solaris.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "os_windows.inline.hpp" stefank@2314: #endif goetz@6461: #ifdef TARGET_OS_FAMILY_aix goetz@6461: # include "os_aix.inline.hpp" goetz@6461: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "os_bsd.inline.hpp" never@3156: #endif stefank@2314: duke@435: // Mutexes used in the VM. duke@435: duke@435: extern Mutex* Patching_lock; // a lock used to guard code patching of compiled code duke@435: extern Monitor* SystemDictionary_lock; // a lock on the system dictonary duke@435: extern Mutex* PackageTable_lock; // a lock on the class loader package table duke@435: extern Mutex* CompiledIC_lock; // a lock used to guard compiled IC patching and access duke@435: extern Mutex* InlineCacheBuffer_lock; // a lock used to guard the InlineCacheBuffer duke@435: extern Mutex* VMStatistic_lock; // a lock used to guard statistics count increment duke@435: extern Mutex* JNIGlobalHandle_lock; // a lock on creating JNI global handles duke@435: extern Mutex* JNIHandleBlockFreeList_lock; // a lock on the JNI handle block free list sspitsyn@4965: extern Mutex* MemberNameTable_lock; // a lock on the MemberNameTable updates duke@435: extern Mutex* JmethodIdCreation_lock; // a lock on creating JNI method identifiers duke@435: extern Mutex* JfieldIdCreation_lock; // a lock on creating JNI static field identifiers duke@435: extern Monitor* JNICritical_lock; // a lock used while entering and exiting JNI critical regions, allows GC to sometimes get in duke@435: extern Mutex* JvmtiThreadState_lock; // a lock on modification of JVMTI thread data duke@435: extern Monitor* JvmtiPendingEvent_lock; // a lock on the JVMTI pending events list ysr@777: extern Monitor* Heap_lock; // a lock on the heap duke@435: extern Mutex* ExpandHeap_lock; // a lock on expanding the heap duke@435: extern Mutex* AdapterHandlerLibrary_lock; // a lock on the AdapterHandlerLibrary duke@435: extern Mutex* SignatureHandlerLibrary_lock; // a lock on the SignatureHandlerLibrary duke@435: extern Mutex* VtableStubs_lock; // a lock on the VtableStubs duke@435: extern Mutex* SymbolTable_lock; // a lock on the symbol table duke@435: extern Mutex* StringTable_lock; // a lock on the interned string table duke@435: extern Mutex* CodeCache_lock; // a lock on the CodeCache, rank is special, use MutexLockerEx duke@435: extern Mutex* MethodData_lock; // a lock on installation of method data duke@435: extern Mutex* RetData_lock; // a lock on installation of RetData inside method data duke@435: extern Mutex* DerivedPointerTableGC_lock; // a lock to protect the derived pointer table duke@435: extern Monitor* VMOperationQueue_lock; // a lock on queue of vm_operations waiting to execute duke@435: extern Monitor* VMOperationRequest_lock; // a lock on Threads waiting for a vm_operation to terminate duke@435: extern Monitor* Safepoint_lock; // a lock used by the safepoint abstraction duke@435: extern Monitor* Threads_lock; // a lock on the Threads table of active Java threads duke@435: // (also used by Safepoints too to block threads creation/destruction) duke@435: extern Monitor* CGC_lock; // used for coordination between duke@435: // fore- & background GC threads. duke@435: extern Mutex* STS_init_lock; // coordinate initialization of SuspendibleThreadSets. duke@435: extern Monitor* SLT_lock; // used in CMS GC for acquiring PLL duke@435: extern Monitor* iCMS_lock; // CMS incremental mode start/stop notification duke@435: extern Monitor* FullGCCount_lock; // in support of "concurrent" full gc ysr@777: extern Monitor* CMark_lock; // used for concurrent mark thread coordination tonyp@1793: extern Mutex* CMRegionStack_lock; // used for protecting accesses to the CM region stack ysr@777: extern Mutex* SATB_Q_FL_lock; // Protects SATB Q ysr@777: // buffer free list. ysr@777: extern Monitor* SATB_Q_CBL_mon; // Protects SATB Q ysr@777: // completed buffer queue. ysr@777: extern Mutex* Shared_SATB_Q_lock; // Lock protecting SATB ysr@777: // queue shared by ysr@777: // non-Java threads. ysr@777: ysr@777: extern Mutex* DirtyCardQ_FL_lock; // Protects dirty card Q ysr@777: // buffer free list. ysr@777: extern Monitor* DirtyCardQ_CBL_mon; // Protects dirty card Q ysr@777: // completed buffer queue. ysr@777: extern Mutex* Shared_DirtyCardQ_lock; // Lock protecting dirty card ysr@777: // queue shared by ysr@777: // non-Java threads. duke@435: // (see option ExplicitGCInvokesConcurrent) duke@435: extern Mutex* ParGCRareEvent_lock; // Synchronizes various (rare) parallel GC ops. ysr@777: extern Mutex* EvacFailureStack_lock; // guards the evac failure scan stack duke@435: extern Mutex* Compile_lock; // a lock held when Compilation is updating code (used to block CodeCache traversal, CHA updates, etc) duke@435: extern Monitor* MethodCompileQueue_lock; // a lock held when method compilations are enqueued, dequeued duke@435: extern Monitor* CompileThread_lock; // a lock held by compile threads during compilation system initialization duke@435: extern Mutex* CompileTaskAlloc_lock; // a lock held when CompileTasks are allocated duke@435: extern Mutex* CompileStatistics_lock; // a lock held when updating compilation statistics duke@435: extern Mutex* MultiArray_lock; // a lock used to guard allocation of multi-dim arrays duke@435: extern Monitor* Terminator_lock; // a lock used to guard termination of the vm duke@435: extern Monitor* BeforeExit_lock; // a lock used to guard cleanups and shutdown hooks duke@435: extern Monitor* Notify_lock; // a lock used to synchronize the start-up of the vm duke@435: extern Monitor* Interrupt_lock; // a lock used for condition variable mediated interrupt processing duke@435: extern Monitor* ProfileVM_lock; // a lock used for profiling the VMThread duke@435: extern Mutex* ProfilePrint_lock; // a lock used to serialize the printing of profiles duke@435: extern Mutex* ExceptionCache_lock; // a lock used to synchronize exception cache updates duke@435: extern Mutex* OsrList_lock; // a lock used to serialize access to OSR queues duke@435: duke@435: #ifndef PRODUCT duke@435: extern Mutex* FullGCALot_lock; // a lock to make FullGCALot MT safe tonyp@3464: #endif // PRODUCT duke@435: extern Mutex* Debug1_lock; // A bunch of pre-allocated locks that can be used for tracing duke@435: extern Mutex* Debug2_lock; // down synchronization related bugs! duke@435: extern Mutex* Debug3_lock; duke@435: duke@435: extern Mutex* RawMonitor_lock; duke@435: extern Mutex* PerfDataMemAlloc_lock; // a lock on the allocator for PerfData memory for performance data duke@435: extern Mutex* PerfDataManager_lock; // a long on access to PerfDataManager resources duke@435: extern Mutex* ParkerFreeList_lock; duke@435: extern Mutex* OopMapCacheAlloc_lock; // protects allocation of oop_map caches duke@435: tonyp@2472: extern Mutex* FreeList_lock; // protects the free region list during safepoints tonyp@2472: extern Monitor* SecondaryFreeList_lock; // protects the secondary free region list tonyp@2472: extern Mutex* OldSets_lock; // protects the old region sets tonyp@3464: extern Monitor* RootRegionScan_lock; // used to notify that the CM threads have finished scanning the IM snapshot regions ysr@777: extern Mutex* MMUTracker_lock; // protects the MMU ysr@777: // tracker data structures ysr@777: extern Mutex* HotCardCache_lock; // protects the hot card cache ysr@777: duke@435: extern Mutex* Management_lock; // a lock used to serialize JVM management kamg@2511: extern Monitor* Service_lock; // a lock used for service thread operation mgronlun@5574: extern Monitor* PeriodicTask_lock; // protects the periodic task structure phh@3427: mgronlun@5574: #ifdef INCLUDE_TRACE mgronlun@5574: extern Mutex* JfrStacktrace_lock; // used to guard access to the JFR stacktrace table phh@3427: extern Monitor* JfrMsg_lock; // protects JFR messaging phh@3427: extern Mutex* JfrBuffer_lock; // protects JFR buffer operations phh@3427: extern Mutex* JfrStream_lock; // protects JFR stream access mgronlun@5574: extern Mutex* JfrThreadGroups_lock; // protects JFR access to Thread Groups mgronlun@5574: #endif duke@435: duke@435: // A MutexLocker provides mutual exclusion with respect to a given mutex duke@435: // for the scope which contains the locker. The lock is an OS lock, not duke@435: // an object lock, and the two do not interoperate. Do not use Mutex-based duke@435: // locks to lock on Java objects, because they will not be respected if a duke@435: // that object is locked using the Java locking mechanism. duke@435: // duke@435: // NOTE WELL!! duke@435: // duke@435: // See orderAccess.hpp. We assume throughout the VM that MutexLocker's duke@435: // and friends constructors do a fence, a lock and an acquire *in that duke@435: // order*. And that their destructors do a release and unlock, in *that* duke@435: // order. If their implementations change such that these assumptions duke@435: // are violated, a whole lot of code will break. 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: duke@435: char *lock_name(Mutex *mutex); duke@435: duke@435: class MutexLocker: StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: public: duke@435: MutexLocker(Monitor * mutex) { duke@435: assert(mutex->rank() != Mutex::special, duke@435: "Special ranked mutex should only use MutexLockerEx"); duke@435: _mutex = mutex; duke@435: _mutex->lock(); duke@435: } duke@435: duke@435: // Overloaded constructor passing current thread duke@435: MutexLocker(Monitor * mutex, Thread *thread) { duke@435: assert(mutex->rank() != Mutex::special, duke@435: "Special ranked mutex should only use MutexLockerEx"); duke@435: _mutex = mutex; duke@435: _mutex->lock(thread); duke@435: } duke@435: duke@435: ~MutexLocker() { duke@435: _mutex->unlock(); duke@435: } duke@435: duke@435: }; duke@435: duke@435: // for debugging: check that we're already owning this lock (or are at a safepoint) duke@435: #ifdef ASSERT duke@435: void assert_locked_or_safepoint(const Monitor * lock); duke@435: void assert_lock_strong(const Monitor * lock); duke@435: #else duke@435: #define assert_locked_or_safepoint(lock) duke@435: #define assert_lock_strong(lock) duke@435: #endif duke@435: duke@435: // A MutexLockerEx behaves like a MutexLocker when its constructor is duke@435: // called with a Mutex. Unlike a MutexLocker, its constructor can also be duke@435: // called with NULL, in which case the MutexLockerEx is a no-op. There duke@435: // is also a corresponding MutexUnlockerEx. We want to keep the duke@435: // basic MutexLocker as fast as possible. MutexLockerEx can also lock duke@435: // without safepoint check. duke@435: duke@435: class MutexLockerEx: public StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: public: duke@435: MutexLockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) { duke@435: _mutex = mutex; duke@435: if (_mutex != NULL) { duke@435: assert(mutex->rank() > Mutex::special || no_safepoint_check, duke@435: "Mutexes with rank special or lower should not do safepoint checks"); duke@435: if (no_safepoint_check) duke@435: _mutex->lock_without_safepoint_check(); duke@435: else duke@435: _mutex->lock(); duke@435: } duke@435: } duke@435: duke@435: ~MutexLockerEx() { duke@435: if (_mutex != NULL) { duke@435: _mutex->unlock(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: // A MonitorLockerEx is like a MutexLockerEx above, except it takes duke@435: // a possibly null Monitor, and allows wait/notify as well which are duke@435: // delegated to the underlying Monitor. duke@435: duke@435: class MonitorLockerEx: public MutexLockerEx { duke@435: private: duke@435: Monitor * _monitor; duke@435: public: duke@435: MonitorLockerEx(Monitor* monitor, duke@435: bool no_safepoint_check = !Mutex::_no_safepoint_check_flag): duke@435: MutexLockerEx(monitor, no_safepoint_check), duke@435: _monitor(monitor) { duke@435: // Superclass constructor did locking duke@435: } duke@435: duke@435: ~MonitorLockerEx() { duke@435: #ifdef ASSERT duke@435: if (_monitor != NULL) { duke@435: assert_lock_strong(_monitor); duke@435: } duke@435: #endif // ASSERT duke@435: // Superclass destructor will do unlocking duke@435: } duke@435: duke@435: bool wait(bool no_safepoint_check = !Mutex::_no_safepoint_check_flag, duke@435: long timeout = 0, duke@435: bool as_suspend_equivalent = !Mutex::_as_suspend_equivalent_flag) { duke@435: if (_monitor != NULL) { duke@435: return _monitor->wait(no_safepoint_check, timeout, as_suspend_equivalent); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: bool notify_all() { duke@435: if (_monitor != NULL) { duke@435: return _monitor->notify_all(); duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: bool notify() { duke@435: if (_monitor != NULL) { duke@435: return _monitor->notify(); duke@435: } duke@435: return true; duke@435: } duke@435: }; duke@435: duke@435: duke@435: duke@435: // A GCMutexLocker is usually initialized with a mutex that is duke@435: // automatically acquired in order to do GC. The function that duke@435: // synchronizes using a GCMutexLocker may be called both during and between duke@435: // GC's. Thus, it must acquire the mutex if GC is not in progress, but not duke@435: // if GC is in progress (since the mutex is already held on its behalf.) duke@435: duke@435: class GCMutexLocker: public StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: bool _locked; duke@435: public: duke@435: GCMutexLocker(Monitor * mutex); duke@435: ~GCMutexLocker() { if (_locked) _mutex->unlock(); } duke@435: }; duke@435: duke@435: duke@435: duke@435: // A MutexUnlocker temporarily exits a previously duke@435: // entered mutex for the scope which contains the unlocker. duke@435: duke@435: class MutexUnlocker: StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: duke@435: public: duke@435: MutexUnlocker(Monitor * mutex) { duke@435: _mutex = mutex; duke@435: _mutex->unlock(); duke@435: } duke@435: duke@435: ~MutexUnlocker() { duke@435: _mutex->lock(); duke@435: } duke@435: }; duke@435: duke@435: // A MutexUnlockerEx temporarily exits a previously duke@435: // entered mutex for the scope which contains the unlocker. duke@435: duke@435: class MutexUnlockerEx: StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: bool _no_safepoint_check; duke@435: duke@435: public: duke@435: MutexUnlockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) { duke@435: _mutex = mutex; duke@435: _no_safepoint_check = no_safepoint_check; duke@435: _mutex->unlock(); duke@435: } duke@435: duke@435: ~MutexUnlockerEx() { duke@435: if (_no_safepoint_check == Mutex::_no_safepoint_check_flag) { duke@435: _mutex->lock_without_safepoint_check(); duke@435: } else { duke@435: _mutex->lock(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: #ifndef PRODUCT duke@435: // duke@435: // A special MutexLocker that allows: duke@435: // - reentrant locking duke@435: // - locking out of order duke@435: // duke@435: // Only too be used for verify code, where we can relaxe out dead-lock duke@435: // dection code a bit (unsafe, but probably ok). This code is NEVER to duke@435: // be included in a product version. duke@435: // duke@435: class VerifyMutexLocker: StackObj { duke@435: private: duke@435: Monitor * _mutex; duke@435: bool _reentrant; duke@435: public: duke@435: VerifyMutexLocker(Monitor * mutex) { duke@435: _mutex = mutex; duke@435: _reentrant = mutex->owned_by_self(); duke@435: if (!_reentrant) { duke@435: // We temp. diable strict safepoint checking, while we require the lock duke@435: FlagSetting fs(StrictSafepointChecks, false); duke@435: _mutex->lock(); duke@435: } duke@435: } duke@435: duke@435: ~VerifyMutexLocker() { duke@435: if (!_reentrant) { duke@435: _mutex->unlock(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: #endif stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_MUTEXLOCKER_HPP