src/share/vm/runtime/mutexLocker.hpp

Fri, 24 Jun 2016 17:12:13 +0800

author
aoqi<aoqi@loongson.cn>
date
Fri, 24 Jun 2016 17:12:13 +0800
changeset 25
873fd82b133d
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

[Code Reorganization] Removed GC related modifications made by Loongson, for example, UseOldNUMA.

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_MUTEXLOCKER_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_MUTEXLOCKER_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/mutex.hpp"
aoqi@0 30 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 31 # include "os_linux.inline.hpp"
aoqi@0 32 #endif
aoqi@0 33 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 34 # include "os_solaris.inline.hpp"
aoqi@0 35 #endif
aoqi@0 36 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 37 # include "os_windows.inline.hpp"
aoqi@0 38 #endif
aoqi@0 39 #ifdef TARGET_OS_FAMILY_aix
aoqi@0 40 # include "os_aix.inline.hpp"
aoqi@0 41 #endif
aoqi@0 42 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 43 # include "os_bsd.inline.hpp"
aoqi@0 44 #endif
aoqi@0 45
aoqi@0 46 // Mutexes used in the VM.
aoqi@0 47
aoqi@0 48 extern Mutex* Patching_lock; // a lock used to guard code patching of compiled code
aoqi@0 49 extern Monitor* SystemDictionary_lock; // a lock on the system dictonary
aoqi@0 50 extern Mutex* PackageTable_lock; // a lock on the class loader package table
aoqi@0 51 extern Mutex* CompiledIC_lock; // a lock used to guard compiled IC patching and access
aoqi@0 52 extern Mutex* InlineCacheBuffer_lock; // a lock used to guard the InlineCacheBuffer
aoqi@0 53 extern Mutex* VMStatistic_lock; // a lock used to guard statistics count increment
aoqi@0 54 extern Mutex* JNIGlobalHandle_lock; // a lock on creating JNI global handles
aoqi@0 55 extern Mutex* JNIHandleBlockFreeList_lock; // a lock on the JNI handle block free list
aoqi@0 56 extern Mutex* MemberNameTable_lock; // a lock on the MemberNameTable updates
aoqi@0 57 extern Mutex* JmethodIdCreation_lock; // a lock on creating JNI method identifiers
aoqi@0 58 extern Mutex* JfieldIdCreation_lock; // a lock on creating JNI static field identifiers
aoqi@0 59 extern Monitor* JNICritical_lock; // a lock used while entering and exiting JNI critical regions, allows GC to sometimes get in
aoqi@0 60 extern Mutex* JvmtiThreadState_lock; // a lock on modification of JVMTI thread data
aoqi@0 61 extern Monitor* JvmtiPendingEvent_lock; // a lock on the JVMTI pending events list
aoqi@0 62 extern Monitor* Heap_lock; // a lock on the heap
aoqi@0 63 extern Mutex* ExpandHeap_lock; // a lock on expanding the heap
aoqi@0 64 extern Mutex* AdapterHandlerLibrary_lock; // a lock on the AdapterHandlerLibrary
aoqi@0 65 extern Mutex* SignatureHandlerLibrary_lock; // a lock on the SignatureHandlerLibrary
aoqi@0 66 extern Mutex* VtableStubs_lock; // a lock on the VtableStubs
aoqi@0 67 extern Mutex* SymbolTable_lock; // a lock on the symbol table
aoqi@0 68 extern Mutex* StringTable_lock; // a lock on the interned string table
aoqi@0 69 extern Monitor* StringDedupQueue_lock; // a lock on the string deduplication queue
aoqi@0 70 extern Mutex* StringDedupTable_lock; // a lock on the string deduplication table
aoqi@0 71 extern Mutex* CodeCache_lock; // a lock on the CodeCache, rank is special, use MutexLockerEx
aoqi@0 72 extern Mutex* MethodData_lock; // a lock on installation of method data
aoqi@0 73 extern Mutex* RetData_lock; // a lock on installation of RetData inside method data
aoqi@0 74 extern Mutex* DerivedPointerTableGC_lock; // a lock to protect the derived pointer table
aoqi@0 75 extern Monitor* VMOperationQueue_lock; // a lock on queue of vm_operations waiting to execute
aoqi@0 76 extern Monitor* VMOperationRequest_lock; // a lock on Threads waiting for a vm_operation to terminate
aoqi@0 77 extern Monitor* Safepoint_lock; // a lock used by the safepoint abstraction
aoqi@0 78 extern Monitor* Threads_lock; // a lock on the Threads table of active Java threads
aoqi@0 79 // (also used by Safepoints too to block threads creation/destruction)
aoqi@0 80 extern Monitor* CGC_lock; // used for coordination between
aoqi@0 81 // fore- & background GC threads.
aoqi@0 82 extern Mutex* STS_init_lock; // coordinate initialization of SuspendibleThreadSets.
aoqi@0 83 extern Monitor* SLT_lock; // used in CMS GC for acquiring PLL
aoqi@0 84 extern Monitor* iCMS_lock; // CMS incremental mode start/stop notification
aoqi@0 85 extern Monitor* FullGCCount_lock; // in support of "concurrent" full gc
aoqi@0 86 extern Monitor* CMark_lock; // used for concurrent mark thread coordination
aoqi@0 87 extern Mutex* CMRegionStack_lock; // used for protecting accesses to the CM region stack
aoqi@0 88 extern Mutex* SATB_Q_FL_lock; // Protects SATB Q
aoqi@0 89 // buffer free list.
aoqi@0 90 extern Monitor* SATB_Q_CBL_mon; // Protects SATB Q
aoqi@0 91 // completed buffer queue.
aoqi@0 92 extern Mutex* Shared_SATB_Q_lock; // Lock protecting SATB
aoqi@0 93 // queue shared by
aoqi@0 94 // non-Java threads.
aoqi@0 95
aoqi@0 96 extern Mutex* DirtyCardQ_FL_lock; // Protects dirty card Q
aoqi@0 97 // buffer free list.
aoqi@0 98 extern Monitor* DirtyCardQ_CBL_mon; // Protects dirty card Q
aoqi@0 99 // completed buffer queue.
aoqi@0 100 extern Mutex* Shared_DirtyCardQ_lock; // Lock protecting dirty card
aoqi@0 101 // queue shared by
aoqi@0 102 // non-Java threads.
aoqi@0 103 // (see option ExplicitGCInvokesConcurrent)
aoqi@0 104 extern Mutex* ParGCRareEvent_lock; // Synchronizes various (rare) parallel GC ops.
aoqi@0 105 extern Mutex* EvacFailureStack_lock; // guards the evac failure scan stack
aoqi@0 106 extern Mutex* Compile_lock; // a lock held when Compilation is updating code (used to block CodeCache traversal, CHA updates, etc)
aoqi@0 107 extern Monitor* MethodCompileQueue_lock; // a lock held when method compilations are enqueued, dequeued
aoqi@0 108 extern Monitor* CompileThread_lock; // a lock held by compile threads during compilation system initialization
aoqi@0 109 extern Mutex* CompileTaskAlloc_lock; // a lock held when CompileTasks are allocated
aoqi@0 110 extern Mutex* CompileStatistics_lock; // a lock held when updating compilation statistics
aoqi@0 111 extern Mutex* MultiArray_lock; // a lock used to guard allocation of multi-dim arrays
aoqi@0 112 extern Monitor* Terminator_lock; // a lock used to guard termination of the vm
aoqi@0 113 extern Monitor* BeforeExit_lock; // a lock used to guard cleanups and shutdown hooks
aoqi@0 114 extern Monitor* Notify_lock; // a lock used to synchronize the start-up of the vm
aoqi@0 115 extern Monitor* Interrupt_lock; // a lock used for condition variable mediated interrupt processing
aoqi@0 116 extern Monitor* ProfileVM_lock; // a lock used for profiling the VMThread
aoqi@0 117 extern Mutex* ProfilePrint_lock; // a lock used to serialize the printing of profiles
aoqi@0 118 extern Mutex* ExceptionCache_lock; // a lock used to synchronize exception cache updates
aoqi@0 119 extern Mutex* OsrList_lock; // a lock used to serialize access to OSR queues
aoqi@0 120
aoqi@0 121 #ifndef PRODUCT
aoqi@0 122 extern Mutex* FullGCALot_lock; // a lock to make FullGCALot MT safe
aoqi@0 123 #endif // PRODUCT
aoqi@0 124 extern Mutex* Debug1_lock; // A bunch of pre-allocated locks that can be used for tracing
aoqi@0 125 extern Mutex* Debug2_lock; // down synchronization related bugs!
aoqi@0 126 extern Mutex* Debug3_lock;
aoqi@0 127
aoqi@0 128 extern Mutex* RawMonitor_lock;
aoqi@0 129 extern Mutex* PerfDataMemAlloc_lock; // a lock on the allocator for PerfData memory for performance data
aoqi@0 130 extern Mutex* PerfDataManager_lock; // a long on access to PerfDataManager resources
aoqi@0 131 extern Mutex* ParkerFreeList_lock;
aoqi@0 132 extern Mutex* OopMapCacheAlloc_lock; // protects allocation of oop_map caches
aoqi@0 133
aoqi@0 134 extern Mutex* FreeList_lock; // protects the free region list during safepoints
aoqi@0 135 extern Monitor* SecondaryFreeList_lock; // protects the secondary free region list
aoqi@0 136 extern Mutex* OldSets_lock; // protects the old region sets
aoqi@0 137 extern Monitor* RootRegionScan_lock; // used to notify that the CM threads have finished scanning the IM snapshot regions
aoqi@0 138 extern Mutex* MMUTracker_lock; // protects the MMU
aoqi@0 139 // tracker data structures
aoqi@0 140 extern Mutex* HotCardCache_lock; // protects the hot card cache
aoqi@0 141
aoqi@0 142 extern Mutex* Management_lock; // a lock used to serialize JVM management
aoqi@0 143 extern Monitor* Service_lock; // a lock used for service thread operation
aoqi@0 144 extern Monitor* PeriodicTask_lock; // protects the periodic task structure
aoqi@0 145
aoqi@0 146 #ifdef INCLUDE_TRACE
aoqi@0 147 extern Mutex* JfrStacktrace_lock; // used to guard access to the JFR stacktrace table
aoqi@0 148 extern Monitor* JfrMsg_lock; // protects JFR messaging
aoqi@0 149 extern Mutex* JfrBuffer_lock; // protects JFR buffer operations
aoqi@0 150 extern Mutex* JfrStream_lock; // protects JFR stream access
aoqi@0 151 extern Mutex* JfrThreadGroups_lock; // protects JFR access to Thread Groups
aoqi@0 152 #endif
aoqi@0 153
aoqi@0 154 // A MutexLocker provides mutual exclusion with respect to a given mutex
aoqi@0 155 // for the scope which contains the locker. The lock is an OS lock, not
aoqi@0 156 // an object lock, and the two do not interoperate. Do not use Mutex-based
aoqi@0 157 // locks to lock on Java objects, because they will not be respected if a
aoqi@0 158 // that object is locked using the Java locking mechanism.
aoqi@0 159 //
aoqi@0 160 // NOTE WELL!!
aoqi@0 161 //
aoqi@0 162 // See orderAccess.hpp. We assume throughout the VM that MutexLocker's
aoqi@0 163 // and friends constructors do a fence, a lock and an acquire *in that
aoqi@0 164 // order*. And that their destructors do a release and unlock, in *that*
aoqi@0 165 // order. If their implementations change such that these assumptions
aoqi@0 166 // are violated, a whole lot of code will break.
aoqi@0 167
aoqi@0 168 // Print all mutexes/monitors that are currently owned by a thread; called
aoqi@0 169 // by fatal error handler.
aoqi@0 170 void print_owned_locks_on_error(outputStream* st);
aoqi@0 171
aoqi@0 172 char *lock_name(Mutex *mutex);
aoqi@0 173
aoqi@0 174 class MutexLocker: StackObj {
aoqi@0 175 private:
aoqi@0 176 Monitor * _mutex;
aoqi@0 177 public:
aoqi@0 178 MutexLocker(Monitor * mutex) {
aoqi@0 179 assert(mutex->rank() != Mutex::special,
aoqi@0 180 "Special ranked mutex should only use MutexLockerEx");
aoqi@0 181 _mutex = mutex;
aoqi@0 182 _mutex->lock();
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 // Overloaded constructor passing current thread
aoqi@0 186 MutexLocker(Monitor * mutex, Thread *thread) {
aoqi@0 187 assert(mutex->rank() != Mutex::special,
aoqi@0 188 "Special ranked mutex should only use MutexLockerEx");
aoqi@0 189 _mutex = mutex;
aoqi@0 190 _mutex->lock(thread);
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 ~MutexLocker() {
aoqi@0 194 _mutex->unlock();
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 };
aoqi@0 198
aoqi@0 199 // for debugging: check that we're already owning this lock (or are at a safepoint)
aoqi@0 200 #ifdef ASSERT
aoqi@0 201 void assert_locked_or_safepoint(const Monitor * lock);
aoqi@0 202 void assert_lock_strong(const Monitor * lock);
aoqi@0 203 #else
aoqi@0 204 #define assert_locked_or_safepoint(lock)
aoqi@0 205 #define assert_lock_strong(lock)
aoqi@0 206 #endif
aoqi@0 207
aoqi@0 208 // A MutexLockerEx behaves like a MutexLocker when its constructor is
aoqi@0 209 // called with a Mutex. Unlike a MutexLocker, its constructor can also be
aoqi@0 210 // called with NULL, in which case the MutexLockerEx is a no-op. There
aoqi@0 211 // is also a corresponding MutexUnlockerEx. We want to keep the
aoqi@0 212 // basic MutexLocker as fast as possible. MutexLockerEx can also lock
aoqi@0 213 // without safepoint check.
aoqi@0 214
aoqi@0 215 class MutexLockerEx: public StackObj {
aoqi@0 216 private:
aoqi@0 217 Monitor * _mutex;
aoqi@0 218 public:
aoqi@0 219 MutexLockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) {
aoqi@0 220 _mutex = mutex;
aoqi@0 221 if (_mutex != NULL) {
aoqi@0 222 assert(mutex->rank() > Mutex::special || no_safepoint_check,
aoqi@0 223 "Mutexes with rank special or lower should not do safepoint checks");
aoqi@0 224 if (no_safepoint_check)
aoqi@0 225 _mutex->lock_without_safepoint_check();
aoqi@0 226 else
aoqi@0 227 _mutex->lock();
aoqi@0 228 }
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 ~MutexLockerEx() {
aoqi@0 232 if (_mutex != NULL) {
aoqi@0 233 _mutex->unlock();
aoqi@0 234 }
aoqi@0 235 }
aoqi@0 236 };
aoqi@0 237
aoqi@0 238 // A MonitorLockerEx is like a MutexLockerEx above, except it takes
aoqi@0 239 // a possibly null Monitor, and allows wait/notify as well which are
aoqi@0 240 // delegated to the underlying Monitor.
aoqi@0 241
aoqi@0 242 class MonitorLockerEx: public MutexLockerEx {
aoqi@0 243 private:
aoqi@0 244 Monitor * _monitor;
aoqi@0 245 public:
aoqi@0 246 MonitorLockerEx(Monitor* monitor,
aoqi@0 247 bool no_safepoint_check = !Mutex::_no_safepoint_check_flag):
aoqi@0 248 MutexLockerEx(monitor, no_safepoint_check),
aoqi@0 249 _monitor(monitor) {
aoqi@0 250 // Superclass constructor did locking
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 ~MonitorLockerEx() {
aoqi@0 254 #ifdef ASSERT
aoqi@0 255 if (_monitor != NULL) {
aoqi@0 256 assert_lock_strong(_monitor);
aoqi@0 257 }
aoqi@0 258 #endif // ASSERT
aoqi@0 259 // Superclass destructor will do unlocking
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 bool wait(bool no_safepoint_check = !Mutex::_no_safepoint_check_flag,
aoqi@0 263 long timeout = 0,
aoqi@0 264 bool as_suspend_equivalent = !Mutex::_as_suspend_equivalent_flag) {
aoqi@0 265 if (_monitor != NULL) {
aoqi@0 266 return _monitor->wait(no_safepoint_check, timeout, as_suspend_equivalent);
aoqi@0 267 }
aoqi@0 268 return false;
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 bool notify_all() {
aoqi@0 272 if (_monitor != NULL) {
aoqi@0 273 return _monitor->notify_all();
aoqi@0 274 }
aoqi@0 275 return true;
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 bool notify() {
aoqi@0 279 if (_monitor != NULL) {
aoqi@0 280 return _monitor->notify();
aoqi@0 281 }
aoqi@0 282 return true;
aoqi@0 283 }
aoqi@0 284 };
aoqi@0 285
aoqi@0 286
aoqi@0 287
aoqi@0 288 // A GCMutexLocker is usually initialized with a mutex that is
aoqi@0 289 // automatically acquired in order to do GC. The function that
aoqi@0 290 // synchronizes using a GCMutexLocker may be called both during and between
aoqi@0 291 // GC's. Thus, it must acquire the mutex if GC is not in progress, but not
aoqi@0 292 // if GC is in progress (since the mutex is already held on its behalf.)
aoqi@0 293
aoqi@0 294 class GCMutexLocker: public StackObj {
aoqi@0 295 private:
aoqi@0 296 Monitor * _mutex;
aoqi@0 297 bool _locked;
aoqi@0 298 public:
aoqi@0 299 GCMutexLocker(Monitor * mutex);
aoqi@0 300 ~GCMutexLocker() { if (_locked) _mutex->unlock(); }
aoqi@0 301 };
aoqi@0 302
aoqi@0 303
aoqi@0 304
aoqi@0 305 // A MutexUnlocker temporarily exits a previously
aoqi@0 306 // entered mutex for the scope which contains the unlocker.
aoqi@0 307
aoqi@0 308 class MutexUnlocker: StackObj {
aoqi@0 309 private:
aoqi@0 310 Monitor * _mutex;
aoqi@0 311
aoqi@0 312 public:
aoqi@0 313 MutexUnlocker(Monitor * mutex) {
aoqi@0 314 _mutex = mutex;
aoqi@0 315 _mutex->unlock();
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 ~MutexUnlocker() {
aoqi@0 319 _mutex->lock();
aoqi@0 320 }
aoqi@0 321 };
aoqi@0 322
aoqi@0 323 // A MutexUnlockerEx temporarily exits a previously
aoqi@0 324 // entered mutex for the scope which contains the unlocker.
aoqi@0 325
aoqi@0 326 class MutexUnlockerEx: StackObj {
aoqi@0 327 private:
aoqi@0 328 Monitor * _mutex;
aoqi@0 329 bool _no_safepoint_check;
aoqi@0 330
aoqi@0 331 public:
aoqi@0 332 MutexUnlockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) {
aoqi@0 333 _mutex = mutex;
aoqi@0 334 _no_safepoint_check = no_safepoint_check;
aoqi@0 335 _mutex->unlock();
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 ~MutexUnlockerEx() {
aoqi@0 339 if (_no_safepoint_check == Mutex::_no_safepoint_check_flag) {
aoqi@0 340 _mutex->lock_without_safepoint_check();
aoqi@0 341 } else {
aoqi@0 342 _mutex->lock();
aoqi@0 343 }
aoqi@0 344 }
aoqi@0 345 };
aoqi@0 346
aoqi@0 347 #ifndef PRODUCT
aoqi@0 348 //
aoqi@0 349 // A special MutexLocker that allows:
aoqi@0 350 // - reentrant locking
aoqi@0 351 // - locking out of order
aoqi@0 352 //
aoqi@0 353 // Only too be used for verify code, where we can relaxe out dead-lock
aoqi@0 354 // dection code a bit (unsafe, but probably ok). This code is NEVER to
aoqi@0 355 // be included in a product version.
aoqi@0 356 //
aoqi@0 357 class VerifyMutexLocker: StackObj {
aoqi@0 358 private:
aoqi@0 359 Monitor * _mutex;
aoqi@0 360 bool _reentrant;
aoqi@0 361 public:
aoqi@0 362 VerifyMutexLocker(Monitor * mutex) {
aoqi@0 363 _mutex = mutex;
aoqi@0 364 _reentrant = mutex->owned_by_self();
aoqi@0 365 if (!_reentrant) {
aoqi@0 366 // We temp. diable strict safepoint checking, while we require the lock
aoqi@0 367 FlagSetting fs(StrictSafepointChecks, false);
aoqi@0 368 _mutex->lock();
aoqi@0 369 }
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 ~VerifyMutexLocker() {
aoqi@0 373 if (!_reentrant) {
aoqi@0 374 _mutex->unlock();
aoqi@0 375 }
aoqi@0 376 }
aoqi@0 377 };
aoqi@0 378
aoqi@0 379 #endif
aoqi@0 380
aoqi@0 381 #endif // SHARE_VM_RUNTIME_MUTEXLOCKER_HPP

mercurial