duke@435: /* duke@435: * Copyright 2003-2007 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_placeholders.cpp.incl" duke@435: duke@435: // Placeholder methods duke@435: duke@435: PlaceholderEntry* PlaceholderTable::new_entry(int hash, symbolOop name, duke@435: oop loader, bool havesupername, duke@435: symbolOop supername) { duke@435: PlaceholderEntry* entry = (PlaceholderEntry*)Hashtable::new_entry(hash, name); duke@435: entry->set_loader(loader); 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); duke@435: entry->set_instanceKlass(NULL); duke@435: return entry; duke@435: } duke@435: 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, duke@435: symbolHandle class_name, Handle class_loader, duke@435: bool havesupername, symbolHandle supername){ duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: assert(!class_name.is_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. duke@435: PlaceholderEntry* entry = new_entry(hash, class_name(), class_loader(), 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, duke@435: symbolHandle class_name, duke@435: Handle class_loader) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: PlaceholderEntry** p = bucket_addr(index); duke@435: while (*p) { duke@435: PlaceholderEntry *probe = *p; duke@435: if (probe->hash() == hash && probe->equals(class_name(), class_loader())) { 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, duke@435: symbolHandle class_name, duke@435: Handle class_loader) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: duke@435: symbolOop class_name_ = class_name(); duke@435: oop class_loader_ = class_loader(); 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 && duke@435: place_probe->equals(class_name_, class_loader_)) { duke@435: return place_probe; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: symbolOop PlaceholderTable::find_entry(int index, unsigned int hash, duke@435: symbolHandle class_name, duke@435: Handle class_loader) { duke@435: PlaceholderEntry* probe = get_entry(index, hash, class_name, class_loader); duke@435: return (probe? probe->klass(): symbolOop(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 duke@435: PlaceholderEntry* PlaceholderTable::find_and_add(int index, unsigned int hash, symbolHandle name, Handle loader, classloadAction action, symbolHandle supername, Thread* thread) { duke@435: PlaceholderEntry* probe = get_entry(index, hash, name, loader); duke@435: if (probe == NULL) { duke@435: // Nothing found, add place holder duke@435: add_entry(index, hash, name, loader, (action == LOAD_SUPER), supername); duke@435: probe = get_entry(index, hash, name, loader); duke@435: } else { duke@435: if (action == LOAD_SUPER) { duke@435: probe->set_havesupername(true); duke@435: 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: duke@435: // placeholder 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: // see parse_stream for redefine classes 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, duke@435: symbolHandle name, Handle loader, Thread* thread) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: PlaceholderEntry *probe = get_entry(index, hash, name, loader); duke@435: if (probe != NULL) { duke@435: // No other threads using this entry duke@435: if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL) duke@435: && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL)) { duke@435: remove_entry(index, hash, name, loader); duke@435: } duke@435: } duke@435: } duke@435: duke@435: PlaceholderTable::PlaceholderTable(int table_size) duke@435: : TwoOopHashtable(table_size, sizeof(PlaceholderEntry)) { duke@435: } duke@435: duke@435: duke@435: void PlaceholderTable::oops_do(OopClosure* 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()) { duke@435: probe->oops_do(f); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void PlaceholderEntry::oops_do(OopClosure* blk) { duke@435: assert(klass() != NULL, "should have a non-null klass"); duke@435: blk->do_oop((oop*)klass_addr()); duke@435: if (_loader != NULL) { duke@435: blk->do_oop(loader_addr()); duke@435: } duke@435: if (_supername != NULL) { duke@435: blk->do_oop((oop*)supername_addr()); duke@435: } duke@435: if (_instanceKlass != NULL) { duke@435: blk->do_oop((oop*)instanceKlass_addr()); duke@435: } duke@435: } duke@435: duke@435: // do all entries in the placeholder table duke@435: void PlaceholderTable::entries_do(void f(symbolOop, oop)) { 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()) { duke@435: f(probe->klass(), probe->loader()); 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 { duke@435: klass()->print_value(); duke@435: if (loader() != NULL) { duke@435: tty->print(", loader "); duke@435: loader()->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: } duke@435: if (instanceKlass() != NULL) { duke@435: tty->print(", instanceKlass "); duke@435: instanceKlass()->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 { duke@435: guarantee(loader() == NULL || loader()->is_instance(), duke@435: "checking type of _loader"); duke@435: guarantee(instanceKlass() == NULL duke@435: || Klass::cast(instanceKlass())->oop_is_instance(), duke@435: "checking type of instanceKlass result"); duke@435: klass()->verify(); 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