src/hotspot/share/runtime/mutex.cpp
changeset 57699 4aea554692aa
parent 57668 33b160ef735c
child 57745 789e967c2731
equal deleted inserted replaced
57698:9dc92e89243a 57699:4aea554692aa
    35 #ifdef ASSERT
    35 #ifdef ASSERT
    36 void Monitor::check_safepoint_state(Thread* thread, bool do_safepoint_check) {
    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.
    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 ?  Monitor::_safepoint_check_never :
    39                                                              Monitor::_safepoint_check_always;
    39                                                              Monitor::_safepoint_check_always;
    40   assert(!thread->is_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 != Monitor::_safepoint_check_never,
    50 void Monitor::lock(Thread * self) {
    50 void Monitor::lock(Thread * self) {
    51   check_safepoint_state(self, true);
    51   check_safepoint_state(self, true);
    52 
    52 
    53 #ifdef CHECK_UNHANDLED_OOPS
    53 #ifdef CHECK_UNHANDLED_OOPS
    54   // 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.
    55   if (self->is_Java_thread()) {
    55   if (self->is_active_Java_thread()) {
    56     self->clear_unhandled_oops();
    56     self->clear_unhandled_oops();
    57   }
    57   }
    58 #endif // CHECK_UNHANDLED_OOPS
    58 #endif // CHECK_UNHANDLED_OOPS
    59 
    59 
    60   DEBUG_ONLY(check_prelock_state(self, true));
    60   DEBUG_ONLY(check_prelock_state(self, true));
    61   assert(_owner != self, "invariant");
    61   assert(_owner != self, "invariant");
    62 
    62 
    63   Monitor* in_flight_monitor = NULL;
    63   Monitor* in_flight_monitor = NULL;
    64   DEBUG_ONLY(int retry_cnt = 0;)
    64   DEBUG_ONLY(int retry_cnt = 0;)
       
    65   bool is_active_Java_thread = self->is_active_Java_thread();
    65   while (!_lock.try_lock()) {
    66   while (!_lock.try_lock()) {
    66     // The lock is contended
    67     // The lock is contended
    67 
    68 
    68   #ifdef ASSERT
    69   #ifdef ASSERT
    69     check_block_state(self);
    70     check_block_state(self);
    70     if (retry_cnt++ > 3) {
    71     if (retry_cnt++ > 3) {
    71       log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);
    72       log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);
    72     }
    73     }
    73   #endif // ASSERT
    74   #endif // ASSERT
    74 
    75 
    75     if (self->is_Java_thread()) {
    76     // Is it a JavaThread participating in the safepoint protocol.
       
    77     if (is_active_Java_thread) {
    76       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
    78       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
    77       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_monitor);
    79       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_monitor);
    78         in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
    80         in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
    79         _lock.lock();
    81         _lock.lock();
    80       }
    82       }
   188   // timeout is in milliseconds - with zero meaning never timeout
   190   // timeout is in milliseconds - with zero meaning never timeout
   189   assert(timeout >= 0, "negative timeout");
   191   assert(timeout >= 0, "negative timeout");
   190 
   192 
   191   assert_owner(self);
   193   assert_owner(self);
   192 
   194 
   193   // Safepoint checking logically implies java_thread
   195   // Safepoint checking logically implies an active JavaThread.
   194   guarantee(self->is_Java_thread(), "invariant");
   196   guarantee(self->is_active_Java_thread(), "invariant");
   195   assert_wait_lock_state(self);
   197   assert_wait_lock_state(self);
   196 
   198 
   197 #ifdef CHECK_UNHANDLED_OOPS
   199 #ifdef CHECK_UNHANDLED_OOPS
   198   // Clear unhandled oops in JavaThreads so we get a crash right away.
   200   // Clear unhandled oops in JavaThreads so we get a crash right away.
   199   self->clear_unhandled_oops();
   201   self->clear_unhandled_oops();
   468 
   470 
   469 
   471 
   470 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
   472 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
   471 void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
   473 void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
   472   if (safepoint_check) {
   474   if (safepoint_check) {
   473     assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
   475     assert((!thread->is_active_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
   474            || rank() == Mutex::special, "wrong thread state for using locks");
   476            || rank() == Mutex::special, "wrong thread state for using locks");
   475     if (thread->is_VM_thread() && !allow_vm_block()) {
   477     if (thread->is_VM_thread() && !allow_vm_block()) {
   476       fatal("VM thread using lock %s (not allowed to block on)", name());
   478       fatal("VM thread using lock %s (not allowed to block on)", name());
   477     }
   479     }
   478     DEBUG_ONLY(if (rank() != Mutex::special) \
   480     DEBUG_ONLY(if (rank() != Mutex::special) \