30 #include "runtime/mutex.hpp" |
30 #include "runtime/mutex.hpp" |
31 |
31 |
32 // Mutexes used in the VM. |
32 // Mutexes used in the VM. |
33 |
33 |
34 extern Mutex* Patching_lock; // a lock used to guard code patching of compiled code |
34 extern Mutex* Patching_lock; // a lock used to guard code patching of compiled code |
35 extern Mutex* CompiledMethod_lock; // a lock used to guard a compiled method |
35 extern Mutex* CompiledMethod_lock; // a lock used to guard a compiled method and OSR queues |
36 extern Monitor* SystemDictionary_lock; // a lock on the system dictionary |
36 extern Monitor* SystemDictionary_lock; // a lock on the system dictionary |
37 extern Mutex* ProtectionDomainSet_lock; // a lock on the pd_set list in the system dictionary |
37 extern Mutex* ProtectionDomainSet_lock; // a lock on the pd_set list in the system dictionary |
38 extern Mutex* SharedDictionary_lock; // a lock on the CDS shared dictionary |
38 extern Mutex* SharedDictionary_lock; // a lock on the CDS shared dictionary |
39 extern Mutex* Module_lock; // a lock on module and package related data structures |
39 extern Mutex* Module_lock; // a lock on module and package related data structures |
40 extern Mutex* CompiledIC_lock; // a lock used to guard compiled IC patching and access |
40 extern Mutex* CompiledIC_lock; // a lock used to guard compiled IC patching and access |
41 extern Mutex* InlineCacheBuffer_lock; // a lock used to guard the InlineCacheBuffer |
41 extern Mutex* InlineCacheBuffer_lock; // a lock used to guard the InlineCacheBuffer |
42 extern Mutex* VMStatistic_lock; // a lock used to guard statistics count increment |
42 extern Mutex* VMStatistic_lock; // a lock used to guard statistics count increment |
43 extern Mutex* JNIGlobalAlloc_lock; // JNI global storage allocate list lock |
|
44 extern Mutex* JNIGlobalActive_lock; // JNI global storage active list lock |
|
45 extern Mutex* JNIWeakAlloc_lock; // JNI weak storage allocate list lock |
|
46 extern Mutex* JNIWeakActive_lock; // JNI weak storage active list lock |
|
47 extern Mutex* StringTableWeakAlloc_lock; // StringTable weak storage allocate list lock |
|
48 extern Mutex* StringTableWeakActive_lock; // STringTable weak storage active list lock |
|
49 extern Mutex* JNIHandleBlockFreeList_lock; // a lock on the JNI handle block free list |
43 extern Mutex* JNIHandleBlockFreeList_lock; // a lock on the JNI handle block free list |
50 extern Mutex* VMWeakAlloc_lock; // VM Weak Handles storage allocate list lock |
|
51 extern Mutex* VMWeakActive_lock; // VM Weak Handles storage active list lock |
|
52 extern Mutex* ResolvedMethodTableWeakAlloc_lock; // ResolvedMethodTable weak storage allocate list |
|
53 extern Mutex* ResolvedMethodTableWeakActive_lock; // ResolvedMethodTable weak storage active list |
|
54 extern Mutex* JmethodIdCreation_lock; // a lock on creating JNI method identifiers |
44 extern Mutex* JmethodIdCreation_lock; // a lock on creating JNI method identifiers |
55 extern Mutex* JfieldIdCreation_lock; // a lock on creating JNI static field identifiers |
45 extern Mutex* JfieldIdCreation_lock; // a lock on creating JNI static field identifiers |
56 extern Monitor* JNICritical_lock; // a lock used while entering and exiting JNI critical regions, allows GC to sometimes get in |
46 extern Monitor* JNICritical_lock; // a lock used while entering and exiting JNI critical regions, allows GC to sometimes get in |
57 extern Mutex* JvmtiThreadState_lock; // a lock on modification of JVMTI thread data |
47 extern Mutex* JvmtiThreadState_lock; // a lock on modification of JVMTI thread data |
58 extern Monitor* Heap_lock; // a lock on the heap |
48 extern Monitor* Heap_lock; // a lock on the heap |
76 extern Mutex* NonJavaThreadsListSync_lock; // a lock for NonJavaThreads list synchronization |
66 extern Mutex* NonJavaThreadsListSync_lock; // a lock for NonJavaThreads list synchronization |
77 extern Monitor* CGC_lock; // used for coordination between |
67 extern Monitor* CGC_lock; // used for coordination between |
78 // fore- & background GC threads. |
68 // fore- & background GC threads. |
79 extern Monitor* STS_lock; // used for joining/leaving SuspendibleThreadSet. |
69 extern Monitor* STS_lock; // used for joining/leaving SuspendibleThreadSet. |
80 extern Monitor* FullGCCount_lock; // in support of "concurrent" full gc |
70 extern Monitor* FullGCCount_lock; // in support of "concurrent" full gc |
81 extern Monitor* SATB_Q_CBL_mon; // Protects SATB Q |
|
82 // completed buffer queue. |
|
83 extern Monitor* DirtyCardQ_CBL_mon; // Protects dirty card Q |
71 extern Monitor* DirtyCardQ_CBL_mon; // Protects dirty card Q |
84 // completed buffer queue. |
72 // completed buffer queue. |
85 extern Mutex* Shared_DirtyCardQ_lock; // Lock protecting dirty card |
73 extern Mutex* Shared_DirtyCardQ_lock; // Lock protecting dirty card |
86 // queue shared by |
74 // queue shared by |
87 // non-Java threads. |
75 // non-Java threads. |
101 extern Monitor* InitCompleted_lock; // a lock used to signal threads waiting on init completed |
89 extern Monitor* InitCompleted_lock; // a lock used to signal threads waiting on init completed |
102 extern Monitor* BeforeExit_lock; // a lock used to guard cleanups and shutdown hooks |
90 extern Monitor* BeforeExit_lock; // a lock used to guard cleanups and shutdown hooks |
103 extern Monitor* Notify_lock; // a lock used to synchronize the start-up of the vm |
91 extern Monitor* Notify_lock; // a lock used to synchronize the start-up of the vm |
104 extern Mutex* ProfilePrint_lock; // a lock used to serialize the printing of profiles |
92 extern Mutex* ProfilePrint_lock; // a lock used to serialize the printing of profiles |
105 extern Mutex* ExceptionCache_lock; // a lock used to synchronize exception cache updates |
93 extern Mutex* ExceptionCache_lock; // a lock used to synchronize exception cache updates |
106 extern Mutex* OsrList_lock; // a lock used to serialize access to OSR queues |
|
107 extern Mutex* NMethodSweeperStats_lock; // a lock used to serialize access to sweeper statistics |
94 extern Mutex* NMethodSweeperStats_lock; // a lock used to serialize access to sweeper statistics |
108 |
95 |
109 #ifndef PRODUCT |
96 #ifndef PRODUCT |
110 extern Mutex* FullGCALot_lock; // a lock to make FullGCALot MT safe |
97 extern Mutex* FullGCALot_lock; // a lock to make FullGCALot MT safe |
111 #endif // PRODUCT |
98 #endif // PRODUCT |
114 extern Mutex* Debug3_lock; |
101 extern Mutex* Debug3_lock; |
115 |
102 |
116 extern Mutex* RawMonitor_lock; |
103 extern Mutex* RawMonitor_lock; |
117 extern Mutex* PerfDataMemAlloc_lock; // a lock on the allocator for PerfData memory for performance data |
104 extern Mutex* PerfDataMemAlloc_lock; // a lock on the allocator for PerfData memory for performance data |
118 extern Mutex* PerfDataManager_lock; // a long on access to PerfDataManager resources |
105 extern Mutex* PerfDataManager_lock; // a long on access to PerfDataManager resources |
119 extern Mutex* ParkerFreeList_lock; |
|
120 extern Mutex* OopMapCacheAlloc_lock; // protects allocation of oop_map caches |
106 extern Mutex* OopMapCacheAlloc_lock; // protects allocation of oop_map caches |
121 |
107 |
122 extern Mutex* FreeList_lock; // protects the free region list during safepoints |
108 extern Mutex* FreeList_lock; // protects the free region list during safepoints |
123 extern Mutex* OldSets_lock; // protects the old region sets |
109 extern Mutex* OldSets_lock; // protects the old region sets |
124 extern Monitor* RootRegionScan_lock; // used to notify that the CM threads have finished scanning the IM snapshot regions |
110 extern Monitor* RootRegionScan_lock; // used to notify that the CM threads have finished scanning the IM snapshot regions |
125 |
111 |
126 extern Mutex* Management_lock; // a lock used to serialize JVM management |
112 extern Mutex* Management_lock; // a lock used to serialize JVM management |
127 extern Monitor* Service_lock; // a lock used for service thread operation |
113 extern Monitor* Service_lock; // a lock used for service thread operation |
|
114 extern Monitor* Notification_lock; // a lock used for notification thread operation |
128 extern Monitor* PeriodicTask_lock; // protects the periodic task structure |
115 extern Monitor* PeriodicTask_lock; // protects the periodic task structure |
129 extern Monitor* RedefineClasses_lock; // locks classes from parallel redefinition |
116 extern Monitor* RedefineClasses_lock; // locks classes from parallel redefinition |
|
117 extern Mutex* Verify_lock; // synchronize initialization of verify library |
130 extern Monitor* ThreadsSMRDelete_lock; // Used by ThreadsSMRSupport to take pressure off the Threads_lock |
118 extern Monitor* ThreadsSMRDelete_lock; // Used by ThreadsSMRSupport to take pressure off the Threads_lock |
|
119 extern Mutex* ThreadIdTableCreate_lock; // Used by ThreadIdTable to lazily create the thread id table |
131 extern Mutex* SharedDecoder_lock; // serializes access to the decoder during normal (not error reporting) use |
120 extern Mutex* SharedDecoder_lock; // serializes access to the decoder during normal (not error reporting) use |
132 extern Mutex* DCmdFactory_lock; // serialize access to DCmdFactory information |
121 extern Mutex* DCmdFactory_lock; // serialize access to DCmdFactory information |
133 #if INCLUDE_NMT |
122 #if INCLUDE_NMT |
134 extern Mutex* NMTQuery_lock; // serialize NMT Dcmd queries |
123 extern Mutex* NMTQuery_lock; // serialize NMT Dcmd queries |
135 #endif |
124 #endif |
153 |
142 |
154 extern Mutex* MetaspaceExpand_lock; // protects Metaspace virtualspace and chunk expansions |
143 extern Mutex* MetaspaceExpand_lock; // protects Metaspace virtualspace and chunk expansions |
155 extern Mutex* ClassLoaderDataGraph_lock; // protects CLDG list, needed for concurrent unloading |
144 extern Mutex* ClassLoaderDataGraph_lock; // protects CLDG list, needed for concurrent unloading |
156 |
145 |
157 |
146 |
158 extern Monitor* CodeHeapStateAnalytics_lock; // lock print functions against concurrent analyze functions. |
147 extern Mutex* CodeHeapStateAnalytics_lock; // lock print functions against concurrent analyze functions. |
159 // Only used locally in PrintCodeCacheLayout processing. |
148 // Only used locally in PrintCodeCacheLayout processing. |
160 |
149 |
161 #if INCLUDE_JVMCI |
150 #if INCLUDE_JVMCI |
162 extern Monitor* JVMCI_lock; // Monitor to control initialization of JVMCI |
151 extern Monitor* JVMCI_lock; // Monitor to control initialization of JVMCI |
163 extern Mutex* JVMCIGlobalAlloc_lock; // JVMCI global storage allocate list lock |
152 #endif |
164 extern Mutex* JVMCIGlobalActive_lock; // JVMCI global storage active list lock |
153 |
165 #endif |
154 extern Mutex* tty_lock; // lock to synchronize output. |
166 |
155 |
167 // A MutexLocker provides mutual exclusion with respect to a given mutex |
156 // A MutexLocker provides mutual exclusion with respect to a given mutex |
168 // for the scope which contains the locker. The lock is an OS lock, not |
157 // for the scope which contains the locker. The lock is an OS lock, not |
169 // an object lock, and the two do not interoperate. Do not use Mutex-based |
158 // an object lock, and the two do not interoperate. Do not use Mutex-based |
170 // locks to lock on Java objects, because they will not be respected if a |
159 // locks to lock on Java objects, because they will not be respected if a |
184 |
173 |
185 char *lock_name(Mutex *mutex); |
174 char *lock_name(Mutex *mutex); |
186 |
175 |
187 // for debugging: check that we're already owning this lock (or are at a safepoint) |
176 // for debugging: check that we're already owning this lock (or are at a safepoint) |
188 #ifdef ASSERT |
177 #ifdef ASSERT |
189 void assert_locked_or_safepoint(const Monitor * lock); |
178 void assert_locked_or_safepoint(const Mutex* lock); |
190 void assert_locked_or_safepoint_weak(const Monitor * lock); |
179 void assert_locked_or_safepoint_weak(const Mutex* lock); |
191 void assert_lock_strong(const Monitor * lock); |
180 void assert_lock_strong(const Mutex* lock); |
192 #else |
181 #else |
193 #define assert_locked_or_safepoint(lock) |
182 #define assert_locked_or_safepoint(lock) |
194 #define assert_locked_or_safepoint_weak(lock) |
183 #define assert_locked_or_safepoint_weak(lock) |
195 #define assert_lock_strong(lock) |
184 #define assert_lock_strong(lock) |
196 #endif |
185 #endif |
197 |
186 |
198 class MutexLocker: public StackObj { |
187 class MutexLocker: public StackObj { |
199 protected: |
188 protected: |
200 Monitor* _mutex; |
189 Mutex* _mutex; |
201 private: |
190 private: |
202 public: |
191 public: |
203 MutexLocker(Monitor* mutex, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
192 MutexLocker(Mutex* mutex, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
204 _mutex(mutex) { |
193 _mutex(mutex) { |
205 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag; |
194 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag; |
206 if (_mutex != NULL) { |
195 if (_mutex != NULL) { |
207 assert(_mutex->rank() > Mutex::special || no_safepoint_check, |
196 assert(_mutex->rank() > Mutex::special || no_safepoint_check, |
208 "Mutexes with rank special or lower should not do safepoint checks"); |
197 "Mutexes with rank special or lower should not do safepoint checks"); |
212 _mutex->lock(); |
201 _mutex->lock(); |
213 } |
202 } |
214 } |
203 } |
215 } |
204 } |
216 |
205 |
217 MutexLocker(Monitor* mutex, Thread* thread, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
206 MutexLocker(Mutex* mutex, Thread* thread, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
218 _mutex(mutex) { |
207 _mutex(mutex) { |
219 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag; |
208 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag; |
220 if (_mutex != NULL) { |
209 if (_mutex != NULL) { |
221 assert(_mutex->rank() > Mutex::special || no_safepoint_check, |
210 assert(_mutex->rank() > Mutex::special || no_safepoint_check, |
222 "Mutexes with rank special or lower should not do safepoint checks"); |
211 "Mutexes with rank special or lower should not do safepoint checks"); |
240 // wait/notify as well which are delegated to the underlying Monitor. |
229 // wait/notify as well which are delegated to the underlying Monitor. |
241 // It also disallows NULL. |
230 // It also disallows NULL. |
242 |
231 |
243 class MonitorLocker: public MutexLocker { |
232 class MonitorLocker: public MutexLocker { |
244 Mutex::SafepointCheckFlag _flag; |
233 Mutex::SafepointCheckFlag _flag; |
|
234 Monitor* _monitor; |
245 public: |
235 public: |
246 MonitorLocker(Monitor* monitor, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
236 MonitorLocker(Monitor* monitor, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
247 MutexLocker(monitor, flag), _flag(flag) { |
237 MutexLocker(monitor, flag), _flag(flag), _monitor(monitor) { |
248 // Superclass constructor did locking |
238 // Superclass constructor did locking |
249 assert(_mutex != NULL, "NULL monitor not allowed"); |
239 assert(_monitor != NULL, "NULL monitor not allowed"); |
250 } |
240 } |
251 |
241 |
252 MonitorLocker(Monitor* monitor, Thread* thread, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
242 MonitorLocker(Monitor* monitor, Thread* thread, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
253 MutexLocker(monitor, thread, flag), _flag(flag) { |
243 MutexLocker(monitor, thread, flag), _flag(flag), _monitor(monitor) { |
254 // Superclass constructor did locking |
244 // Superclass constructor did locking |
255 assert(_mutex != NULL, "NULL monitor not allowed"); |
245 assert(_monitor != NULL, "NULL monitor not allowed"); |
256 } |
246 } |
257 |
247 |
258 bool wait(long timeout = 0, |
248 bool wait(long timeout = 0, |
259 bool as_suspend_equivalent = !Mutex::_as_suspend_equivalent_flag) { |
249 bool as_suspend_equivalent = !Mutex::_as_suspend_equivalent_flag) { |
260 if (_flag == Mutex::_safepoint_check_flag) { |
250 if (_flag == Mutex::_safepoint_check_flag) { |
261 return _mutex->wait(timeout, as_suspend_equivalent); |
251 return _monitor->wait(timeout, as_suspend_equivalent); |
262 } else { |
252 } else { |
263 return _mutex->wait_without_safepoint_check(timeout); |
253 return _monitor->wait_without_safepoint_check(timeout); |
264 } |
254 } |
265 return false; |
255 return false; |
266 } |
256 } |
267 |
257 |
268 void notify_all() { |
258 void notify_all() { |
269 _mutex->notify_all(); |
259 _monitor->notify_all(); |
270 } |
260 } |
271 |
261 |
272 void notify() { |
262 void notify() { |
273 _mutex->notify(); |
263 _monitor->notify(); |
274 } |
264 } |
275 }; |
265 }; |
276 |
266 |
277 |
267 |
278 // A GCMutexLocker is usually initialized with a mutex that is |
268 // A GCMutexLocker is usually initialized with a mutex that is |
281 // GC's. Thus, it must acquire the mutex if GC is not in progress, but not |
271 // GC's. Thus, it must acquire the mutex if GC is not in progress, but not |
282 // if GC is in progress (since the mutex is already held on its behalf.) |
272 // if GC is in progress (since the mutex is already held on its behalf.) |
283 |
273 |
284 class GCMutexLocker: public StackObj { |
274 class GCMutexLocker: public StackObj { |
285 private: |
275 private: |
286 Monitor* _mutex; |
276 Mutex* _mutex; |
287 bool _locked; |
277 bool _locked; |
288 public: |
278 public: |
289 GCMutexLocker(Monitor* mutex); |
279 GCMutexLocker(Mutex* mutex); |
290 ~GCMutexLocker() { if (_locked) _mutex->unlock(); } |
280 ~GCMutexLocker() { if (_locked) _mutex->unlock(); } |
291 }; |
281 }; |
292 |
282 |
293 // A MutexUnlocker temporarily exits a previously |
283 // A MutexUnlocker temporarily exits a previously |
294 // entered mutex for the scope which contains the unlocker. |
284 // entered mutex for the scope which contains the unlocker. |
295 |
285 |
296 class MutexUnlocker: StackObj { |
286 class MutexUnlocker: StackObj { |
297 private: |
287 private: |
298 Monitor* _mutex; |
288 Mutex* _mutex; |
299 bool _no_safepoint_check; |
289 bool _no_safepoint_check; |
300 |
290 |
301 public: |
291 public: |
302 MutexUnlocker(Monitor* mutex, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
292 MutexUnlocker(Mutex* mutex, Mutex::SafepointCheckFlag flag = Mutex::_safepoint_check_flag) : |
303 _mutex(mutex), |
293 _mutex(mutex), |
304 _no_safepoint_check(flag) { |
294 _no_safepoint_check(flag) { |
305 _mutex->unlock(); |
295 _mutex->unlock(); |
306 } |
296 } |
307 |
297 |