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