< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

  45 #include "runtime/mutexLocker.hpp"
  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/os.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/safepointMechanism.inline.hpp"
  50 #include "runtime/safepointVerifiers.hpp"
  51 #include "runtime/sharedRuntime.hpp"
  52 #include "runtime/stubRoutines.hpp"
  53 #include "runtime/synchronizer.hpp"
  54 #include "runtime/threads.hpp"
  55 #include "runtime/timer.hpp"
  56 #include "runtime/timerTrace.hpp"
  57 #include "runtime/trimNativeHeap.hpp"
  58 #include "runtime/vframe.hpp"
  59 #include "runtime/vmThread.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/concurrentHashTable.inline.hpp"
  62 #include "utilities/concurrentHashTableTasks.inline.hpp"
  63 #include "utilities/dtrace.hpp"
  64 #include "utilities/events.hpp"

  65 #include "utilities/globalCounter.inline.hpp"
  66 #include "utilities/globalDefinitions.hpp"
  67 #include "utilities/linkedlist.hpp"
  68 #include "utilities/preserveException.hpp"
  69 
  70 class ObjectMonitorDeflationLogging;
  71 
  72 void MonitorList::add(ObjectMonitor* m) {
  73   ObjectMonitor* head;
  74   do {
  75     head = AtomicAccess::load(&_head);
  76     m->set_next_om(head);
  77   } while (AtomicAccess::cmpxchg(&_head, head, m) != head);
  78 
  79   size_t count = AtomicAccess::add(&_count, 1u, memory_order_relaxed);
  80   size_t old_max;
  81   do {
  82     old_max = AtomicAccess::load(&_max);
  83     if (count <= old_max) {
  84       break;

 581 
 582 static SharedGlobals GVars;
 583 
 584 // hashCode() generation :
 585 //
 586 // Possibilities:
 587 // * MD5Digest of {obj,stw_random}
 588 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 589 // * A DES- or AES-style SBox[] mechanism
 590 // * One of the Phi-based schemes, such as:
 591 //   2654435761 = 2^32 * Phi (golden ratio)
 592 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 593 // * A variation of Marsaglia's shift-xor RNG scheme.
 594 // * (obj ^ stw_random) is appealing, but can result
 595 //   in undesirable regularity in the hashCode values of adjacent objects
 596 //   (objects allocated back-to-back, in particular).  This could potentially
 597 //   result in hashtable collisions and reduced hashtable efficiency.
 598 //   There are simple ways to "diffuse" the middle address bits over the
 599 //   generated hashCode values:
 600 
 601 static intptr_t get_next_hash(Thread* current, oop obj) {
 602   intptr_t value = 0;
 603   if (hashCode == 0) {
 604     // This form uses global Park-Miller RNG.
 605     // On MP system we'll have lots of RW access to a global, so the
 606     // mechanism induces lots of coherency traffic.
 607     value = os::random();
 608   } else if (hashCode == 1) {
 609     // This variation has the property of being stable (idempotent)
 610     // between STW operations.  This can be useful in some of the 1-0
 611     // synchronization schemes.
 612     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 613     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 614   } else if (hashCode == 2) {
 615     value = 1;            // for sensitivity testing
 616   } else if (hashCode == 3) {
 617     value = ++GVars.hc_sequence;
 618   } else if (hashCode == 4) {
 619     value = cast_from_oop<intptr_t>(obj);
 620   } else {
 621     // Marsaglia's xor-shift scheme with thread-specific state
 622     // This is probably the best overall implementation -- we'll
 623     // likely make this the default in future releases.
 624     unsigned t = current->_hashStateX;
 625     t ^= (t << 11);
 626     current->_hashStateX = current->_hashStateY;
 627     current->_hashStateY = current->_hashStateZ;
 628     current->_hashStateZ = current->_hashStateW;
 629     unsigned v = current->_hashStateW;
 630     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 631     current->_hashStateW = v;
 632     value = v;










 633   }
 634 
 635   value &= markWord::hash_mask;
 636   if (value == 0) value = 0xBAD;
 637   assert(value != markWord::no_hash, "invariant");
 638   return value;
 639 }
 640 
 641 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 642   while (true) {
 643     ObjectMonitor* monitor = nullptr;
 644     markWord temp, test;
 645     intptr_t hash;
 646     markWord mark = obj->mark_acquire();
 647     // If UseObjectMonitorTable is set the hash can simply be installed in the
 648     // object header, since the monitor isn't in the object header.
 649     if (UseObjectMonitorTable || !mark.has_monitor()) {



















 650       hash = mark.hash();
 651       if (hash != 0) {                     // if it has a hash, just return it
 652         return hash;
 653       }
 654       hash = get_next_hash(current, obj);  // get a new hash
 655       temp = mark.copy_set_hash(hash);     // merge the hash into header
 656                                            // try to install the hash
 657       test = obj->cas_set_mark(temp, mark);
 658       if (test == mark) {                  // if the hash was installed, return it
 659         return hash;
 660       }
 661       // CAS failed, retry
 662       continue;
 663 
 664       // Failed to install the hash. It could be that another thread
 665       // installed the hash just before our attempt or inflation has
 666       // occurred or... so we fall thru to inflate the monitor for
 667       // stability and then install the hash.
 668     } else {
 669       assert(!mark.is_unlocked() && !mark.is_fast_locked(), "invariant");

 718         // If we add any new usages of the header/dmw field, this code
 719         // will need to be updated.
 720         hash = test.hash();
 721         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 722         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
 723       }
 724       if (monitor->is_being_async_deflated() && !UseObjectMonitorTable) {
 725         // If we detect that async deflation has occurred, then we
 726         // attempt to restore the header/dmw to the object's header
 727         // so that we only retry once if the deflater thread happens
 728         // to be slow.
 729         monitor->install_displaced_markword_in_object(obj);
 730         continue;
 731       }
 732     }
 733     // We finally get the hash.
 734     return hash;
 735   }
 736 }
 737 



















 738 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
 739                                                    Handle h_obj) {
 740   assert(current == JavaThread::current(), "Can only be called on current thread");
 741   oop obj = h_obj();
 742 
 743   markWord mark = obj->mark_acquire();
 744 
 745   if (mark.is_fast_locked()) {
 746     // fast-locking case, see if lock is in current's lock stack
 747     return current->lock_stack().contains(h_obj());
 748   }
 749 
 750   while (mark.has_monitor()) {
 751     ObjectMonitor* monitor = read_monitor(current, obj, mark);
 752     if (monitor != nullptr) {
 753       return monitor->is_entered(current) != 0;
 754     }
 755     // Racing with inflation/deflation, retry
 756     mark = obj->mark_acquire();
 757 

1453     monitors_iterate([&](ObjectMonitor* monitor) {
1454       if (is_interesting(monitor)) {
1455         const oop obj = monitor->object_peek();
1456         const intptr_t hash = UseObjectMonitorTable ? monitor->hash() : monitor->header().hash();
1457         ResourceMark rm;
1458         out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(monitor),
1459                    monitor->is_busy(), hash != 0, monitor->has_owner(),
1460                    p2i(obj), obj == nullptr ? "" : obj->klass()->external_name());
1461         if (monitor->is_busy()) {
1462           out->print(" (%s)", monitor->is_busy_to_string(&ss));
1463           ss.reset();
1464         }
1465         out->cr();
1466       }
1467     });
1468   }
1469 
1470   out->flush();
1471 }
1472 












1473 // -----------------------------------------------------------------------------
1474 // ConcurrentHashTable storing links from objects to ObjectMonitors
1475 class ObjectMonitorTable : AllStatic {
1476   struct Config {
1477     using Value = ObjectMonitor*;
1478     static uintx get_hash(Value const& value, bool* is_dead) {
1479       return (uintx)value->hash();
1480     }
1481     static void* allocate_node(void* context, size_t size, Value const& value) {
1482       ObjectMonitorTable::inc_items_count();
1483       return AllocateHeap(size, mtObjectMonitor);
1484     };
1485     static void free_node(void* context, void* memory, Value const& value) {
1486       ObjectMonitorTable::dec_items_count();
1487       FreeHeap(memory);
1488     }
1489   };
1490   using ConcurrentTable = ConcurrentHashTable<Config, mtObjectMonitor>;
1491 
1492   static ConcurrentTable* _table;
1493   static volatile size_t _items_count;
1494   static size_t _table_size;
1495   static volatile bool _resize;
1496 
1497   class Lookup : public StackObj {
1498     oop _obj;
1499 
1500    public:
1501     explicit Lookup(oop obj) : _obj(obj) {}
1502 
1503     uintx get_hash() const {
1504       uintx hash = _obj->mark().hash();
1505       assert(hash != 0, "should have a hash");
1506       return hash;
1507     }
1508 
1509     bool equals(ObjectMonitor** value) {
1510       assert(*value != nullptr, "must be");
1511       return (*value)->object_refers_to(_obj);
1512     }
1513 
1514     bool is_dead(ObjectMonitor** value) {
1515       assert(*value != nullptr, "must be");
1516       return false;
1517     }
1518   };
1519 
1520   class LookupMonitor : public StackObj {
1521     ObjectMonitor* _monitor;
1522 
1523    public:
1524     explicit LookupMonitor(ObjectMonitor* monitor) : _monitor(monitor) {}
1525 
1526     uintx get_hash() const {

1593     _table_size = table_size();
1594     _resize = false;
1595   }
1596 
1597   static void verify_monitor_get_result(oop obj, ObjectMonitor* monitor) {
1598 #ifdef ASSERT
1599     if (SafepointSynchronize::is_at_safepoint()) {
1600       bool has_monitor = obj->mark().has_monitor();
1601       assert(has_monitor == (monitor != nullptr),
1602           "Inconsistency between markWord and ObjectMonitorTable has_monitor: %s monitor: " PTR_FORMAT,
1603           BOOL_TO_STR(has_monitor), p2i(monitor));
1604     }
1605 #endif
1606   }
1607 
1608   static ObjectMonitor* monitor_get(Thread* current, oop obj) {
1609     ObjectMonitor* result = nullptr;
1610     Lookup lookup_f(obj);
1611     auto found_f = [&](ObjectMonitor** found) {
1612       assert((*found)->object_peek() == obj, "must be");

1613       result = *found;
1614     };
1615     _table->get(current, lookup_f, found_f);
1616     verify_monitor_get_result(obj, result);
1617     return result;
1618   }
1619 
1620   static void try_notify_grow() {
1621     if (!_table->is_max_size_reached() && !AtomicAccess::load(&_resize)) {
1622       AtomicAccess::store(&_resize, true);
1623       if (Service_lock->try_lock()) {
1624         Service_lock->notify();
1625         Service_lock->unlock();
1626       }
1627     }
1628   }
1629 
1630   static bool should_shrink() {
1631     // Not implemented;
1632     return false;

1721     LookupMonitor lookup_f(monitor);
1722     return _table->remove(current, lookup_f);
1723   }
1724 
1725   static bool contains_monitor(Thread* current, ObjectMonitor* monitor) {
1726     LookupMonitor lookup_f(monitor);
1727     bool result = false;
1728     auto found_f = [&](ObjectMonitor** found) {
1729       result = true;
1730     };
1731     _table->get(current, lookup_f, found_f);
1732     return result;
1733   }
1734 
1735   static void print_on(outputStream* st) {
1736     auto printer = [&] (ObjectMonitor** entry) {
1737        ObjectMonitor* om = *entry;
1738        oop obj = om->object_peek();
1739        st->print("monitor=" PTR_FORMAT ", ", p2i(om));
1740        st->print("object=" PTR_FORMAT, p2i(obj));
1741        assert(obj->mark().hash() == om->hash(), "hash must match");
1742        st->cr();
1743        return true;
1744     };
1745     if (SafepointSynchronize::is_at_safepoint()) {
1746       _table->do_safepoint_scan(printer);
1747     } else {
1748       _table->do_scan(Thread::current(), printer);
1749     }
1750   }
1751 };
1752 
1753 ObjectMonitorTable::ConcurrentTable* ObjectMonitorTable::_table = nullptr;
1754 volatile size_t ObjectMonitorTable::_items_count = 0;
1755 size_t ObjectMonitorTable::_table_size = 0;
1756 volatile bool ObjectMonitorTable::_resize = false;
1757 
1758 ObjectMonitor* ObjectSynchronizer::get_or_insert_monitor_from_table(oop object, JavaThread* current, bool* inserted) {
1759   ObjectMonitor* monitor = get_monitor_from_table(current, object);
1760   if (monitor != nullptr) {
1761     *inserted = false;

1809   ObjectMonitor* monitor = get_or_insert_monitor_from_table(object, current, &inserted);
1810 
1811   if (inserted) {
1812     log_inflate(current, object, cause);
1813     if (event.should_commit()) {
1814       post_monitor_inflate_event(&event, object, cause);
1815     }
1816 
1817     // The monitor has an anonymous owner so it is safe from async deflation.
1818     ObjectSynchronizer::_in_use_list.add(monitor);
1819   }
1820 
1821   return monitor;
1822 }
1823 
1824 // Add the hashcode to the monitor to match the object and put it in the hashtable.
1825 ObjectMonitor* ObjectSynchronizer::add_monitor(JavaThread* current, ObjectMonitor* monitor, oop obj) {
1826   assert(UseObjectMonitorTable, "must be");
1827   assert(obj == monitor->object(), "must be");
1828 
1829   intptr_t hash = obj->mark().hash();
1830   assert(hash != 0, "must be set when claiming the object monitor");
1831   monitor->set_hash(hash);
1832 
1833   return ObjectMonitorTable::monitor_put_get(current, monitor, obj);
1834 }
1835 
1836 bool ObjectSynchronizer::remove_monitor(Thread* current, ObjectMonitor* monitor, oop obj) {
1837   assert(UseObjectMonitorTable, "must be");
1838   assert(monitor->object_peek() == obj, "must be, cleared objects are removed by is_dead");
1839 
1840   return ObjectMonitorTable::remove_monitor_entry(current, monitor);
1841 }
1842 
1843 void ObjectSynchronizer::deflate_mark_word(oop obj) {
1844   assert(UseObjectMonitorTable, "must be");
1845 
1846   markWord mark = obj->mark_acquire();
1847   assert(!mark.has_no_hash(), "obj with inflated monitor must have had a hash");
1848 
1849   while (mark.has_monitor()) {

  45 #include "runtime/mutexLocker.hpp"
  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/os.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/safepointMechanism.inline.hpp"
  50 #include "runtime/safepointVerifiers.hpp"
  51 #include "runtime/sharedRuntime.hpp"
  52 #include "runtime/stubRoutines.hpp"
  53 #include "runtime/synchronizer.hpp"
  54 #include "runtime/threads.hpp"
  55 #include "runtime/timer.hpp"
  56 #include "runtime/timerTrace.hpp"
  57 #include "runtime/trimNativeHeap.hpp"
  58 #include "runtime/vframe.hpp"
  59 #include "runtime/vmThread.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/concurrentHashTable.inline.hpp"
  62 #include "utilities/concurrentHashTableTasks.inline.hpp"
  63 #include "utilities/dtrace.hpp"
  64 #include "utilities/events.hpp"
  65 #include "utilities/fastHash.hpp"
  66 #include "utilities/globalCounter.inline.hpp"
  67 #include "utilities/globalDefinitions.hpp"
  68 #include "utilities/linkedlist.hpp"
  69 #include "utilities/preserveException.hpp"
  70 
  71 class ObjectMonitorDeflationLogging;
  72 
  73 void MonitorList::add(ObjectMonitor* m) {
  74   ObjectMonitor* head;
  75   do {
  76     head = AtomicAccess::load(&_head);
  77     m->set_next_om(head);
  78   } while (AtomicAccess::cmpxchg(&_head, head, m) != head);
  79 
  80   size_t count = AtomicAccess::add(&_count, 1u, memory_order_relaxed);
  81   size_t old_max;
  82   do {
  83     old_max = AtomicAccess::load(&_max);
  84     if (count <= old_max) {
  85       break;

 582 
 583 static SharedGlobals GVars;
 584 
 585 // hashCode() generation :
 586 //
 587 // Possibilities:
 588 // * MD5Digest of {obj,stw_random}
 589 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 590 // * A DES- or AES-style SBox[] mechanism
 591 // * One of the Phi-based schemes, such as:
 592 //   2654435761 = 2^32 * Phi (golden ratio)
 593 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 594 // * A variation of Marsaglia's shift-xor RNG scheme.
 595 // * (obj ^ stw_random) is appealing, but can result
 596 //   in undesirable regularity in the hashCode values of adjacent objects
 597 //   (objects allocated back-to-back, in particular).  This could potentially
 598 //   result in hashtable collisions and reduced hashtable efficiency.
 599 //   There are simple ways to "diffuse" the middle address bits over the
 600 //   generated hashCode values:
 601 
 602 intptr_t ObjectSynchronizer::get_next_hash(Thread* current, oop obj) {
 603   intptr_t value = 0;
 604   if (hashCode == 0) {
 605     // This form uses global Park-Miller RNG.
 606     // On MP system we'll have lots of RW access to a global, so the
 607     // mechanism induces lots of coherency traffic.
 608     value = os::random();
 609   } else if (hashCode == 1) {
 610     // This variation has the property of being stable (idempotent)
 611     // between STW operations.  This can be useful in some of the 1-0
 612     // synchronization schemes.
 613     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 614     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 615   } else if (hashCode == 2) {
 616     value = 1;            // for sensitivity testing
 617   } else if (hashCode == 3) {
 618     value = ++GVars.hc_sequence;
 619   } else if (hashCode == 4) {
 620     value = cast_from_oop<intptr_t>(obj);
 621   } else if (hashCode == 5) {
 622     // Marsaglia's xor-shift scheme with thread-specific state
 623     // This is probably the best overall implementation -- we'll
 624     // likely make this the default in future releases.
 625     unsigned t = current->_hashStateX;
 626     t ^= (t << 11);
 627     current->_hashStateX = current->_hashStateY;
 628     current->_hashStateY = current->_hashStateZ;
 629     current->_hashStateZ = current->_hashStateW;
 630     unsigned v = current->_hashStateW;
 631     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 632     current->_hashStateW = v;
 633     value = v;
 634   } else {
 635     assert(UseCompactObjectHeaders, "Only with compact i-hash");
 636 #ifdef _LP64
 637     uint64_t val = cast_from_oop<uint64_t>(obj);
 638     uint32_t hash = FastHash::get_hash32((uint32_t)val, (uint32_t)(val >> 32));
 639 #else
 640     uint32_t val = cast_from_oop<uint32_t>(obj);
 641     uint32_t hash = FastHash::get_hash32(val, UCONST64(0xAAAAAAAA));
 642 #endif
 643     value= static_cast<intptr_t>(hash);
 644   }
 645 
 646   value &= markWord::hash_mask;
 647   if (hashCode != 6 && value == 0) value = 0xBAD;
 648   assert(value != markWord::no_hash || hashCode == 6, "invariant");
 649   return value;
 650 }
 651 
 652 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 653   while (true) {
 654     ObjectMonitor* monitor = nullptr;
 655     markWord temp, test;
 656     intptr_t hash;
 657     markWord mark = obj->mark_acquire();
 658     if (UseCompactObjectHeaders) {
 659       if (mark.is_hashed()) {
 660         return get_hash(mark, obj);
 661       }
 662       intptr_t hash = get_next_hash(current, obj);  // get a new hash
 663       markWord new_mark;
 664       if (mark.is_not_hashed_expanded()) {
 665         new_mark = mark.set_hashed_expanded();
 666         int offset = mark.klass()->hash_offset_in_bytes(obj, mark);
 667         obj->int_field_put(offset, (jint) hash);
 668       } else {
 669         new_mark = mark.set_hashed_not_expanded();
 670       }
 671       markWord old_mark = obj->cas_set_mark(new_mark, mark);
 672       if (old_mark == mark) {
 673         return hash;
 674       }
 675       // CAS failed, retry.
 676       continue;
 677     } else if (UseObjectMonitorTable || !mark.has_monitor()) {
 678       // If UseObjectMonitorTable is set the hash can simply be installed in the
 679       // object header, since the monitor isn't in the object header.
 680       hash = mark.hash();
 681       if (hash != 0) {                     // if it has a hash, just return it
 682         return hash;
 683       }
 684       hash = get_next_hash(current, obj);  // get a new hash
 685       temp = mark.copy_set_hash(hash);     // merge the hash into header
 686                                            // try to install the hash
 687       test = obj->cas_set_mark(temp, mark);
 688       if (test == mark) {                  // if the hash was installed, return it
 689         return hash;
 690       }
 691       // CAS failed, retry
 692       continue;
 693 
 694       // Failed to install the hash. It could be that another thread
 695       // installed the hash just before our attempt or inflation has
 696       // occurred or... so we fall thru to inflate the monitor for
 697       // stability and then install the hash.
 698     } else {
 699       assert(!mark.is_unlocked() && !mark.is_fast_locked(), "invariant");

 748         // If we add any new usages of the header/dmw field, this code
 749         // will need to be updated.
 750         hash = test.hash();
 751         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 752         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
 753       }
 754       if (monitor->is_being_async_deflated() && !UseObjectMonitorTable) {
 755         // If we detect that async deflation has occurred, then we
 756         // attempt to restore the header/dmw to the object's header
 757         // so that we only retry once if the deflater thread happens
 758         // to be slow.
 759         monitor->install_displaced_markword_in_object(obj);
 760         continue;
 761       }
 762     }
 763     // We finally get the hash.
 764     return hash;
 765   }
 766 }
 767 
 768 
 769 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj, Klass* klass) {
 770   assert(UseCompactObjectHeaders, "Only with compact i-hash");
 771   //assert(mark.is_neutral() | mark.is_fast_locked(), "only from neutral or fast-locked mark: " INTPTR_FORMAT, mark.value());
 772   assert(mark.is_hashed(), "only from hashed or copied object");
 773   if (mark.is_hashed_expanded()) {
 774     return obj->int_field(klass->hash_offset_in_bytes(obj, mark));
 775   } else {
 776     assert(mark.is_hashed_not_expanded(), "must be hashed");
 777     assert(hashCode == 6 || hashCode == 2, "must have idempotent hashCode");
 778     // Already marked as hashed, but not yet copied. Recompute hash and return it.
 779     return ObjectSynchronizer::get_next_hash(nullptr, obj); // recompute hash
 780   }
 781 }
 782 
 783 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj) {
 784   return get_hash(mark, obj, mark.klass());
 785 }
 786 
 787 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
 788                                                    Handle h_obj) {
 789   assert(current == JavaThread::current(), "Can only be called on current thread");
 790   oop obj = h_obj();
 791 
 792   markWord mark = obj->mark_acquire();
 793 
 794   if (mark.is_fast_locked()) {
 795     // fast-locking case, see if lock is in current's lock stack
 796     return current->lock_stack().contains(h_obj());
 797   }
 798 
 799   while (mark.has_monitor()) {
 800     ObjectMonitor* monitor = read_monitor(current, obj, mark);
 801     if (monitor != nullptr) {
 802       return monitor->is_entered(current) != 0;
 803     }
 804     // Racing with inflation/deflation, retry
 805     mark = obj->mark_acquire();
 806 

1502     monitors_iterate([&](ObjectMonitor* monitor) {
1503       if (is_interesting(monitor)) {
1504         const oop obj = monitor->object_peek();
1505         const intptr_t hash = UseObjectMonitorTable ? monitor->hash() : monitor->header().hash();
1506         ResourceMark rm;
1507         out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(monitor),
1508                    monitor->is_busy(), hash != 0, monitor->has_owner(),
1509                    p2i(obj), obj == nullptr ? "" : obj->klass()->external_name());
1510         if (monitor->is_busy()) {
1511           out->print(" (%s)", monitor->is_busy_to_string(&ss));
1512           ss.reset();
1513         }
1514         out->cr();
1515       }
1516     });
1517   }
1518 
1519   out->flush();
1520 }
1521 
1522 static uintx objhash(oop obj) {
1523   if (UseCompactObjectHeaders) {
1524     uintx hash = ObjectSynchronizer::get_hash(obj->mark(), obj);
1525     assert(hash != 0, "should have a hash");
1526     return hash;
1527   } else {
1528     uintx hash = obj->mark().hash();
1529     assert(hash != 0, "should have a hash");
1530     return hash;
1531   }
1532 }
1533 
1534 // -----------------------------------------------------------------------------
1535 // ConcurrentHashTable storing links from objects to ObjectMonitors
1536 class ObjectMonitorTable : AllStatic {
1537   struct Config {
1538     using Value = ObjectMonitor*;
1539     static uintx get_hash(Value const& value, bool* is_dead) {
1540       return (uintx)value->hash();
1541     }
1542     static void* allocate_node(void* context, size_t size, Value const& value) {
1543       ObjectMonitorTable::inc_items_count();
1544       return AllocateHeap(size, mtObjectMonitor);
1545     };
1546     static void free_node(void* context, void* memory, Value const& value) {
1547       ObjectMonitorTable::dec_items_count();
1548       FreeHeap(memory);
1549     }
1550   };
1551   using ConcurrentTable = ConcurrentHashTable<Config, mtObjectMonitor>;
1552 
1553   static ConcurrentTable* _table;
1554   static volatile size_t _items_count;
1555   static size_t _table_size;
1556   static volatile bool _resize;
1557 
1558   class Lookup : public StackObj {
1559     oop _obj;
1560 
1561    public:
1562     explicit Lookup(oop obj) : _obj(obj) {}
1563 
1564     uintx get_hash() const {
1565       return objhash(_obj);


1566     }
1567 
1568     bool equals(ObjectMonitor** value) {
1569       assert(*value != nullptr, "must be");
1570       return (*value)->object_refers_to(_obj);
1571     }
1572 
1573     bool is_dead(ObjectMonitor** value) {
1574       assert(*value != nullptr, "must be");
1575       return false;
1576     }
1577   };
1578 
1579   class LookupMonitor : public StackObj {
1580     ObjectMonitor* _monitor;
1581 
1582    public:
1583     explicit LookupMonitor(ObjectMonitor* monitor) : _monitor(monitor) {}
1584 
1585     uintx get_hash() const {

1652     _table_size = table_size();
1653     _resize = false;
1654   }
1655 
1656   static void verify_monitor_get_result(oop obj, ObjectMonitor* monitor) {
1657 #ifdef ASSERT
1658     if (SafepointSynchronize::is_at_safepoint()) {
1659       bool has_monitor = obj->mark().has_monitor();
1660       assert(has_monitor == (monitor != nullptr),
1661           "Inconsistency between markWord and ObjectMonitorTable has_monitor: %s monitor: " PTR_FORMAT,
1662           BOOL_TO_STR(has_monitor), p2i(monitor));
1663     }
1664 #endif
1665   }
1666 
1667   static ObjectMonitor* monitor_get(Thread* current, oop obj) {
1668     ObjectMonitor* result = nullptr;
1669     Lookup lookup_f(obj);
1670     auto found_f = [&](ObjectMonitor** found) {
1671       assert((*found)->object_peek() == obj, "must be");
1672       assert(objhash(obj) == (uintx)(*found)->hash(), "hash must match");
1673       result = *found;
1674     };
1675     _table->get(current, lookup_f, found_f);
1676     verify_monitor_get_result(obj, result);
1677     return result;
1678   }
1679 
1680   static void try_notify_grow() {
1681     if (!_table->is_max_size_reached() && !AtomicAccess::load(&_resize)) {
1682       AtomicAccess::store(&_resize, true);
1683       if (Service_lock->try_lock()) {
1684         Service_lock->notify();
1685         Service_lock->unlock();
1686       }
1687     }
1688   }
1689 
1690   static bool should_shrink() {
1691     // Not implemented;
1692     return false;

1781     LookupMonitor lookup_f(monitor);
1782     return _table->remove(current, lookup_f);
1783   }
1784 
1785   static bool contains_monitor(Thread* current, ObjectMonitor* monitor) {
1786     LookupMonitor lookup_f(monitor);
1787     bool result = false;
1788     auto found_f = [&](ObjectMonitor** found) {
1789       result = true;
1790     };
1791     _table->get(current, lookup_f, found_f);
1792     return result;
1793   }
1794 
1795   static void print_on(outputStream* st) {
1796     auto printer = [&] (ObjectMonitor** entry) {
1797        ObjectMonitor* om = *entry;
1798        oop obj = om->object_peek();
1799        st->print("monitor=" PTR_FORMAT ", ", p2i(om));
1800        st->print("object=" PTR_FORMAT, p2i(obj));
1801        assert(objhash(obj) == (uintx)om->hash(), "hash must match");
1802        st->cr();
1803        return true;
1804     };
1805     if (SafepointSynchronize::is_at_safepoint()) {
1806       _table->do_safepoint_scan(printer);
1807     } else {
1808       _table->do_scan(Thread::current(), printer);
1809     }
1810   }
1811 };
1812 
1813 ObjectMonitorTable::ConcurrentTable* ObjectMonitorTable::_table = nullptr;
1814 volatile size_t ObjectMonitorTable::_items_count = 0;
1815 size_t ObjectMonitorTable::_table_size = 0;
1816 volatile bool ObjectMonitorTable::_resize = false;
1817 
1818 ObjectMonitor* ObjectSynchronizer::get_or_insert_monitor_from_table(oop object, JavaThread* current, bool* inserted) {
1819   ObjectMonitor* monitor = get_monitor_from_table(current, object);
1820   if (monitor != nullptr) {
1821     *inserted = false;

1869   ObjectMonitor* monitor = get_or_insert_monitor_from_table(object, current, &inserted);
1870 
1871   if (inserted) {
1872     log_inflate(current, object, cause);
1873     if (event.should_commit()) {
1874       post_monitor_inflate_event(&event, object, cause);
1875     }
1876 
1877     // The monitor has an anonymous owner so it is safe from async deflation.
1878     ObjectSynchronizer::_in_use_list.add(monitor);
1879   }
1880 
1881   return monitor;
1882 }
1883 
1884 // Add the hashcode to the monitor to match the object and put it in the hashtable.
1885 ObjectMonitor* ObjectSynchronizer::add_monitor(JavaThread* current, ObjectMonitor* monitor, oop obj) {
1886   assert(UseObjectMonitorTable, "must be");
1887   assert(obj == monitor->object(), "must be");
1888 
1889   intptr_t hash = objhash(obj);
1890   assert(hash != 0, "must be set when claiming the object monitor");
1891   monitor->set_hash(hash);
1892 
1893   return ObjectMonitorTable::monitor_put_get(current, monitor, obj);
1894 }
1895 
1896 bool ObjectSynchronizer::remove_monitor(Thread* current, ObjectMonitor* monitor, oop obj) {
1897   assert(UseObjectMonitorTable, "must be");
1898   assert(monitor->object_peek() == obj, "must be, cleared objects are removed by is_dead");
1899 
1900   return ObjectMonitorTable::remove_monitor_entry(current, monitor);
1901 }
1902 
1903 void ObjectSynchronizer::deflate_mark_word(oop obj) {
1904   assert(UseObjectMonitorTable, "must be");
1905 
1906   markWord mark = obj->mark_acquire();
1907   assert(!mark.has_no_hash(), "obj with inflated monitor must have had a hash");
1908 
1909   while (mark.has_monitor()) {
< prev index next >