843 // thread acquires the lock and then drops the lock, at which time the
844 // exiting thread will notice and unpark the stranded thread, or, (b)
845 // the timer expires. If the lock is high traffic then the stranding latency
846 // will be low due to (a). If the lock is low traffic then the odds of
847 // stranding are lower, although the worst-case stranding latency
848 // is longer. Critically, we don't want to put excessive load in the
849 // platform's timer subsystem. We want to minimize both the timer injection
850 // rate (timers created/sec) as well as the number of timers active at
851 // any one time. (more precisely, we want to minimize timer-seconds, which is
852 // the integral of the # of active timers at any instant over time).
853 // Both impinge on OS scalability. Given that, at most one thread parked on
854 // a monitor will use a timer.
855 //
856 // There is also the risk of a futile wake-up. If we drop the lock
857 // another thread can reacquire the lock immediately, and we can
858 // then wake a thread unnecessarily. This is benign, and we've
859 // structured the code so the windows are short and the frequency
860 // of such futile wakups is low.
861
862 void ObjectMonitor::exit(bool not_suspended, TRAPS) {
863 Thread * const Self = THREAD;
864 if (THREAD != _owner) {
865 void* cur = _owner;
866 if (THREAD->is_lock_owned((address)cur)) {
867 assert(_recursions == 0, "invariant");
868 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
869 _recursions = 0;
870 } else {
871 // Apparent unbalanced locking ...
872 // Naively we'd like to throw IllegalMonitorStateException.
873 // As a practical matter we can neither allocate nor throw an
874 // exception as ::exit() can be called from leaf routines.
875 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
876 // Upon deeper reflection, however, in a properly run JVM the only
877 // way we should encounter this situation is in the presence of
878 // unbalanced JNI locking. TODO: CheckJNICalls.
879 // See also: CR4414101
880 #ifdef ASSERT
881 LogStreamHandle(Error, monitorinflation) lsh;
882 lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
883 " is exiting an ObjectMonitor it does not own.", p2i(THREAD));
884 lsh.print_cr("The imbalance is possibly caused by JNI locking.");
885 print_debug_style_on(&lsh);
1103 OM_PERFDATA_OP(Parks, inc());
1104 }
1105
1106
1107 // -----------------------------------------------------------------------------
1108 // Class Loader deadlock handling.
1109 //
1110 // complete_exit exits a lock returning recursion count
1111 // complete_exit/reenter operate as a wait without waiting
1112 // complete_exit requires an inflated monitor
1113 // The _owner field is not always the Thread addr even with an
1114 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1115 // thread due to contention.
1116 intx ObjectMonitor::complete_exit(TRAPS) {
1117 Thread * const Self = THREAD;
1118 assert(Self->is_Java_thread(), "Must be Java thread!");
1119 JavaThread *jt = (JavaThread *)THREAD;
1120
1121 assert(InitDone, "Unexpectedly not initialized");
1122
1123 if (THREAD != _owner) {
1124 void* cur = _owner;
1125 if (THREAD->is_lock_owned((address)cur)) {
1126 assert(_recursions == 0, "internal state error");
1127 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
1128 _recursions = 0;
1129 }
1130 }
1131
1132 guarantee(Self == _owner, "complete_exit not owner");
1133 intx save = _recursions; // record the old recursion count
1134 _recursions = 0; // set the recursion level to be 0
1135 exit(true, Self); // exit the monitor
1136 guarantee(_owner != Self, "invariant");
1137 return save;
1138 }
1139
1140 // reenter() enters a lock and sets recursion count
1141 // complete_exit/reenter operate as a wait without waiting
1142 void ObjectMonitor::reenter(intx recursions, TRAPS) {
1143 Thread * const Self = THREAD;
1144 assert(Self->is_Java_thread(), "Must be Java thread!");
1152 }
1153
1154 // Checks that the current THREAD owns this monitor and causes an
1155 // immediate return if it doesn't. We don't use the CHECK macro
1156 // because we want the IMSE to be the only exception that is thrown
1157 // from the call site when false is returned. Any other pending
1158 // exception is ignored.
1159 #define CHECK_OWNER() \
1160 do { \
1161 if (!check_owner(THREAD)) { \
1162 assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1163 return; \
1164 } \
1165 } while (false)
1166
1167 // Returns true if the specified thread owns the ObjectMonitor.
1168 // Otherwise returns false and throws IllegalMonitorStateException
1169 // (IMSE). If there is a pending exception and the specified thread
1170 // is not the owner, that exception will be replaced by the IMSE.
1171 bool ObjectMonitor::check_owner(Thread* THREAD) {
1172 if (_owner == THREAD) {
1173 return true;
1174 }
1175 void* cur = _owner;
1176 if (THREAD->is_lock_owned((address)cur)) {
1177 set_owner_from_BasicLock(cur, THREAD); // Convert from BasicLock* to Thread*.
1178 _recursions = 0;
1179 return true;
1180 }
1181 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1182 "current thread is not owner", false);
1183 }
1184
1185 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1186 ObjectMonitor* monitor,
1187 jlong notifier_tid,
1188 jlong timeout,
1189 bool timedout) {
1190 assert(event != NULL, "invariant");
1191 assert(monitor != NULL, "invariant");
1192 event->set_monitorClass(((oop)monitor->object())->klass());
1193 event->set_timeout(timeout);
1194 event->set_address((uintptr_t)monitor->object_addr());
1195 event->set_notifier(notifier_tid);
|
843 // thread acquires the lock and then drops the lock, at which time the
844 // exiting thread will notice and unpark the stranded thread, or, (b)
845 // the timer expires. If the lock is high traffic then the stranding latency
846 // will be low due to (a). If the lock is low traffic then the odds of
847 // stranding are lower, although the worst-case stranding latency
848 // is longer. Critically, we don't want to put excessive load in the
849 // platform's timer subsystem. We want to minimize both the timer injection
850 // rate (timers created/sec) as well as the number of timers active at
851 // any one time. (more precisely, we want to minimize timer-seconds, which is
852 // the integral of the # of active timers at any instant over time).
853 // Both impinge on OS scalability. Given that, at most one thread parked on
854 // a monitor will use a timer.
855 //
856 // There is also the risk of a futile wake-up. If we drop the lock
857 // another thread can reacquire the lock immediately, and we can
858 // then wake a thread unnecessarily. This is benign, and we've
859 // structured the code so the windows are short and the frequency
860 // of such futile wakups is low.
861
862 void ObjectMonitor::exit(bool not_suspended, TRAPS) {
863 Thread* const Self = THREAD;
864 void* cur = Atomic::load(&_owner);
865 if (THREAD != cur) {
866 if (THREAD->is_lock_owned((address)cur)) {
867 assert(_recursions == 0, "invariant");
868 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
869 _recursions = 0;
870 } else {
871 // Apparent unbalanced locking ...
872 // Naively we'd like to throw IllegalMonitorStateException.
873 // As a practical matter we can neither allocate nor throw an
874 // exception as ::exit() can be called from leaf routines.
875 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
876 // Upon deeper reflection, however, in a properly run JVM the only
877 // way we should encounter this situation is in the presence of
878 // unbalanced JNI locking. TODO: CheckJNICalls.
879 // See also: CR4414101
880 #ifdef ASSERT
881 LogStreamHandle(Error, monitorinflation) lsh;
882 lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
883 " is exiting an ObjectMonitor it does not own.", p2i(THREAD));
884 lsh.print_cr("The imbalance is possibly caused by JNI locking.");
885 print_debug_style_on(&lsh);
1103 OM_PERFDATA_OP(Parks, inc());
1104 }
1105
1106
1107 // -----------------------------------------------------------------------------
1108 // Class Loader deadlock handling.
1109 //
1110 // complete_exit exits a lock returning recursion count
1111 // complete_exit/reenter operate as a wait without waiting
1112 // complete_exit requires an inflated monitor
1113 // The _owner field is not always the Thread addr even with an
1114 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1115 // thread due to contention.
1116 intx ObjectMonitor::complete_exit(TRAPS) {
1117 Thread * const Self = THREAD;
1118 assert(Self->is_Java_thread(), "Must be Java thread!");
1119 JavaThread *jt = (JavaThread *)THREAD;
1120
1121 assert(InitDone, "Unexpectedly not initialized");
1122
1123 void* cur = Atomic::load(&_owner);
1124 if (THREAD != cur) {
1125 if (THREAD->is_lock_owned((address)cur)) {
1126 assert(_recursions == 0, "internal state error");
1127 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
1128 _recursions = 0;
1129 }
1130 }
1131
1132 guarantee(Self == _owner, "complete_exit not owner");
1133 intx save = _recursions; // record the old recursion count
1134 _recursions = 0; // set the recursion level to be 0
1135 exit(true, Self); // exit the monitor
1136 guarantee(_owner != Self, "invariant");
1137 return save;
1138 }
1139
1140 // reenter() enters a lock and sets recursion count
1141 // complete_exit/reenter operate as a wait without waiting
1142 void ObjectMonitor::reenter(intx recursions, TRAPS) {
1143 Thread * const Self = THREAD;
1144 assert(Self->is_Java_thread(), "Must be Java thread!");
1152 }
1153
1154 // Checks that the current THREAD owns this monitor and causes an
1155 // immediate return if it doesn't. We don't use the CHECK macro
1156 // because we want the IMSE to be the only exception that is thrown
1157 // from the call site when false is returned. Any other pending
1158 // exception is ignored.
1159 #define CHECK_OWNER() \
1160 do { \
1161 if (!check_owner(THREAD)) { \
1162 assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1163 return; \
1164 } \
1165 } while (false)
1166
1167 // Returns true if the specified thread owns the ObjectMonitor.
1168 // Otherwise returns false and throws IllegalMonitorStateException
1169 // (IMSE). If there is a pending exception and the specified thread
1170 // is not the owner, that exception will be replaced by the IMSE.
1171 bool ObjectMonitor::check_owner(Thread* THREAD) {
1172 void* cur = Atomic::load(&_owner);
1173 if (cur == THREAD) {
1174 return true;
1175 }
1176 if (THREAD->is_lock_owned((address)cur)) {
1177 set_owner_from_BasicLock(cur, THREAD); // Convert from BasicLock* to Thread*.
1178 _recursions = 0;
1179 return true;
1180 }
1181 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1182 "current thread is not owner", false);
1183 }
1184
1185 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1186 ObjectMonitor* monitor,
1187 jlong notifier_tid,
1188 jlong timeout,
1189 bool timedout) {
1190 assert(event != NULL, "invariant");
1191 assert(monitor != NULL, "invariant");
1192 event->set_monitorClass(((oop)monitor->object())->klass());
1193 event->set_timeout(timeout);
1194 event->set_address((uintptr_t)monitor->object_addr());
1195 event->set_notifier(notifier_tid);
|