annotate src/share/vm/runtime/mutexLocker.hpp @ 844:bd02caa94611

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