67 68 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \ 69 char* bytes = NULL; \ 70 int len = 0; \ 71 jlong jtid = SharedRuntime::get_java_tid(thread); \ 72 Symbol* klassname = ((oop)(obj))->klass()->name(); \ 73 if (klassname != NULL) { \ 74 bytes = (char*)klassname->bytes(); \ 75 len = klassname->utf8_length(); \ 76 } 77 78 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ 79 { \ 80 if (DTraceMonitorProbes) { \ 81 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 82 HOTSPOT_MONITOR_WAIT(jtid, \ 83 (uintptr_t)(monitor), bytes, len, (millis)); \ 84 } \ 85 } 86 87 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED 88 89 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \ 90 { \ 91 if (DTraceMonitorProbes) { \ 92 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 93 HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */ \ 94 (uintptr_t)(monitor), bytes, len); \ 95 } \ 96 } 97 98 #else // ndef DTRACE_ENABLED 99 100 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} 101 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} 102 103 #endif // ndef DTRACE_ENABLED 104 105 // This exists only as a workaround of dtrace bug 6254741 106 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) { 127 static volatile int gMonitorFreeCount = 0; // # on gFreeList 128 static volatile int gMonitorPopulation = 0; // # Extant -- in circulation 129 130 #define CHAINMARKER (cast_to_oop<intptr_t>(-1)) 131 132 133 // =====================> Quick functions 134 135 // The quick_* forms are special fast-path variants used to improve 136 // performance. In the simplest case, a "quick_*" implementation could 137 // simply return false, in which case the caller will perform the necessary 138 // state transitions and call the slow-path form. 139 // The fast-path is designed to handle frequently arising cases in an efficient 140 // manner and is just a degenerate "optimistic" variant of the slow-path. 141 // returns true -- to indicate the call was satisfied. 142 // returns false -- to indicate the call needs the services of the slow-path. 143 // A no-loitering ordinance is in effect for code in the quick_* family 144 // operators: safepoints or indefinite blocking (blocking that might span a 145 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon 146 // entry. 147 148 // The LockNode emitted directly at the synchronization site would have 149 // been too big if it were to have included support for the cases of inflated 150 // recursive enter and exit, so they go here instead. 151 // Note that we can't safely call AsyncPrintJavaStack() from within 152 // quick_enter() as our thread state remains _in_Java. 153 154 bool ObjectSynchronizer::quick_enter(oop obj, Thread * Self, 155 BasicLock * Lock) { 156 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 157 assert(Self->is_Java_thread(), "invariant"); 158 assert(((JavaThread *) Self)->thread_state() == _thread_in_Java, "invariant"); 159 No_Safepoint_Verifier nsv; 160 if (obj == NULL) return false; // Need to throw NPE 161 const markOop mark = obj->mark(); 162 163 if (mark->has_monitor()) { 164 ObjectMonitor * const m = mark->monitor(); 165 assert(m->object() == obj, "invariant"); 166 Thread * const owner = (Thread *) m->_owner; 167 | 67 68 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \ 69 char* bytes = NULL; \ 70 int len = 0; \ 71 jlong jtid = SharedRuntime::get_java_tid(thread); \ 72 Symbol* klassname = ((oop)(obj))->klass()->name(); \ 73 if (klassname != NULL) { \ 74 bytes = (char*)klassname->bytes(); \ 75 len = klassname->utf8_length(); \ 76 } 77 78 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ 79 { \ 80 if (DTraceMonitorProbes) { \ 81 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 82 HOTSPOT_MONITOR_WAIT(jtid, \ 83 (uintptr_t)(monitor), bytes, len, (millis)); \ 84 } \ 85 } 86 87 #define HOTSPOT_MONITOR_PROBE_notify HOTSPOT_MONITOR_NOTIFY 88 #define HOTSPOT_MONITOR_PROBE_notifyAll HOTSPOT_MONITOR_NOTIFYALL 89 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED 90 91 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \ 92 { \ 93 if (DTraceMonitorProbes) { \ 94 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 95 HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */ \ 96 (uintptr_t)(monitor), bytes, len); \ 97 } \ 98 } 99 100 #else // ndef DTRACE_ENABLED 101 102 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} 103 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} 104 105 #endif // ndef DTRACE_ENABLED 106 107 // This exists only as a workaround of dtrace bug 6254741 108 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) { 129 static volatile int gMonitorFreeCount = 0; // # on gFreeList 130 static volatile int gMonitorPopulation = 0; // # Extant -- in circulation 131 132 #define CHAINMARKER (cast_to_oop<intptr_t>(-1)) 133 134 135 // =====================> Quick functions 136 137 // The quick_* forms are special fast-path variants used to improve 138 // performance. In the simplest case, a "quick_*" implementation could 139 // simply return false, in which case the caller will perform the necessary 140 // state transitions and call the slow-path form. 141 // The fast-path is designed to handle frequently arising cases in an efficient 142 // manner and is just a degenerate "optimistic" variant of the slow-path. 143 // returns true -- to indicate the call was satisfied. 144 // returns false -- to indicate the call needs the services of the slow-path. 145 // A no-loitering ordinance is in effect for code in the quick_* family 146 // operators: safepoints or indefinite blocking (blocking that might span a 147 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon 148 // entry. 149 // 150 // An interesting optimization is to have the JIT recognize the following 151 // common idiom: 152 // synchronized (someobj) { .... ; notify(); } 153 // That is, we find a notify() or notifyAll() call that immediately precedes 154 // the monitorexit operation. In that case the JIT could fuse the operations 155 // into a single notifyAndExit() runtime primitive. 156 157 bool ObjectSynchronizer::quick_notify(oopDesc * obj, Thread * Self, bool All) { 158 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 159 assert(Self->is_Java_thread(), "invariant"); 160 assert(((JavaThread *) Self)->thread_state() == _thread_in_Java, "invariant"); 161 No_Safepoint_Verifier nsv; 162 if (obj == NULL) return false; 163 const markOop mark = obj->mark(); 164 165 if (mark->has_locker() && Self->is_lock_owned((address)mark->locker())) { 166 // Degenerate notify 167 // stack-locked by caller so by definition the implied waitset is empty. 168 return true; 169 } 170 171 if (mark->has_monitor()) { 172 ObjectMonitor * const mon = mark->monitor(); 173 assert(mon->object() == obj, "invariant"); 174 if (mon->owner() != Self) return false; 175 176 // As long as the object is unbiased and doesn't require safepoint revocation 177 // and is owned by the caller we can transfer a thread or threads from 178 // the waitset to the entrylist here and now, avoiding the slow-path. 179 // That is, the only case where the slow-path is mandatory is 180 // when the object is biased or we need to throw IMSX exceptions. 181 if (mon->first_waiter() != NULL) { 182 if (All) { 183 DTRACE_MONITOR_PROBE(notifyAll, mon, obj, Self); 184 } else { 185 DTRACE_MONITOR_PROBE(notify, mon, obj, Self); 186 } 187 int tally = 0; 188 for (;;) { 189 if (mon->first_waiter() == NULL) break; 190 mon->INotify(Self); 191 ++tally; 192 if (!All) break; 193 } 194 if (ObjectMonitor::_sync_Notifications != NULL) { 195 ObjectMonitor::_sync_Notifications->inc(tally); 196 } 197 } 198 return true; 199 } 200 201 return false; // revert to slow-path 202 } 203 204 205 // The LockNode emitted directly at the synchronization site would have 206 // been too big if it were to have included support for the cases of inflated 207 // recursive enter and exit, so they go here instead. 208 // Note that we can't safely call AsyncPrintJavaStack() from within 209 // quick_enter() as our thread state remains _in_Java. 210 211 bool ObjectSynchronizer::quick_enter(oop obj, Thread * Self, 212 BasicLock * Lock) { 213 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 214 assert(Self->is_Java_thread(), "invariant"); 215 assert(((JavaThread *) Self)->thread_state() == _thread_in_Java, "invariant"); 216 No_Safepoint_Verifier nsv; 217 if (obj == NULL) return false; // Need to throw NPE 218 const markOop mark = obj->mark(); 219 220 if (mark->has_monitor()) { 221 ObjectMonitor * const m = mark->monitor(); 222 assert(m->object() == obj, "invariant"); 223 Thread * const owner = (Thread *) m->_owner; 224 |