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 }
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.
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
1318 // Deadlock avoidance rules require us to acquire Mutexes only in
1319 // a global total order. For example m1 is the lowest ranked mutex
1320 // that the thread holds and m2 is the mutex the thread is trying
1321 // to acquire, then deadlock avoidance rules require that the rank
1322 // of m2 be less than the rank of m1.
1323 // The rank Mutex::native is an exception in that it is not subject
1324 // to the verification rules.
1325 // Here are some further notes relating to mutex acquisition anomalies:
1326 // . under Solaris, the interrupt lock gets acquired when doing
1327 // profiling, so any lock could be held.
1328 // . it is also ok to acquire Safepoint_lock at the very end while we
1329 // already hold Terminator_lock - may happen because of periodic safepoints
1330 if (this->rank() != Mutex::native &&
1331 this->rank() != Mutex::suspend_resume &&
1332 locks != NULL && locks->rank() <= this->rank() &&
1333 !SafepointSynchronize::is_at_safepoint() &&
1334 this != Interrupt_lock && this != ProfileVM_lock &&
1335 !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
1336 SafepointSynchronize::is_synchronizing())) {
1337 new_owner->print_owned_locks();
1338 fatal(err_msg("acquiring lock %s/%d out of order with lock %s/%d -- "
1339 "possible deadlock", this->name(), this->rank(),
1340 locks->name(), locks->rank()));
1341 }
1342
1343 this->_next = new_owner->_owned_locks;
1344 new_owner->_owned_locks = this;
1345 #endif
1346
1347 } else {
1348 // the thread is releasing this lock
1349
1350 Thread* old_owner = _owner;
1351 debug_only(_last_owner = old_owner);
1352
1353 assert(old_owner != NULL, "removing the owner thread of an unowned mutex");
1354 assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
1355
1356 _owner = NULL; // set the owner
1357
1358 #ifdef ASSERT
1359 Monitor *locks = old_owner->owned_locks();
1360
1369 }
1370 }
1371 assert(found, "Removing a lock not owned");
1372 if (prev == NULL) {
1373 old_owner->_owned_locks = _next;
1374 } else {
1375 prev->_next = _next;
1376 }
1377 _next = NULL;
1378 #endif
1379 }
1380 }
1381
1382
1383 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
1384 void Monitor::check_prelock_state(Thread *thread) {
1385 assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
1386 || rank() == Mutex::special, "wrong thread state for using locks");
1387 if (StrictSafepointChecks) {
1388 if (thread->is_VM_thread() && !allow_vm_block()) {
1389 fatal(err_msg("VM thread using lock %s (not allowed to block on)",
1390 name()));
1391 }
1392 debug_only(if (rank() != Mutex::special) \
1393 thread->check_for_valid_safepoint_state(false);)
1394 }
1395 if (thread->is_Watcher_thread()) {
1396 assert(!WatcherThread::watcher_thread()->has_crash_protection(),
1397 "locking not allowed when crash protection is set");
1398 }
1399 }
1400
1401 void Monitor::check_block_state(Thread *thread) {
1402 if (!_allow_vm_block && thread->is_VM_thread()) {
1403 warning("VM thread blocked on lock");
1404 print();
1405 BREAKPOINT;
1406 }
1407 assert(_owner != thread, "deadlock: blocking on monitor owned by current thread");
1408 }
1409
1410 #endif // PRODUCT
|
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 "This lock should never have a safepoint check: %s", name());
901
902 #ifdef CHECK_UNHANDLED_OOPS
903 // Clear unhandled oops so we get a crash right away. Only clear for non-vm
904 // or GC threads.
905 if (Self->is_Java_thread()) {
906 Self->clear_unhandled_oops();
907 }
908 #endif // CHECK_UNHANDLED_OOPS
909
910 debug_only(check_prelock_state(Self));
911 assert(_owner != Self, "invariant");
912 assert(_OnDeck != Self->_MutexEvent, "invariant");
913
914 if (TryFast()) {
915 Exeunt:
916 assert(ILocked(), "invariant");
917 assert(owner() == NULL, "invariant");
918 set_owner(Self);
919 return;
920 }
942 ILock(Self);
943 } else {
944 // Mirabile dictu
945 ILock(Self);
946 }
947 goto Exeunt;
948 }
949
950 void Monitor::lock() {
951 this->lock(Thread::current());
952 }
953
954 // Lock without safepoint check - a degenerate variant of lock().
955 // Should ONLY be used by safepoint code and other code
956 // that is guaranteed not to block while running inside the VM. If this is called with
957 // thread state set to be in VM, the safepoint synchronization code will deadlock!
958
959 void Monitor::lock_without_safepoint_check(Thread * Self) {
960 // Ensure that the Monitor does not require or allow safepoint checks.
961 assert(_safepoint_check_required != Monitor::_safepoint_check_always,
962 "This lock should always have a safepoint check: %s", name());
963 assert(_owner != Self, "invariant");
964 ILock(Self);
965 assert(_owner == NULL, "invariant");
966 set_owner(Self);
967 }
968
969 void Monitor::lock_without_safepoint_check() {
970 lock_without_safepoint_check(Thread::current());
971 }
972
973
974 // Returns true if thread succeeds in grabbing the lock, otherwise false.
975
976 bool Monitor::try_lock() {
977 Thread * const Self = Thread::current();
978 debug_only(check_prelock_state(Self));
979 // assert(!thread->is_inside_signal_handler(), "don't lock inside signal handler");
980
981 // Special case, where all Java threads are stopped.
982 // The lock may have been acquired but _owner is not yet set.
1074 ParkEvent::Release(ESelf); // surrender the ParkEvent
1075 goto Exeunt;
1076 }
1077
1078 void Monitor::jvm_raw_unlock() {
1079 // Nearly the same as Monitor::unlock() ...
1080 // directly set _owner instead of using set_owner(null)
1081 _owner = NULL;
1082 if (_snuck) { // ???
1083 assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak");
1084 _snuck = false;
1085 return;
1086 }
1087 IUnlock(false);
1088 }
1089
1090 bool Monitor::wait(bool no_safepoint_check, long timeout,
1091 bool as_suspend_equivalent) {
1092 // Make sure safepoint checking is used properly.
1093 assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
1094 "This lock should never have a safepoint check: %s", name());
1095 assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
1096 "This lock should always have a safepoint check: %s", name());
1097
1098 Thread * const Self = Thread::current();
1099 assert(_owner == Self, "invariant");
1100 assert(ILocked(), "invariant");
1101
1102 // as_suspend_equivalent logically implies !no_safepoint_check
1103 guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
1104 // !no_safepoint_check logically implies java_thread
1105 guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
1106
1107 #ifdef ASSERT
1108 Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
1109 assert(least != this, "Specification of get_least_... call above");
1110 if (least != NULL && least->rank() <= special) {
1111 tty->print("Attempting to wait on monitor %s/%d while holding"
1112 " lock %s/%d -- possible deadlock",
1113 name(), rank(), least->name(), least->rank());
1114 assert(false, "Shouldn't block(wait) while holding a lock of rank special");
1115 }
1116 #endif // ASSERT
1316 // Deadlock avoidance rules require us to acquire Mutexes only in
1317 // a global total order. For example m1 is the lowest ranked mutex
1318 // that the thread holds and m2 is the mutex the thread is trying
1319 // to acquire, then deadlock avoidance rules require that the rank
1320 // of m2 be less than the rank of m1.
1321 // The rank Mutex::native is an exception in that it is not subject
1322 // to the verification rules.
1323 // Here are some further notes relating to mutex acquisition anomalies:
1324 // . under Solaris, the interrupt lock gets acquired when doing
1325 // profiling, so any lock could be held.
1326 // . it is also ok to acquire Safepoint_lock at the very end while we
1327 // already hold Terminator_lock - may happen because of periodic safepoints
1328 if (this->rank() != Mutex::native &&
1329 this->rank() != Mutex::suspend_resume &&
1330 locks != NULL && locks->rank() <= this->rank() &&
1331 !SafepointSynchronize::is_at_safepoint() &&
1332 this != Interrupt_lock && this != ProfileVM_lock &&
1333 !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
1334 SafepointSynchronize::is_synchronizing())) {
1335 new_owner->print_owned_locks();
1336 fatal("acquiring lock %s/%d out of order with lock %s/%d -- "
1337 "possible deadlock", this->name(), this->rank(),
1338 locks->name(), locks->rank());
1339 }
1340
1341 this->_next = new_owner->_owned_locks;
1342 new_owner->_owned_locks = this;
1343 #endif
1344
1345 } else {
1346 // the thread is releasing this lock
1347
1348 Thread* old_owner = _owner;
1349 debug_only(_last_owner = old_owner);
1350
1351 assert(old_owner != NULL, "removing the owner thread of an unowned mutex");
1352 assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
1353
1354 _owner = NULL; // set the owner
1355
1356 #ifdef ASSERT
1357 Monitor *locks = old_owner->owned_locks();
1358
1367 }
1368 }
1369 assert(found, "Removing a lock not owned");
1370 if (prev == NULL) {
1371 old_owner->_owned_locks = _next;
1372 } else {
1373 prev->_next = _next;
1374 }
1375 _next = NULL;
1376 #endif
1377 }
1378 }
1379
1380
1381 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
1382 void Monitor::check_prelock_state(Thread *thread) {
1383 assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
1384 || rank() == Mutex::special, "wrong thread state for using locks");
1385 if (StrictSafepointChecks) {
1386 if (thread->is_VM_thread() && !allow_vm_block()) {
1387 fatal("VM thread using lock %s (not allowed to block on)", name());
1388 }
1389 debug_only(if (rank() != Mutex::special) \
1390 thread->check_for_valid_safepoint_state(false);)
1391 }
1392 if (thread->is_Watcher_thread()) {
1393 assert(!WatcherThread::watcher_thread()->has_crash_protection(),
1394 "locking not allowed when crash protection is set");
1395 }
1396 }
1397
1398 void Monitor::check_block_state(Thread *thread) {
1399 if (!_allow_vm_block && thread->is_VM_thread()) {
1400 warning("VM thread blocked on lock");
1401 print();
1402 BREAKPOINT;
1403 }
1404 assert(_owner != thread, "deadlock: blocking on monitor owned by current thread");
1405 }
1406
1407 #endif // PRODUCT
|