< prev index next >

src/hotspot/share/runtime/mutex.cpp

Print this page
rev 56464 : 8231707: Improve Mutex inlining
Contributed-by: robbin.ehn@oracle.com, claes.redestad@oracle.com


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/log.hpp"
  27 #include "runtime/interfaceSupport.inline.hpp"
  28 #include "runtime/mutex.hpp"
  29 #include "runtime/osThread.hpp"
  30 #include "runtime/safepointMechanism.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "utilities/events.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #ifdef ASSERT
  36 void Mutex::check_block_state(Thread* thread) {
  37   if (!_allow_vm_block && thread->is_VM_thread()) {
  38     // JavaThreads are checked to make sure that they do not hold _allow_vm_block locks during operations
  39     // that could safepoint.  Make sure the vm thread never uses locks with _allow_vm_block == false.
  40     fatal("VM thread could block on lock that may be held by a JavaThread during safepoint: %s", name());
  41   }
  42 
  43   assert(!os::ThreadCrashProtection::is_crash_protected(thread),
  44          "locking not allowed when crash protection is set");
  45 }
  46 
  47 void Mutex::check_safepoint_state(Thread* thread) {
  48   check_block_state(thread);
  49 
  50   // If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never.
  51   if (thread->is_active_Java_thread()) {
  52     assert(_safepoint_check_required != _safepoint_check_never,
  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 
 133 
 134 // Returns true if thread succeeds in grabbing the lock, otherwise false.
 135 
 136 bool Mutex::try_lock() {
 137   Thread * const self = Thread::current();
 138   // Some safepoint_check_always locks use try_lock, so cannot check
 139   // safepoint state, but can check blocking state.
 140   check_block_state(self);
 141   if (_lock.try_lock()) {
 142     assert_owner(NULL);
 143     set_owner(self);
 144     return true;
 145   }
 146   return false;
 147 }
 148 
 149 void Mutex::release_for_safepoint() {
 150   assert_owner(NULL);
 151   _lock.unlock();
 152 }
 153 
 154 void Mutex::unlock() {
 155   assert_owner(Thread::current());
 156   set_owner(NULL);
 157   _lock.unlock();
 158 }
 159 
 160 void Monitor::notify() {
 161   assert_owner(Thread::current());
 162   _lock.notify();
 163 }
 164 
 165 void Monitor::notify_all() {
 166   assert_owner(Thread::current());
 167   _lock.notify_all();
 168 }
 169 
 170 #ifdef ASSERT
 171 void Monitor::assert_wait_lock_state(Thread* self) {
 172   Mutex* least = get_least_ranked_lock_besides_this(self->owned_locks());
 173   assert(least != this, "Specification of get_least_... call above");
 174   if (least != NULL && least->rank() <= special) {
 175     ::tty->print("Attempting to wait on monitor %s/%d while holding"
 176                " lock %s/%d -- possible deadlock",
 177                name(), rank(), least->name(), least->rank());
 178     assert(false, "Shouldn't block(wait) while holding a lock of rank special");




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/log.hpp"
  27 #include "runtime/interfaceSupport.inline.hpp"
  28 #include "runtime/mutex.hpp"
  29 #include "runtime/osThread.hpp"
  30 #include "runtime/safepointMechanism.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "utilities/events.inline.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #ifdef ASSERT
  36 void Mutex::check_block_state(Thread* thread) {
  37   if (!_allow_vm_block && thread->is_VM_thread()) {
  38     // JavaThreads are checked to make sure that they do not hold _allow_vm_block locks during operations
  39     // that could safepoint.  Make sure the vm thread never uses locks with _allow_vm_block == false.
  40     fatal("VM thread could block on lock that may be held by a JavaThread during safepoint: %s", name());
  41   }
  42 
  43   assert(!os::ThreadCrashProtection::is_crash_protected(thread),
  44          "locking not allowed when crash protection is set");
  45 }
  46 
  47 void Mutex::check_safepoint_state(Thread* thread) {
  48   check_block_state(thread);
  49 
  50   // If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never.
  51   if (thread->is_active_Java_thread()) {
  52     assert(_safepoint_check_required != _safepoint_check_never,
  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_slow(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     // The lock is contended
  79 
  80   #ifdef ASSERT
  81     if (retry_cnt++ > 3) {
  82       log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
  83     }
  84   #endif // ASSERT
  85 
  86     // Is it a JavaThread participating in the safepoint protocol.
  87     if (is_active_Java_thread) {
  88       assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
  89       { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
  90         in_flight_mutex = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
  91         _lock.lock();
  92       }
  93       if (in_flight_mutex != NULL) {
  94         // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
  95         break;
  96       }
  97     } else {
  98       _lock.lock();
  99       break;
 100     }
 101   } while(!_lock.try_lock());








 102 










 103   assert_owner(NULL);
 104   set_owner(self);
 105 }
 106 






























 107 
 108 void Monitor::notify() {
 109   assert_owner(Thread::current());
 110   _lock.notify();
 111 }
 112 
 113 void Monitor::notify_all() {
 114   assert_owner(Thread::current());
 115   _lock.notify_all();
 116 }
 117 
 118 #ifdef ASSERT
 119 void Monitor::assert_wait_lock_state(Thread* self) {
 120   Mutex* least = get_least_ranked_lock_besides_this(self->owned_locks());
 121   assert(least != this, "Specification of get_least_... call above");
 122   if (least != NULL && least->rank() <= special) {
 123     ::tty->print("Attempting to wait on monitor %s/%d while holding"
 124                " lock %s/%d -- possible deadlock",
 125                name(), rank(), least->name(), least->rank());
 126     assert(false, "Shouldn't block(wait) while holding a lock of rank special");


< prev index next >