< prev index next >

src/hotspot/share/runtime/mutex.cpp

Print this page
rev 56464 : 8231707: Improve Mutex inlining
Reviewed-by: coleenp, rehn


  53            "This lock should %s have a safepoint check for Java threads: %s",
  54            _safepoint_check_required ? "always" : "never", name());
  55 
  56     // Also check NoSafepointVerifier, and thread state is _thread_in_vm
  57     thread->check_for_valid_safepoint_state();
  58   } else {
  59     // If initialized with safepoint_check_never, a NonJavaThread should never ask to safepoint check either.
  60     assert(_safepoint_check_required != _safepoint_check_never,
  61            "NonJavaThread should not check for safepoint");
  62   }
  63 }
  64 
  65 void Mutex::check_no_safepoint_state(Thread* thread) {
  66   check_block_state(thread);
  67   assert(!thread->is_active_Java_thread() || _safepoint_check_required != _safepoint_check_always,
  68          "This lock should %s have a safepoint check for Java threads: %s",
  69          _safepoint_check_required ? "always" : "never", name());
  70 }
  71 #endif // ASSERT
  72 
  73 void Mutex::lock(Thread* self) {
  74   check_safepoint_state(self);
  75 
  76   assert(_owner != self, "invariant");
  77 
  78   Mutex* in_flight_mutex = NULL;
  79   DEBUG_ONLY(int retry_cnt = 0;)
  80   bool is_active_Java_thread = self->is_active_Java_thread();
  81   while (!_lock.try_lock()) {
  82     // The lock is contended
  83 
  84   #ifdef ASSERT
  85     if (retry_cnt++ > 3) {
  86       log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
  87     }
  88   #endif // ASSERT
  89 
  90     // Is it a JavaThread participating in the safepoint protocol.
  91     if (is_active_Java_thread) {
  92       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
  93       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
  94         in_flight_mutex = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
  95         _lock.lock();
  96       }
  97       if (in_flight_mutex != NULL) {
  98         // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
  99         break;
 100       }
 101     } else {
 102       _lock.lock();
 103       break;
 104     }











 105   }
 106 
 107   assert_owner(NULL);
 108   set_owner(self);
 109 }
 110 
 111 void Mutex::lock() {
 112   this->lock(Thread::current());
 113 }
 114 
 115 // 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
 117 // acquiring this lock. If the thread blocks acquiring the lock it is not
 118 // safepoint-safe and so will prevent a safepoint from being reached. If used
 119 // in the wrong way this can lead to a deadlock with the safepoint code.
 120 
 121 void Mutex::lock_without_safepoint_check(Thread * self) {
 122   check_no_safepoint_state(self);
 123   assert(_owner != self, "invariant");
 124   _lock.lock();
 125   assert_owner(NULL);
 126   set_owner(self);
 127 }
 128 
 129 void Mutex::lock_without_safepoint_check() {
 130   lock_without_safepoint_check(Thread::current());
 131 }
 132 




  53            "This lock should %s have a safepoint check for Java threads: %s",
  54            _safepoint_check_required ? "always" : "never", name());
  55 
  56     // Also check NoSafepointVerifier, and thread state is _thread_in_vm
  57     thread->check_for_valid_safepoint_state();
  58   } else {
  59     // If initialized with safepoint_check_never, a NonJavaThread should never ask to safepoint check either.
  60     assert(_safepoint_check_required != _safepoint_check_never,
  61            "NonJavaThread should not check for safepoint");
  62   }
  63 }
  64 
  65 void Mutex::check_no_safepoint_state(Thread* thread) {
  66   check_block_state(thread);
  67   assert(!thread->is_active_Java_thread() || _safepoint_check_required != _safepoint_check_always,
  68          "This lock should %s have a safepoint check for Java threads: %s",
  69          _safepoint_check_required ? "always" : "never", name());
  70 }
  71 #endif // ASSERT
  72 
  73 void Mutex::lock_contended(Thread* self) {
  74   Mutex *in_flight_mutex = NULL;




  75   DEBUG_ONLY(int retry_cnt = 0;)
  76   bool is_active_Java_thread = self->is_active_Java_thread();
  77   do {


  78     #ifdef ASSERT
  79     if (retry_cnt++ > 3) {
  80       log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
  81     }
  82     #endif // ASSERT
  83 
  84     // Is it a JavaThread participating in the safepoint protocol.
  85     if (is_active_Java_thread) {
  86       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
  87       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
  88         in_flight_mutex = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
  89         _lock.lock();
  90       }
  91       if (in_flight_mutex != NULL) {
  92         // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
  93         break;
  94       }
  95     } else {
  96       _lock.lock();
  97       break;
  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);
 110   }
 111 
 112   assert_owner(NULL);
 113   set_owner(self);
 114 }
 115 
 116 void Mutex::lock() {
 117   lock(Thread::current());
 118 }
 119 
 120 // Lock without safepoint check - a degenerate variant of lock() for use by
 121 // JavaThreads when it is known to be safe to not check for a safepoint when
 122 // acquiring this lock. If the thread blocks acquiring the lock it is not
 123 // safepoint-safe and so will prevent a safepoint from being reached. If used
 124 // in the wrong way this can lead to a deadlock with the safepoint code.
 125 
 126 void Mutex::lock_without_safepoint_check(Thread * self) {
 127   check_no_safepoint_state(self);
 128   assert(_owner != self, "invariant");
 129   _lock.lock();
 130   assert_owner(NULL);
 131   set_owner(self);
 132 }
 133 
 134 void Mutex::lock_without_safepoint_check() {
 135   lock_without_safepoint_check(Thread::current());
 136 }
 137 


< prev index next >