30 #include "runtime/safepointMechanism.inline.hpp" |
30 #include "runtime/safepointMechanism.inline.hpp" |
31 #include "runtime/thread.inline.hpp" |
31 #include "runtime/thread.inline.hpp" |
32 #include "utilities/events.hpp" |
32 #include "utilities/events.hpp" |
33 #include "utilities/macros.hpp" |
33 #include "utilities/macros.hpp" |
34 |
34 |
|
35 #ifdef ASSERT |
|
36 void Monitor::check_safepoint_state(Thread* thread, bool do_safepoint_check) { |
|
37 // If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never. |
|
38 SafepointCheckRequired not_allowed = do_safepoint_check ? Monitor::_safepoint_check_never : |
|
39 Monitor::_safepoint_check_always; |
|
40 assert(!thread->is_Java_thread() || _safepoint_check_required != not_allowed, |
|
41 "This lock should %s have a safepoint check for Java threads: %s", |
|
42 _safepoint_check_required ? "always" : "never", name()); |
|
43 |
|
44 // If defined with safepoint_check_never, a NonJavaThread should never ask to safepoint check either. |
|
45 assert(thread->is_Java_thread() || !do_safepoint_check || _safepoint_check_required != Monitor::_safepoint_check_never, |
|
46 "NonJavaThread should not check for safepoint"); |
|
47 } |
|
48 #endif // ASSERT |
35 |
49 |
36 void Monitor::lock(Thread * self) { |
50 void Monitor::lock(Thread * self) { |
37 // Ensure that the Monitor requires/allows safepoint checks. |
51 check_safepoint_state(self, true); |
38 assert(_safepoint_check_required != Monitor::_safepoint_check_never, |
|
39 "This lock should never have a safepoint check: %s", name()); |
|
40 |
52 |
41 #ifdef CHECK_UNHANDLED_OOPS |
53 #ifdef CHECK_UNHANDLED_OOPS |
42 // Clear unhandled oops in JavaThreads so we get a crash right away. |
54 // Clear unhandled oops in JavaThreads so we get a crash right away. |
43 if (self->is_Java_thread()) { |
55 if (self->is_Java_thread()) { |
44 self->clear_unhandled_oops(); |
56 self->clear_unhandled_oops(); |
89 // acquiring this lock. If the thread blocks acquiring the lock it is not |
101 // acquiring this lock. If the thread blocks acquiring the lock it is not |
90 // safepoint-safe and so will prevent a safepoint from being reached. If used |
102 // safepoint-safe and so will prevent a safepoint from being reached. If used |
91 // in the wrong way this can lead to a deadlock with the safepoint code. |
103 // in the wrong way this can lead to a deadlock with the safepoint code. |
92 |
104 |
93 void Monitor::lock_without_safepoint_check(Thread * self) { |
105 void Monitor::lock_without_safepoint_check(Thread * self) { |
94 // Ensure that the Monitor does not require safepoint checks. |
106 check_safepoint_state(self, false); |
95 assert(_safepoint_check_required != Monitor::_safepoint_check_always, |
|
96 "This lock should always have a safepoint check: %s", name()); |
|
97 assert(_owner != self, "invariant"); |
107 assert(_owner != self, "invariant"); |
98 _lock.lock(); |
108 _lock.lock(); |
99 assert_owner(NULL); |
109 assert_owner(NULL); |
100 set_owner(self); |
110 set_owner(self); |
101 } |
111 } |
152 } |
162 } |
153 } |
163 } |
154 #endif // ASSERT |
164 #endif // ASSERT |
155 |
165 |
156 bool Monitor::wait_without_safepoint_check(long timeout) { |
166 bool Monitor::wait_without_safepoint_check(long timeout) { |
157 // Make sure safepoint checking is used properly. |
167 Thread* const self = Thread::current(); |
158 assert(_safepoint_check_required != Monitor::_safepoint_check_always, |
168 check_safepoint_state(self, false); |
159 "This lock should always have a safepoint check: %s", name()); |
|
160 |
169 |
161 // timeout is in milliseconds - with zero meaning never timeout |
170 // timeout is in milliseconds - with zero meaning never timeout |
162 assert(timeout >= 0, "negative timeout"); |
171 assert(timeout >= 0, "negative timeout"); |
163 |
172 |
164 Thread * const self = Thread::current(); |
|
165 assert_owner(self); |
173 assert_owner(self); |
166 assert_wait_lock_state(self); |
174 assert_wait_lock_state(self); |
167 |
175 |
168 // conceptually set the owner to NULL in anticipation of |
176 // conceptually set the owner to NULL in anticipation of |
169 // abdicating the lock in wait |
177 // abdicating the lock in wait |
172 set_owner(self); |
180 set_owner(self); |
173 return wait_status != 0; // return true IFF timeout |
181 return wait_status != 0; // return true IFF timeout |
174 } |
182 } |
175 |
183 |
176 bool Monitor::wait(long timeout, bool as_suspend_equivalent) { |
184 bool Monitor::wait(long timeout, bool as_suspend_equivalent) { |
177 // Make sure safepoint checking is used properly. |
185 Thread* const self = Thread::current(); |
178 assert(_safepoint_check_required != Monitor::_safepoint_check_never, |
186 check_safepoint_state(self, true); |
179 "This lock should never have a safepoint check: %s", name()); |
|
180 |
187 |
181 // timeout is in milliseconds - with zero meaning never timeout |
188 // timeout is in milliseconds - with zero meaning never timeout |
182 assert(timeout >= 0, "negative timeout"); |
189 assert(timeout >= 0, "negative timeout"); |
183 |
190 |
184 Thread* const self = Thread::current(); |
|
185 assert_owner(self); |
191 assert_owner(self); |
186 |
192 |
187 // Safepoint checking logically implies java_thread |
193 // Safepoint checking logically implies java_thread |
188 guarantee(self->is_Java_thread(), "invariant"); |
194 guarantee(self->is_Java_thread(), "invariant"); |
189 assert_wait_lock_state(self); |
195 assert_wait_lock_state(self); |
271 Monitor::Monitor() { |
277 Monitor::Monitor() { |
272 assert(os::mutex_init_done(), "Too early!"); |
278 assert(os::mutex_init_done(), "Too early!"); |
273 ClearMonitor(this); |
279 ClearMonitor(this); |
274 } |
280 } |
275 |
281 |
|
282 |
|
283 // Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes. |
|
284 bool is_sometimes_ok(const char* name) { |
|
285 return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0); |
|
286 } |
|
287 |
276 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block, |
288 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block, |
277 SafepointCheckRequired safepoint_check_required) { |
289 SafepointCheckRequired safepoint_check_required) { |
278 assert(os::mutex_init_done(), "Too early!"); |
290 assert(os::mutex_init_done(), "Too early!"); |
279 ClearMonitor(this, name); |
291 ClearMonitor(this, name); |
280 #ifdef ASSERT |
292 #ifdef ASSERT |
281 _allow_vm_block = allow_vm_block; |
293 _allow_vm_block = allow_vm_block; |
282 _rank = Rank; |
294 _rank = Rank; |
283 NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;) |
295 NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;) |
|
296 |
|
297 assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name), |
|
298 "Lock has _safepoint_check_sometimes %s", name); |
284 #endif |
299 #endif |
285 } |
300 } |
286 |
301 |
287 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block, |
302 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block, |
288 SafepointCheckRequired safepoint_check_required) { |
303 SafepointCheckRequired safepoint_check_required) { |
289 ClearMonitor((Monitor *) this, name); |
304 ClearMonitor((Monitor *) this, name); |
290 #ifdef ASSERT |
305 #ifdef ASSERT |
291 _allow_vm_block = allow_vm_block; |
306 _allow_vm_block = allow_vm_block; |
292 _rank = Rank; |
307 _rank = Rank; |
293 NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;) |
308 NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;) |
|
309 |
|
310 assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name), |
|
311 "Lock has _safepoint_check_sometimes %s", name); |
294 #endif |
312 #endif |
295 } |
313 } |
296 |
314 |
297 bool Monitor::owned_by_self() const { |
315 bool Monitor::owned_by_self() const { |
298 return _owner == Thread::current(); |
316 return _owner == Thread::current(); |