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