src/share/vm/runtime/mutexLocker.hpp

Fri, 11 Apr 2014 12:29:24 +0200

author
pliden
date
Fri, 11 Apr 2014 12:29:24 +0200
changeset 6906
581e70386ec9
parent 6518
62c54fcc0a35
child 7535
7ae4e26cb1e0
child 7547
f46871c6c063
permissions
-rw-r--r--

8039147: Cleanup SuspendibleThreadSet
Reviewed-by: brutisso, tschatzl, mgerdin

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

mercurial