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