src/hotspot/share/runtime/mutex.cpp
changeset 58454 d873ce07465d
parent 58409 a595e67d6683
child 58679 9c3209ff7550
child 59143 1037c4d14378
equal deleted inserted replaced
58453:ca80b8395923 58454:d873ce07465d
    68          "This lock should %s have a safepoint check for Java threads: %s",
    68          "This lock should %s have a safepoint check for Java threads: %s",
    69          _safepoint_check_required ? "always" : "never", name());
    69          _safepoint_check_required ? "always" : "never", name());
    70 }
    70 }
    71 #endif // ASSERT
    71 #endif // ASSERT
    72 
    72 
    73 void Mutex::lock(Thread* self) {
    73 void Mutex::lock_contended(Thread* self) {
    74   check_safepoint_state(self);
    74   Mutex *in_flight_mutex = NULL;
    75 
       
    76   assert(_owner != self, "invariant");
       
    77 
       
    78   Mutex* in_flight_mutex = NULL;
       
    79   DEBUG_ONLY(int retry_cnt = 0;)
    75   DEBUG_ONLY(int retry_cnt = 0;)
    80   bool is_active_Java_thread = self->is_active_Java_thread();
    76   bool is_active_Java_thread = self->is_active_Java_thread();
    81   while (!_lock.try_lock()) {
    77   do {
    82     // The lock is contended
    78     #ifdef ASSERT
    83 
       
    84   #ifdef ASSERT
       
    85     if (retry_cnt++ > 3) {
    79     if (retry_cnt++ > 3) {
    86       log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
    80       log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
    87     }
    81     }
    88   #endif // ASSERT
    82     #endif // ASSERT
    89 
    83 
    90     // Is it a JavaThread participating in the safepoint protocol.
    84     // Is it a JavaThread participating in the safepoint protocol.
    91     if (is_active_Java_thread) {
    85     if (is_active_Java_thread) {
    92       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
    86       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
    93       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
    87       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
   100       }
    94       }
   101     } else {
    95     } else {
   102       _lock.lock();
    96       _lock.lock();
   103       break;
    97       break;
   104     }
    98     }
       
    99   } while (!_lock.try_lock());
       
   100 }
       
   101 
       
   102 void Mutex::lock(Thread* self) {
       
   103   check_safepoint_state(self);
       
   104 
       
   105   assert(_owner != self, "invariant");
       
   106 
       
   107   if (!_lock.try_lock()) {
       
   108     // The lock is contended, use contended slow-path function to lock
       
   109     lock_contended(self);
   105   }
   110   }
   106 
   111 
   107   assert_owner(NULL);
   112   assert_owner(NULL);
   108   set_owner(self);
   113   set_owner(self);
   109 }
   114 }
   110 
   115 
   111 void Mutex::lock() {
   116 void Mutex::lock() {
   112   this->lock(Thread::current());
   117   lock(Thread::current());
   113 }
   118 }
   114 
   119 
   115 // Lock without safepoint check - a degenerate variant of lock() for use by
   120 // Lock without safepoint check - a degenerate variant of lock() for use by
   116 // JavaThreads when it is known to be safe to not check for a safepoint when
   121 // JavaThreads when it is known to be safe to not check for a safepoint when
   117 // acquiring this lock. If the thread blocks acquiring the lock it is not
   122 // acquiring this lock. If the thread blocks acquiring the lock it is not