src/share/vm/runtime/mutexLocker.hpp

Wed, 12 Mar 2008 18:09:34 -0700

author
dcubed
date
Wed, 12 Mar 2008 18:09:34 -0700
changeset 484
31000d79ec71
parent 435
a61af66fc99e
child 490
2a8eb116ebbe
permissions
-rw-r--r--

6453355: 4/4 new No_Safepoint_Verifier uses fail during GC
Summary: (for Serguei) Clean up use of No_Safepoint_Verifier in JVM TI
Reviewed-by: dcubed

duke@435 1 /*
duke@435 2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Mutexes used in the VM.
duke@435 26
duke@435 27 extern Mutex* Patching_lock; // a lock used to guard code patching of compiled code
duke@435 28 extern Monitor* SystemDictionary_lock; // a lock on the system dictonary
duke@435 29 extern Mutex* PackageTable_lock; // a lock on the class loader package table
duke@435 30 extern Mutex* CompiledIC_lock; // a lock used to guard compiled IC patching and access
duke@435 31 extern Mutex* InlineCacheBuffer_lock; // a lock used to guard the InlineCacheBuffer
duke@435 32 extern Mutex* VMStatistic_lock; // a lock used to guard statistics count increment
duke@435 33 extern Mutex* JNIGlobalHandle_lock; // a lock on creating JNI global handles
duke@435 34 extern Mutex* JNIHandleBlockFreeList_lock; // a lock on the JNI handle block free list
duke@435 35 extern Mutex* JNICachedItableIndex_lock; // a lock on caching an itable index during JNI invoke
duke@435 36 extern Mutex* JmethodIdCreation_lock; // a lock on creating JNI method identifiers
duke@435 37 extern Mutex* JfieldIdCreation_lock; // a lock on creating JNI static field identifiers
duke@435 38 extern Monitor* JNICritical_lock; // a lock used while entering and exiting JNI critical regions, allows GC to sometimes get in
duke@435 39 extern Mutex* JvmtiThreadState_lock; // a lock on modification of JVMTI thread data
duke@435 40 extern Monitor* JvmtiPendingEvent_lock; // a lock on the JVMTI pending events list
duke@435 41 extern Mutex* Heap_lock; // a lock on the heap
duke@435 42 extern Mutex* ExpandHeap_lock; // a lock on expanding the heap
duke@435 43 extern Mutex* AdapterHandlerLibrary_lock; // a lock on the AdapterHandlerLibrary
duke@435 44 extern Mutex* SignatureHandlerLibrary_lock; // a lock on the SignatureHandlerLibrary
duke@435 45 extern Mutex* VtableStubs_lock; // a lock on the VtableStubs
duke@435 46 extern Mutex* SymbolTable_lock; // a lock on the symbol table
duke@435 47 extern Mutex* StringTable_lock; // a lock on the interned string table
duke@435 48 extern Mutex* CodeCache_lock; // a lock on the CodeCache, rank is special, use MutexLockerEx
duke@435 49 extern Mutex* MethodData_lock; // a lock on installation of method data
duke@435 50 extern Mutex* RetData_lock; // a lock on installation of RetData inside method data
duke@435 51 extern Mutex* DerivedPointerTableGC_lock; // a lock to protect the derived pointer table
duke@435 52 extern Monitor* VMOperationQueue_lock; // a lock on queue of vm_operations waiting to execute
duke@435 53 extern Monitor* VMOperationRequest_lock; // a lock on Threads waiting for a vm_operation to terminate
duke@435 54 extern Monitor* Safepoint_lock; // a lock used by the safepoint abstraction
duke@435 55 extern Monitor* SerializePage_lock; // a lock used when VMThread changing serialize memory page permission during safepoint
duke@435 56 extern Monitor* Threads_lock; // a lock on the Threads table of active Java threads
duke@435 57 // (also used by Safepoints too to block threads creation/destruction)
duke@435 58 extern Monitor* CGC_lock; // used for coordination between
duke@435 59 // fore- & background GC threads.
duke@435 60 extern Mutex* STS_init_lock; // coordinate initialization of SuspendibleThreadSets.
duke@435 61 extern Monitor* SLT_lock; // used in CMS GC for acquiring PLL
duke@435 62 extern Monitor* iCMS_lock; // CMS incremental mode start/stop notification
duke@435 63 extern Monitor* FullGCCount_lock; // in support of "concurrent" full gc
duke@435 64 // (see option ExplicitGCInvokesConcurrent)
duke@435 65 extern Mutex* ParGCRareEvent_lock; // Synchronizes various (rare) parallel GC ops.
duke@435 66 extern Mutex* Compile_lock; // a lock held when Compilation is updating code (used to block CodeCache traversal, CHA updates, etc)
duke@435 67 extern Monitor* MethodCompileQueue_lock; // a lock held when method compilations are enqueued, dequeued
duke@435 68 #ifdef TIERED
duke@435 69 extern Monitor* C1_lock; // a lock to ensure on single c1 compile is ever active
duke@435 70 #endif // TIERED
duke@435 71 extern Monitor* CompileThread_lock; // a lock held by compile threads during compilation system initialization
duke@435 72 extern Mutex* CompileTaskAlloc_lock; // a lock held when CompileTasks are allocated
duke@435 73 extern Mutex* CompileStatistics_lock; // a lock held when updating compilation statistics
duke@435 74 extern Mutex* MultiArray_lock; // a lock used to guard allocation of multi-dim arrays
duke@435 75 extern Monitor* Terminator_lock; // a lock used to guard termination of the vm
duke@435 76 extern Monitor* BeforeExit_lock; // a lock used to guard cleanups and shutdown hooks
duke@435 77 extern Monitor* Notify_lock; // a lock used to synchronize the start-up of the vm
duke@435 78 extern Monitor* Interrupt_lock; // a lock used for condition variable mediated interrupt processing
duke@435 79 extern Monitor* ProfileVM_lock; // a lock used for profiling the VMThread
duke@435 80 extern Mutex* ProfilePrint_lock; // a lock used to serialize the printing of profiles
duke@435 81 extern Mutex* ExceptionCache_lock; // a lock used to synchronize exception cache updates
duke@435 82 extern Mutex* OsrList_lock; // a lock used to serialize access to OSR queues
duke@435 83
duke@435 84 #ifndef PRODUCT
duke@435 85 extern Mutex* FullGCALot_lock; // a lock to make FullGCALot MT safe
duke@435 86 #endif
duke@435 87 extern Mutex* Debug1_lock; // A bunch of pre-allocated locks that can be used for tracing
duke@435 88 extern Mutex* Debug2_lock; // down synchronization related bugs!
duke@435 89 extern Mutex* Debug3_lock;
duke@435 90
duke@435 91 extern Mutex* RawMonitor_lock;
duke@435 92 extern Mutex* PerfDataMemAlloc_lock; // a lock on the allocator for PerfData memory for performance data
duke@435 93 extern Mutex* PerfDataManager_lock; // a long on access to PerfDataManager resources
duke@435 94 extern Mutex* ParkerFreeList_lock;
duke@435 95 extern Mutex* OopMapCacheAlloc_lock; // protects allocation of oop_map caches
duke@435 96
duke@435 97 extern Mutex* Management_lock; // a lock used to serialize JVM management
duke@435 98 extern Monitor* LowMemory_lock; // a lock used for low memory detection
duke@435 99
duke@435 100 // A MutexLocker provides mutual exclusion with respect to a given mutex
duke@435 101 // for the scope which contains the locker. The lock is an OS lock, not
duke@435 102 // an object lock, and the two do not interoperate. Do not use Mutex-based
duke@435 103 // locks to lock on Java objects, because they will not be respected if a
duke@435 104 // that object is locked using the Java locking mechanism.
duke@435 105 //
duke@435 106 // NOTE WELL!!
duke@435 107 //
duke@435 108 // See orderAccess.hpp. We assume throughout the VM that MutexLocker's
duke@435 109 // and friends constructors do a fence, a lock and an acquire *in that
duke@435 110 // order*. And that their destructors do a release and unlock, in *that*
duke@435 111 // order. If their implementations change such that these assumptions
duke@435 112 // are violated, a whole lot of code will break.
duke@435 113
duke@435 114 // Print all mutexes/monitors that are currently owned by a thread; called
duke@435 115 // by fatal error handler.
duke@435 116 void print_owned_locks_on_error(outputStream* st);
duke@435 117
duke@435 118 char *lock_name(Mutex *mutex);
duke@435 119
duke@435 120 class MutexLocker: StackObj {
duke@435 121 private:
duke@435 122 Monitor * _mutex;
duke@435 123 public:
duke@435 124 MutexLocker(Monitor * mutex) {
duke@435 125 assert(mutex->rank() != Mutex::special,
duke@435 126 "Special ranked mutex should only use MutexLockerEx");
duke@435 127 _mutex = mutex;
duke@435 128 _mutex->lock();
duke@435 129 }
duke@435 130
duke@435 131 // Overloaded constructor passing current thread
duke@435 132 MutexLocker(Monitor * mutex, Thread *thread) {
duke@435 133 assert(mutex->rank() != Mutex::special,
duke@435 134 "Special ranked mutex should only use MutexLockerEx");
duke@435 135 _mutex = mutex;
duke@435 136 _mutex->lock(thread);
duke@435 137 }
duke@435 138
duke@435 139 ~MutexLocker() {
duke@435 140 _mutex->unlock();
duke@435 141 }
duke@435 142
duke@435 143 };
duke@435 144
duke@435 145 // for debugging: check that we're already owning this lock (or are at a safepoint)
duke@435 146 #ifdef ASSERT
duke@435 147 void assert_locked_or_safepoint(const Monitor * lock);
duke@435 148 void assert_lock_strong(const Monitor * lock);
duke@435 149 #else
duke@435 150 #define assert_locked_or_safepoint(lock)
duke@435 151 #define assert_lock_strong(lock)
duke@435 152 #endif
duke@435 153
duke@435 154 // A MutexLockerEx behaves like a MutexLocker when its constructor is
duke@435 155 // called with a Mutex. Unlike a MutexLocker, its constructor can also be
duke@435 156 // called with NULL, in which case the MutexLockerEx is a no-op. There
duke@435 157 // is also a corresponding MutexUnlockerEx. We want to keep the
duke@435 158 // basic MutexLocker as fast as possible. MutexLockerEx can also lock
duke@435 159 // without safepoint check.
duke@435 160
duke@435 161 class MutexLockerEx: public StackObj {
duke@435 162 private:
duke@435 163 Monitor * _mutex;
duke@435 164 public:
duke@435 165 MutexLockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) {
duke@435 166 _mutex = mutex;
duke@435 167 if (_mutex != NULL) {
duke@435 168 assert(mutex->rank() > Mutex::special || no_safepoint_check,
duke@435 169 "Mutexes with rank special or lower should not do safepoint checks");
duke@435 170 if (no_safepoint_check)
duke@435 171 _mutex->lock_without_safepoint_check();
duke@435 172 else
duke@435 173 _mutex->lock();
duke@435 174 }
duke@435 175 }
duke@435 176
duke@435 177 ~MutexLockerEx() {
duke@435 178 if (_mutex != NULL) {
duke@435 179 _mutex->unlock();
duke@435 180 }
duke@435 181 }
duke@435 182 };
duke@435 183
duke@435 184 // A MonitorLockerEx is like a MutexLockerEx above, except it takes
duke@435 185 // a possibly null Monitor, and allows wait/notify as well which are
duke@435 186 // delegated to the underlying Monitor.
duke@435 187
duke@435 188 class MonitorLockerEx: public MutexLockerEx {
duke@435 189 private:
duke@435 190 Monitor * _monitor;
duke@435 191 public:
duke@435 192 MonitorLockerEx(Monitor* monitor,
duke@435 193 bool no_safepoint_check = !Mutex::_no_safepoint_check_flag):
duke@435 194 MutexLockerEx(monitor, no_safepoint_check),
duke@435 195 _monitor(monitor) {
duke@435 196 // Superclass constructor did locking
duke@435 197 }
duke@435 198
duke@435 199 ~MonitorLockerEx() {
duke@435 200 #ifdef ASSERT
duke@435 201 if (_monitor != NULL) {
duke@435 202 assert_lock_strong(_monitor);
duke@435 203 }
duke@435 204 #endif // ASSERT
duke@435 205 // Superclass destructor will do unlocking
duke@435 206 }
duke@435 207
duke@435 208 bool wait(bool no_safepoint_check = !Mutex::_no_safepoint_check_flag,
duke@435 209 long timeout = 0,
duke@435 210 bool as_suspend_equivalent = !Mutex::_as_suspend_equivalent_flag) {
duke@435 211 if (_monitor != NULL) {
duke@435 212 return _monitor->wait(no_safepoint_check, timeout, as_suspend_equivalent);
duke@435 213 }
duke@435 214 return false;
duke@435 215 }
duke@435 216
duke@435 217 bool notify_all() {
duke@435 218 if (_monitor != NULL) {
duke@435 219 return _monitor->notify_all();
duke@435 220 }
duke@435 221 return true;
duke@435 222 }
duke@435 223
duke@435 224 bool notify() {
duke@435 225 if (_monitor != NULL) {
duke@435 226 return _monitor->notify();
duke@435 227 }
duke@435 228 return true;
duke@435 229 }
duke@435 230 };
duke@435 231
duke@435 232
duke@435 233
duke@435 234 // A GCMutexLocker is usually initialized with a mutex that is
duke@435 235 // automatically acquired in order to do GC. The function that
duke@435 236 // synchronizes using a GCMutexLocker may be called both during and between
duke@435 237 // GC's. Thus, it must acquire the mutex if GC is not in progress, but not
duke@435 238 // if GC is in progress (since the mutex is already held on its behalf.)
duke@435 239
duke@435 240 class GCMutexLocker: public StackObj {
duke@435 241 private:
duke@435 242 Monitor * _mutex;
duke@435 243 bool _locked;
duke@435 244 public:
duke@435 245 GCMutexLocker(Monitor * mutex);
duke@435 246 ~GCMutexLocker() { if (_locked) _mutex->unlock(); }
duke@435 247 };
duke@435 248
duke@435 249
duke@435 250
duke@435 251 // A MutexUnlocker temporarily exits a previously
duke@435 252 // entered mutex for the scope which contains the unlocker.
duke@435 253
duke@435 254 class MutexUnlocker: StackObj {
duke@435 255 private:
duke@435 256 Monitor * _mutex;
duke@435 257
duke@435 258 public:
duke@435 259 MutexUnlocker(Monitor * mutex) {
duke@435 260 _mutex = mutex;
duke@435 261 _mutex->unlock();
duke@435 262 }
duke@435 263
duke@435 264 ~MutexUnlocker() {
duke@435 265 _mutex->lock();
duke@435 266 }
duke@435 267 };
duke@435 268
duke@435 269 // A MutexUnlockerEx temporarily exits a previously
duke@435 270 // entered mutex for the scope which contains the unlocker.
duke@435 271
duke@435 272 class MutexUnlockerEx: StackObj {
duke@435 273 private:
duke@435 274 Monitor * _mutex;
duke@435 275 bool _no_safepoint_check;
duke@435 276
duke@435 277 public:
duke@435 278 MutexUnlockerEx(Monitor * mutex, bool no_safepoint_check = !Mutex::_no_safepoint_check_flag) {
duke@435 279 _mutex = mutex;
duke@435 280 _no_safepoint_check = no_safepoint_check;
duke@435 281 _mutex->unlock();
duke@435 282 }
duke@435 283
duke@435 284 ~MutexUnlockerEx() {
duke@435 285 if (_no_safepoint_check == Mutex::_no_safepoint_check_flag) {
duke@435 286 _mutex->lock_without_safepoint_check();
duke@435 287 } else {
duke@435 288 _mutex->lock();
duke@435 289 }
duke@435 290 }
duke@435 291 };
duke@435 292
duke@435 293 #ifndef PRODUCT
duke@435 294 //
duke@435 295 // A special MutexLocker that allows:
duke@435 296 // - reentrant locking
duke@435 297 // - locking out of order
duke@435 298 //
duke@435 299 // Only too be used for verify code, where we can relaxe out dead-lock
duke@435 300 // dection code a bit (unsafe, but probably ok). This code is NEVER to
duke@435 301 // be included in a product version.
duke@435 302 //
duke@435 303 class VerifyMutexLocker: StackObj {
duke@435 304 private:
duke@435 305 Monitor * _mutex;
duke@435 306 bool _reentrant;
duke@435 307 public:
duke@435 308 VerifyMutexLocker(Monitor * mutex) {
duke@435 309 _mutex = mutex;
duke@435 310 _reentrant = mutex->owned_by_self();
duke@435 311 if (!_reentrant) {
duke@435 312 // We temp. diable strict safepoint checking, while we require the lock
duke@435 313 FlagSetting fs(StrictSafepointChecks, false);
duke@435 314 _mutex->lock();
duke@435 315 }
duke@435 316 }
duke@435 317
duke@435 318 ~VerifyMutexLocker() {
duke@435 319 if (!_reentrant) {
duke@435 320 _mutex->unlock();
duke@435 321 }
duke@435 322 }
duke@435 323 };
duke@435 324
duke@435 325 #endif

mercurial