Mercurial > hg > truffle
comparison src/share/vm/runtime/objectMonitor.cpp @ 14518:d8041d695d19
Merged with jdk9/dev/hotspot changeset 3812c088b945
author | twisti |
---|---|
date | Tue, 11 Mar 2014 18:45:59 -0700 |
parents | 28f281e8de1d |
children | 4ca6dc0799b6 |
comparison
equal
deleted
inserted
replaced
14141:f97c5ec83832 | 14518:d8041d695d19 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
52 #endif | 52 #endif |
53 #ifdef TARGET_OS_FAMILY_bsd | 53 #ifdef TARGET_OS_FAMILY_bsd |
54 # include "os_bsd.inline.hpp" | 54 # include "os_bsd.inline.hpp" |
55 #endif | 55 #endif |
56 | 56 |
57 #if defined(__GNUC__) && !defined(IA64) | 57 #if defined(__GNUC__) && !defined(IA64) && !defined(PPC64) |
58 // Need to inhibit inlining for older versions of GCC to avoid build-time failures | 58 // Need to inhibit inlining for older versions of GCC to avoid build-time failures |
59 #define ATTR __attribute__((noinline)) | 59 #define ATTR __attribute__((noinline)) |
60 #else | 60 #else |
61 #define ATTR | 61 #define ATTR |
62 #endif | 62 #endif |
76 if (klassname != NULL) { \ | 76 if (klassname != NULL) { \ |
77 bytes = (char*)klassname->bytes(); \ | 77 bytes = (char*)klassname->bytes(); \ |
78 len = klassname->utf8_length(); \ | 78 len = klassname->utf8_length(); \ |
79 } | 79 } |
80 | 80 |
81 #ifndef USDT2 | |
82 | |
83 HS_DTRACE_PROBE_DECL4(hotspot, monitor__notify, | |
84 jlong, uintptr_t, char*, int); | |
85 HS_DTRACE_PROBE_DECL4(hotspot, monitor__notifyAll, | |
86 jlong, uintptr_t, char*, int); | |
87 HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__enter, | |
88 jlong, uintptr_t, char*, int); | |
89 HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__entered, | |
90 jlong, uintptr_t, char*, int); | |
91 HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__exit, | |
92 jlong, uintptr_t, char*, int); | |
93 | |
94 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ | |
95 { \ | |
96 if (DTraceMonitorProbes) { \ | |
97 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ | |
98 HS_DTRACE_PROBE5(hotspot, monitor__wait, jtid, \ | |
99 (monitor), bytes, len, (millis)); \ | |
100 } \ | |
101 } | |
102 | |
103 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \ | |
104 { \ | |
105 if (DTraceMonitorProbes) { \ | |
106 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ | |
107 HS_DTRACE_PROBE4(hotspot, monitor__##probe, jtid, \ | |
108 (uintptr_t)(monitor), bytes, len); \ | |
109 } \ | |
110 } | |
111 | |
112 #else /* USDT2 */ | |
113 | |
114 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ | 81 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ |
115 { \ | 82 { \ |
116 if (DTraceMonitorProbes) { \ | 83 if (DTraceMonitorProbes) { \ |
117 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ | 84 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ |
118 HOTSPOT_MONITOR_WAIT(jtid, \ | 85 HOTSPOT_MONITOR_WAIT(jtid, \ |
133 HOTSPOT_MONITOR_##probe(jtid, \ | 100 HOTSPOT_MONITOR_##probe(jtid, \ |
134 (uintptr_t)(monitor), bytes, len); \ | 101 (uintptr_t)(monitor), bytes, len); \ |
135 } \ | 102 } \ |
136 } | 103 } |
137 | 104 |
138 #endif /* USDT2 */ | |
139 #else // ndef DTRACE_ENABLED | 105 #else // ndef DTRACE_ENABLED |
140 | 106 |
141 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} | 107 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} |
142 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} | 108 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} |
143 | 109 |
232 // More precisely, the CAS-based "push" onto cxq is ABA-oblivious. | 198 // More precisely, the CAS-based "push" onto cxq is ABA-oblivious. |
233 // | 199 // |
234 // * Taken together, the cxq and the EntryList constitute or form a | 200 // * Taken together, the cxq and the EntryList constitute or form a |
235 // single logical queue of threads stalled trying to acquire the lock. | 201 // single logical queue of threads stalled trying to acquire the lock. |
236 // We use two distinct lists to improve the odds of a constant-time | 202 // We use two distinct lists to improve the odds of a constant-time |
237 // dequeue operation after acquisition (in the ::enter() epilog) and | 203 // dequeue operation after acquisition (in the ::enter() epilogue) and |
238 // to reduce heat on the list ends. (c.f. Michael Scott's "2Q" algorithm). | 204 // to reduce heat on the list ends. (c.f. Michael Scott's "2Q" algorithm). |
239 // A key desideratum is to minimize queue & monitor metadata manipulation | 205 // A key desideratum is to minimize queue & monitor metadata manipulation |
240 // that occurs while holding the monitor lock -- that is, we want to | 206 // that occurs while holding the monitor lock -- that is, we want to |
241 // minimize monitor lock holds times. Note that even a small amount of | 207 // minimize monitor lock holds times. Note that even a small amount of |
242 // fixed spinning will greatly reduce the # of enqueue-dequeue operations | 208 // fixed spinning will greatly reduce the # of enqueue-dequeue operations |
380 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this); | 346 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this); |
381 | 347 |
382 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt); | 348 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt); |
383 if (JvmtiExport::should_post_monitor_contended_enter()) { | 349 if (JvmtiExport::should_post_monitor_contended_enter()) { |
384 JvmtiExport::post_monitor_contended_enter(jt, this); | 350 JvmtiExport::post_monitor_contended_enter(jt, this); |
351 | |
352 // The current thread does not yet own the monitor and does not | |
353 // yet appear on any queues that would get it made the successor. | |
354 // This means that the JVMTI_EVENT_MONITOR_CONTENDED_ENTER event | |
355 // handler cannot accidentally consume an unpark() meant for the | |
356 // ParkEvent associated with this ObjectMonitor. | |
385 } | 357 } |
386 | 358 |
387 OSThreadContendState osts(Self->osthread()); | 359 OSThreadContendState osts(Self->osthread()); |
388 ThreadBlockInVM tbivm(jt); | 360 ThreadBlockInVM tbivm(jt); |
389 | 361 |
437 // spinning we could increment JVMStat counters, etc. | 409 // spinning we could increment JVMStat counters, etc. |
438 | 410 |
439 DTRACE_MONITOR_PROBE(contended__entered, this, object(), jt); | 411 DTRACE_MONITOR_PROBE(contended__entered, this, object(), jt); |
440 if (JvmtiExport::should_post_monitor_contended_entered()) { | 412 if (JvmtiExport::should_post_monitor_contended_entered()) { |
441 JvmtiExport::post_monitor_contended_entered(jt, this); | 413 JvmtiExport::post_monitor_contended_entered(jt, this); |
414 | |
415 // The current thread already owns the monitor and is not going to | |
416 // call park() for the remainder of the monitor enter protocol. So | |
417 // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED | |
418 // event handler consumed an unpark() issued by the thread that | |
419 // just exited the monitor. | |
442 } | 420 } |
443 | 421 |
444 if (event.should_commit()) { | 422 if (event.should_commit()) { |
445 event.set_klass(((oop)this->object())->klass()); | 423 event.set_klass(((oop)this->object())->klass()); |
446 event.set_previousOwner((TYPE_JAVALANGTHREAD)_previous_owner_tid); | 424 event.set_previousOwner((TYPE_JAVALANGTHREAD)_previous_owner_tid); |
675 // by LDing cxq|EntryList. Newly arrived threads -- that is, threads | 653 // by LDing cxq|EntryList. Newly arrived threads -- that is, threads |
676 // that arrive on cxq after the ST:MEMBAR, above -- will set Responsible | 654 // that arrive on cxq after the ST:MEMBAR, above -- will set Responsible |
677 // non-null and elect a new "Responsible" timer thread. | 655 // non-null and elect a new "Responsible" timer thread. |
678 // | 656 // |
679 // This thread executes: | 657 // This thread executes: |
680 // ST Responsible=null; MEMBAR (in enter epilog - here) | 658 // ST Responsible=null; MEMBAR (in enter epilogue - here) |
681 // LD cxq|EntryList (in subsequent exit) | 659 // LD cxq|EntryList (in subsequent exit) |
682 // | 660 // |
683 // Entering threads in the slow/contended path execute: | 661 // Entering threads in the slow/contended path execute: |
684 // ST cxq=nonnull; MEMBAR; LD Responsible (in enter prolog) | 662 // ST cxq=nonnull; MEMBAR; LD Responsible (in enter prolog) |
685 // The (ST cxq; MEMBAR) is accomplished with CAS(). | 663 // The (ST cxq; MEMBAR) is accomplished with CAS(). |
1454 // post monitor waited event. Note that this is past-tense, we are done waiting. | 1432 // post monitor waited event. Note that this is past-tense, we are done waiting. |
1455 if (JvmtiExport::should_post_monitor_waited()) { | 1433 if (JvmtiExport::should_post_monitor_waited()) { |
1456 // Note: 'false' parameter is passed here because the | 1434 // Note: 'false' parameter is passed here because the |
1457 // wait was not timed out due to thread interrupt. | 1435 // wait was not timed out due to thread interrupt. |
1458 JvmtiExport::post_monitor_waited(jt, this, false); | 1436 JvmtiExport::post_monitor_waited(jt, this, false); |
1437 | |
1438 // In this short circuit of the monitor wait protocol, the | |
1439 // current thread never drops ownership of the monitor and | |
1440 // never gets added to the wait queue so the current thread | |
1441 // cannot be made the successor. This means that the | |
1442 // JVMTI_EVENT_MONITOR_WAITED event handler cannot accidentally | |
1443 // consume an unpark() meant for the ParkEvent associated with | |
1444 // this ObjectMonitor. | |
1459 } | 1445 } |
1460 if (event.should_commit()) { | 1446 if (event.should_commit()) { |
1461 post_monitor_wait_event(&event, 0, millis, false); | 1447 post_monitor_wait_event(&event, 0, millis, false); |
1462 } | 1448 } |
1463 TEVENT (Wait - Throw IEX) ; | 1449 TEVENT (Wait - Throw IEX) ; |
1496 intptr_t save = _recursions; // record the old recursion count | 1482 intptr_t save = _recursions; // record the old recursion count |
1497 _waiters++; // increment the number of waiters | 1483 _waiters++; // increment the number of waiters |
1498 _recursions = 0; // set the recursion level to be 1 | 1484 _recursions = 0; // set the recursion level to be 1 |
1499 exit (true, Self) ; // exit the monitor | 1485 exit (true, Self) ; // exit the monitor |
1500 guarantee (_owner != Self, "invariant") ; | 1486 guarantee (_owner != Self, "invariant") ; |
1501 | |
1502 // As soon as the ObjectMonitor's ownership is dropped in the exit() | |
1503 // call above, another thread can enter() the ObjectMonitor, do the | |
1504 // notify(), and exit() the ObjectMonitor. If the other thread's | |
1505 // exit() call chooses this thread as the successor and the unpark() | |
1506 // call happens to occur while this thread is posting a | |
1507 // MONITOR_CONTENDED_EXIT event, then we run the risk of the event | |
1508 // handler using RawMonitors and consuming the unpark(). | |
1509 // | |
1510 // To avoid the problem, we re-post the event. This does no harm | |
1511 // even if the original unpark() was not consumed because we are the | |
1512 // chosen successor for this monitor. | |
1513 if (node._notified != 0 && _succ == Self) { | |
1514 node._event->unpark(); | |
1515 } | |
1516 | 1487 |
1517 // The thread is on the WaitSet list - now park() it. | 1488 // The thread is on the WaitSet list - now park() it. |
1518 // On MP systems it's conceivable that a brief spin before we park | 1489 // On MP systems it's conceivable that a brief spin before we park |
1519 // could be profitable. | 1490 // could be profitable. |
1520 // | 1491 // |
1593 // (Don't cache naked oops over safepoints, of course). | 1564 // (Don't cache naked oops over safepoints, of course). |
1594 | 1565 |
1595 // post monitor waited event. Note that this is past-tense, we are done waiting. | 1566 // post monitor waited event. Note that this is past-tense, we are done waiting. |
1596 if (JvmtiExport::should_post_monitor_waited()) { | 1567 if (JvmtiExport::should_post_monitor_waited()) { |
1597 JvmtiExport::post_monitor_waited(jt, this, ret == OS_TIMEOUT); | 1568 JvmtiExport::post_monitor_waited(jt, this, ret == OS_TIMEOUT); |
1569 | |
1570 if (node._notified != 0 && _succ == Self) { | |
1571 // In this part of the monitor wait-notify-reenter protocol it | |
1572 // is possible (and normal) for another thread to do a fastpath | |
1573 // monitor enter-exit while this thread is still trying to get | |
1574 // to the reenter portion of the protocol. | |
1575 // | |
1576 // The ObjectMonitor was notified and the current thread is | |
1577 // the successor which also means that an unpark() has already | |
1578 // been done. The JVMTI_EVENT_MONITOR_WAITED event handler can | |
1579 // consume the unpark() that was done when the successor was | |
1580 // set because the same ParkEvent is shared between Java | |
1581 // monitors and JVM/TI RawMonitors (for now). | |
1582 // | |
1583 // We redo the unpark() to ensure forward progress, i.e., we | |
1584 // don't want all pending threads hanging (parked) with none | |
1585 // entering the unlocked monitor. | |
1586 node._event->unpark(); | |
1587 } | |
1598 } | 1588 } |
1599 | 1589 |
1600 if (event.should_commit()) { | 1590 if (event.should_commit()) { |
1601 post_monitor_wait_event(&event, node._notifier_tid, millis, ret == OS_TIMEOUT); | 1591 post_monitor_wait_event(&event, node._notifier_tid, millis, ret == OS_TIMEOUT); |
1602 } | 1592 } |
2029 if (MaxSpin >= 0) { | 2019 if (MaxSpin >= 0) { |
2030 if (_Spinner > MaxSpin) { | 2020 if (_Spinner > MaxSpin) { |
2031 TEVENT (Spin abort -- too many spinners) ; | 2021 TEVENT (Spin abort -- too many spinners) ; |
2032 return 0 ; | 2022 return 0 ; |
2033 } | 2023 } |
2034 // Slighty racy, but benign ... | 2024 // Slightly racy, but benign ... |
2035 Adjust (&_Spinner, 1) ; | 2025 Adjust (&_Spinner, 1) ; |
2036 } | 2026 } |
2037 | 2027 |
2038 // We're good to spin ... spin ingress. | 2028 // We're good to spin ... spin ingress. |
2039 // CONSIDER: use Prefetch::write() to avoid RTS->RTO upgrades | 2029 // CONSIDER: use Prefetch::write() to avoid RTS->RTO upgrades |