src/share/vm/runtime/mutex.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8047290absolutely_final Sdiff src/share/vm/runtime

src/share/vm/runtime/mutex.cpp

Print this page




 878 // it'll stall at the TBIVM reentry state transition after having acquired the
 879 // underlying lock, but before having set _owner and having entered the actual
 880 // critical section.  The lock-sneaking facility leverages that fact and allowed the
 881 // VM thread to logically acquire locks that had already be physically locked by mutators
 882 // but where mutators were known blocked by the reentry thread state transition.
 883 //
 884 // If we were to modify the Monitor-Mutex so that TBIVM state transitions tightly
 885 // wrapped calls to park(), then we could likely do away with sneaking.  We'd
 886 // decouple lock acquisition and parking.  The critical invariant  to eliminating
 887 // sneaking is to ensure that we never "physically" acquire the lock while TBIVM.
 888 // An easy way to accomplish this is to wrap the park calls in a narrow TBIVM jacket.
 889 // One difficulty with this approach is that the TBIVM wrapper could recurse and
 890 // call lock() deep from within a lock() call, while the MutexEvent was already enqueued.
 891 // Using a stack (N=2 at minimum) of ParkEvents would take care of that problem.
 892 //
 893 // But of course the proper ultimate approach is to avoid schemes that require explicit
 894 // sneaking or dependence on any any clever invariants or subtle implementation properties
 895 // of Mutex-Monitor and instead directly address the underlying design flaw.
 896 
 897 void Monitor::lock(Thread * Self) {





 898 #ifdef CHECK_UNHANDLED_OOPS
 899   // Clear unhandled oops so we get a crash right away.  Only clear for non-vm
 900   // or GC threads.
 901   if (Self->is_Java_thread()) {
 902     Self->clear_unhandled_oops();
 903   }
 904 #endif // CHECK_UNHANDLED_OOPS
 905 
 906   debug_only(check_prelock_state(Self));
 907   assert(_owner != Self, "invariant");
 908   assert(_OnDeck != Self->_MutexEvent, "invariant");
 909 
 910   if (TryFast()) {
 911  Exeunt:
 912     assert(ILocked(), "invariant");
 913     assert(owner() == NULL, "invariant");
 914     set_owner(Self);
 915     return;
 916   }
 917 


 936     assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
 937     ThreadBlockInVM tbivm((JavaThread *) Self);
 938     ILock(Self);
 939   } else {
 940     // Mirabile dictu
 941     ILock(Self);
 942   }
 943   goto Exeunt;
 944 }
 945 
 946 void Monitor::lock() {
 947   this->lock(Thread::current());
 948 }
 949 
 950 // Lock without safepoint check - a degenerate variant of lock().
 951 // Should ONLY be used by safepoint code and other code
 952 // that is guaranteed not to block while running inside the VM. If this is called with
 953 // thread state set to be in VM, the safepoint synchronization code will deadlock!
 954 
 955 void Monitor::lock_without_safepoint_check(Thread * Self) {




 956   assert(_owner != Self, "invariant");
 957   ILock(Self);
 958   assert(_owner == NULL, "invariant");
 959   set_owner(Self);
 960 }
 961 
 962 void Monitor::lock_without_safepoint_check() {
 963   lock_without_safepoint_check(Thread::current());
 964 }
 965 
 966 
 967 // Returns true if thread succeeds in grabbing the lock, otherwise false.
 968 
 969 bool Monitor::try_lock() {
 970   Thread * const Self = Thread::current();
 971   debug_only(check_prelock_state(Self));
 972   // assert(!thread->is_inside_signal_handler(), "don't lock inside signal handler");
 973 
 974   // Special case, where all Java threads are stopped.
 975   // The lock may have been acquired but _owner is not yet set.


1065   assert(_OnDeck == ESelf, "invariant");
1066   _OnDeck = NULL;
1067   ParkEvent::Release(ESelf);      // surrender the ParkEvent
1068   goto Exeunt;
1069 }
1070 
1071 void Monitor::jvm_raw_unlock() {
1072   // Nearly the same as Monitor::unlock() ...
1073   // directly set _owner instead of using set_owner(null)
1074   _owner = NULL;
1075   if (_snuck) {         // ???
1076     assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak");
1077     _snuck = false;
1078     return;
1079   }
1080   IUnlock(false);
1081 }
1082 
1083 bool Monitor::wait(bool no_safepoint_check, long timeout,
1084                    bool as_suspend_equivalent) {






1085   Thread * const Self = Thread::current();
1086   assert(_owner == Self, "invariant");
1087   assert(ILocked(), "invariant");
1088 
1089   // as_suspend_equivalent logically implies !no_safepoint_check
1090   guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
1091   // !no_safepoint_check logically implies java_thread
1092   guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
1093 
1094   #ifdef ASSERT
1095   Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
1096   assert(least != this, "Specification of get_least_... call above");
1097   if (least != NULL && least->rank() <= special) {
1098     tty->print("Attempting to wait on monitor %s/%d while holding"
1099                " lock %s/%d -- possible deadlock",
1100                name(), rank(), least->name(), least->rank());
1101     assert(false, "Shouldn't block(wait) while holding a lock of rank special");
1102   }
1103   #endif // ASSERT
1104 


1151 }
1152 
1153 void Monitor::ClearMonitor(Monitor * m, const char *name) {
1154   m->_owner             = NULL;
1155   m->_snuck             = false;
1156   if (name == NULL) {
1157     strcpy(m->_name, "UNKNOWN");
1158   } else {
1159     strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
1160     m->_name[MONITOR_NAME_LEN - 1] = '\0';
1161   }
1162   m->_LockWord.FullWord = 0;
1163   m->_EntryList         = NULL;
1164   m->_OnDeck            = NULL;
1165   m->_WaitSet           = NULL;
1166   m->_WaitLock[0]       = 0;
1167 }
1168 
1169 Monitor::Monitor() { ClearMonitor(this); }
1170 
1171 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block) {

1172   ClearMonitor(this, name);
1173 #ifdef ASSERT
1174   _allow_vm_block  = allow_vm_block;
1175   _rank            = Rank;

1176 #endif
1177 }
1178 
1179 Mutex::~Mutex() {
1180   assert((UNS(_owner)|UNS(_LockWord.FullWord)|UNS(_EntryList)|UNS(_WaitSet)|UNS(_OnDeck)) == 0, "");
1181 }
1182 
1183 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block) {

1184   ClearMonitor((Monitor *) this, name);
1185 #ifdef ASSERT
1186   _allow_vm_block   = allow_vm_block;
1187   _rank             = Rank;

1188 #endif
1189 }
1190 
1191 bool Monitor::owned_by_self() const {
1192   bool ret = _owner == Thread::current();
1193   assert(!ret || _LockWord.Bytes[_LSBINDEX] != 0, "invariant");
1194   return ret;
1195 }
1196 
1197 void Monitor::print_on_error(outputStream* st) const {
1198   st->print("[" PTR_FORMAT, this);
1199   st->print("] %s", _name);
1200   st->print(" - owner thread: " PTR_FORMAT, _owner);
1201 }
1202 
1203 
1204 
1205 
1206 // ----------------------------------------------------------------------------------
1207 // Non-product code




 878 // it'll stall at the TBIVM reentry state transition after having acquired the
 879 // underlying lock, but before having set _owner and having entered the actual
 880 // critical section.  The lock-sneaking facility leverages that fact and allowed the
 881 // VM thread to logically acquire locks that had already be physically locked by mutators
 882 // but where mutators were known blocked by the reentry thread state transition.
 883 //
 884 // If we were to modify the Monitor-Mutex so that TBIVM state transitions tightly
 885 // wrapped calls to park(), then we could likely do away with sneaking.  We'd
 886 // decouple lock acquisition and parking.  The critical invariant  to eliminating
 887 // sneaking is to ensure that we never "physically" acquire the lock while TBIVM.
 888 // An easy way to accomplish this is to wrap the park calls in a narrow TBIVM jacket.
 889 // One difficulty with this approach is that the TBIVM wrapper could recurse and
 890 // call lock() deep from within a lock() call, while the MutexEvent was already enqueued.
 891 // Using a stack (N=2 at minimum) of ParkEvents would take care of that problem.
 892 //
 893 // But of course the proper ultimate approach is to avoid schemes that require explicit
 894 // sneaking or dependence on any any clever invariants or subtle implementation properties
 895 // of Mutex-Monitor and instead directly address the underlying design flaw.
 896 
 897 void Monitor::lock(Thread * Self) {
 898   // Ensure that the Monitor requires/allows safepoint checks.
 899   assert(_safepoint_check_required != Monitor::_safepoint_check_never,
 900          err_msg("This lock should never have a safepoint check: %s",
 901                  name()));
 902 
 903 #ifdef CHECK_UNHANDLED_OOPS
 904   // Clear unhandled oops so we get a crash right away.  Only clear for non-vm
 905   // or GC threads.
 906   if (Self->is_Java_thread()) {
 907     Self->clear_unhandled_oops();
 908   }
 909 #endif // CHECK_UNHANDLED_OOPS
 910 
 911   debug_only(check_prelock_state(Self));
 912   assert(_owner != Self, "invariant");
 913   assert(_OnDeck != Self->_MutexEvent, "invariant");
 914 
 915   if (TryFast()) {
 916  Exeunt:
 917     assert(ILocked(), "invariant");
 918     assert(owner() == NULL, "invariant");
 919     set_owner(Self);
 920     return;
 921   }
 922 


 941     assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
 942     ThreadBlockInVM tbivm((JavaThread *) Self);
 943     ILock(Self);
 944   } else {
 945     // Mirabile dictu
 946     ILock(Self);
 947   }
 948   goto Exeunt;
 949 }
 950 
 951 void Monitor::lock() {
 952   this->lock(Thread::current());
 953 }
 954 
 955 // Lock without safepoint check - a degenerate variant of lock().
 956 // Should ONLY be used by safepoint code and other code
 957 // that is guaranteed not to block while running inside the VM. If this is called with
 958 // thread state set to be in VM, the safepoint synchronization code will deadlock!
 959 
 960 void Monitor::lock_without_safepoint_check(Thread * Self) {
 961   // Ensure that the Monitor does not require or allow safepoint checks.
 962   assert(_safepoint_check_required != Monitor::_safepoint_check_always,
 963          err_msg("This lock should always have a safepoint check: %s",
 964                  name())); 
 965   assert(_owner != Self, "invariant");
 966   ILock(Self);
 967   assert(_owner == NULL, "invariant");
 968   set_owner(Self);
 969 }
 970 
 971 void Monitor::lock_without_safepoint_check() {
 972   lock_without_safepoint_check(Thread::current());
 973 }
 974 
 975 
 976 // Returns true if thread succeeds in grabbing the lock, otherwise false.
 977 
 978 bool Monitor::try_lock() {
 979   Thread * const Self = Thread::current();
 980   debug_only(check_prelock_state(Self));
 981   // assert(!thread->is_inside_signal_handler(), "don't lock inside signal handler");
 982 
 983   // Special case, where all Java threads are stopped.
 984   // The lock may have been acquired but _owner is not yet set.


1074   assert(_OnDeck == ESelf, "invariant");
1075   _OnDeck = NULL;
1076   ParkEvent::Release(ESelf);      // surrender the ParkEvent
1077   goto Exeunt;
1078 }
1079 
1080 void Monitor::jvm_raw_unlock() {
1081   // Nearly the same as Monitor::unlock() ...
1082   // directly set _owner instead of using set_owner(null)
1083   _owner = NULL;
1084   if (_snuck) {         // ???
1085     assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak");
1086     _snuck = false;
1087     return;
1088   }
1089   IUnlock(false);
1090 }
1091 
1092 bool Monitor::wait(bool no_safepoint_check, long timeout,
1093                    bool as_suspend_equivalent) {
1094   // Make sure safepoint checking is used properly.
1095   assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
1096          err_msg("This lock should never have a safepoint check: %s", name()));
1097   assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
1098          err_msg("This lock should always have a safepoint check: %s", name()));
1099 
1100   Thread * const Self = Thread::current();
1101   assert(_owner == Self, "invariant");
1102   assert(ILocked(), "invariant");
1103 
1104   // as_suspend_equivalent logically implies !no_safepoint_check
1105   guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
1106   // !no_safepoint_check logically implies java_thread
1107   guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
1108 
1109   #ifdef ASSERT
1110   Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
1111   assert(least != this, "Specification of get_least_... call above");
1112   if (least != NULL && least->rank() <= special) {
1113     tty->print("Attempting to wait on monitor %s/%d while holding"
1114                " lock %s/%d -- possible deadlock",
1115                name(), rank(), least->name(), least->rank());
1116     assert(false, "Shouldn't block(wait) while holding a lock of rank special");
1117   }
1118   #endif // ASSERT
1119 


1166 }
1167 
1168 void Monitor::ClearMonitor(Monitor * m, const char *name) {
1169   m->_owner             = NULL;
1170   m->_snuck             = false;
1171   if (name == NULL) {
1172     strcpy(m->_name, "UNKNOWN");
1173   } else {
1174     strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
1175     m->_name[MONITOR_NAME_LEN - 1] = '\0';
1176   }
1177   m->_LockWord.FullWord = 0;
1178   m->_EntryList         = NULL;
1179   m->_OnDeck            = NULL;
1180   m->_WaitSet           = NULL;
1181   m->_WaitLock[0]       = 0;
1182 }
1183 
1184 Monitor::Monitor() { ClearMonitor(this); }
1185 
1186 Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
1187                  SafepointCheckRequired safepoint_check_required) {
1188   ClearMonitor(this, name);
1189 #ifdef ASSERT
1190   _allow_vm_block  = allow_vm_block;
1191   _rank            = Rank;
1192   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1193 #endif
1194 }
1195 
1196 Mutex::~Mutex() {
1197   assert((UNS(_owner)|UNS(_LockWord.FullWord)|UNS(_EntryList)|UNS(_WaitSet)|UNS(_OnDeck)) == 0, "");
1198 }
1199 
1200 Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
1201              SafepointCheckRequired safepoint_check_required) {
1202   ClearMonitor((Monitor *) this, name);
1203 #ifdef ASSERT
1204   _allow_vm_block   = allow_vm_block;
1205   _rank             = Rank;
1206   NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1207 #endif
1208 }
1209 
1210 bool Monitor::owned_by_self() const {
1211   bool ret = _owner == Thread::current();
1212   assert(!ret || _LockWord.Bytes[_LSBINDEX] != 0, "invariant");
1213   return ret;
1214 }
1215 
1216 void Monitor::print_on_error(outputStream* st) const {
1217   st->print("[" PTR_FORMAT, this);
1218   st->print("] %s", _name);
1219   st->print(" - owner thread: " PTR_FORMAT, _owner);
1220 }
1221 
1222 
1223 
1224 
1225 // ----------------------------------------------------------------------------------
1226 // Non-product code


src/share/vm/runtime/mutex.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File