< prev index next >

src/hotspot/share/runtime/mutex.cpp

Print this page




  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 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.
  38   SafepointCheckRequired not_allowed = do_safepoint_check ?  Monitor::_safepoint_check_never :
  39                                                              Monitor::_safepoint_check_always;
  40   assert(!thread->is_Java_thread() || _safepoint_check_required != not_allowed,
  41          "This lock should %s have a safepoint check for Java threads: %s",
  42          _safepoint_check_required ? "always" : "never", name());
  43 }
  44 #endif // ASSERT
  45 
  46 void Monitor::lock(Thread * self) {
  47   check_safepoint_state(self, true);


  48 
  49 #ifdef CHECK_UNHANDLED_OOPS
  50   // Clear unhandled oops in JavaThreads so we get a crash right away.
  51   if (self->is_Java_thread()) {
  52     self->clear_unhandled_oops();
  53   }
  54 #endif // CHECK_UNHANDLED_OOPS
  55 
  56   DEBUG_ONLY(check_prelock_state(self, StrictSafepointChecks));
  57   assert(_owner != self, "invariant");
  58 
  59   Monitor* in_flight_monitor = NULL;
  60   DEBUG_ONLY(int retry_cnt = 0;)
  61   while (!_lock.try_lock()) {
  62     // The lock is contended
  63 
  64   #ifdef ASSERT
  65     check_block_state(self);
  66     if (retry_cnt++ > 3) {
  67       log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);


  82       _lock.lock();
  83       break;
  84     }
  85   }
  86 
  87   assert_owner(NULL);
  88   set_owner(self);
  89 }
  90 
  91 void Monitor::lock() {
  92   this->lock(Thread::current());
  93 }
  94 
  95 // Lock without safepoint check - a degenerate variant of lock() for use by
  96 // JavaThreads when it is known to be safe to not check for a safepoint when
  97 // acquiring this lock. If the thread blocks acquiring the lock it is not
  98 // safepoint-safe and so will prevent a safepoint from being reached. If used
  99 // in the wrong way this can lead to a deadlock with the safepoint code.
 100 
 101 void Monitor::lock_without_safepoint_check(Thread * self) {
 102   check_safepoint_state(self, false);


 103   assert(_owner != self, "invariant");
 104   _lock.lock();
 105   assert_owner(NULL);
 106   set_owner(self);
 107 }
 108 
 109 void Monitor::lock_without_safepoint_check() {
 110   lock_without_safepoint_check(Thread::current());
 111 }
 112 
 113 
 114 // Returns true if thread succeeds in grabbing the lock, otherwise false.
 115 
 116 bool Monitor::try_lock() {
 117   Thread * const self = Thread::current();
 118   DEBUG_ONLY(check_prelock_state(self, false);)
 119 
 120   if (_lock.try_lock()) {
 121     assert_owner(NULL);
 122     set_owner(self);


 129   assert_owner(NULL);
 130   _lock.unlock();
 131 }
 132 
 133 void Monitor::unlock() {
 134   assert_owner(Thread::current());
 135   set_owner(NULL);
 136   _lock.unlock();
 137 }
 138 
 139 void Monitor::notify() {
 140   assert_owner(Thread::current());
 141   _lock.notify();
 142 }
 143 
 144 void Monitor::notify_all() {
 145   assert_owner(Thread::current());
 146   _lock.notify_all();
 147 }
 148 
 149 #ifdef ASSERT
 150 void Monitor::assert_wait_lock_state(Thread* self) {
 151   Monitor* least = get_least_ranked_lock_besides_this(self->owned_locks());
 152   assert(least != this, "Specification of get_least_... call above");
 153   if (least != NULL && least->rank() <= special) {
 154     ::tty->print("Attempting to wait on monitor %s/%d while holding"
 155                " lock %s/%d -- possible deadlock",
 156                name(), rank(), least->name(), least->rank());
 157     assert(false, "Shouldn't block(wait) while holding a lock of rank special");
 158   }
 159 }
 160 #endif // ASSERT
 161 
 162 bool Monitor::wait_without_safepoint_check(long timeout) {
 163   Thread* const self = Thread::current();
 164   check_safepoint_state(self, false);
 165 
 166   // timeout is in milliseconds - with zero meaning never timeout
 167   assert(timeout >= 0, "negative timeout");
 168 
 169   assert_owner(self);
 170   assert_wait_lock_state(self);
 171 
 172   // conceptually set the owner to NULL in anticipation of
 173   // abdicating the lock in wait
 174   set_owner(NULL);
 175   int wait_status = _lock.wait(timeout);
 176   set_owner(self);
 177   return wait_status != 0;          // return true IFF timeout
 178 }
 179 
 180 bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
 181   Thread* const self = Thread::current();
 182   check_safepoint_state(self, true);
 183 
 184   // timeout is in milliseconds - with zero meaning never timeout
 185   assert(timeout >= 0, "negative timeout");
 186 

 187   assert_owner(self);
 188 
 189   // Safepoint checking logically implies java_thread
 190   guarantee(self->is_Java_thread(), "invariant");
 191   assert_wait_lock_state(self);

 192 
 193 #ifdef CHECK_UNHANDLED_OOPS
 194   // Clear unhandled oops in JavaThreads so we get a crash right away.
 195   self->clear_unhandled_oops();
 196 #endif // CHECK_UNHANDLED_OOPS






 197 
 198   int wait_status;
 199   // conceptually set the owner to NULL in anticipation of
 200   // abdicating the lock in wait
 201   set_owner(NULL);
 202   JavaThread *jt = (JavaThread *)self;
 203   Monitor* in_flight_monitor = NULL;
 204 
 205   {
 206     ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
 207     OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
 208     if (as_suspend_equivalent) {
 209       jt->set_suspend_equivalent();
 210       // cleared by handle_special_suspend_equivalent_condition() or
 211       // java_suspend_self()
 212     }
 213 
 214     wait_status = _lock.wait(timeout);
 215     in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck













 216 
 217     // were we externally suspended while we were waiting?
 218     if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
 219       // Our event wait has finished and we own the lock, but
 220       // while we were waiting another thread suspended us. We don't
 221       // want to hold the lock while suspended because that
 222       // would surprise the thread that suspended us.
 223       _lock.unlock();
 224       jt->java_suspend_self();
 225       _lock.lock();




 226     }
 227   }
 228 
 229   if (in_flight_monitor != NULL) {
 230     // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
 231     assert_owner(NULL);
 232     // Conceptually reestablish ownership of the lock.
 233     set_owner(self);
 234   } else {
 235     lock(self);

 236   }
 237 
 238   return wait_status != 0;          // return true IFF timeout
 239 }
 240 
 241 
 242 // Temporary JVM_RawMonitor* support.
 243 // Yet another degenerate version of Monitor::lock() or lock_without_safepoint_check()
 244 // jvm_raw_lock() and _unlock() can be called by non-Java threads via JVM_RawMonitorEnter.
 245 // There's no expectation that JVM_RawMonitors will interoperate properly with the native
 246 // Mutex-Monitor constructs.  We happen to implement JVM_RawMonitors in terms of
 247 // native Mutex-Monitors simply as a matter of convenience.
 248 
 249 void Monitor::jvm_raw_lock() {
 250   _lock.lock();
 251   assert_owner(NULL);
 252 }
 253 
 254 void Monitor::jvm_raw_unlock() {
 255   assert_owner(NULL);
 256   _lock.unlock();
 257 }
 258 
 259 Monitor::~Monitor() {
 260   assert_owner(NULL);
 261 }
 262 
 263 void Monitor::ClearMonitor(Monitor * m, const char *name) {
 264   m->_owner             = NULL;
 265   if (name == NULL) {
 266     strcpy(m->_name, "UNKNOWN");
 267   } else {
 268     strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
 269     m->_name[MONITOR_NAME_LEN - 1] = '\0';
 270   }
 271 }
 272 
 273 Monitor::Monitor() {
 274   assert(os::mutex_init_done(), "Too early!");
 275   ClearMonitor(this);
 276 }
 277 
 278 
 279 // Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes.
 280 bool is_sometimes_ok(const char* name) {
 281   return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0);
 282 }
 283 
 284 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
 285                  SafepointCheckRequired safepoint_check_required) {
 286   assert(os::mutex_init_done(), "Too early!");
 287   ClearMonitor(this, name);
 288 #ifdef ASSERT
 289   _allow_vm_block  = allow_vm_block;
 290   _rank            = Rank;
 291   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
 292 
 293   assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name),
 294          "Lock has _safepoint_check_sometimes %s", name);
 295 #endif
 296 }
 297 
 298 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
 299              SafepointCheckRequired safepoint_check_required) {
 300   ClearMonitor((Monitor *) this, name);
 301 #ifdef ASSERT
 302   _allow_vm_block   = allow_vm_block;
 303   _rank             = Rank;
 304   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
 305 
 306   assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name),
 307          "Lock has _safepoint_check_sometimes %s", name);
 308 #endif
 309 }
 310 
 311 bool Monitor::owned_by_self() const {
 312   return _owner == Thread::current();
 313 }
 314 
 315 void Monitor::print_on_error(outputStream* st) const {
 316   st->print("[" PTR_FORMAT, p2i(this));
 317   st->print("] %s", _name);
 318   st->print(" - owner thread: " PTR_FORMAT, p2i(_owner));
 319 }
 320 
 321 // ----------------------------------------------------------------------------------
 322 // Non-product code
 323 
 324 #ifndef PRODUCT
 325 void Monitor::print_on(outputStream* st) const {
 326   st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
 327                p2i(this), _name, p2i(_owner));




  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 
  36 void Monitor::lock(Thread * self) {
  37   // Ensure that the Monitor requires/allows safepoint checks.
  38   assert(_safepoint_check_required != Monitor::_safepoint_check_never,
  39          "This lock should never have a safepoint check: %s", name());
  40 
  41 #ifdef CHECK_UNHANDLED_OOPS
  42   // Clear unhandled oops in JavaThreads so we get a crash right away.
  43   if (self->is_Java_thread()) {
  44     self->clear_unhandled_oops();
  45   }
  46 #endif // CHECK_UNHANDLED_OOPS
  47 
  48   DEBUG_ONLY(check_prelock_state(self, StrictSafepointChecks));
  49   assert(_owner != self, "invariant");
  50 
  51   Monitor* in_flight_monitor = NULL;
  52   DEBUG_ONLY(int retry_cnt = 0;)
  53   while (!_lock.try_lock()) {
  54     // The lock is contended
  55 
  56   #ifdef ASSERT
  57     check_block_state(self);
  58     if (retry_cnt++ > 3) {
  59       log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);


  74       _lock.lock();
  75       break;
  76     }
  77   }
  78 
  79   assert_owner(NULL);
  80   set_owner(self);
  81 }
  82 
  83 void Monitor::lock() {
  84   this->lock(Thread::current());
  85 }
  86 
  87 // Lock without safepoint check - a degenerate variant of lock() for use by
  88 // JavaThreads when it is known to be safe to not check for a safepoint when
  89 // acquiring this lock. If the thread blocks acquiring the lock it is not
  90 // safepoint-safe and so will prevent a safepoint from being reached. If used
  91 // in the wrong way this can lead to a deadlock with the safepoint code.
  92 
  93 void Monitor::lock_without_safepoint_check(Thread * self) {
  94   // Ensure that the Monitor does not require safepoint checks.
  95   assert(_safepoint_check_required != Monitor::_safepoint_check_always,
  96          "This lock should always have a safepoint check: %s", name());
  97   assert(_owner != self, "invariant");
  98   _lock.lock();
  99   assert_owner(NULL);
 100   set_owner(self);
 101 }
 102 
 103 void Monitor::lock_without_safepoint_check() {
 104   lock_without_safepoint_check(Thread::current());
 105 }
 106 
 107 
 108 // Returns true if thread succeeds in grabbing the lock, otherwise false.
 109 
 110 bool Monitor::try_lock() {
 111   Thread * const self = Thread::current();
 112   DEBUG_ONLY(check_prelock_state(self, false);)
 113 
 114   if (_lock.try_lock()) {
 115     assert_owner(NULL);
 116     set_owner(self);


 123   assert_owner(NULL);
 124   _lock.unlock();
 125 }
 126 
 127 void Monitor::unlock() {
 128   assert_owner(Thread::current());
 129   set_owner(NULL);
 130   _lock.unlock();
 131 }
 132 
 133 void Monitor::notify() {
 134   assert_owner(Thread::current());
 135   _lock.notify();
 136 }
 137 
 138 void Monitor::notify_all() {
 139   assert_owner(Thread::current());
 140   _lock.notify_all();
 141 }
 142 
 143 bool Monitor::wait(bool no_safepoint_check, long timeout,
 144                    bool as_suspend_equivalent) {
 145   // Make sure safepoint checking is used properly.
 146   assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
 147          "This lock should never have a safepoint check: %s", name());
 148   assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
 149          "This lock should always have a safepoint check: %s", name());



























 150 
 151   // timeout is in milliseconds - with zero meaning never timeout
 152   assert(timeout >= 0, "negative timeout");
 153 
 154   Thread * const self = Thread::current();
 155   assert_owner(self);
 156 
 157   // as_suspend_equivalent logically implies !no_safepoint_check
 158   guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
 159   // !no_safepoint_check logically implies java_thread
 160   guarantee(no_safepoint_check || self->is_Java_thread(), "invariant");
 161 
 162   #ifdef ASSERT
 163   Monitor * least = get_least_ranked_lock_besides_this(self->owned_locks());
 164   assert(least != this, "Specification of get_least_... call above");
 165   if (least != NULL && least->rank() <= special) {
 166     ::tty->print("Attempting to wait on monitor %s/%d while holding"
 167                " lock %s/%d -- possible deadlock",
 168                name(), rank(), least->name(), least->rank());
 169     assert(false, "Shouldn't block(wait) while holding a lock of rank special");
 170   }
 171   #endif // ASSERT
 172 
 173   int wait_status;
 174   // conceptually set the owner to NULL in anticipation of
 175   // abdicating the lock in wait
 176   set_owner(NULL);
 177   if (no_safepoint_check) {











 178     wait_status = _lock.wait(timeout);
 179     set_owner(self);
 180   } else {
 181     assert(self->is_Java_thread(), "invariant");
 182     JavaThread *jt = (JavaThread *)self;
 183     Monitor* in_flight_monitor = NULL;
 184 
 185     {
 186       ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
 187       OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
 188       if (as_suspend_equivalent) {
 189         jt->set_suspend_equivalent();
 190         // cleared by handle_special_suspend_equivalent_condition() or
 191         // java_suspend_self()
 192       }
 193 
 194       wait_status = _lock.wait(timeout);
 195       in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
 196 
 197       // were we externally suspended while we were waiting?
 198       if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
 199         // Our event wait has finished and we own the lock, but
 200         // while we were waiting another thread suspended us. We don't
 201         // want to hold the lock while suspended because that
 202         // would surprise the thread that suspended us.
 203         _lock.unlock();
 204         jt->java_suspend_self();
 205         _lock.lock();
 206       }
 207     }

 208 
 209     if (in_flight_monitor != NULL) {
 210       // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
 211       assert_owner(NULL);
 212       // Conceptually reestablish ownership of the lock.
 213       set_owner(self);
 214     } else {
 215       lock(self);
 216     }
 217   }

 218   return wait_status != 0;          // return true IFF timeout
 219 }
 220 
 221 
 222 // Temporary JVM_RawMonitor* support.
 223 // Yet another degenerate version of Monitor::lock() or lock_without_safepoint_check()
 224 // jvm_raw_lock() and _unlock() can be called by non-Java threads via JVM_RawMonitorEnter.
 225 // There's no expectation that JVM_RawMonitors will interoperate properly with the native
 226 // Mutex-Monitor constructs.  We happen to implement JVM_RawMonitors in terms of
 227 // native Mutex-Monitors simply as a matter of convenience.
 228 
 229 void Monitor::jvm_raw_lock() {
 230   _lock.lock();
 231   assert_owner(NULL);
 232 }
 233 
 234 void Monitor::jvm_raw_unlock() {
 235   assert_owner(NULL);
 236   _lock.unlock();
 237 }
 238 
 239 Monitor::~Monitor() {
 240   assert_owner(NULL);
 241 }
 242 
 243 void Monitor::ClearMonitor(Monitor * m, const char *name) {
 244   m->_owner             = NULL;
 245   if (name == NULL) {
 246     strcpy(m->_name, "UNKNOWN");
 247   } else {
 248     strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
 249     m->_name[MONITOR_NAME_LEN - 1] = '\0';
 250   }
 251 }
 252 
 253 Monitor::Monitor() {
 254   assert(os::mutex_init_done(), "Too early!");
 255   ClearMonitor(this);
 256 }
 257 






 258 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
 259                  SafepointCheckRequired safepoint_check_required) {
 260   assert(os::mutex_init_done(), "Too early!");
 261   ClearMonitor(this, name);
 262 #ifdef ASSERT
 263   _allow_vm_block  = allow_vm_block;
 264   _rank            = Rank;
 265   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)



 266 #endif
 267 }
 268 
 269 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
 270              SafepointCheckRequired safepoint_check_required) {
 271   ClearMonitor((Monitor *) this, name);
 272 #ifdef ASSERT
 273   _allow_vm_block   = allow_vm_block;
 274   _rank             = Rank;
 275   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)



 276 #endif
 277 }
 278 
 279 bool Monitor::owned_by_self() const {
 280   return _owner == Thread::current();
 281 }
 282 
 283 void Monitor::print_on_error(outputStream* st) const {
 284   st->print("[" PTR_FORMAT, p2i(this));
 285   st->print("] %s", _name);
 286   st->print(" - owner thread: " PTR_FORMAT, p2i(_owner));
 287 }
 288 
 289 // ----------------------------------------------------------------------------------
 290 // Non-product code
 291 
 292 #ifndef PRODUCT
 293 void Monitor::print_on(outputStream* st) const {
 294   st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
 295                p2i(this), _name, p2i(_owner));


< prev index next >