duke@435: /* acorn@4425: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/placeholders.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/fieldType.hpp" stefank@2314: #include "utilities/hashtable.inline.hpp" duke@435: duke@435: // Placeholder methods duke@435: coleenp@2497: PlaceholderEntry* PlaceholderTable::new_entry(int hash, Symbol* name, coleenp@4037: ClassLoaderData* loader_data, coleenp@4037: bool havesupername, coleenp@2497: Symbol* supername) { zgu@3900: PlaceholderEntry* entry = (PlaceholderEntry*)Hashtable::new_entry(hash, name); coleenp@2497: // Hashtable with Symbol* literal must increment and decrement refcount. coleenp@2497: name->increment_refcount(); coleenp@4037: entry->set_loader_data(loader_data); duke@435: entry->set_havesupername(havesupername); duke@435: entry->set_supername(supername); duke@435: entry->set_superThreadQ(NULL); duke@435: entry->set_loadInstanceThreadQ(NULL); duke@435: entry->set_defineThreadQ(NULL); duke@435: entry->set_definer(NULL); hseigel@4278: entry->set_instance_klass(NULL); duke@435: return entry; duke@435: } duke@435: coleenp@2497: void PlaceholderTable::free_entry(PlaceholderEntry* entry) { coleenp@2497: // decrement Symbol refcount here because Hashtable doesn't. coleenp@2497: entry->literal()->decrement_refcount(); coleenp@2497: if (entry->supername() != NULL) entry->supername()->decrement_refcount(); zgu@3900: Hashtable::free_entry(entry); coleenp@2497: } coleenp@2497: duke@435: duke@435: // Placeholder objects represent classes currently being loaded. duke@435: // All threads examining the placeholder table must hold the duke@435: // SystemDictionary_lock, so we don't need special precautions duke@435: // on store ordering here. duke@435: void PlaceholderTable::add_entry(int index, unsigned int hash, coleenp@4037: Symbol* class_name, ClassLoaderData* loader_data, coleenp@2497: bool havesupername, Symbol* supername){ duke@435: assert_locked_or_safepoint(SystemDictionary_lock); coleenp@2497: assert(class_name != NULL, "adding NULL obj"); duke@435: duke@435: // Both readers and writers are locked so it's safe to just duke@435: // create the placeholder and insert it in the list without a membar. coleenp@4037: PlaceholderEntry* entry = new_entry(hash, class_name, loader_data, havesupername, supername); duke@435: add_entry(index, entry); duke@435: } duke@435: duke@435: duke@435: // Remove a placeholder object. duke@435: void PlaceholderTable::remove_entry(int index, unsigned int hash, coleenp@2497: Symbol* class_name, coleenp@4037: ClassLoaderData* loader_data) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: PlaceholderEntry** p = bucket_addr(index); duke@435: while (*p) { duke@435: PlaceholderEntry *probe = *p; coleenp@4037: if (probe->hash() == hash && probe->equals(class_name, loader_data)) { duke@435: // Delete entry duke@435: *p = probe->next(); duke@435: free_entry(probe); duke@435: return; duke@435: } duke@435: p = probe->next_addr(); duke@435: } duke@435: } duke@435: duke@435: PlaceholderEntry* PlaceholderTable::get_entry(int index, unsigned int hash, coleenp@2497: Symbol* class_name, coleenp@4037: ClassLoaderData* loader_data) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: duke@435: for (PlaceholderEntry *place_probe = bucket(index); duke@435: place_probe != NULL; duke@435: place_probe = place_probe->next()) { duke@435: if (place_probe->hash() == hash && coleenp@4037: place_probe->equals(class_name, loader_data)) { duke@435: return place_probe; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: coleenp@2497: Symbol* PlaceholderTable::find_entry(int index, unsigned int hash, coleenp@2497: Symbol* class_name, coleenp@4037: ClassLoaderData* loader_data) { coleenp@4037: PlaceholderEntry* probe = get_entry(index, hash, class_name, loader_data); coleenp@2497: return (probe? probe->klassname(): (Symbol*)NULL); duke@435: } duke@435: duke@435: // find_and_add returns probe pointer - old or new duke@435: // If no entry exists, add a placeholder entry duke@435: // If entry exists, reuse entry duke@435: // For both, push SeenThread for classloadAction duke@435: // if havesupername: this is used for circularity for instanceklass loading coleenp@4037: PlaceholderEntry* PlaceholderTable::find_and_add(int index, unsigned int hash, coleenp@4037: Symbol* name, coleenp@4037: ClassLoaderData* loader_data, coleenp@4037: classloadAction action, coleenp@4037: Symbol* supername, coleenp@4037: Thread* thread) { coleenp@4037: PlaceholderEntry* probe = get_entry(index, hash, name, loader_data); duke@435: if (probe == NULL) { duke@435: // Nothing found, add place holder coleenp@4037: add_entry(index, hash, name, loader_data, (action == LOAD_SUPER), supername); coleenp@4037: probe = get_entry(index, hash, name, loader_data); duke@435: } else { duke@435: if (action == LOAD_SUPER) { duke@435: probe->set_havesupername(true); coleenp@2497: probe->set_supername(supername); duke@435: } duke@435: } duke@435: if (probe) probe->add_seen_thread(thread, action); duke@435: return probe; duke@435: } duke@435: duke@435: acorn@4425: // placeholder is used to track class loading internal states duke@435: // placeholder existence now for loading superclass/superinterface duke@435: // superthreadQ tracks class circularity, while loading superclass/superinterface duke@435: // loadInstanceThreadQ tracks load_instance_class calls duke@435: // definer() tracks the single thread that owns define token duke@435: // defineThreadQ tracks waiters on defining thread's results duke@435: // 1st claimant creates placeholder duke@435: // find_and_add adds SeenThread entry for appropriate queue duke@435: // All claimants remove SeenThread after completing action duke@435: // On removal: if definer and all queues empty, remove entry duke@435: // Note: you can be in both placeholders and systemDictionary duke@435: // Therefore - must always check SD first duke@435: // Ignores the case where entry is not found duke@435: void PlaceholderTable::find_and_remove(int index, unsigned int hash, acorn@4425: Symbol* name, ClassLoaderData* loader_data, acorn@4425: classloadAction action, acorn@4425: Thread* thread) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); coleenp@4037: PlaceholderEntry *probe = get_entry(index, hash, name, loader_data); duke@435: if (probe != NULL) { acorn@4425: probe->remove_seen_thread(thread, action); acorn@4425: // If no other threads using this entry, and this thread is not using this entry for other states duke@435: if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL) duke@435: && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL)) { coleenp@4037: remove_entry(index, hash, name, loader_data); duke@435: } duke@435: } duke@435: } duke@435: duke@435: PlaceholderTable::PlaceholderTable(int table_size) zgu@3900: : TwoOopHashtable(table_size, sizeof(PlaceholderEntry)) { duke@435: } duke@435: duke@435: coleenp@4037: void PlaceholderTable::classes_do(KlassClosure* f) { duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (PlaceholderEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: probe->classes_do(f); duke@435: } duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: void PlaceholderEntry::classes_do(KlassClosure* closure) { coleenp@2497: assert(klassname() != NULL, "should have a non-null klass"); duke@435: if (_instanceKlass != NULL) { hseigel@4278: closure->do_klass(instance_klass()); duke@435: } duke@435: } duke@435: duke@435: // do all entries in the placeholder table coleenp@4037: void PlaceholderTable::entries_do(void f(Symbol*)) { duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (PlaceholderEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: f(probe->klassname()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: // Note, doesn't append a cr duke@435: void PlaceholderEntry::print() const { coleenp@2497: klassname()->print_value(); coleenp@4037: if (loader_data() != NULL) { duke@435: tty->print(", loader "); coleenp@4037: loader_data()->print_value(); duke@435: } duke@435: if (supername() != NULL) { duke@435: tty->print(", supername "); duke@435: supername()->print_value(); duke@435: } duke@435: if (definer() != NULL) { duke@435: tty->print(", definer "); duke@435: definer()->print_value(); duke@435: } hseigel@4278: if (instance_klass() != NULL) { coleenp@4037: tty->print(", InstanceKlass "); hseigel@4278: instance_klass()->print_value(); duke@435: } duke@435: tty->print("\n"); duke@435: tty->print("loadInstanceThreadQ threads:"); duke@435: loadInstanceThreadQ()->printActionQ(); duke@435: tty->print("\n"); duke@435: tty->print("superThreadQ threads:"); duke@435: superThreadQ()->printActionQ(); duke@435: tty->print("\n"); duke@435: tty->print("defineThreadQ threads:"); duke@435: defineThreadQ()->printActionQ(); duke@435: tty->print("\n"); duke@435: } duke@435: #endif duke@435: duke@435: void PlaceholderEntry::verify() const { coleenp@4037: guarantee(loader_data() != NULL, "Must have been setup."); coleenp@4037: guarantee(loader_data()->class_loader() == NULL || loader_data()->class_loader()->is_instance(), duke@435: "checking type of _loader"); hseigel@4278: guarantee(instance_klass() == NULL hseigel@4278: || instance_klass()->oop_is_instance(), hseigel@4278: "checking type of instance_klass result"); duke@435: } duke@435: duke@435: void PlaceholderTable::verify() { duke@435: int element_count = 0; duke@435: for (int pindex = 0; pindex < table_size(); pindex++) { duke@435: for (PlaceholderEntry* probe = bucket(pindex); duke@435: probe != NULL; duke@435: probe = probe->next()) { duke@435: probe->verify(); duke@435: element_count++; // both klasses and place holders count duke@435: } duke@435: } duke@435: guarantee(number_of_entries() == element_count, duke@435: "Verify of system dictionary failed"); duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: void PlaceholderTable::print() { duke@435: for (int pindex = 0; pindex < table_size(); pindex++) { duke@435: for (PlaceholderEntry* probe = bucket(pindex); duke@435: probe != NULL; duke@435: probe = probe->next()) { duke@435: if (Verbose) tty->print("%4d: ", pindex); duke@435: tty->print(" place holder "); duke@435: duke@435: probe->print(); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: } duke@435: #endif