< prev index next >

src/hotspot/share/runtime/mutex.cpp

Print this page

        

@@ -30,23 +30,15 @@
 #include "runtime/safepointMechanism.inline.hpp"
 #include "runtime/thread.inline.hpp"
 #include "utilities/events.hpp"
 #include "utilities/macros.hpp"
 
-#ifdef ASSERT
-void Monitor::check_safepoint_state(Thread* thread, bool do_safepoint_check) {
-  // If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never.
-  SafepointCheckRequired not_allowed = do_safepoint_check ?  Monitor::_safepoint_check_never :
-                                                             Monitor::_safepoint_check_always;
-  assert(!thread->is_Java_thread() || _safepoint_check_required != not_allowed,
-         "This lock should %s have a safepoint check for Java threads: %s",
-         _safepoint_check_required ? "always" : "never", name());
-}
-#endif // ASSERT
 
 void Monitor::lock(Thread * self) {
-  check_safepoint_state(self, true);
+  // Ensure that the Monitor requires/allows safepoint checks.
+  assert(_safepoint_check_required != Monitor::_safepoint_check_never,
+         "This lock should never have a safepoint check: %s", name());
 
 #ifdef CHECK_UNHANDLED_OOPS
   // Clear unhandled oops in JavaThreads so we get a crash right away.
   if (self->is_Java_thread()) {
     self->clear_unhandled_oops();

@@ -97,11 +89,13 @@
 // acquiring this lock. If the thread blocks acquiring the lock it is not
 // safepoint-safe and so will prevent a safepoint from being reached. If used
 // in the wrong way this can lead to a deadlock with the safepoint code.
 
 void Monitor::lock_without_safepoint_check(Thread * self) {
-  check_safepoint_state(self, false);
+  // Ensure that the Monitor does not require safepoint checks.
+  assert(_safepoint_check_required != Monitor::_safepoint_check_always,
+         "This lock should always have a safepoint check: %s", name());
   assert(_owner != self, "invariant");
   _lock.lock();
   assert_owner(NULL);
   set_owner(self);
 }

@@ -144,99 +138,85 @@
 void Monitor::notify_all() {
   assert_owner(Thread::current());
   _lock.notify_all();
 }
 
-#ifdef ASSERT
-void Monitor::assert_wait_lock_state(Thread* self) {
-  Monitor* least = get_least_ranked_lock_besides_this(self->owned_locks());
-  assert(least != this, "Specification of get_least_... call above");
-  if (least != NULL && least->rank() <= special) {
-    ::tty->print("Attempting to wait on monitor %s/%d while holding"
-               " lock %s/%d -- possible deadlock",
-               name(), rank(), least->name(), least->rank());
-    assert(false, "Shouldn't block(wait) while holding a lock of rank special");
-  }
-}
-#endif // ASSERT
-
-bool Monitor::wait_without_safepoint_check(long timeout) {
-  Thread* const self = Thread::current();
-  check_safepoint_state(self, false);
-
-  // timeout is in milliseconds - with zero meaning never timeout
-  assert(timeout >= 0, "negative timeout");
-
-  assert_owner(self);
-  assert_wait_lock_state(self);
-
-  // conceptually set the owner to NULL in anticipation of
-  // abdicating the lock in wait
-  set_owner(NULL);
-  int wait_status = _lock.wait(timeout);
-  set_owner(self);
-  return wait_status != 0;          // return true IFF timeout
-}
-
-bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
-  Thread* const self = Thread::current();
-  check_safepoint_state(self, true);
+bool Monitor::wait(bool no_safepoint_check, long timeout,
+                   bool as_suspend_equivalent) {
+  // Make sure safepoint checking is used properly.
+  assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
+         "This lock should never have a safepoint check: %s", name());
+  assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
+         "This lock should always have a safepoint check: %s", name());
 
   // timeout is in milliseconds - with zero meaning never timeout
   assert(timeout >= 0, "negative timeout");
 
+  Thread * const self = Thread::current();
   assert_owner(self);
 
-  // Safepoint checking logically implies java_thread
-  guarantee(self->is_Java_thread(), "invariant");
-  assert_wait_lock_state(self);
+  // as_suspend_equivalent logically implies !no_safepoint_check
+  guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
+  // !no_safepoint_check logically implies java_thread
+  guarantee(no_safepoint_check || self->is_Java_thread(), "invariant");
 
-#ifdef CHECK_UNHANDLED_OOPS
-  // Clear unhandled oops in JavaThreads so we get a crash right away.
-  self->clear_unhandled_oops();
-#endif // CHECK_UNHANDLED_OOPS
+  #ifdef ASSERT
+  Monitor * least = get_least_ranked_lock_besides_this(self->owned_locks());
+  assert(least != this, "Specification of get_least_... call above");
+  if (least != NULL && least->rank() <= special) {
+    ::tty->print("Attempting to wait on monitor %s/%d while holding"
+               " lock %s/%d -- possible deadlock",
+               name(), rank(), least->name(), least->rank());
+    assert(false, "Shouldn't block(wait) while holding a lock of rank special");
+  }
+  #endif // ASSERT
 
   int wait_status;
   // conceptually set the owner to NULL in anticipation of
   // abdicating the lock in wait
   set_owner(NULL);
-  JavaThread *jt = (JavaThread *)self;
-  Monitor* in_flight_monitor = NULL;
-
-  {
-    ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
-    OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
-    if (as_suspend_equivalent) {
-      jt->set_suspend_equivalent();
-      // cleared by handle_special_suspend_equivalent_condition() or
-      // java_suspend_self()
-    }
-
+  if (no_safepoint_check) {
     wait_status = _lock.wait(timeout);
-    in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
+    set_owner(self);
+  } else {
+    assert(self->is_Java_thread(), "invariant");
+    JavaThread *jt = (JavaThread *)self;
+    Monitor* in_flight_monitor = NULL;
+
+    {
+      ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
+      OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
+      if (as_suspend_equivalent) {
+        jt->set_suspend_equivalent();
+        // cleared by handle_special_suspend_equivalent_condition() or
+        // java_suspend_self()
+      }
 
-    // were we externally suspended while we were waiting?
-    if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
-      // Our event wait has finished and we own the lock, but
-      // while we were waiting another thread suspended us. We don't
-      // want to hold the lock while suspended because that
-      // would surprise the thread that suspended us.
-      _lock.unlock();
-      jt->java_suspend_self();
-      _lock.lock();
+      wait_status = _lock.wait(timeout);
+      in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
+
+      // were we externally suspended while we were waiting?
+      if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
+        // Our event wait has finished and we own the lock, but
+        // while we were waiting another thread suspended us. We don't
+        // want to hold the lock while suspended because that
+        // would surprise the thread that suspended us.
+        _lock.unlock();
+        jt->java_suspend_self();
+        _lock.lock();
+      }
     }
-  }
 
-  if (in_flight_monitor != NULL) {
-    // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
-    assert_owner(NULL);
-    // Conceptually reestablish ownership of the lock.
-    set_owner(self);
-  } else {
-    lock(self);
+    if (in_flight_monitor != NULL) {
+      // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
+      assert_owner(NULL);
+      // Conceptually reestablish ownership of the lock.
+      set_owner(self);
+    } else {
+      lock(self);
+    }
   }
-
   return wait_status != 0;          // return true IFF timeout
 }
 
 
 // Temporary JVM_RawMonitor* support.

@@ -273,40 +253,28 @@
 Monitor::Monitor() {
   assert(os::mutex_init_done(), "Too early!");
   ClearMonitor(this);
 }
 
-
-// Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes.
-bool is_sometimes_ok(const char* name) {
-  return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0);
-}
-
 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
                  SafepointCheckRequired safepoint_check_required) {
   assert(os::mutex_init_done(), "Too early!");
   ClearMonitor(this, name);
 #ifdef ASSERT
   _allow_vm_block  = allow_vm_block;
   _rank            = Rank;
   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
-
-  assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name),
-         "Lock has _safepoint_check_sometimes %s", name);
 #endif
 }
 
 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
              SafepointCheckRequired safepoint_check_required) {
   ClearMonitor((Monitor *) this, name);
 #ifdef ASSERT
   _allow_vm_block   = allow_vm_block;
   _rank             = Rank;
   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
-
-  assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name),
-         "Lock has _safepoint_check_sometimes %s", name);
 #endif
 }
 
 bool Monitor::owned_by_self() const {
   return _owner == Thread::current();
< prev index next >