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 |
35 #ifdef ASSERT |
36 void Monitor::check_safepoint_state(Thread* thread, bool do_safepoint_check) { |
36 void Mutex::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. |
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 : |
38 SafepointCheckRequired not_allowed = do_safepoint_check ? Mutex::_safepoint_check_never : |
39 Monitor::_safepoint_check_always; |
39 Mutex::_safepoint_check_always; |
40 assert(!thread->is_active_Java_thread() || _safepoint_check_required != not_allowed, |
40 assert(!thread->is_active_Java_thread() || _safepoint_check_required != not_allowed, |
41 "This lock should %s have a safepoint check for Java threads: %s", |
41 "This lock should %s have a safepoint check for Java threads: %s", |
42 _safepoint_check_required ? "always" : "never", name()); |
42 _safepoint_check_required ? "always" : "never", name()); |
43 |
43 |
44 // If defined with safepoint_check_never, a NonJavaThread should never ask to safepoint check either. |
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, |
45 assert(thread->is_Java_thread() || !do_safepoint_check || _safepoint_check_required != Mutex::_safepoint_check_never, |
46 "NonJavaThread should not check for safepoint"); |
46 "NonJavaThread should not check for safepoint"); |
47 } |
47 } |
48 #endif // ASSERT |
48 #endif // ASSERT |
49 |
49 |
50 void Monitor::lock(Thread * self) { |
50 void Mutex::lock(Thread * self) { |
51 check_safepoint_state(self, true); |
51 check_safepoint_state(self, true); |
52 |
52 |
53 DEBUG_ONLY(check_prelock_state(self, true)); |
53 DEBUG_ONLY(check_prelock_state(self, true)); |
54 assert(_owner != self, "invariant"); |
54 assert(_owner != self, "invariant"); |
55 |
55 |
56 Monitor* in_flight_monitor = NULL; |
56 Mutex* in_flight_mutex = NULL; |
57 DEBUG_ONLY(int retry_cnt = 0;) |
57 DEBUG_ONLY(int retry_cnt = 0;) |
58 bool is_active_Java_thread = self->is_active_Java_thread(); |
58 bool is_active_Java_thread = self->is_active_Java_thread(); |
59 while (!_lock.try_lock()) { |
59 while (!_lock.try_lock()) { |
60 // The lock is contended |
60 // The lock is contended |
61 |
61 |
62 #ifdef ASSERT |
62 #ifdef ASSERT |
63 check_block_state(self); |
63 check_block_state(self); |
64 if (retry_cnt++ > 3) { |
64 if (retry_cnt++ > 3) { |
65 log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name); |
65 log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name); |
66 } |
66 } |
67 #endif // ASSERT |
67 #endif // ASSERT |
68 |
68 |
69 // Is it a JavaThread participating in the safepoint protocol. |
69 // Is it a JavaThread participating in the safepoint protocol. |
70 if (is_active_Java_thread) { |
70 if (is_active_Java_thread) { |
71 assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex"); |
71 assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex"); |
72 { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_monitor); |
72 { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex); |
73 in_flight_monitor = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
73 in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
74 _lock.lock(); |
74 _lock.lock(); |
75 } |
75 } |
76 if (in_flight_monitor != NULL) { |
76 if (in_flight_mutex != NULL) { |
77 // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck |
77 // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck |
78 break; |
78 break; |
79 } |
79 } |
80 } else { |
80 } else { |
81 _lock.lock(); |
81 _lock.lock(); |
85 |
85 |
86 assert_owner(NULL); |
86 assert_owner(NULL); |
87 set_owner(self); |
87 set_owner(self); |
88 } |
88 } |
89 |
89 |
90 void Monitor::lock() { |
90 void Mutex::lock() { |
91 this->lock(Thread::current()); |
91 this->lock(Thread::current()); |
92 } |
92 } |
93 |
93 |
94 // Lock without safepoint check - a degenerate variant of lock() for use by |
94 // Lock without safepoint check - a degenerate variant of lock() for use by |
95 // JavaThreads when it is known to be safe to not check for a safepoint when |
95 // JavaThreads when it is known to be safe to not check for a safepoint when |
96 // acquiring this lock. If the thread blocks acquiring the lock it is not |
96 // acquiring this lock. If the thread blocks acquiring the lock it is not |
97 // safepoint-safe and so will prevent a safepoint from being reached. If used |
97 // safepoint-safe and so will prevent a safepoint from being reached. If used |
98 // in the wrong way this can lead to a deadlock with the safepoint code. |
98 // in the wrong way this can lead to a deadlock with the safepoint code. |
99 |
99 |
100 void Monitor::lock_without_safepoint_check(Thread * self) { |
100 void Mutex::lock_without_safepoint_check(Thread * self) { |
101 check_safepoint_state(self, false); |
101 check_safepoint_state(self, false); |
102 assert(_owner != self, "invariant"); |
102 assert(_owner != self, "invariant"); |
103 _lock.lock(); |
103 _lock.lock(); |
104 assert_owner(NULL); |
104 assert_owner(NULL); |
105 set_owner(self); |
105 set_owner(self); |
106 } |
106 } |
107 |
107 |
108 void Monitor::lock_without_safepoint_check() { |
108 void Mutex::lock_without_safepoint_check() { |
109 lock_without_safepoint_check(Thread::current()); |
109 lock_without_safepoint_check(Thread::current()); |
110 } |
110 } |
111 |
111 |
112 |
112 |
113 // Returns true if thread succeeds in grabbing the lock, otherwise false. |
113 // Returns true if thread succeeds in grabbing the lock, otherwise false. |
114 |
114 |
115 bool Monitor::try_lock() { |
115 bool Mutex::try_lock() { |
116 Thread * const self = Thread::current(); |
116 Thread * const self = Thread::current(); |
117 DEBUG_ONLY(check_prelock_state(self, false);) |
117 DEBUG_ONLY(check_prelock_state(self, false);) |
118 |
118 |
119 if (_lock.try_lock()) { |
119 if (_lock.try_lock()) { |
120 assert_owner(NULL); |
120 assert_owner(NULL); |
145 _lock.notify_all(); |
145 _lock.notify_all(); |
146 } |
146 } |
147 |
147 |
148 #ifdef ASSERT |
148 #ifdef ASSERT |
149 void Monitor::assert_wait_lock_state(Thread* self) { |
149 void Monitor::assert_wait_lock_state(Thread* self) { |
150 Monitor* least = get_least_ranked_lock_besides_this(self->owned_locks()); |
150 Mutex* least = get_least_ranked_lock_besides_this(self->owned_locks()); |
151 assert(least != this, "Specification of get_least_... call above"); |
151 assert(least != this, "Specification of get_least_... call above"); |
152 if (least != NULL && least->rank() <= special) { |
152 if (least != NULL && least->rank() <= special) { |
153 ::tty->print("Attempting to wait on monitor %s/%d while holding" |
153 ::tty->print("Attempting to wait on monitor %s/%d while holding" |
154 " lock %s/%d -- possible deadlock", |
154 " lock %s/%d -- possible deadlock", |
155 name(), rank(), least->name(), least->rank()); |
155 name(), rank(), least->name(), least->rank()); |
192 int wait_status; |
192 int wait_status; |
193 // conceptually set the owner to NULL in anticipation of |
193 // conceptually set the owner to NULL in anticipation of |
194 // abdicating the lock in wait |
194 // abdicating the lock in wait |
195 set_owner(NULL); |
195 set_owner(NULL); |
196 JavaThread *jt = (JavaThread *)self; |
196 JavaThread *jt = (JavaThread *)self; |
197 Monitor* in_flight_monitor = NULL; |
197 Mutex* in_flight_mutex = NULL; |
198 |
198 |
199 { |
199 { |
200 ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor); |
200 ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_mutex); |
201 OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */); |
201 OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */); |
202 if (as_suspend_equivalent) { |
202 if (as_suspend_equivalent) { |
203 jt->set_suspend_equivalent(); |
203 jt->set_suspend_equivalent(); |
204 // cleared by handle_special_suspend_equivalent_condition() or |
204 // cleared by handle_special_suspend_equivalent_condition() or |
205 // java_suspend_self() |
205 // java_suspend_self() |
206 } |
206 } |
207 |
207 |
208 wait_status = _lock.wait(timeout); |
208 wait_status = _lock.wait(timeout); |
209 in_flight_monitor = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
209 in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
210 |
210 |
211 // were we externally suspended while we were waiting? |
211 // were we externally suspended while we were waiting? |
212 if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) { |
212 if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) { |
213 // Our event wait has finished and we own the lock, but |
213 // Our event wait has finished and we own the lock, but |
214 // while we were waiting another thread suspended us. We don't |
214 // while we were waiting another thread suspended us. We don't |
230 } |
230 } |
231 |
231 |
232 return wait_status != 0; // return true IFF timeout |
232 return wait_status != 0; // return true IFF timeout |
233 } |
233 } |
234 |
234 |
235 Monitor::~Monitor() { |
235 Mutex::~Mutex() { |
236 assert_owner(NULL); |
236 assert_owner(NULL); |
237 } |
237 } |
238 |
238 |
239 // Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes. |
239 // Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes. |
240 bool is_sometimes_ok(const char* name) { |
240 bool is_sometimes_ok(const char* name) { |
241 return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0); |
241 return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0); |
242 } |
242 } |
243 |
243 |
244 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block, |
244 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block, |
245 SafepointCheckRequired safepoint_check_required) : _owner(NULL) { |
245 SafepointCheckRequired safepoint_check_required) : _owner(NULL) { |
246 assert(os::mutex_init_done(), "Too early!"); |
246 assert(os::mutex_init_done(), "Too early!"); |
247 if (name == NULL) { |
247 if (name == NULL) { |
248 strcpy(_name, "UNKNOWN"); |
248 strcpy(_name, "UNKNOWN"); |
249 } else { |
249 } else { |
250 strncpy(_name, name, MONITOR_NAME_LEN - 1); |
250 strncpy(_name, name, MUTEX_NAME_LEN - 1); |
251 _name[MONITOR_NAME_LEN - 1] = '\0'; |
251 _name[MUTEX_NAME_LEN - 1] = '\0'; |
252 } |
252 } |
253 #ifdef ASSERT |
253 #ifdef ASSERT |
254 _allow_vm_block = allow_vm_block; |
254 _allow_vm_block = allow_vm_block; |
255 _rank = Rank; |
255 _rank = Rank; |
256 _safepoint_check_required = safepoint_check_required; |
256 _safepoint_check_required = safepoint_check_required; |
257 |
257 |
258 assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name), |
258 assert(_safepoint_check_required != Mutex::_safepoint_check_sometimes || is_sometimes_ok(name), |
259 "Lock has _safepoint_check_sometimes %s", name); |
259 "Lock has _safepoint_check_sometimes %s", name); |
260 #endif |
260 #endif |
261 } |
261 } |
262 |
262 |
263 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block, |
263 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block, |
264 SafepointCheckRequired safepoint_check_required) : |
264 SafepointCheckRequired safepoint_check_required) : |
265 Monitor(Rank, name, allow_vm_block, safepoint_check_required) {} |
265 Mutex(Rank, name, allow_vm_block, safepoint_check_required) {} |
266 |
266 |
267 bool Monitor::owned_by_self() const { |
267 bool Mutex::owned_by_self() const { |
268 return _owner == Thread::current(); |
268 return _owner == Thread::current(); |
269 } |
269 } |
270 |
270 |
271 void Monitor::print_on_error(outputStream* st) const { |
271 void Mutex::print_on_error(outputStream* st) const { |
272 st->print("[" PTR_FORMAT, p2i(this)); |
272 st->print("[" PTR_FORMAT, p2i(this)); |
273 st->print("] %s", _name); |
273 st->print("] %s", _name); |
274 st->print(" - owner thread: " PTR_FORMAT, p2i(_owner)); |
274 st->print(" - owner thread: " PTR_FORMAT, p2i(_owner)); |
275 } |
275 } |
276 |
276 |
277 // ---------------------------------------------------------------------------------- |
277 // ---------------------------------------------------------------------------------- |
278 // Non-product code |
278 // Non-product code |
279 |
279 |
280 #ifndef PRODUCT |
280 #ifndef PRODUCT |
281 void Monitor::print_on(outputStream* st) const { |
281 void Mutex::print_on(outputStream* st) const { |
282 st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT, |
282 st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT, |
283 p2i(this), _name, p2i(_owner)); |
283 p2i(this), _name, p2i(_owner)); |
284 } |
284 } |
285 #endif |
285 #endif |
286 |
286 |
287 #ifndef PRODUCT |
287 #ifndef PRODUCT |
288 #ifdef ASSERT |
288 #ifdef ASSERT |
289 |
289 |
290 void Monitor::assert_owner(Thread * expected) { |
290 void Mutex::assert_owner(Thread * expected) { |
291 const char* msg = "invalid owner"; |
291 const char* msg = "invalid owner"; |
292 if (expected == NULL) { |
292 if (expected == NULL) { |
293 msg = "should be un-owned"; |
293 msg = "should be un-owned"; |
294 } |
294 } |
295 else if (expected == Thread::current()) { |
295 else if (expected == Thread::current()) { |
354 // Called immediately after lock acquisition or release as a diagnostic |
354 // Called immediately after lock acquisition or release as a diagnostic |
355 // to track the lock-set of the thread and test for rank violations that |
355 // to track the lock-set of the thread and test for rank violations that |
356 // might indicate exposure to deadlock. |
356 // might indicate exposure to deadlock. |
357 // Rather like an EventListener for _owner (:>). |
357 // Rather like an EventListener for _owner (:>). |
358 |
358 |
359 void Monitor::set_owner_implementation(Thread *new_owner) { |
359 void Mutex::set_owner_implementation(Thread *new_owner) { |
360 // This function is solely responsible for maintaining |
360 // This function is solely responsible for maintaining |
361 // and checking the invariant that threads and locks |
361 // and checking the invariant that threads and locks |
362 // are in a 1/N relation, with some some locks unowned. |
362 // are in a 1/N relation, with some some locks unowned. |
363 // It uses the Mutex::_owner, Mutex::_next, and |
363 // It uses the Mutex::_owner, Mutex::_next, and |
364 // Thread::_owned_locks fields, and no other function |
364 // Thread::_owned_locks fields, and no other function |
438 } |
438 } |
439 } |
439 } |
440 |
440 |
441 |
441 |
442 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock() |
442 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock() |
443 void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) { |
443 void Mutex::check_prelock_state(Thread *thread, bool safepoint_check) { |
444 if (safepoint_check) { |
444 if (safepoint_check) { |
445 assert((!thread->is_active_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm) |
445 assert((!thread->is_active_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm) |
446 || rank() == Mutex::special, "wrong thread state for using locks"); |
446 || rank() == Mutex::special, "wrong thread state for using locks"); |
447 if (thread->is_VM_thread() && !allow_vm_block()) { |
447 if (thread->is_VM_thread() && !allow_vm_block()) { |
448 fatal("VM thread using lock %s (not allowed to block on)", name()); |
448 fatal("VM thread using lock %s (not allowed to block on)", name()); |